description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | MOD = 998244353
list_size = 2100
f_list = [1] * list_size
f_r_list = [1] * list_size
for i in range(list_size - 1):
f_list[i + 1] = int(f_list[i] * (i + 2) % MOD)
def power(n, x):
if x == 1:
return n
elif x % 2 == 0:
return power(int(n * n % MOD), int(x / 2))
else:
return int(n * power(n, x - 1) % MOD)
f_r_list[-1] = power(f_list[-1], MOD - 2)
for i in range(2, list_size + 1):
f_r_list[-i] = int(f_r_list[-i + 1] * (list_size + 2 - i) % MOD)
def comb(n, r):
if n < r:
return 0
elif n == 0 or r == 0 or n == r:
return 1
else:
return f_list[n - 1] * f_r_list[n - r - 1] % MOD * f_r_list[r - 1] % MOD
n, m, k = map(int, input().split())
print(m * (m - 1) ** k % MOD * comb(n - 1, k) % MOD) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = map(int, input().split())
comb = 1
power = 1
fact = 1
M = 998244353
for i in range(k):
comb = comb * (n - 1 - i)
power *= m - 1
power = power % M
fact *= k - i
print(comb // fact % M * m * (power % M) % M) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | MOD = 998244353
def pow(base, exp):
if exp == 0:
return 1
if exp % 2 == 1:
return pow(base, exp - 1) * base % MOD
tmp = pow(base, exp // 2)
return tmp * tmp % MOD
def inv_mod(x):
return pow(x, MOD - 2)
def C(n, k):
if k < n // 2:
return C(n, n - k)
result = 1
for x in range(k + 1, n + 1):
result = result * x % MOD
for x in range(1, n - k + 1):
result = result * inv_mod(x) % MOD
return result
n, m, k = map(int, input().split())
print(m * C(n - 1, n - 1 - k) * pow(m - 1, k) % MOD) | ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | MOD = 998244353
def f(x):
ans = 1
for i in range(1, x):
ans = ans * i
return ans
n, m, k = map(int, input().split())
print(m * (m - 1) ** k * f(n) // f(k + 1) // f(n - k) % MOD) | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | s = input()
s = s.split(" ")
n = int(s[0])
m = int(s[1])
k = int(s[2])
ans = 1
if k > 0:
for i in range(n - k, n):
ans = ans * i
for i in range(1, k + 1):
ans = ans // i
ans = ans * m
for i in range(k):
ans = ans * (m - 1)
pass
print(ans % 998244353) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | import sys
inf = float("inf")
mod = 998244353
def get_array():
return list(map(int, sys.stdin.readline().split()))
def get_ints():
return map(int, sys.stdin.readline().split())
def input():
return sys.stdin.readline()
def main():
n, m, k = get_ints()
dp[1][0] = m
for i in range(1, n):
for j in range(k + 1):
dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod
dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j]) * (m - 1) % mod
print(dp[n][k])
dp = [[(0) for _ in range(2005)] for _ in range(2005)]
main() | IMPORT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def calc(n, k):
return fact[n] * pow(fact[n - k] * fact[k], mod - 2, mod) % mod
mod = 998244353
n, m, k = map(int, input().split())
fact = [1]
for i in range(1, 2001):
fact.append(fact[-1] * i % mod)
print(calc(n - 1, k) * m * pow(m - 1, k, mod) % mod) | FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | stdin = lambda type_="int", sep=" ": list(map(eval(type_), input().split(sep)))
joint = lambda sep=" ", *args: sep.join(
str(i) if type(i) != list else sep.join(map(str, i)) for i in args
)
def binomial(n, k):
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in range(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0
def solve(n, m, k):
parts = m * pow(m - 1, k)
return binomial(n - 1, k) * parts % 998244353
n, m, k = stdin()
print(solve(n, m, k)) | ASSIGN VAR STRING STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def power(x, y):
res = 1
while y != 0:
if y & 1:
res = res * x % mod
x = x * x % mod
y >>= 1
return res
n, m, k = map(int, input().split())
mod = 998244353
jc = [1]
for i in range(n - 1):
jc.append(jc[i] * (i + 1) % mod)
ans = jc[n - 1] * power(m - 1, k) % mod * m % mod
ans = ans * power(jc[k], mod - 2) % mod * power(jc[n - 1 - k], mod - 2) % mod
print(ans) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = map(int, input().split())
mod = 998244353
dp = [[(0) for i in range(n)] for j in range(n)]
dp[0][0] = m
for i in range(1, n):
for j in range(0, min(i + 1, k + 1)):
if j == 0:
dp[i][j] = dp[i - 1][j]
elif j == i + 1:
dp[i][j] = dp[i - 1][j] * (m - 1) % mod
else:
dp[i][j] = (dp[i - 1][j - 1] * (m - 1) + dp[i - 1][j]) % mod
print(dp[n - 1][k] % mod) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | l = list(map(int, input().strip().split()))
n = l[0]
m = l[1]
k = l[2]
mod = 998244353
soma = 0
def power_mod(base, power, mod):
res = 1
while power:
if power % 2 == 1:
res = res * base % mod
base = base * base % mod
power = power >> 1
return res
def inverse(n, mod):
return power_mod(n, mod - 2, mod)
def choose(n, r, mod):
factorial = []
factorial.append(1)
for i in range(1, n + 1):
factorial.append(i * factorial[i - 1] % mod)
inverse_factorial = []
for _ in range(n + 1):
inverse_factorial.append(0)
inverse_factorial[n] = inverse(factorial[n], mod)
for i in range(n - 1, -1, -1):
inverse_factorial[i] = (i + 1) * inverse_factorial[i + 1] % mod
numerator = factorial[n]
denominator_inverse = inverse_factorial[r] * inverse_factorial[n - r] % mod
return numerator * denominator_inverse % mod
nGroups = k + 1
group_size_count = choose(n - 1, k, mod)
colour_distribution_count = m * power_mod(m - 1, nGroups - 1, mod) % mod
res = group_size_count * colour_distribution_count % mod
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = list(map(int, input().split()))
mod = 998244353
same_bricks = n - 1 - k
total = m
for i in range(k):
total *= m - 1
total %= mod
val = 1
for i in range(n - k, n):
val *= i
for i in range(1, k + 1):
val //= i
total *= val
total %= mod
print(total) | ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = [int(x) for x in input().split()]
def perm(n, k):
s = 1
for i in range(n - k + 1, n + 1):
s *= i
return s
def choose(n, k):
if k > n // 2:
return choose(n, n - k)
else:
return perm(n, k) // perm(k, k)
ans = m
for i in range(0, k):
ans = ans * (m - 1) % 998244353
print(ans * choose(n - 1, k) % 998244353) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | MOD = 998244353
n, nColors, nDiff = map(int, input().split())
dp = [[(0) for _ in range(nDiff + 1)] for _ in range(n)]
dp[0][0] = nColors
for size in range(1, n):
for cnt in range(nDiff + 1):
dp[size][cnt] += dp[size - 1][cnt]
dp[size][cnt] %= MOD
if cnt - 1 >= 0:
dp[size][cnt] += dp[size - 1][cnt - 1] * (nColors - 1) % MOD
dp[size][cnt] %= MOD
print(dp[n - 1][nDiff]) | ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = map(int, input().split())
dp = [([0] * (n + 1)) for i in range(n)]
MOD = 998244353
dp[0][0] = m
for i in range(1, n):
for j in range(k + 1):
dp[i][j + 1] += dp[i - 1][j] * (m - 1)
dp[i][j] += dp[i - 1][j]
dp[i][j + 1] %= MOD
dp[i][j] %= MOD
print(dp[n - 1][k]) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def ncr(n, r):
return fact(n) // (fact(r) * fact(n - r))
def fact(n):
res = 1
for i in range(2, n + 1):
res = res * i
return res
n, m, k = map(int, input().split())
mod = 998244353
a = ncr(n - 1, k)
if k >= 1:
c = (m - 1) ** k
else:
c = 1
c = c % mod
ans = a * m * c
print(ans % mod) | FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | N, M, K = map(int, input().split())
MOD = 998244353
dp = [([0] * 2002) for i in range(2002)]
dp[1][0] = M
for i in range(1, N):
for j in range(K + 1):
dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % MOD
dp[i + 1][j + 1] = dp[i][j] * (M - 1) % MOD
print(dp[N][K]) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | import sys
mod = 998244353
bricks, colors, left = map(int, input().split())
dp = [([0] * (left + 1)) for i in range(bricks + 1)]
if left == 0:
print(colors)
sys.exit()
for i in range(1, bricks + 1):
dp[i][0] = colors
for i in range(1, bricks + 1):
for j in range(1, left + 1):
dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1] * (colors - 1)) % mod
print(dp[bricks][left] % mod) | IMPORT ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | s = input().split()
n, m, k = list(map(int, s))
MOD = 998244353
ans = m
for i in range(k):
ans = ans * (m - 1) % MOD
if k > n // 2:
k = n - 1 - k
for i in range(1, k + 1):
ans = ans * (n - 1 - k + i) // i
print(ans % MOD) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def binomialCoef(n, k):
C = [[(0) for x in range(k + 1)] for x in range(n + 1)]
mod = 998244353
for i in range(n + 1):
for j in range(min(i, k) + 1):
if j == 0 or j == i:
C[i][j] = 1
else:
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod
return C[n][k]
n, m, k = map(int, input().split())
mod = 998244353
y = binomialCoef(n - 1, k)
z = m * (m - 1) ** k % mod
print(int(y * z % mod)) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = list(map(int, input().split()))
ans = 1
for i in range(1, k + 1):
ans *= n - i
for i in range(k, 0, -1):
ans //= i
ans = ans * m % 998244353
for i in range(k):
ans = ans * (m - 1) % 998244353
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | MOD = 998244353
def bpow(a, b):
sol = 1
while b:
if b & 1:
sol *= a
if sol >= MOD:
sol %= MOD
a *= a
if a >= MOD:
a %= MOD
b >>= 1
return sol
fac = []
fac += [1]
for i in range(1, 2001):
fac.append(fac[i - 1] * i % MOD)
ifac = []
for i in range(0, 2001):
ifac.append(bpow(fac[i], MOD - 2))
def nCr(a, b):
return fac[a] * ifac[a - b] * ifac[b] % MOD
n, m, k = map(int, input().split())
print(nCr(n - 1, k) * m * bpow(m - 1, k) % MOD) | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
def combinations(n, k):
return factorial(n) // (factorial(k) * factorial(n - k))
def main():
n, m, k = map(int, input().split())
print(combinations(n - 1, k) * m * (m - 1) ** k % 998244353)
main() | FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def fact(n):
if n == 0:
return 1
else:
return fact(n - 1) * n
def C(n, k):
return fact(n) // (fact(n - k) * fact(k))
n, m, k = map(int, input().split())
print(m * (m - 1) ** k * C(n - 1, k) % 998244353) | FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | mod = 998244353
n, m, k = map(int, input().split())
a = pow(m - 1, k, mod) * m % mod
for i in range(n - k, n):
a = a * i
for i in range(1, k + 1):
a //= i
print(a % mod) | ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = map(int, input().split(" "))
MOD = 998244353
dp = [([0] * (k + 1)) for _ in range(n)]
dp[0][0] = m
for i in range(1, n):
for j in range(min(i + 1, k + 1)):
dp[i][j] = (
(0 if j == 0 else dp[i - 1][j - 1] * (m - 1) % MOD) + dp[i - 1][j]
) % MOD
print(dp[-1][-1]) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = list(map(int, input().split()))
d = [([0] * (k + 1)) for _ in range(0, n + 1)]
MOD = 998244353
for i in range(1, n + 1):
for j in range(0, k + 1):
if j == 0:
d[i][j] = m
else:
d[i][j] = (d[i - 1][j] + d[i - 1][j - 1] * (m - 1)) % MOD
print(d[n][k]) | ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | MOD = 998244353
n, m, k = map(int, input().split())
a = [1]
for _ in range(n - 1):
a = [(sum(x) % MOD) for x in zip([0] + a, a + [0])]
print(m * pow(m - 1, k, MOD) * a[k] % MOD) | ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR BIN_OP VAR LIST NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | data = input().split()
n = int(data[0])
m = int(data[1])
k = int(data[2])
K = 998244353
gt = [(0) for i in range(2005)]
rev = [(0) for i in range(2005)]
def mu(a, n):
if n == 0:
return 1
q = mu(a, n // 2)
if n % 2 == 0:
return q * q % K
return q * q % K * a % K
gt[0] = rev[0] = 1
for i in range(1, 2001):
gt[i] = gt[i - 1] * i % K
rev[i] = mu(gt[i], K - 2)
res = gt[n - 1] * rev[k] % K * rev[n - k - 1] % K
res = res * m % K
res = res * mu(m - 1, k) % K
print(res) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = map(int, input().split())
mod = 998244353
def combination_mod(k, n, mod):
fact_k = 1
for i in range(2, k + 1):
fact_k = fact_k * i % mod
fact_n = 1
for i in range(2, n + 1):
fact_n = fact_n * i % mod
fact_n_k = 1
for i in range(2, n - k + 1):
fact_n_k = fact_n_k * i % mod
return fact_n * pow(fact_k, mod - 2, mod) * pow(fact_n_k, mod - 2, mod) % mod
print(combination_mod(k, n - 1, mod) * m * pow(m - 1, k, mod) % mod) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | import sys
MOD = 998244353
n, m, k = list(map(int, input().strip().split()))
memo = dict()
def C(n, m, k, first):
if n == 0:
if k == 0:
return 1
else:
return 0
if k == 0:
if first:
return m
else:
return 0
if (n, m, k, first) in memo:
return memo[n, m, k, first]
vseh = 0
if first:
for i in range(n):
vseh += m * C(i, m, k, False)
else:
for i in range(n):
vseh += (m - 1) * C(i, m, k - 1, False)
memo[n, m, k, first] = vseh % MOD
return memo[n, m, k, first]
def C2(n, m, k):
matrika = [[(0) for _ in range(n + 1)] for _ in range(k + 1)]
matrika[0][0] = 1
for j in range(1, k + 1):
acc = 0
for a in range(1, n + 1):
acc += (m - 1) * matrika[j - 1][a - 1]
matrika[j][a] = acc % MOD
result = m * sum(matrika[k][:-1])
return result % MOD
print(C2(n, m, k)) | IMPORT ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER IF VAR RETURN VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR VAR FUNC_DEF 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | R = lambda: map(int, input().split())
n, m, k = R()
dp = [([0] * (k + 2)) for _ in range(n + 2)]
for i in range(1, n + 1):
dp[i][0] = m
for i in range(1, n + 1):
for j in range(1, k + 1):
dp[i][j] = (dp[i - 1][j] + (m - 1) * dp[i - 1][j - 1]) % 998244353
print(dp[n][k]) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | M = 998244353
def f(k):
r = 1
for i in range(2, k + 1):
r *= i
return r
n, m, k = map(int, input().split())
print(f(n - 1) * pow(f(k) * f(n - k - 1), M - 2, M) * m * pow(m - 1, k, M) % M) | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | MOD = 998244353
def power(x, a):
if a == 0:
return 1
z = power(x, a // 2)
z = z * z % MOD
if a % 2:
z = z * x % MOD
return z
def fact(n):
factn = 1
for i in range(2, n + 1):
factn = factn * i % MOD
return factn
def ncr(n, r):
return fact(n) * power(fact(r), MOD - 2) * power(fact(n - r), MOD - 2) % MOD
[n, m, k] = list(map(int, input().split()))
print(ncr(n - 1, k) * m * power(m - 1, k) % MOD) | ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN LIST VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | import sys
sys.setrecursionlimit(1000000)
M = 998244353
def Qpow(x, y):
if y == 0:
return 1
else:
ret = Qpow(x, int(y / 2)) % M
ret *= ret
ret %= M
if y % 2 == 1:
ret *= x
ret %= M
return ret
n, m, k = map(int, input().split())
fact = [(0) for i in range(0, 5005)]
fact[0] = 1
for i in range(1, 5002 + 1):
fact[i] = fact[i - 1] * i % M
way = fact[k] * fact[n - 1 - k] % M
way = fact[n - 1] * Qpow(way, M - 2) % M % M
ans = way * (Qpow(m - 1, k) % M + M) * m % M
if ans < 0:
ans += M
ans %= M
print(ans) | IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | mod = 998244353
fac = [1]
for i in range(1, 2001):
fac.append(fac[-1] * i % mod)
def fexp(x, y):
ans = 1
while y > 0:
if y % 2 == 1:
ans = ans * x % mod
x = x * x % mod
y //= 2
return ans
def rev(x):
return fexp(x, mod - 2)
def c(n, k):
return fac[n] * rev(fac[n - k]) * rev(fac[k]) % mod
n, m, k = map(int, input().split(" "))
print(c(n - 1, k) * m * fexp(m - 1, k) % mod) | ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | P = 998244353
def C(n, k):
nonlocal P
k = min(k, n - k)
p = 1
for i in range(n, n - k, -1):
p *= i
for i in range(k, 0, -1):
assert p % i == 0
p //= i
return p % P
n, m, k = list(map(int, input().split()))
r = C(n - 1, k)
r = r * m % P
for i in range(k):
r = r * (m - 1) % P
print(r) | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = list(map(int, input().split()))
MOD = 998244353
ans = m * pow(m - 1, k, MOD) % MOD
for i in range(n - k, n):
ans = ans * i
for i in range(n - k, n):
ans = ans // (n - i)
print(ans % MOD) | ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integer — the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = [int(x) for x in input().split()]
dp = [0] * (n - 1)
if len(dp) > 0:
dp[0] = n - 1
n -= 1
for i in range(1, n):
n -= 1
dp[i] = dp[i - 1] * n // (i + 1)
if k == 0:
print(m)
else:
print(dp[k - 1] * m * (m - 1) ** k % 998244353)
else:
print(m) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
ADZEN is a popular advertising firm in your city that owns all $n$ billboard locations on Main street. The city council passed a new zoning ordinance mandating that no more than $\boldsymbol{\mbox{k}}$ consecutive billboards may be up at any given time. For example, if there are $n=3$ billboards on Main street and $k=1$, ADZEN must remove either the middle billboard, the first two billboards, the last two billboards or the first and last billboard.
Being a for-profit company, ADZEN wants to lose as little advertising revenue as possible when removing the billboards. They want to comply with the new ordinance in such a way that the remaining billboards maximize their total revenues (i.e., the sum of revenues generated by the billboards left standing on Main street).
Given $n$, $\boldsymbol{\mbox{k}}$, and the revenue of each of the $n$ billboards, find and print the maximum profit that ADZEN can earn while complying with the zoning ordinance. Assume that Main street is a straight, contiguous block of $n$ billboards that can be removed but cannot be reordered in any way.
For example, if there are $n=7$ billboards, and $k=3$ is the maximum number of consecutive billboards that can be active, with $revenues=[5,6,4,2,10,8,4]$, then the maximum revenue that can be generated is $37$: $5+6+4+10+8+4=37$.
Function Description
Complete the billboards function in the editor below. It should return an integer that represents the maximum revenue that can be generated under the rules.
billboards has the following parameter(s):
k: an integer that represents the longest contiguous group of billboards allowed
revenue: an integer array where each element represents the revenue potential for a billboard at that index
Input Format
The first line contains two space-separated integers, $n$ (the number of billboards) and $\boldsymbol{\mbox{k}}$ (the maximum number of billboards that can stand together on any part of the road).
Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains an integer denoting the revenue value of billboard $\boldsymbol{i}$ (where $0\leq i<n$).
Constraints
$1\leq n\leq10^5$
$1\leq k\leq n$
$0\leq\:\text{revenue value of any billboard}\:\leq2\cdot10^9$
Output Format
Print a single integer denoting the maximum profit ADZEN can earn from Main street after complying with the city's ordinance.
Sample Input 0
6 2
1
2
3
1
6
10
Sample Output 0
21
Explanation 0
There are $n=6$ billboards, and we must remove some of them so that no more than $k=2$ billboards are immediately next to one another.
We remove the first and fourth billboards, which gives us the configuration _ 2 3 _ 6 10 and a profit of $2+3+6+10=21$. As no other configuration has a profit greater than $21$, we print $21$ as our answer.
Sample Input 1
5 4
1
2
3
4
5
Sample Output 1
14
Explanation 1
There are $n=5$ billboards, and we must remove some of them so that no more than $k=4$ billboards are immediately next to one another.
We remove the first billboard, which gives us the configuration _ 2 3 4 5 and a profit of $2+3+4+5=14$. As no other configuration has a profit greater than $\mathbf{14}$, we print $\mathbf{14}$ as our answer. | import sys
n, k = list(map(int, sys.stdin.readline().split()))
profits = [int(sys.stdin.readline()) for i in range(n)]
total = sum(profits)
if k == n:
print(total)
else:
x = profits[: k + 1]
min_value = min(x)
min_index = x.index(min_value)
for i in range(k + 1, n):
if i - min_index >= k:
min_value = min(x[i - (k + 1) : i + 1])
min_index = x.index(min_value)
x.append(min_value + profits[i])
if x[i] < min_value:
min_value = x[i]
min_index = i
print(total - min(x[n - (k + 1) :])) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER |
ADZEN is a popular advertising firm in your city that owns all $n$ billboard locations on Main street. The city council passed a new zoning ordinance mandating that no more than $\boldsymbol{\mbox{k}}$ consecutive billboards may be up at any given time. For example, if there are $n=3$ billboards on Main street and $k=1$, ADZEN must remove either the middle billboard, the first two billboards, the last two billboards or the first and last billboard.
Being a for-profit company, ADZEN wants to lose as little advertising revenue as possible when removing the billboards. They want to comply with the new ordinance in such a way that the remaining billboards maximize their total revenues (i.e., the sum of revenues generated by the billboards left standing on Main street).
Given $n$, $\boldsymbol{\mbox{k}}$, and the revenue of each of the $n$ billboards, find and print the maximum profit that ADZEN can earn while complying with the zoning ordinance. Assume that Main street is a straight, contiguous block of $n$ billboards that can be removed but cannot be reordered in any way.
For example, if there are $n=7$ billboards, and $k=3$ is the maximum number of consecutive billboards that can be active, with $revenues=[5,6,4,2,10,8,4]$, then the maximum revenue that can be generated is $37$: $5+6+4+10+8+4=37$.
Function Description
Complete the billboards function in the editor below. It should return an integer that represents the maximum revenue that can be generated under the rules.
billboards has the following parameter(s):
k: an integer that represents the longest contiguous group of billboards allowed
revenue: an integer array where each element represents the revenue potential for a billboard at that index
Input Format
The first line contains two space-separated integers, $n$ (the number of billboards) and $\boldsymbol{\mbox{k}}$ (the maximum number of billboards that can stand together on any part of the road).
Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains an integer denoting the revenue value of billboard $\boldsymbol{i}$ (where $0\leq i<n$).
Constraints
$1\leq n\leq10^5$
$1\leq k\leq n$
$0\leq\:\text{revenue value of any billboard}\:\leq2\cdot10^9$
Output Format
Print a single integer denoting the maximum profit ADZEN can earn from Main street after complying with the city's ordinance.
Sample Input 0
6 2
1
2
3
1
6
10
Sample Output 0
21
Explanation 0
There are $n=6$ billboards, and we must remove some of them so that no more than $k=2$ billboards are immediately next to one another.
We remove the first and fourth billboards, which gives us the configuration _ 2 3 _ 6 10 and a profit of $2+3+6+10=21$. As no other configuration has a profit greater than $21$, we print $21$ as our answer.
Sample Input 1
5 4
1
2
3
4
5
Sample Output 1
14
Explanation 1
There are $n=5$ billboards, and we must remove some of them so that no more than $k=4$ billboards are immediately next to one another.
We remove the first billboard, which gives us the configuration _ 2 3 4 5 and a profit of $2+3+4+5=14$. As no other configuration has a profit greater than $\mathbf{14}$, we print $\mathbf{14}$ as our answer. | arr1 = [int(i) for i in input().strip().split(" ")]
n, k = arr1[0], arr1[1]
arr2 = []
for i in range(n):
arr2.append(int(input().strip()))
max_profit = sum(arr2)
def minimum_pairings(array1):
array2 = []
i = 0
while i < len(array1) - 1:
array2.append(min(array1[i], array1[i + 1]))
i += 2
if i == len(array1) - 1:
array2.append(array1[-1])
return array2
def find_minimum(arr):
minimum_candidates = [arr]
while len(minimum_candidates[-1]) != 1:
minimum_candidates.append(minimum_pairings(minimum_candidates[-1]))
return minimum_candidates
def update_minimums(tree, index):
a = index
for i in range(len(tree) - 1):
if a == len(tree[i]) - 1 and a % 2 == 0:
b = a
else:
b = a + 1 - 2 * (a % 2)
tree[i + 1][a // 2] = min(tree[i][a], tree[i][b])
a //= 2
return tree
min_tree = []
for i in range(k + 1):
min_tree.append(arr2[i])
min_tree = find_minimum(min_tree)
for i in range(k + 1, n):
j = i % (k + 1)
min_tree[0][j] = arr2[i] + min_tree[-1][0]
update_minimums(min_tree, j)
print(max_profit - min_tree[-1][0]) | ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER |
ADZEN is a popular advertising firm in your city that owns all $n$ billboard locations on Main street. The city council passed a new zoning ordinance mandating that no more than $\boldsymbol{\mbox{k}}$ consecutive billboards may be up at any given time. For example, if there are $n=3$ billboards on Main street and $k=1$, ADZEN must remove either the middle billboard, the first two billboards, the last two billboards or the first and last billboard.
Being a for-profit company, ADZEN wants to lose as little advertising revenue as possible when removing the billboards. They want to comply with the new ordinance in such a way that the remaining billboards maximize their total revenues (i.e., the sum of revenues generated by the billboards left standing on Main street).
Given $n$, $\boldsymbol{\mbox{k}}$, and the revenue of each of the $n$ billboards, find and print the maximum profit that ADZEN can earn while complying with the zoning ordinance. Assume that Main street is a straight, contiguous block of $n$ billboards that can be removed but cannot be reordered in any way.
For example, if there are $n=7$ billboards, and $k=3$ is the maximum number of consecutive billboards that can be active, with $revenues=[5,6,4,2,10,8,4]$, then the maximum revenue that can be generated is $37$: $5+6+4+10+8+4=37$.
Function Description
Complete the billboards function in the editor below. It should return an integer that represents the maximum revenue that can be generated under the rules.
billboards has the following parameter(s):
k: an integer that represents the longest contiguous group of billboards allowed
revenue: an integer array where each element represents the revenue potential for a billboard at that index
Input Format
The first line contains two space-separated integers, $n$ (the number of billboards) and $\boldsymbol{\mbox{k}}$ (the maximum number of billboards that can stand together on any part of the road).
Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains an integer denoting the revenue value of billboard $\boldsymbol{i}$ (where $0\leq i<n$).
Constraints
$1\leq n\leq10^5$
$1\leq k\leq n$
$0\leq\:\text{revenue value of any billboard}\:\leq2\cdot10^9$
Output Format
Print a single integer denoting the maximum profit ADZEN can earn from Main street after complying with the city's ordinance.
Sample Input 0
6 2
1
2
3
1
6
10
Sample Output 0
21
Explanation 0
There are $n=6$ billboards, and we must remove some of them so that no more than $k=2$ billboards are immediately next to one another.
We remove the first and fourth billboards, which gives us the configuration _ 2 3 _ 6 10 and a profit of $2+3+6+10=21$. As no other configuration has a profit greater than $21$, we print $21$ as our answer.
Sample Input 1
5 4
1
2
3
4
5
Sample Output 1
14
Explanation 1
There are $n=5$ billboards, and we must remove some of them so that no more than $k=4$ billboards are immediately next to one another.
We remove the first billboard, which gives us the configuration _ 2 3 4 5 and a profit of $2+3+4+5=14$. As no other configuration has a profit greater than $\mathbf{14}$, we print $\mathbf{14}$ as our answer. | n, k = map(int, input().split())
a = []
for i in range(n):
a.append(int(input()))
a.append(0)
dp = [(-1) for i in range(n + 2)]
val = [(0) for i in range(k + 1)]
val[k] = sum(a[k:])
for i in reversed(range(k)):
val[i] = a[i] + val[i + 1]
for i in reversed(range(n - k, n + 2)):
dp[i] = 0
for i in reversed(range(n - k)):
ta = 999999999999
if dp[i + 1] != dp[i + k + 2] + a[i + k + 1]:
ta = min(dp[i + 1] + a[i], dp[i + 1])
else:
for j in range(i, i + k + 1):
t = a[j] + dp[j + 1]
if t < ta:
ta = t
dp[i] = ta
for i in range(k + 1):
val[i] -= dp[i]
print(max(val)) | 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 EXPR FUNC_CALL 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 VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
ADZEN is a popular advertising firm in your city that owns all $n$ billboard locations on Main street. The city council passed a new zoning ordinance mandating that no more than $\boldsymbol{\mbox{k}}$ consecutive billboards may be up at any given time. For example, if there are $n=3$ billboards on Main street and $k=1$, ADZEN must remove either the middle billboard, the first two billboards, the last two billboards or the first and last billboard.
Being a for-profit company, ADZEN wants to lose as little advertising revenue as possible when removing the billboards. They want to comply with the new ordinance in such a way that the remaining billboards maximize their total revenues (i.e., the sum of revenues generated by the billboards left standing on Main street).
Given $n$, $\boldsymbol{\mbox{k}}$, and the revenue of each of the $n$ billboards, find and print the maximum profit that ADZEN can earn while complying with the zoning ordinance. Assume that Main street is a straight, contiguous block of $n$ billboards that can be removed but cannot be reordered in any way.
For example, if there are $n=7$ billboards, and $k=3$ is the maximum number of consecutive billboards that can be active, with $revenues=[5,6,4,2,10,8,4]$, then the maximum revenue that can be generated is $37$: $5+6+4+10+8+4=37$.
Function Description
Complete the billboards function in the editor below. It should return an integer that represents the maximum revenue that can be generated under the rules.
billboards has the following parameter(s):
k: an integer that represents the longest contiguous group of billboards allowed
revenue: an integer array where each element represents the revenue potential for a billboard at that index
Input Format
The first line contains two space-separated integers, $n$ (the number of billboards) and $\boldsymbol{\mbox{k}}$ (the maximum number of billboards that can stand together on any part of the road).
Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains an integer denoting the revenue value of billboard $\boldsymbol{i}$ (where $0\leq i<n$).
Constraints
$1\leq n\leq10^5$
$1\leq k\leq n$
$0\leq\:\text{revenue value of any billboard}\:\leq2\cdot10^9$
Output Format
Print a single integer denoting the maximum profit ADZEN can earn from Main street after complying with the city's ordinance.
Sample Input 0
6 2
1
2
3
1
6
10
Sample Output 0
21
Explanation 0
There are $n=6$ billboards, and we must remove some of them so that no more than $k=2$ billboards are immediately next to one another.
We remove the first and fourth billboards, which gives us the configuration _ 2 3 _ 6 10 and a profit of $2+3+6+10=21$. As no other configuration has a profit greater than $21$, we print $21$ as our answer.
Sample Input 1
5 4
1
2
3
4
5
Sample Output 1
14
Explanation 1
There are $n=5$ billboards, and we must remove some of them so that no more than $k=4$ billboards are immediately next to one another.
We remove the first billboard, which gives us the configuration _ 2 3 4 5 and a profit of $2+3+4+5=14$. As no other configuration has a profit greater than $\mathbf{14}$, we print $\mathbf{14}$ as our answer. | inp = input("")
con = inp.split(" ")
n = int(con[0])
k = int(con[1])
cost = []
for i in range(n):
a = input("")
cost.append(int(a))
sliced = cost[:k]
scam = sum(sliced)
R = [(scam - i) for i in sliced[::-1]] + [scam]
inter = scam
a = k + 1
b = n + 1
cumm = a
while cumm < b:
for j in range(a, b):
x = R.index(inter)
if x == k:
R.pop()
R = [inter] + [(cost[j - 1] + i) for i in R]
inter = max(R)
cumm += 1
a = j + 1
break
elif j + k - x >= b:
chunk = cost[j - 1 :]
Sum = sum(chunk)
R_1 = [(inter + Sum - i) for i in (chunk[::-1] if chunk != [] else [0])]
R = R_1 + [(Sum + i) for i in R[x : x + 1 + k - len(R_1)]]
cumm = b
inter = max(R)
break
else:
chunk = cost[j - 1 : k - x + j - 1]
Sum = sum(chunk)
R = [(inter + Sum - i) for i in (chunk[::-1] if chunk != [] else [0])] + [
(Sum + i) for i in R[: x + 1]
]
inter = max(R)
cumm += k - x
count = cumm
a = j + k - x
break
print(inter) | ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER LIST VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR LIST VAR NUMBER LIST NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR LIST VAR NUMBER LIST NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
ADZEN is a popular advertising firm in your city that owns all $n$ billboard locations on Main street. The city council passed a new zoning ordinance mandating that no more than $\boldsymbol{\mbox{k}}$ consecutive billboards may be up at any given time. For example, if there are $n=3$ billboards on Main street and $k=1$, ADZEN must remove either the middle billboard, the first two billboards, the last two billboards or the first and last billboard.
Being a for-profit company, ADZEN wants to lose as little advertising revenue as possible when removing the billboards. They want to comply with the new ordinance in such a way that the remaining billboards maximize their total revenues (i.e., the sum of revenues generated by the billboards left standing on Main street).
Given $n$, $\boldsymbol{\mbox{k}}$, and the revenue of each of the $n$ billboards, find and print the maximum profit that ADZEN can earn while complying with the zoning ordinance. Assume that Main street is a straight, contiguous block of $n$ billboards that can be removed but cannot be reordered in any way.
For example, if there are $n=7$ billboards, and $k=3$ is the maximum number of consecutive billboards that can be active, with $revenues=[5,6,4,2,10,8,4]$, then the maximum revenue that can be generated is $37$: $5+6+4+10+8+4=37$.
Function Description
Complete the billboards function in the editor below. It should return an integer that represents the maximum revenue that can be generated under the rules.
billboards has the following parameter(s):
k: an integer that represents the longest contiguous group of billboards allowed
revenue: an integer array where each element represents the revenue potential for a billboard at that index
Input Format
The first line contains two space-separated integers, $n$ (the number of billboards) and $\boldsymbol{\mbox{k}}$ (the maximum number of billboards that can stand together on any part of the road).
Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains an integer denoting the revenue value of billboard $\boldsymbol{i}$ (where $0\leq i<n$).
Constraints
$1\leq n\leq10^5$
$1\leq k\leq n$
$0\leq\:\text{revenue value of any billboard}\:\leq2\cdot10^9$
Output Format
Print a single integer denoting the maximum profit ADZEN can earn from Main street after complying with the city's ordinance.
Sample Input 0
6 2
1
2
3
1
6
10
Sample Output 0
21
Explanation 0
There are $n=6$ billboards, and we must remove some of them so that no more than $k=2$ billboards are immediately next to one another.
We remove the first and fourth billboards, which gives us the configuration _ 2 3 _ 6 10 and a profit of $2+3+6+10=21$. As no other configuration has a profit greater than $21$, we print $21$ as our answer.
Sample Input 1
5 4
1
2
3
4
5
Sample Output 1
14
Explanation 1
There are $n=5$ billboards, and we must remove some of them so that no more than $k=4$ billboards are immediately next to one another.
We remove the first billboard, which gives us the configuration _ 2 3 4 5 and a profit of $2+3+4+5=14$. As no other configuration has a profit greater than $\mathbf{14}$, we print $\mathbf{14}$ as our answer. | import sys
class Max_slice:
def __init__(self, w):
self.w = w
self.x = []
def add(self, i, val):
while self.x and val >= self.x[-1][1]:
self.x.pop()
while self.x and self.x[0][0] <= i - self.w:
self.x.pop(0)
self.x.append((i, val))
return self.x[0][1]
def billboards(line):
n, k = map(int, next(line).split())
d = [0]
for _ in range(n):
d.append(int(next(line)) + d[-1])
for _ in range(k - 1):
d.append(0)
f = [0] * k
q0, q1 = 0, 0
r = 0
q = Max_slice(k)
for i in range(n, 0, -1):
f[i % k] = d[i] + q0
_m = q.add(n - i - 1, d[i] + q0)
q0 = q1
q1 = _m - d[i - 1]
if q1 > r:
r = q1
print(r)
billboards(sys.stdin) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF WHILE VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
ADZEN is a popular advertising firm in your city that owns all $n$ billboard locations on Main street. The city council passed a new zoning ordinance mandating that no more than $\boldsymbol{\mbox{k}}$ consecutive billboards may be up at any given time. For example, if there are $n=3$ billboards on Main street and $k=1$, ADZEN must remove either the middle billboard, the first two billboards, the last two billboards or the first and last billboard.
Being a for-profit company, ADZEN wants to lose as little advertising revenue as possible when removing the billboards. They want to comply with the new ordinance in such a way that the remaining billboards maximize their total revenues (i.e., the sum of revenues generated by the billboards left standing on Main street).
Given $n$, $\boldsymbol{\mbox{k}}$, and the revenue of each of the $n$ billboards, find and print the maximum profit that ADZEN can earn while complying with the zoning ordinance. Assume that Main street is a straight, contiguous block of $n$ billboards that can be removed but cannot be reordered in any way.
For example, if there are $n=7$ billboards, and $k=3$ is the maximum number of consecutive billboards that can be active, with $revenues=[5,6,4,2,10,8,4]$, then the maximum revenue that can be generated is $37$: $5+6+4+10+8+4=37$.
Function Description
Complete the billboards function in the editor below. It should return an integer that represents the maximum revenue that can be generated under the rules.
billboards has the following parameter(s):
k: an integer that represents the longest contiguous group of billboards allowed
revenue: an integer array where each element represents the revenue potential for a billboard at that index
Input Format
The first line contains two space-separated integers, $n$ (the number of billboards) and $\boldsymbol{\mbox{k}}$ (the maximum number of billboards that can stand together on any part of the road).
Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains an integer denoting the revenue value of billboard $\boldsymbol{i}$ (where $0\leq i<n$).
Constraints
$1\leq n\leq10^5$
$1\leq k\leq n$
$0\leq\:\text{revenue value of any billboard}\:\leq2\cdot10^9$
Output Format
Print a single integer denoting the maximum profit ADZEN can earn from Main street after complying with the city's ordinance.
Sample Input 0
6 2
1
2
3
1
6
10
Sample Output 0
21
Explanation 0
There are $n=6$ billboards, and we must remove some of them so that no more than $k=2$ billboards are immediately next to one another.
We remove the first and fourth billboards, which gives us the configuration _ 2 3 _ 6 10 and a profit of $2+3+6+10=21$. As no other configuration has a profit greater than $21$, we print $21$ as our answer.
Sample Input 1
5 4
1
2
3
4
5
Sample Output 1
14
Explanation 1
There are $n=5$ billboards, and we must remove some of them so that no more than $k=4$ billboards are immediately next to one another.
We remove the first billboard, which gives us the configuration _ 2 3 4 5 and a profit of $2+3+4+5=14$. As no other configuration has a profit greater than $\mathbf{14}$, we print $\mathbf{14}$ as our answer. | def f(b, index):
return
str = input().split()
n, k = int(str[0]), int(str[1])
b = []
dp = [([0] * 2) for i in range(n)]
for i in range(n):
b.append(int(input()))
dp[0][0] = 0
dp[0][1] = b[0]
max_index = 0
for i in range(1, n):
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1])
if i < k:
dp[i][1] = dp[i - 1][1] + b[i]
else:
s = b[i]
dp[i][1] = b[i] + dp[i - 1][0]
if max_index <= i - k:
for j in range(i - 1, i - k, -1):
s += b[j]
if dp[i][1] < s + dp[j - 1][0]:
max_index = j
dp[i][1] = s + dp[j - 1][0]
else:
dp[i][1] = max(dp[i][1], dp[i - 1][1] + b[i])
print(max(dp[n - 1][0], dp[n - 1][1])) | FUNC_DEF RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER |
ADZEN is a popular advertising firm in your city that owns all $n$ billboard locations on Main street. The city council passed a new zoning ordinance mandating that no more than $\boldsymbol{\mbox{k}}$ consecutive billboards may be up at any given time. For example, if there are $n=3$ billboards on Main street and $k=1$, ADZEN must remove either the middle billboard, the first two billboards, the last two billboards or the first and last billboard.
Being a for-profit company, ADZEN wants to lose as little advertising revenue as possible when removing the billboards. They want to comply with the new ordinance in such a way that the remaining billboards maximize their total revenues (i.e., the sum of revenues generated by the billboards left standing on Main street).
Given $n$, $\boldsymbol{\mbox{k}}$, and the revenue of each of the $n$ billboards, find and print the maximum profit that ADZEN can earn while complying with the zoning ordinance. Assume that Main street is a straight, contiguous block of $n$ billboards that can be removed but cannot be reordered in any way.
For example, if there are $n=7$ billboards, and $k=3$ is the maximum number of consecutive billboards that can be active, with $revenues=[5,6,4,2,10,8,4]$, then the maximum revenue that can be generated is $37$: $5+6+4+10+8+4=37$.
Function Description
Complete the billboards function in the editor below. It should return an integer that represents the maximum revenue that can be generated under the rules.
billboards has the following parameter(s):
k: an integer that represents the longest contiguous group of billboards allowed
revenue: an integer array where each element represents the revenue potential for a billboard at that index
Input Format
The first line contains two space-separated integers, $n$ (the number of billboards) and $\boldsymbol{\mbox{k}}$ (the maximum number of billboards that can stand together on any part of the road).
Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains an integer denoting the revenue value of billboard $\boldsymbol{i}$ (where $0\leq i<n$).
Constraints
$1\leq n\leq10^5$
$1\leq k\leq n$
$0\leq\:\text{revenue value of any billboard}\:\leq2\cdot10^9$
Output Format
Print a single integer denoting the maximum profit ADZEN can earn from Main street after complying with the city's ordinance.
Sample Input 0
6 2
1
2
3
1
6
10
Sample Output 0
21
Explanation 0
There are $n=6$ billboards, and we must remove some of them so that no more than $k=2$ billboards are immediately next to one another.
We remove the first and fourth billboards, which gives us the configuration _ 2 3 _ 6 10 and a profit of $2+3+6+10=21$. As no other configuration has a profit greater than $21$, we print $21$ as our answer.
Sample Input 1
5 4
1
2
3
4
5
Sample Output 1
14
Explanation 1
There are $n=5$ billboards, and we must remove some of them so that no more than $k=4$ billboards are immediately next to one another.
We remove the first billboard, which gives us the configuration _ 2 3 4 5 and a profit of $2+3+4+5=14$. As no other configuration has a profit greater than $\mathbf{14}$, we print $\mathbf{14}$ as our answer. | N, K = map(int, input().split())
X = [0]
for _ in range(N):
X.append(int(input()))
nocut = [(0) for i in range(N + 1)]
cut = [(0) for i in range(N + 1)]
lastcut = 0
roll = 0
for i in range(1, N + 1):
cut[i] = max(cut[i - 1], nocut[i - 1])
if lastcut < i - K:
roll = -1
for j in range(0, K):
if cut[i - K + j] > roll:
roll = cut[i - K + j]
lastcut = i - K + j
roll += X[i - K + j + 1]
nocut[i] = roll
else:
if cut[i - 1] > roll:
roll = cut[i - 1]
lastcut = i - 1
roll += X[i]
nocut[i] = roll
print(max(cut[N], nocut[N])) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
ADZEN is a popular advertising firm in your city that owns all $n$ billboard locations on Main street. The city council passed a new zoning ordinance mandating that no more than $\boldsymbol{\mbox{k}}$ consecutive billboards may be up at any given time. For example, if there are $n=3$ billboards on Main street and $k=1$, ADZEN must remove either the middle billboard, the first two billboards, the last two billboards or the first and last billboard.
Being a for-profit company, ADZEN wants to lose as little advertising revenue as possible when removing the billboards. They want to comply with the new ordinance in such a way that the remaining billboards maximize their total revenues (i.e., the sum of revenues generated by the billboards left standing on Main street).
Given $n$, $\boldsymbol{\mbox{k}}$, and the revenue of each of the $n$ billboards, find and print the maximum profit that ADZEN can earn while complying with the zoning ordinance. Assume that Main street is a straight, contiguous block of $n$ billboards that can be removed but cannot be reordered in any way.
For example, if there are $n=7$ billboards, and $k=3$ is the maximum number of consecutive billboards that can be active, with $revenues=[5,6,4,2,10,8,4]$, then the maximum revenue that can be generated is $37$: $5+6+4+10+8+4=37$.
Function Description
Complete the billboards function in the editor below. It should return an integer that represents the maximum revenue that can be generated under the rules.
billboards has the following parameter(s):
k: an integer that represents the longest contiguous group of billboards allowed
revenue: an integer array where each element represents the revenue potential for a billboard at that index
Input Format
The first line contains two space-separated integers, $n$ (the number of billboards) and $\boldsymbol{\mbox{k}}$ (the maximum number of billboards that can stand together on any part of the road).
Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains an integer denoting the revenue value of billboard $\boldsymbol{i}$ (where $0\leq i<n$).
Constraints
$1\leq n\leq10^5$
$1\leq k\leq n$
$0\leq\:\text{revenue value of any billboard}\:\leq2\cdot10^9$
Output Format
Print a single integer denoting the maximum profit ADZEN can earn from Main street after complying with the city's ordinance.
Sample Input 0
6 2
1
2
3
1
6
10
Sample Output 0
21
Explanation 0
There are $n=6$ billboards, and we must remove some of them so that no more than $k=2$ billboards are immediately next to one another.
We remove the first and fourth billboards, which gives us the configuration _ 2 3 _ 6 10 and a profit of $2+3+6+10=21$. As no other configuration has a profit greater than $21$, we print $21$ as our answer.
Sample Input 1
5 4
1
2
3
4
5
Sample Output 1
14
Explanation 1
There are $n=5$ billboards, and we must remove some of them so that no more than $k=4$ billboards are immediately next to one another.
We remove the first billboard, which gives us the configuration _ 2 3 4 5 and a profit of $2+3+4+5=14$. As no other configuration has a profit greater than $\mathbf{14}$, we print $\mathbf{14}$ as our answer. | NK = input().split(" ")
N = int(NK[0])
K = int(NK[1])
profits = []
for billboard in range(N):
profits.append(int(input()))
total = sum(profits)
if K >= N:
print(total)
elif K == 0:
print(0)
else:
DPtable = []
queue = []
for index, profit in enumerate(profits[: K + 1]):
DPtable.append(profit)
while len(queue) != 0 and profit <= profits[queue[-1]]:
queue.pop()
queue.append(index)
for index, profit in enumerate(profits[K + 1 :]):
index = index + K + 1
DPtable.append(DPtable[queue[0]] + profit)
while len(queue) != 0 and DPtable[index] <= DPtable[queue[-1]]:
queue.pop()
while len(queue) != 0 and queue[0] <= index - K - 1:
queue.pop(0)
queue.append(index)
DPtable.append(DPtable[queue[0]])
print(total - DPtable[-1]) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER |
Moving ahead, Gudi now enters a room where the floor is divided into N x M square tiles, forming a grid with N rows and M columns.
Each square in the grid contains a number of Magical Orbs that Gudi has to absorb if she steps on it. Each Orb increases her Kii power, which is needed to fight the demons. Gudi enters the room on (1, 1) with Zero Kii power and has to makes her way to the exit gate on (N, M), absorbing the Orbs on the way. From the current cell, she can move only to the adjacent cell in East, South or South-East direction i.e. from (i, j) to either (i, j+1) , (i+1, j) or (i+1, j+1).
However, her capacity for the Orbs is limited. If she absorbs more than K Orbs, she will explode with the large amount of Kii energy! Help her find a way to absorb as any many Orbs as she can, without exploding.
Input
The first line contains T. T testcases follow.
First line of each testcase contains 3 space-separated integers, N, M, K.
Each of the following N lines contain M space-separated integers, describing the grid.
Output
Print the maximum number of Orbs that she can absorb without exploding or "-1" (without the quotes) if it can not be done i.e. if there does not exist such a path. Print the answer to each testcase in a new line.
Constraints:
1 ≤ T ≤ 10
1 ≤ N,M ≤ 100
1 ≤ K ≤ 500
1 ≤ Values in the Grid ≤ 50
SAMPLE INPUT
2
3 3 7
2 3 1
6 1 9
8 2 3
3 4 9
2 3 4 1
6 5 5 3
5 2 3 4
SAMPLE OUTPUT
6
-1
Explanation
In the first testcase, she can move on squares (1,1) , (2,2) and (3,3) to complete her journey with 6 Orbs.
In the second testcase, every possible path leads to the absorption of more than 9 Orbs. | T = int(input())
for t in range(0, T):
N, M, K = [int(x) for x in input().split()]
a = []
for i in range(0, N):
a.append([int(x) for x in input().split()])
d = []
for i in range(0, N):
d.append([])
for j in range(0, M):
d[i].append(set())
if a[0][0] <= K:
d[0][0].add(a[0][0])
for i in range(1, M):
for k in d[0][i - 1]:
next_d = k + a[0][i]
if next_d <= K:
d[0][i].add(next_d)
for i in range(1, N):
for k in d[i - 1][0]:
next_d = k + a[i][0]
if next_d <= K:
d[i][0].add(next_d)
for i in range(1, N):
for j in range(1, M):
for k in d[i - 1][j - 1]:
next_d = k + a[i][j]
if next_d <= K:
d[i][j].add(next_d)
for k in d[i][j - 1]:
next_d = k + a[i][j]
if next_d <= K:
d[i][j].add(next_d)
for k in d[i - 1][j]:
next_d = k + a[i][j]
if next_d <= K:
d[i][j].add(next_d)
if len(d[N - 1][M - 1]) == 0:
print(-1)
else:
print(max(d[N - 1][M - 1])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Moving ahead, Gudi now enters a room where the floor is divided into N x M square tiles, forming a grid with N rows and M columns.
Each square in the grid contains a number of Magical Orbs that Gudi has to absorb if she steps on it. Each Orb increases her Kii power, which is needed to fight the demons. Gudi enters the room on (1, 1) with Zero Kii power and has to makes her way to the exit gate on (N, M), absorbing the Orbs on the way. From the current cell, she can move only to the adjacent cell in East, South or South-East direction i.e. from (i, j) to either (i, j+1) , (i+1, j) or (i+1, j+1).
However, her capacity for the Orbs is limited. If she absorbs more than K Orbs, she will explode with the large amount of Kii energy! Help her find a way to absorb as any many Orbs as she can, without exploding.
Input
The first line contains T. T testcases follow.
First line of each testcase contains 3 space-separated integers, N, M, K.
Each of the following N lines contain M space-separated integers, describing the grid.
Output
Print the maximum number of Orbs that she can absorb without exploding or "-1" (without the quotes) if it can not be done i.e. if there does not exist such a path. Print the answer to each testcase in a new line.
Constraints:
1 ≤ T ≤ 10
1 ≤ N,M ≤ 100
1 ≤ K ≤ 500
1 ≤ Values in the Grid ≤ 50
SAMPLE INPUT
2
3 3 7
2 3 1
6 1 9
8 2 3
3 4 9
2 3 4 1
6 5 5 3
5 2 3 4
SAMPLE OUTPUT
6
-1
Explanation
In the first testcase, she can move on squares (1,1) , (2,2) and (3,3) to complete her journey with 6 Orbs.
In the second testcase, every possible path leads to the absorption of more than 9 Orbs. | def process(N, M, MAX):
table[0][0] = [arr[0][0]]
for i in range(1, M):
table[0][i] = [table[0][i - 1][0] + arr[0][i]]
for i in range(1, N):
table[i][0] = [table[i - 1][0][0] + arr[i][0]]
for i in range(1, N):
for j in range(1, M):
ans = set()
val = arr[i][j]
for v in table[i][j - 1]:
tmp = val + v
if tmp <= MAX:
ans.add(tmp)
for v in table[i - 1][j]:
tmp = val + v
if tmp <= MAX:
ans.add(tmp)
for v in table[i - 1][j - 1]:
tmp = val + v
if tmp <= MAX:
ans.add(tmp)
table[i][j] = sorted(ans)
ans = table[N - 1][M - 1]
if ans:
if max(ans) > MAX:
print(-1)
else:
print(max(ans))
else:
print(-1)
T = eval(input())
for i in range(T):
N, M, MAX = list(map(int, input().split(" ")))
arr = []
for V in range(N):
arr.append(list(map(int, input().split(" "))))
table = [[[] for c in range(M)] for r in range(N)]
process(N, M, MAX) | FUNC_DEF ASSIGN VAR NUMBER NUMBER LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
This is the first subtask of problem F. The only differences between this and the second subtask are the constraints on the value of $m$ and the time limit. You need to solve both subtasks in order to hack this one.
There are $n+1$ distinct colours in the universe, numbered $0$ through $n$. There is a strip of paper $m$ centimetres long initially painted with colour $0$.
Alice took a brush and painted the strip using the following process. For each $i$ from $1$ to $n$, in this order, she picks two integers $0 \leq a_i < b_i \leq m$, such that the segment $[a_i, b_i]$ is currently painted with a single colour, and repaints it with colour $i$.
Alice chose the segments in such a way that each centimetre is now painted in some colour other than $0$. Formally, the segment $[i-1, i]$ is painted with colour $c_i$ ($c_i \neq 0$). Every colour other than $0$ is visible on the strip.
Count the number of different pairs of sequences $\{a_i\}_{i=1}^n$, $\{b_i\}_{i=1}^n$ that result in this configuration.
Since this number may be large, output it modulo $998244353$.
-----Input-----
The first line contains a two integers $n$, $m$ ($1 \leq n \leq 500$, $n = m$) — the number of colours excluding the colour $0$ and the length of the paper, respectively.
The second line contains $m$ space separated integers $c_1, c_2, \ldots, c_m$ ($1 \leq c_i \leq n$) — the colour visible on the segment $[i-1, i]$ after the process ends. It is guaranteed that for all $j$ between $1$ and $n$ there is an index $k$ such that $c_k = j$.
Note that since in this subtask $n = m$, this means that $c$ is a permutation of integers $1$ through $n$.
-----Output-----
Output a single integer — the number of ways Alice can perform the painting, modulo $998244353$.
-----Examples-----
Input
3 3
1 2 3
Output
5
Input
7 7
4 5 1 6 2 3 7
Output
165
-----Note-----
In the first example, there are $5$ ways, all depicted in the figure below. Here, $0$ is white, $1$ is red, $2$ is green and $3$ is blue.
[Image]
Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour $2$.
[Image] | n, m = map(int, input().split())
l = list(map(int, input().split()))
index = [[(0) for i in range(n)] for j in range(n)]
for i in range(n):
mini = 10000000000000
for j in range(i, n):
if l[j] < mini:
inde = j
mini = l[j]
index[i][j] = inde
prime = 998244353
d = {}
val = [[(1) for i in range(n + 1)] for j in range(n + 1)]
for i in range(n):
for j in range(n - i):
if i == 0:
val[j][j + i] = 1
elif i == 1:
val[j][j + i] = 2
else:
ind = index[j][j + i]
sumap = 0
sumak = 0
for p in range(j, ind + 1):
sumap += val[j][p - 1] * val[p][ind - 1] % prime
for k in range(ind, j + i + 1):
sumak += val[ind + 1][k] * val[k + 1][j + i] % prime
val[j][j + i] = sumap * sumak % prime
print(val[0][n - 1]) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
This is the first subtask of problem F. The only differences between this and the second subtask are the constraints on the value of $m$ and the time limit. You need to solve both subtasks in order to hack this one.
There are $n+1$ distinct colours in the universe, numbered $0$ through $n$. There is a strip of paper $m$ centimetres long initially painted with colour $0$.
Alice took a brush and painted the strip using the following process. For each $i$ from $1$ to $n$, in this order, she picks two integers $0 \leq a_i < b_i \leq m$, such that the segment $[a_i, b_i]$ is currently painted with a single colour, and repaints it with colour $i$.
Alice chose the segments in such a way that each centimetre is now painted in some colour other than $0$. Formally, the segment $[i-1, i]$ is painted with colour $c_i$ ($c_i \neq 0$). Every colour other than $0$ is visible on the strip.
Count the number of different pairs of sequences $\{a_i\}_{i=1}^n$, $\{b_i\}_{i=1}^n$ that result in this configuration.
Since this number may be large, output it modulo $998244353$.
-----Input-----
The first line contains a two integers $n$, $m$ ($1 \leq n \leq 500$, $n = m$) — the number of colours excluding the colour $0$ and the length of the paper, respectively.
The second line contains $m$ space separated integers $c_1, c_2, \ldots, c_m$ ($1 \leq c_i \leq n$) — the colour visible on the segment $[i-1, i]$ after the process ends. It is guaranteed that for all $j$ between $1$ and $n$ there is an index $k$ such that $c_k = j$.
Note that since in this subtask $n = m$, this means that $c$ is a permutation of integers $1$ through $n$.
-----Output-----
Output a single integer — the number of ways Alice can perform the painting, modulo $998244353$.
-----Examples-----
Input
3 3
1 2 3
Output
5
Input
7 7
4 5 1 6 2 3 7
Output
165
-----Note-----
In the first example, there are $5$ ways, all depicted in the figure below. Here, $0$ is white, $1$ is red, $2$ is green and $3$ is blue.
[Image]
Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour $2$.
[Image] | def main():
line = input().split()
n = int(line[0])
line = input().split()
v = [int(x) for x in line]
mod = 998244353
dp = [([1] * (n + 5)) for i in range(n + 5)]
for sz in range(2, n + 1):
for lo in range(1, n - sz + 2):
hi = lo + sz - 1
pos, num = -1, n + 5
for k in range(lo, hi + 1):
if v[k - 1] < num:
num = v[k - 1]
pos = k
s1, s2 = 0, 0
for k in range(lo, pos + 1):
cnt = dp[lo][k - 1] * dp[k][pos - 1] % mod
s1 = (s1 + cnt) % mod
for k in range(pos, hi + 1):
cnt = dp[pos + 1][k] * dp[k + 1][hi] % mod
s2 = (s2 + cnt) % mod
dp[lo][hi] = s1 * s2 % mod
print(dp[1][n])
main() | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR |
This is the first subtask of problem F. The only differences between this and the second subtask are the constraints on the value of $m$ and the time limit. You need to solve both subtasks in order to hack this one.
There are $n+1$ distinct colours in the universe, numbered $0$ through $n$. There is a strip of paper $m$ centimetres long initially painted with colour $0$.
Alice took a brush and painted the strip using the following process. For each $i$ from $1$ to $n$, in this order, she picks two integers $0 \leq a_i < b_i \leq m$, such that the segment $[a_i, b_i]$ is currently painted with a single colour, and repaints it with colour $i$.
Alice chose the segments in such a way that each centimetre is now painted in some colour other than $0$. Formally, the segment $[i-1, i]$ is painted with colour $c_i$ ($c_i \neq 0$). Every colour other than $0$ is visible on the strip.
Count the number of different pairs of sequences $\{a_i\}_{i=1}^n$, $\{b_i\}_{i=1}^n$ that result in this configuration.
Since this number may be large, output it modulo $998244353$.
-----Input-----
The first line contains a two integers $n$, $m$ ($1 \leq n \leq 500$, $n = m$) — the number of colours excluding the colour $0$ and the length of the paper, respectively.
The second line contains $m$ space separated integers $c_1, c_2, \ldots, c_m$ ($1 \leq c_i \leq n$) — the colour visible on the segment $[i-1, i]$ after the process ends. It is guaranteed that for all $j$ between $1$ and $n$ there is an index $k$ such that $c_k = j$.
Note that since in this subtask $n = m$, this means that $c$ is a permutation of integers $1$ through $n$.
-----Output-----
Output a single integer — the number of ways Alice can perform the painting, modulo $998244353$.
-----Examples-----
Input
3 3
1 2 3
Output
5
Input
7 7
4 5 1 6 2 3 7
Output
165
-----Note-----
In the first example, there are $5$ ways, all depicted in the figure below. Here, $0$ is white, $1$ is red, $2$ is green and $3$ is blue.
[Image]
Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour $2$.
[Image] | import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [(a - 1) for a in A]
MININDLIST = [([i] * m) for i in range(m)]
for i in range(m):
MIN = A[i]
MININDNOW = i
for j in range(i, m):
if A[j] < MIN:
MIN = A[j]
MININDNOW = j
MININDLIST[i][j] = MININDNOW
mod = 998244353
SCORE = [0] * (m + 1) ** 2
def calc(i, j, m):
if SCORE[i * (m + 1) + j] != 0:
return SCORE[i * (m + 1) + j]
if j == i + 1 or i == j:
return 1
MININD = MININDLIST[i][j - 1]
ANS1 = ANS2 = 0
for mi in range(i, MININD + 1):
ANS1 = (ANS1 + calc(i, mi, m) * calc(mi, MININD, m)) % mod
for Mi in range(MININD + 1, j + 1):
ANS2 = (ANS2 + calc(MININD + 1, Mi, m) * calc(Mi, j, m)) % mod
SCORE[i * (m + 1) + j] = ANS1 * ANS2 % mod
return SCORE[i * (m + 1) + j]
print(calc(0, m, m)) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR |
This is the first subtask of problem F. The only differences between this and the second subtask are the constraints on the value of $m$ and the time limit. You need to solve both subtasks in order to hack this one.
There are $n+1$ distinct colours in the universe, numbered $0$ through $n$. There is a strip of paper $m$ centimetres long initially painted with colour $0$.
Alice took a brush and painted the strip using the following process. For each $i$ from $1$ to $n$, in this order, she picks two integers $0 \leq a_i < b_i \leq m$, such that the segment $[a_i, b_i]$ is currently painted with a single colour, and repaints it with colour $i$.
Alice chose the segments in such a way that each centimetre is now painted in some colour other than $0$. Formally, the segment $[i-1, i]$ is painted with colour $c_i$ ($c_i \neq 0$). Every colour other than $0$ is visible on the strip.
Count the number of different pairs of sequences $\{a_i\}_{i=1}^n$, $\{b_i\}_{i=1}^n$ that result in this configuration.
Since this number may be large, output it modulo $998244353$.
-----Input-----
The first line contains a two integers $n$, $m$ ($1 \leq n \leq 500$, $n = m$) — the number of colours excluding the colour $0$ and the length of the paper, respectively.
The second line contains $m$ space separated integers $c_1, c_2, \ldots, c_m$ ($1 \leq c_i \leq n$) — the colour visible on the segment $[i-1, i]$ after the process ends. It is guaranteed that for all $j$ between $1$ and $n$ there is an index $k$ such that $c_k = j$.
Note that since in this subtask $n = m$, this means that $c$ is a permutation of integers $1$ through $n$.
-----Output-----
Output a single integer — the number of ways Alice can perform the painting, modulo $998244353$.
-----Examples-----
Input
3 3
1 2 3
Output
5
Input
7 7
4 5 1 6 2 3 7
Output
165
-----Note-----
In the first example, there are $5$ ways, all depicted in the figure below. Here, $0$ is white, $1$ is red, $2$ is green and $3$ is blue.
[Image]
Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour $2$.
[Image] | class SparseTable:
def __init__(self, array, n):
n = len(array)
self.row_size = n.bit_length()
self.log_table = [0] * (n + 1)
for i in range(2, n + 1):
self.log_table[i] = self.log_table[i // 2] + 1
self.sparse_table = [([0] * n) for _ in range(self.row_size)]
for i in range(n):
self.sparse_table[0][i] = array[i]
for row in range(1, self.row_size):
for i in range(n - (1 << row) + 1):
self.sparse_table[row][i] = self._merge(
self.sparse_table[row - 1][i],
self.sparse_table[row - 1][i + (1 << row - 1)],
)
def _merge(self, num1, num2):
return min(num1, num2)
def query(self, l, r):
row = self.log_table[r - l]
return self._merge(
self.sparse_table[row][l], self.sparse_table[row][r - (1 << row)]
)
n, m = map(int, input().split())
a = list(map(int, input().split()))
MOD = 998244353
sp = SparseTable(a, n)
to_ind = {}
for i in range(n):
to_ind[a[i]] = i
dp = [([-1] * (n + 1)) for i in range(n + 1)]
def solve(l, r):
if dp[l][r] != -1:
return dp[l][r]
if l == r:
dp[l][r] = 1
return 1
if r - l == 1:
dp[l][r] = 1
return 1
ind = to_ind[sp.query(l, r)]
res1 = 0
res2 = 0
for i in range(ind + 1, r + 1):
res1 += solve(ind + 1, i) * solve(i, r)
res1 %= MOD
for i in range(l, ind + 1):
res2 += solve(l, i) * solve(i, ind)
res2 %= MOD
dp[l][r] = max(res1, 1) * max(res2, 1)
dp[l][r] %= MOD
return dp[l][r]
print(solve(0, n) % MOD) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR |
Given string s consisting of digits 0-9 and a number N, the task is to count the number of subsequences that are divisible by N.
Note: Answer can be large, output answer modulo 10^{9} + 7
Example 1:
Input: s = "1234", N = 4
Output: 4
Explanation: The subsequences 4, 12, 24 and
124 are divisible by 4.
Example 2:
Input: s = "330", N = 6
Output: 4
Explanation: The subsequences 30, 30, 330
and 0 are divisible by 6.
Your Task:
You don't need to read input or print anything. Complete the function countDivisibleSubseq() which takes s and N as input parameters and returns the integer value
Expected Time Complexity: O(|s|*N)
Expected Auxiliary Space: O(|s|*N)
Constraints:
1 ≤ |s|*N ≤ 10^{6} | class Solution:
def countDivisibleSubseq(self, str, n):
mod = int(1000000000.0 + 7)
l = len(str)
dp = [[(0) for x in range(l)] for y in range(n)]
dp[int(str[0]) % n][0] += 1
for i in range(1, l):
dp[int(str[i]) % n][i] += 1
for j in range(n):
dp[j][i] += dp[j][i - 1]
dp[(j * 10 + int(str[i])) % n][i] += dp[j][i - 1]
return dp[0][l - 1] % mod | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR |
Given string s consisting of digits 0-9 and a number N, the task is to count the number of subsequences that are divisible by N.
Note: Answer can be large, output answer modulo 10^{9} + 7
Example 1:
Input: s = "1234", N = 4
Output: 4
Explanation: The subsequences 4, 12, 24 and
124 are divisible by 4.
Example 2:
Input: s = "330", N = 6
Output: 4
Explanation: The subsequences 30, 30, 330
and 0 are divisible by 6.
Your Task:
You don't need to read input or print anything. Complete the function countDivisibleSubseq() which takes s and N as input parameters and returns the integer value
Expected Time Complexity: O(|s|*N)
Expected Auxiliary Space: O(|s|*N)
Constraints:
1 ≤ |s|*N ≤ 10^{6} | class Solution:
def countDivisibleSubseq(self, s, N):
memo = {}
s = str(s)
return (getcount(s, 0, 0, N, memo) - 1) % (10**9 + 7)
def getcount(s, idx, rem, k, memo):
n = len(s)
if idx == n:
return 1 if rem == 0 else 0
if hashed(idx, rem) in memo:
return memo[hashed(idx, rem)]
count = 0
count += getcount(s, idx + 1, rem, k, memo)
count += getcount(s, idx + 1, (rem * 10 + int(s[idx])) % k, k, memo)
memo[hashed(idx, rem)] = count
return count
def hashed(a, b):
return str(a) + ":" + str(b) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR |
Given string s consisting of digits 0-9 and a number N, the task is to count the number of subsequences that are divisible by N.
Note: Answer can be large, output answer modulo 10^{9} + 7
Example 1:
Input: s = "1234", N = 4
Output: 4
Explanation: The subsequences 4, 12, 24 and
124 are divisible by 4.
Example 2:
Input: s = "330", N = 6
Output: 4
Explanation: The subsequences 30, 30, 330
and 0 are divisible by 6.
Your Task:
You don't need to read input or print anything. Complete the function countDivisibleSubseq() which takes s and N as input parameters and returns the integer value
Expected Time Complexity: O(|s|*N)
Expected Auxiliary Space: O(|s|*N)
Constraints:
1 ≤ |s|*N ≤ 10^{6} | class Solution:
def countDivisibleSubseq(self, s, N):
MOD = 10**9 + 7
def rec(index, rem):
if index == len(s):
return int(rem == 0)
nt = rec(index + 1, rem)
tk = rec(index + 1, (rem * 10 + int(s[index])) % N)
return nt + tk
dp = [[(-1) for _ in range(N + 1)] for _ in range(len(s) + 1)]
def mem(index, rem):
if index == len(s):
return int(rem == 0)
if dp[index][rem] != -1:
return dp[index][rem]
nt = mem(index + 1, rem)
tk = mem(index + 1, (rem * 10 + int(s[index])) % N)
dp[index][rem] = nt + tk
return dp[index][rem]
return mem(0, N) % MOD
return rec(0, N) % MOD | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR NUMBER VAR VAR |
Given string s consisting of digits 0-9 and a number N, the task is to count the number of subsequences that are divisible by N.
Note: Answer can be large, output answer modulo 10^{9} + 7
Example 1:
Input: s = "1234", N = 4
Output: 4
Explanation: The subsequences 4, 12, 24 and
124 are divisible by 4.
Example 2:
Input: s = "330", N = 6
Output: 4
Explanation: The subsequences 30, 30, 330
and 0 are divisible by 6.
Your Task:
You don't need to read input or print anything. Complete the function countDivisibleSubseq() which takes s and N as input parameters and returns the integer value
Expected Time Complexity: O(|s|*N)
Expected Auxiliary Space: O(|s|*N)
Constraints:
1 ≤ |s|*N ≤ 10^{6} | class Solution:
def countDivisibleSubseq(self, s, N):
mod = int(1000000007)
l = len(s)
dp = [[(0) for x in range(n)] for y in range(l)]
for i in range(0, l):
dp[i][int(s[i]) % N] = 1
dp[i][int(s[i]) % N] = dp[i][int(s[i]) % N] % mod
for i in range(1, l):
for j in range(0, N):
dp[i][j] += dp[i - 1][j]
dp[i][j] = dp[i][j] % mod
dp[i][(j * 10 + int(s[i])) % N] += dp[i - 1][j]
dp[i][(j * 10 + int(s[i])) % N] = dp[i][(j * 10 + int(s[i])) % N] % mod
return dp[l - 1][0] % mod | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR |
You are given a directed graph $G$ which can contain loops (edges from a vertex to itself). Multi-edges are absent in $G$ which means that for all ordered pairs $(u, v)$ exists at most one edge from $u$ to $v$. Vertices are numbered from $1$ to $n$.
A path from $u$ to $v$ is a sequence of edges such that:
vertex $u$ is the start of the first edge in the path;
vertex $v$ is the end of the last edge in the path;
for all pairs of adjacent edges next edge starts at the vertex that the previous edge ends on.
We will assume that the empty sequence of edges is a path from $u$ to $u$.
For each vertex $v$ output one of four values:
$0$, if there are no paths from $1$ to $v$;
$1$, if there is only one path from $1$ to $v$;
$2$, if there is more than one path from $1$ to $v$ and the number of paths is finite;
$-1$, if the number of paths from $1$ to $v$ is infinite.
Let's look at the example shown in the figure.
Then:
the answer for vertex $1$ is $1$: there is only one path from $1$ to $1$ (path with length $0$);
the answer for vertex $2$ is $0$: there are no paths from $1$ to $2$;
the answer for vertex $3$ is $1$: there is only one path from $1$ to $3$ (it is the edge $(1, 3)$);
the answer for vertex $4$ is $2$: there are more than one paths from $1$ to $4$ and the number of paths are finite (two paths: $[(1, 3), (3, 4)]$ and $[(1, 4)]$);
the answer for vertex $5$ is $-1$: the number of paths from $1$ to $5$ is infinite (the loop can be used in a path many times);
the answer for vertex $6$ is $-1$: the number of paths from $1$ to $6$ is infinite (the loop can be used in a path many times).
-----Input-----
The first contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow. Before each test case, there is an empty line.
The first line of the test case contains two integers $n$ and $m$ ($1 \le n \le 4 \cdot 10^5, 0 \le m \le 4 \cdot 10^5$) — numbers of vertices and edges in graph respectively. The next $m$ lines contain edges descriptions. Each line contains two integers $a_i$, $b_i$ ($1 \le a_i, b_i \le n$) — the start and the end of the $i$-th edge. The vertices of the graph are numbered from $1$ to $n$. The given graph can contain loops (it is possible that $a_i = b_i$), but cannot contain multi-edges (it is not possible that $a_i = a_j$ and $b_i = b_j$ for $i \ne j$).
The sum of $n$ over all test cases does not exceed $4 \cdot 10^5$. Similarly, the sum of $m$ over all test cases does not exceed $4 \cdot 10^5$.
-----Output-----
Output $t$ lines. The $i$-th line should contain an answer for the $i$-th test case: a sequence of $n$ integers from $-1$ to $2$.
-----Examples-----
Input
5
6 7
1 4
1 3
3 4
4 5
2 1
5 5
5 6
1 0
3 3
1 2
2 3
3 1
5 0
4 4
1 2
2 3
1 4
4 3
Output
1 0 1 2 -1 -1
1
-1 -1 -1
1 0 0 0 0
1 1 2 1
-----Note-----
None | import sys
sys.setrecursionlimit(600000)
def dfs_1(v):
q = [v, 0]
while len(q) != 0:
i = q.pop()
v = q.pop()
if i == 0:
color[v] = 1
if i == len(g[v]):
color[v] = 2
continue
q.append(v)
q.append(i + 1)
w = g[v][i]
if color[w] == 0:
q.append(w)
q.append(0)
elif color[w] == 1:
in_cycles.append(w)
elif color[w] == 2:
more_than_one.append(w)
def dfs_2(v):
q = [v, 0]
while len(q) != 0:
i = q.pop()
v = q.pop()
if i == 0:
color_tmp[v] = 1
if i == len(g[v]):
color_tmp[v] = 2
continue
q.append(v)
q.append(i + 1)
w = g[v][i]
if color_tmp[w] == 0:
q.append(w)
q.append(0)
def fill_not_accessible(color, answer):
for i, c in enumerate(color):
if c == 0:
answer[i] = 0
def fill_more_than_one(color, answer):
for i, c in enumerate(color):
if c == 2:
answer[i] = 2
def fill_inf(color, answer):
for i, c in enumerate(color):
if c == 2:
answer[i] = -1
t = int(input())
for _ in range(t):
input()
n, m = map(int, input().split())
color = [(0) for _ in range(n + 1)]
g = {i: [] for i in range(n + 1)}
in_cycles = []
more_than_one = []
answer = [(1) for _ in range(n + 1)]
for _ in range(m):
a, b = map(int, input().split())
g[a].append(b)
dfs_1(1)
fill_not_accessible(color, answer)
color_tmp = [(0) for _ in range(n + 1)]
for x in more_than_one:
if color_tmp[x] == 0:
dfs_2(x)
fill_more_than_one(color_tmp, answer)
color_tmp = [(0) for _ in range(n + 1)]
for x in in_cycles:
if color_tmp[x] == 0:
dfs_2(x)
fill_inf(color_tmp, answer)
print(" ".join(map(str, answer[1:]))) | IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER 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 NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER |
You are given a directed graph $G$ which can contain loops (edges from a vertex to itself). Multi-edges are absent in $G$ which means that for all ordered pairs $(u, v)$ exists at most one edge from $u$ to $v$. Vertices are numbered from $1$ to $n$.
A path from $u$ to $v$ is a sequence of edges such that:
vertex $u$ is the start of the first edge in the path;
vertex $v$ is the end of the last edge in the path;
for all pairs of adjacent edges next edge starts at the vertex that the previous edge ends on.
We will assume that the empty sequence of edges is a path from $u$ to $u$.
For each vertex $v$ output one of four values:
$0$, if there are no paths from $1$ to $v$;
$1$, if there is only one path from $1$ to $v$;
$2$, if there is more than one path from $1$ to $v$ and the number of paths is finite;
$-1$, if the number of paths from $1$ to $v$ is infinite.
Let's look at the example shown in the figure.
Then:
the answer for vertex $1$ is $1$: there is only one path from $1$ to $1$ (path with length $0$);
the answer for vertex $2$ is $0$: there are no paths from $1$ to $2$;
the answer for vertex $3$ is $1$: there is only one path from $1$ to $3$ (it is the edge $(1, 3)$);
the answer for vertex $4$ is $2$: there are more than one paths from $1$ to $4$ and the number of paths are finite (two paths: $[(1, 3), (3, 4)]$ and $[(1, 4)]$);
the answer for vertex $5$ is $-1$: the number of paths from $1$ to $5$ is infinite (the loop can be used in a path many times);
the answer for vertex $6$ is $-1$: the number of paths from $1$ to $6$ is infinite (the loop can be used in a path many times).
-----Input-----
The first contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow. Before each test case, there is an empty line.
The first line of the test case contains two integers $n$ and $m$ ($1 \le n \le 4 \cdot 10^5, 0 \le m \le 4 \cdot 10^5$) — numbers of vertices and edges in graph respectively. The next $m$ lines contain edges descriptions. Each line contains two integers $a_i$, $b_i$ ($1 \le a_i, b_i \le n$) — the start and the end of the $i$-th edge. The vertices of the graph are numbered from $1$ to $n$. The given graph can contain loops (it is possible that $a_i = b_i$), but cannot contain multi-edges (it is not possible that $a_i = a_j$ and $b_i = b_j$ for $i \ne j$).
The sum of $n$ over all test cases does not exceed $4 \cdot 10^5$. Similarly, the sum of $m$ over all test cases does not exceed $4 \cdot 10^5$.
-----Output-----
Output $t$ lines. The $i$-th line should contain an answer for the $i$-th test case: a sequence of $n$ integers from $-1$ to $2$.
-----Examples-----
Input
5
6 7
1 4
1 3
3 4
4 5
2 1
5 5
5 6
1 0
3 3
1 2
2 3
3 1
5 0
4 4
1 2
2 3
1 4
4 3
Output
1 0 1 2 -1 -1
1
-1 -1 -1
1 0 0 0 0
1 1 2 1
-----Note-----
None | import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
def scc_dfs1(s, links, status, postorder):
stack = [s]
status[s] = 0
while stack:
v = stack[-1]
limit = len(links[v])
while status[v] < limit:
u = links[v][status[v]]
status[v] += 1
if status[u] != -1:
continue
stack.append(u)
status[u] = 0
break
else:
stack.pop()
postorder.append(v)
return
def scc_dfs2(s, rev_links, status):
stack = [s]
status[s] = -1
scc = [s]
while stack:
v = stack.pop()
for u in rev_links[v]:
if status[u] != -1:
stack.append(u)
status[u] = -1
scc.append(u)
return scc
def strongly_connected_components(n, links, rev_links):
status = [-1] * n
postorder = []
for v in range(n):
if status[v] != -1:
continue
scc_dfs1(v, links, status, postorder)
postorder.reverse()
sccs = []
for v in postorder:
if status[v] == -1:
continue
sccs.append(scc_dfs2(v, rev_links, status))
return sccs
def main():
t = int(input())
INF = pow(10, 18)
for _ in range(t):
dummy = input()
n, m = map(int, input().split())
G = [[] for _ in range(n)]
RG = [[] for _ in range(n)]
self_v = set([])
for _ in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
G[a].append(b)
RG[b].append(a)
if a == b:
self_v.add(a)
TT = strongly_connected_components(n, G, RG)
ans = [0] * n
ans[0] = 1
for cluster in TT:
if len(cluster) > 1:
Flag = True
for p in cluster:
if not Flag:
break
for pre in RG[p]:
if ans[pre] == -1:
Flag = False
break
ans[p] += ans[pre]
if ans[p] > 0:
Flag = False
break
if not Flag:
for p in cluster:
ans[p] = -1
else:
for p in cluster:
ans[p] = 0
else:
p = cluster[0]
if p in self_v:
Flag = True
for pre in RG[p]:
if ans[pre] == -1:
Flag = False
break
ans[p] += ans[pre]
if ans[p] > 0:
Flag = False
break
if not Flag:
ans[p] = -1
else:
for pre in RG[p]:
if ans[pre] == -1:
ans[p] = -1
break
else:
ans[p] += ans[pre]
ans[p] = min(ans[p], 2)
print(*ans)
main() | IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR 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 VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
n = len(string)
dp = [[(-1) for i in range(n)] for j in range(n)]
def recursive(string, left=0, right=n - 1):
if left > right:
return True
if left == right:
dp[left][right] = True
return True
if dp[left][right] != -1:
return dp[left][right]
if string[left] == string[right]:
dp[left][right] = recursive(string, left + 1, right - 1)
else:
dp[left][right] = False
recursive(string, left + 1, right)
recursive(string, left, right - 1)
return dp[left][right]
recursive(string)
dp2 = [[(-1) for i in range(n)] for j in range(n)]
def findminimum(string, dp, dp2, left=0, right=0):
if right == len(string) == left:
return 0
if left > right or right >= len(string):
return float("inf")
if dp[left][right] != -1:
return dp[left][right]
dp[left][right] = findminimum(string, dp, dp2, left, right + 1)
if dp2[left][right]:
dp[left][right] = min(
dp[left][right],
1 + findminimum(string, dp, dp2, right + 1, right + 1),
)
return dp[left][right]
return findminimum(string, dp2, dp) - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, str):
n = len(str)
dp = [0] * n
palindrome = [([False] * n) for _ in range(n)]
for i in range(n):
min_cut = i
for j in range(i + 1):
if str[i] == str[j] and (i - j < 2 or palindrome[j + 1][i - 1]):
palindrome[j][i] = True
if j == 0:
min_cut = 0
else:
min_cut = min(min_cut, dp[j - 1] + 1)
dp[i] = min_cut
return dp[n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
mp = {}
res = self.solve(string, mp)
return res
def solve(self, string, mp):
if len(string) == 0 or string == string[::-1]:
return 0
if string in mp:
return mp[string]
res = float("inf")
for i in range(len(string)):
prefix = string[: i + 1]
if prefix == prefix[::-1]:
res = min(res, 1 + self.solve(string[i + 1 :], mp))
mp[string] = res
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, s):
def calc(i):
if i == len(s):
return 0
if dp[i] != -1:
return dp[i]
else:
mxc = float("inf")
for k in range(i, len(s)):
if s[i : k + 1] == s[i : k + 1][::-1]:
cnt = 1 + calc(k + 1)
mxc = min(mxc, cnt)
dp[i] = mxc
return dp[i]
dp = [(-1) for i in range(len(s) + 1)]
return calc(0) - 1 | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
n = len(string)
dp = [0] * (n + 1)
dp[n] = 0
for i in reversed(range(n)):
minCost = float("inf")
for j in range(i, n):
cur_string = string[i : j + 1]
if self.isPalindrome(cur_string):
cost = 1 + dp[j + 1]
minCost = min(minCost, cost)
dp[i] = minCost
return dp[0] - 1
def isPalindrome(self, cur_string):
left = 0
right = len(cur_string) - 1
while left < right:
if cur_string[left] != cur_string[right]:
return False
left += 1
right -= 1
return True | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
n = len(string)
dp = [(0) for _ in range(n + 1)]
for i in range(n - 1, -1, -1):
minCuts = float("inf")
for j in range(i, n):
if self.isPalindrome(string[i : j + 1]):
cuts = 1 + dp[j + 1]
minCuts = min(cuts, minCuts)
dp[i] = minCuts
return dp[0] - 1
def isPalindrome(self, x):
return x == x[::-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
n = len(string)
def ispalindrome(strs, st, end):
while st <= end:
if strs[st] == strs[end]:
st += 1
end -= 1
else:
return False
return True
dp = [-1] * (n + 1)
def findsol(string, i, n):
if i == n:
return 0
if dp[i] != -1:
return dp[i]
minval = 999999999
for x in range(i + 1, n + 1):
if ispalindrome(string, i, x - 1) == True:
minval = min(minval, findsol(string, x, n))
dp[i] = minval + 1
return minval + 1
return findsol(string, 0, n) - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, str):
n = len(str)
P = [[(False) for _ in range(n)] for _ in range(n)]
for i in range(n):
P[i][i] = True
for L in range(2, n + 1):
for i in range(n - L + 1):
j = i + L - 1
if L == 2:
P[i][j] = str[i] == str[j]
else:
P[i][j] = str[i] == str[j] and P[i + 1][j - 1]
C = [float("inf") for _ in range(n)]
for j in range(n):
if P[0][j]:
C[j] = 0
else:
for i in range(j):
if P[i + 1][j]:
C[j] = min(C[j], C[i] + 1)
return C[n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | def check(s):
if s == s[::-1]:
return True
else:
return False
class Solution:
def palindromicPartition(self, string):
dp = {}
def fun(s, i, j):
if i >= j:
return 0
if check(s[i:j]):
return 0
if (i, j) in dp:
return dp[i, j]
m = float("inf")
for k in range(i, j):
if check(s[i : k + 1]):
temp = fun(s, k + 1, j) + 1
m = min(m, temp)
dp[i, j] = m
return dp[i, j]
return fun(string, 0, len(string)) | FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, S):
def palind(f):
l = len(f)
for i in range(l // 2):
if f[i] != f[l - i - 1]:
return 0
return 1
def manu(i, n, s):
if i == n:
return 0
if dp[i][n] != -1:
return dp[i][n]
x = ""
mini = 10**9
for j in range(i, n):
x += s[j]
if palind(x):
c = 1 + manu(j + 1, n, s)
mini = min(mini, c)
dp[i][n] = mini
return mini
dp = [[(-1) for i in range(len(S) + 1)] for i in range(len(S) + 1)]
return manu(0, len(S), S) - 1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, s):
n = len(s)
dp = [0] * (n + 1)
p = [([False] * (n + 1)) for _ in range(n + 1)]
for i in range(1, n + 1):
cost = float("inf")
for k in range(i, 0, -1):
if s[k - 1] == s[i - 1] and (i - k <= 2 or p[k][i - 2]):
p[k - 1][i - 1] = True
curr = 1 + dp[k - 1]
cost = min(cost, curr)
dp[i] = cost
return dp[n] - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def ispalindrome(self, start, end, string):
while start < end:
if string[start] != string[end]:
return False
start += 1
end -= 1
return True
def palindromicPartition(self, string):
n = len(string)
dp = [(0) for i in range(n + 1)]
def rec(idx):
if idx == n:
return 0
if dp[idx] != -1:
return dp[idx]
maxi = float("inf")
p, np = float("inf"), float("inf")
for i in range(idx, n):
if self.ispalindrome(idx, i, string):
p = 1 + rec(i + 1)
maxi = min(maxi, p)
dp[idx] = maxi
return dp[idx]
for idx in range(n - 1, -1, -1):
maxi = float("inf")
p, np = float("inf"), float("inf")
for i in range(idx, n):
if self.ispalindrome(idx, i, string):
p = 1 + dp[i + 1]
maxi = min(maxi, p)
dp[idx] = maxi
return dp[0] - 1 | CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, S):
N = len(S)
isp = [([True] * N) for _ in range(N)]
for add in range(1, N):
for j in range(N - add):
isp[j][j + add] = S[j] == S[j + add] and isp[j + 1][j + add - 1]
dp = [1] * N + [0]
for i in range(1, N):
v = 1 + dp[i - 1]
for j in range(i):
if S[i] == S[j] and isp[j + 1][i - 1]:
v = min(v, dp[j - 1] + 1)
dp[i] = v
return dp[N - 1] - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
dp = [(0) for i in range(len(string))]
p = [[(False) for i in range(len(string))] for j in range(len(string))]
for i in range(len(string)):
m = i
for j in range(i + 1):
if string[i] == string[j] and (i - j < 2 or p[j + 1][i - 1]):
p[j][i] = True
m = min(m, 0 if j == 0 else dp[j - 1] + 1)
dp[i] = m
return dp[len(string) - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
n = len(string)
dp = [(-1) for i in range(n)]
return self.f(0, n, string, dp) - 1
def f(self, i, n, string, dp):
mincost = float("inf")
if i == n:
return 0
if dp[i] != -1:
return dp[i]
for j in range(i, n):
if self.isPalindrome(string, i, j):
cost = 1 + self.f(j + 1, n, string, dp)
mincost = min(mincost, cost)
dp[i] = mincost
return dp[i]
def isPalindrome(self, string, i, j):
while i < j:
if string[i] != string[j]:
return False
i += 1
j -= 1
return True | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def __init__(self):
self.res = -float("inf")
def check_palin(self, x):
n = len(x)
for i in range(n // 2):
if x[i] != x[-i - 1]:
return False
return True
def solve(self, i, j, st, dp):
if i >= j:
return 0
if self.check_palin(st[i : j + 1]):
return 0
if dp[i][j] != -1:
return dp[i][j]
min_cut = float("inf")
for ind in range(i, j):
if self.check_palin(st[i : ind + 1]):
x = self.solve(ind + 1, j, st, dp) + 1
min_cut = min(min_cut, x)
dp[i][j] = min_cut
return dp[i][j]
def palindromicPartition(self, string):
n = len(string)
dp = [[(-1) for i in range(n + 2)] for j in range(n + 2)]
r = self.solve(0, n - 1, string, dp)
return r | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def is_palindrome(string, i, j):
while i < j:
if string[i] != string[j]:
return False
i += 1
j -= 1
return True
def palindromicPartition(self, string):
n = len(string)
dp1 = [([False] * n) for _ in range(n)]
dp2 = [0] * n
for i in range(n):
dp1[i][i] = True
for length in range(2, n + 1):
for start in range(n - length + 1):
end = start + length - 1
if length == 2:
dp1[start][end] = string[start] == string[end]
else:
dp1[start][end] = (
string[start] == string[end] and dp1[start + 1][end - 1]
)
for i in range(n):
if dp1[0][i]:
dp2[i] = 0
else:
dp2[i] = float("inf")
for j in range(i):
if dp1[j + 1][i] and dp2[j] + 1 < dp2[i]:
dp2[i] = dp2[j] + 1
return dp2[n - 1] | CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def isPalin(self, i, j, string):
while i < j:
if string[i] != string[j]:
return False
j -= 1
i += 1
return True
def recur(self, i, string, n, dp, dpP):
if i == n:
return 0
if dp[i] != -1:
return dp[i]
mini = float("inf")
for j in range(i, n):
if self.isPalin(i, j, string):
cost = 1 + self.recur(j + 1, string, n, dp, dpP)
mini = min(mini, cost)
dp[i] = mini
return mini
def palindromicPartition(self, string):
n = len(string)
dp = [float("inf") for i in range(len(string))]
dp.append(0)
for i in range(n - 1, -1, -1):
for j in range(i, n):
if self.isPalin(i, j, string):
cost = 1 + dp[j + 1]
dp[i] = min(dp[i], cost)
return dp[0] - 1 | CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
n = len(string)
P = [([False] * n) for _ in range(n)]
for i in range(n):
P[i][i] = True
for i in range(n - 1):
P[i][i + 1] = string[i] == string[i + 1]
for l in range(3, n + 1):
for i in range(n - l + 1):
j = i + l - 1
P[i][j] = string[i] == string[j] and P[i + 1][j - 1]
C = [i for i in range(n)]
for i in range(n):
if P[0][i]:
C[i] = 0
else:
for j in range(i):
if P[j + 1][i]:
C[i] = min(C[i], C[j] + 1)
return C[n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, s):
def check(i, j, s):
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
dp = [-1] * len(s)
def f(i, s):
if i == len(s):
return 0
minCnt = 10**9
if dp[i] != -1:
return dp[i]
for j in range(i, len(s)):
if check(i, j, s):
cnt = 1 + f(j + 1, s)
minCnt = min(minCnt, cnt)
dp[i] = minCnt
return dp[i]
return f(0, s) - 1 | CLASS_DEF FUNC_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR RETURN BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
is_pali = [[(False) for i in range(len(string))] for j in range(len(string))]
for k in range(len(string)):
i, j = 0, k
while j < len(string):
if k == 0:
is_pali[i][j] = True
elif k == 1:
if string[i] == string[j]:
is_pali[i][j] = True
elif string[i] == string[j] and is_pali[i + 1][j - 1] is True:
is_pali[i][j] = True
i += 1
j += 1
dp = [float("inf")] * len(string)
dp[0] = 0
for i in range(1, len(string)):
if is_pali[0][i] is True:
dp[i] = 0
else:
for j in range(i, 0, -1):
if is_pali[j][i] is True:
dp[i] = min(dp[i], 1 + dp[j - 1])
return dp[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
n = len(string)
dp = [0] * n
pal = [([False] * n) for _ in range(n)]
for i in range(n):
pal[i][i] = True
for l in range(2, n + 1):
for i in range(n - l + 1):
j = i + l - 1
if l == 2:
pal[i][j] = string[i] == string[j]
else:
pal[i][j] = string[i] == string[j] and pal[i + 1][j - 1]
for i in range(n):
min_cuts = i
for j in range(i + 1):
if pal[j][i]:
if j == 0:
min_cuts = 0
else:
min_cuts = min(min_cuts, dp[j - 1] + 1)
dp[i] = min_cuts
return dp[n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | def chk(l, r, s):
while l < r:
if s[l] == s[r]:
r -= 1
l += 1
else:
return False
return True
def f(l, r, s, dp):
if (l, r) in dp:
return dp[l, r]
if chk(l, r, s):
return 1
ans = r - l + 1
for i in range(l, r):
if chk(l, i, s):
ans = min(ans, 1 + f(i + 1, r, s, dp))
dp[l, r] = ans
return ans
class Solution:
def palindromicPartition(self, string):
dp = {}
s = list(string)
return f(0, len(s) - 1, s, dp) - 1 | FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
def ispal(s):
return s == s[::-1]
def solve(i, j, string):
if i >= j:
return 0
if dp[i][j] != -1:
return dp[i][j]
if ispal(string[i : j + 1]):
return 0
ans = 10000000000.0
for k in range(i, j):
if ispal(string[i : k + 1]):
temp = 1 + solve(k + 1, j, string)
ans = min(ans, temp)
dp[i][j] = ans
return dp[i][j]
n = len(string)
dp = [([-1] * 1001) for i in range(1001)]
return solve(0, n - 1, string) | CLASS_DEF FUNC_DEF FUNC_DEF RETURN VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | def is_palindrome(s):
return s == s[::-1]
class Solution:
def helper(self, s):
if self.d.get(s) is not None:
return self.d[s]
if not s:
return 0
ans = 10**9
for i in range(len(s)):
if is_palindrome(s[: i + 1]):
ans = min(ans, 1 + self.helper(s[i + 1 :]))
self.d[s] = ans
return ans
def palindromicPartition(self, string):
self.d = {}
return self.helper(string) - 1 | FUNC_DEF RETURN VAR VAR NUMBER CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NONE RETURN VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT RETURN BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | import sys
class Solution:
def isPalindrome(self, str, i, j):
while i < j:
if str[i] != str[j]:
return False
i += 1
j -= 1
return True
def palindromicPartition(self, str):
n = len(str)
dp = [0] * (n + 1)
for i in range(n - 1, -1, -1):
mn = sys.maxsize
for cut in range(i, n):
if self.isPalindrome(str, i, cut):
cost = 1 + dp[cut + 1]
mn = min(mn, cost)
dp[i] = mn
return dp[0] - 1 | IMPORT CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
def ispallindrome(s, i, j, dp):
if i >= j:
dp[i][j] = 0
return dp[i][j]
if s[i] == s[j]:
dp[i][j] = ispallindrome(s, i + 1, j - 1, dp)
return dp[i][j]
return dp[i][j]
def helper(s, i, j, dp):
if i >= j:
return 0
if dp[i][j] != -1:
return dp[i][j]
if ispallindrome(s, i, j, dp) == 0:
dp[i][j] = 0
return dp[i][j]
ans = float("inf")
for k in range(i, j):
if dp[i][k] != -1:
left = dp[i][k]
else:
left = ispallindrome(s, i, k, dp)
dp[i][k] = left
if left != 0:
continue
if dp[k + 1][j] != -1:
right = dp[k + 1][j]
else:
right = helper(s, k + 1, j, dp)
dp[k + 1][j] = right
temp_ans = helper(s, k + 1, j, dp) + 1
ans = min(ans, temp_ans)
dp[i][j] = ans
return dp[i][j]
dp = [([-1] * len(string)) for i in range(len(string))]
return helper(string, 0, len(string) - 1, dp) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
n = len(string)
if n != 0:
dp = [([False] * n) for i in range(n)]
for i in range(n):
dp[i][i] = True
if i != n - 1:
if string[i] == string[i + 1]:
dp[i][i + 1] = True
for c in range(2, n):
i = 0
j = c
while i < n and j < n:
if string[i] != string[j]:
dp[i][j] = False
else:
dp[i][j] = dp[i + 1][j - 1]
i += 1
j += 1
mc = [0] * len(string)
mc[0] = 0
for i in range(1, n):
if dp[0][i] == True:
mc[i] = 0
else:
mc[i] = 100000
for k in range(1, i + 1):
if dp[k][i]:
mc[i] = min(mc[i], 1 + mc[k - 1])
return mc[n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, s):
n = len(s)
dp = [[(-1) for i in range(n + 2)] for j in range(n + 2)]
def ispal(st):
return st == st[::-1]
def solve(i, j):
if i >= j or ispal(s[i : j + 1]):
return 0
if dp[i][j] != -1:
return dp[i][j]
mn = float("inf")
for k in range(i, j):
if ispal(s[i : k + 1]):
temp = solve(k + 1, j) + 1
mn = min(mn, temp)
dp[i][j] = mn
return mn
x = solve(0, n - 1)
return x | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR VAR NUMBER FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | def helper(string, i, n, dp, ans):
ans[0] = 0
for j in range(1, n):
if dp[0][j]:
ans[j] = 0
else:
temp = int(1000000000.0)
for i in range(j, 0, -1):
if dp[i][j]:
temp = min(temp, ans[i - 1])
ans[j] = temp + 1
class Solution:
def palindromicPartition(self, string):
n = len(string)
dp = [[(False) for _ in range(n)] for _ in range(n)]
for j in range(n):
for i in range(n):
if j - i == 0 and i == j:
dp[i][j] = True
elif j - i == 1 and string[i] == string[j]:
dp[i][j] = True
elif j - i >= 2 and string[i] == string[j] and dp[i + 1][j - 1] == True:
dp[i][j] = True
ans = [0] * n
helper(string, 0, n, dp, ans)
return ans[n - 1] | FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | import sys
class Solution:
def palindromicPartition(self, str1):
n = len(str1)
C = [0] * (n + 1)
P = [[(False) for x in range(n + 1)] for y in range(n + 1)]
for i in range(n):
P[i][i] = True
for L in range(2, n + 1):
for i in range(n - L + 1):
j = i + L - 1
if L == 2:
P[i][j] = str1[i] == str1[j]
else:
P[i][j] = str1[i] == str1[j] and P[i + 1][j - 1]
for i in range(n):
if P[0][i] == True:
C[i] = 0
else:
C[i] = sys.maxsize
for j in range(i):
if P[j + 1][i] == True and 1 + C[j] < C[i]:
C[i] = 1 + C[j]
return C[n - 1] | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR RETURN VAR BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, s: str) -> int:
n = len(s)
dp = [([False] * n) for _ in range(n)]
cuts = [0] * n
for i in range(n):
dp[i][i] = True
for j in range(i):
if s[i] == s[j] and (i == j + 1 or dp[j + 1][i - 1]):
dp[j][i] = True
for i in range(n):
if dp[0][i]:
cuts[i] = 0
else:
cuts[i] = i
for j in range(1, i + 1):
if dp[j][i]:
cuts[i] = min(cuts[i], cuts[j - 1] + 1)
return cuts[n - 1] | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
dp = {}
return self.helper(string, dp)
def helper(self, string, dp):
n = len(string)
if n == 0:
dp[string] = 0
return 0
if string in dp:
return dp[string]
if string == string[::-1]:
dp[string] = 0
return 0
minimum = float("inf")
for i in range(1, n + 1):
if string[:i] == string[:i][::-1]:
minimum = min(minimum, 1 + self.helper(string[i:], dp))
dp[string] = minimum
return dp[string] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, s):
N = len(string)
dp = [[(-1) for i in range(N + 2)] for j in range(N + 2)]
def ispalindrome(s1):
return s1 == s1[::-1]
def solve(i, j):
if i >= j:
return 0
if dp[i][j] != -1:
return dp[i][j]
if ispalindrome(s[i : j + 1]):
return 0
ans = 999999
for k in range(i, j):
if ispalindrome(s[i : k + 1]):
temp = solve(k + 1, j) + 1
ans = min(ans, temp)
dp[i][j] = ans
return ans
return solve(0, N - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string: str) -> int:
n = len(string)
dp = [([False] * n) for _ in range(n)]
for i in range(n):
dp[i][i] = True
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
if string[i] == string[j] and (length == 2 or dp[i + 1][j - 1]):
dp[i][j] = True
cuts = [float("inf")] * n
for i in range(n):
if dp[0][i]:
cuts[i] = 0
else:
for j in range(i):
if dp[j + 1][i]:
cuts[i] = min(cuts[i], cuts[j] + 1)
return cuts[n - 1] | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, s):
def checkPalindrome(string, i, j):
l, r = i, j
while l < r:
if string[l] != string[r]:
return False
l += 1
r -= 1
return True
def solve(string, i, j, dp):
if i >= j:
return 0
if dp[i][j] != -1:
return dp[i][j]
if checkPalindrome(string, i, j):
dp[i][j] = 0
return 0
ans = float("inf")
for k in range(i, j):
if checkPalindrome(string, i, k):
temp = 1 + solve(string, k + 1, j, dp)
ans = min(ans, temp)
dp[i][j] = ans
return ans
n = len(s)
dp = [[(-1) for j in range(n + 1)] for i in range(n + 1)]
ans = solve(s, 0, n - 1, dp)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
n = len(string)
def ispalindrome(start, end, str):
while start < end:
if str[start] != str[end]:
return False
start += 1
end -= 1
return True
dp = [(0) for i in range(n + 1)]
def rec(idx):
if idx == n:
return 0
if dp[idx] != -1:
return dp[idx]
mini = int(1000000000.0)
for i in range(idx, n):
if ispalindrome(idx, i, string):
cost = 1 + rec(i + 1)
mini = min(mini, cost)
dp[idx] = mini
return dp[idx]
for idx in range(n - 1, -1, -1):
mini = int(1000000000.0)
for i in range(idx, n):
if ispalindrome(idx, i, string):
cost = 1 + dp[i + 1]
mini = min(mini, cost)
dp[idx] = mini
return dp[0] - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def palindromicPartition(self, string):
n = len(string)
def check_pali(s, e):
i, j = s, e
while i < j:
if string[i] != string[j]:
return False
i += 1
j -= 1
return True
maxi = 10**9
dp = [-1] * n
def find(start):
if start == n or check_pali(start, n - 1):
return 0
if dp[start] != -1:
return dp[start]
ans = maxi
for i in range(start, n):
if check_pali(start, i):
ans = min(ans, find(i + 1))
dp[start] = ans + 1
return dp[start]
return find(0) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def fun(self, i, s, n, dp):
if i == n:
return 0
if dp[i] != -1:
return dp[i]
temp = ""
mini = float("inf")
for j in range(i, n):
temp += s[j]
if temp == temp[::-1]:
cost = 1 + self.fun(j + 1, s, n, dp)
mini = min(mini, cost)
dp[i] = mini
return dp[i]
def palindromicPartition(self, string):
dp = [(-1) for i in range(len(string) + 1)]
return self.fun(0, string, len(string), dp) - 1 | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | import sys
sys.setrecursionlimit(10**7)
class Solution:
def palindromicPartition(self, string):
d = dict()
def dfs(s, i, j):
if i == j:
s1 = s[i : j + 1]
if s1 == s1[::-1]:
return -1
return 10**9
if d.get((i, j)) != None:
return d[i, j]
y = 10**9
for k in range(i, j):
s1 = s[i : k + 1]
if s1 == s1[::-1]:
a = 1 + dfs(s, k + 1, j)
y = min(y, a)
d[i, j] = y
return y
return dfs(string, 0, len(string)) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NONE RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 ≤ length of str ≤ 500 | class Solution:
def isPalindrome(self, string, a, b):
while a <= b:
if string[a] != string[b]:
return False
a += 1
b -= 1
return True
def palindromicPartition(self, string):
n = len(string)
dp = [(-1) for _ in range(n + 1)]
dp[n] = 0
for i in range(n - 1, -1, -1):
minCuts = float("inf")
for j in range(i, n):
if self.isPalindrome(string, i, j):
minCuts = min(minCuts, 1 + dp[j + 1])
dp[i] = minCuts
return dp[0] - 1 | CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.