description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β€” he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100) β€” the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
import sys def minrest(v, n, i, last): if i >= n: return 0 if dp[i][last] != -1: return dp[i][last] if v[i] == 0: dp[i][last] = minrest(v, n, i + 1, 0) + 1 elif v[i] == 1: if v[i] == last: dp[i][last] = minrest(v, n, i + 1, 0) + 1 else: dp[i][last] = minrest(v, n, i + 1, 1) elif v[i] == 2: if v[i] == last: dp[i][last] = minrest(v, n, i + 1, 0) + 1 else: dp[i][last] = minrest(v, n, i + 1, 2) elif v[i] == 3: if last == 1: a = sys.maxsize else: a = minrest(v, n, i + 1, 1) if last == 2: b = sys.maxsize else: b = minrest(v, n, i + 1, 2) dp[i][last] = min(a, b) return dp[i][last] dp = [([-1] * 3) for i in range(105)] n = int(input()) v = list(map(int, input().split())) ans = minrest(v, n, 0, 0) print(ans)
IMPORT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β€” he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100) β€” the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = [int(i) for i in input().split()] action = 0 rest = 0 for i in range(n): if a[i] == 0: rest += 1 action = 0 elif a[i] == 1: if action != 1: action = 1 else: rest += 1 action = 0 elif a[i] == 2: if action != 2: action = 2 else: rest += 1 action = 0 elif action == 1: action = 2 elif action == 2: action = 1 else: j = i + 1 while j < n and a[j] == 3: j += 1 if j != n: if (j - i) % 2 + a[j] == 2: action = 2 else: action = 1 print(rest)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β€” he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100) β€” the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
def main(): input() a1 = a2 = x1 = x2 = z = 0 for b in map(int, input()[::2]): if 0 < b < 3: if a1 == b: a1 = 0 x1 += 1 else: a1 = b if a2 == b: a2 = 0 x2 += 1 else: a2 = b if x1 != x2: if x1 > x2: x1, a1 = x2, a2 else: x2, a2 = x1, a1 elif b: a1 = 1 if a1 != 1 else 2 a2 = 2 if a2 != 2 else 1 else: a1 = a2 = 0 z += 1 print(x1 + z) def __starting_point(): main() __starting_point()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER IF NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β€” he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100) β€” the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
int(input()) descanso = anterior = 0 for i in [int(k) for k in input().split()]: if i == 0 or i == anterior: descanso += 1 anterior = 0 elif i == 3: if anterior != 0: anterior = 3 - anterior else: anterior = i print(descanso)
EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β€” he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100) β€” the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
import sys inp = sys.stdin n = int(inp.readline()) a = list(map(int, inp.readline().split())) best = [0, 0, 0] for i in range(n): nx_best = [0, 0, 0] if a[i] in (1, 3): nx_best[1] = max(best[0], best[2]) + 1 if a[i] in (2, 3): nx_best[2] = max(best[0], best[1]) + 1 nx_best[0] = max(best) best = nx_best[:] print(n - max(best))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β€” he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100) β€” the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) dp = [([0] * n) for i in range(2)] if a[0] == 0: dp[0][0] = 1 dp[1][0] = 1 elif a[0] == 1: dp[0][0] = 1 elif a[0] == 2: dp[1][0] = 1 for i in range(1, n): if a[i] == 0: dp[0][i] = 1 + min(dp[0][i - 1], dp[1][i - 1]) dp[1][i] = 1 + min(dp[0][i - 1], dp[1][i - 1]) elif a[i] == 1: dp[0][i] = 1 + min(dp[0][i - 1], dp[1][i - 1]) dp[1][i] = dp[0][i - 1] elif a[i] == 2: dp[0][i] = dp[1][i - 1] dp[1][i] = 1 + min(dp[0][i - 1], dp[1][i - 1]) else: dp[0][i] = dp[1][i - 1] dp[1][i] = dp[0][i - 1] print(min(dp[0][-1], dp[1][-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β€” he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100) β€” the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) m = [int(x) for x in input().split()] l = [] if m[0] == 0: l.append([0, 0, 0]) elif m[0] == 1: l.append([0, 1, 0]) elif m[0] == 2: l.append([0, 0, 1]) else: l.append([0, 1, 1]) for i in range(1, n): if m[i] == 0: l.append([max(l[i - 1]), max(l[i - 1]), max(l[i - 1])]) elif m[i] == 1: l.append( [ max(l[i - 1]), max(l[i - 1][0], l[i - 1][2]) + 1, max(l[i - 1][0], l[i - 1][1]), ] ) elif m[i] == 2: l.append( [ max(l[i - 1]), max(l[i - 1][0], l[i - 1][2]), max(l[i - 1][0], l[i - 1][1]) + 1, ] ) else: l.append( [ max(l[i - 1]), max(l[i - 1][0], l[i - 1][2]) + 1, max(l[i - 1][0], l[i - 1][1]) + 1, ] ) print(n - max(l[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β€” he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100) β€” the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
def sport(v): return v >= 2 def contest(v): return v in [1, 3] LAST_CONTEST = 0 LAST_SPORT = 1 def __starting_point(): n = int(input()) a = list(map(int, input().split())) prev = [0, 0] dp = [0, 0] for i in range(n): v = a[i] curr = [dp[0], dp[1]] if sport(v): curr[LAST_SPORT] = max( dp[LAST_SPORT], 1 + prev[LAST_SPORT], 1 + dp[LAST_CONTEST] ) if contest(v): curr[LAST_CONTEST] = max( dp[LAST_CONTEST], 1 + prev[LAST_CONTEST], 1 + dp[LAST_SPORT] ) prev = dp dp = curr print(n - max(dp)) __starting_point()
FUNC_DEF RETURN VAR NUMBER FUNC_DEF RETURN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β€” he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100) β€” the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) d = list(map(int, input().split()))[:n] dp = [[int(101) for i in range(100)] for j in range(3)] dp[0][0] = 1 if d[0] == 1 or d[0] == 3: dp[1][0] = 0 if d[0] == 2 or d[0] == 3: dp[2][0] = 0 for i in range(1, n): dp[0][i] = 1 + min(dp[0][i - 1], dp[1][i - 1], dp[2][i - 1]) if d[i] == 1 or d[i] == 3: dp[1][i] = min(dp[0][i - 1], dp[2][i - 1]) if d[i] == 2 or d[i] == 3: dp[2][i] = min(dp[0][i - 1], dp[1][i - 1]) print(min(dp[0][n - 1], dp[1][n - 1], dp[2][n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β€” he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100) β€” the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
INF = 1000 n = int(input()) desc = list(map(int, input().split())) r = {} g = {} c = {} r[0] = 0 g[0] = 0 c[0] = 0 i = 0 for x in desc: i += 1 if x == 0: g[i] = INF c[i] = INF r[i] = 1 + min([g[i - 1], r[i - 1], c[i - 1]]) elif x == 1: g[i] = INF c[i] = min(r[i - 1], g[i - 1]) r[i] = 1 + min([g[i - 1], r[i - 1], c[i - 1]]) elif x == 2: g[i] = min(r[i - 1], c[i - 1]) c[i] = INF r[i] = 1 + min([g[i - 1], r[i - 1], c[i - 1]]) elif x == 3: r[i] = 1 + min([g[i - 1], r[i - 1], c[i - 1]]) g[i] = min(r[i - 1], c[i - 1]) c[i] = min(r[i - 1], g[i - 1]) print(min([r[n], g[n], c[n]]))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array $a$ of length $n$, consisting only of the numbers $0$ and $1$, and the number $k$. Exactly $k$ times the following happens: Two numbers $i$ and $j$ are chosen equiprobable such that ($1 \leq i < j \leq n$). The numbers in the $i$ and $j$ positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the $a$ array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either $0$ or it can be represented as $\dfrac{P}{Q}$, where $P$ and $Q$ are coprime integers and $Q \not\equiv 0~\pmod {10^9+7}$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq n \leq 100, 1 \leq k \leq 10^9$)Β β€” the length of the array $a$ and the number of operations. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$)Β β€” the description of the array $a$. -----Output----- If the desired probability is $0$, print $0$, otherwise print the value $P \cdot Q^{-1}$ $\pmod {10^9+7}$, where $P$ and $Q$ are defined above. -----Examples----- Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 -----Note----- In the first example, all possible variants of the final array $a$, after applying exactly two operations: $(0, 1, 0)$, $(0, 0, 1)$, $(1, 0, 0)$, $(1, 0, 0)$, $(0, 1, 0)$, $(0, 0, 1)$, $(0, 0, 1)$, $(1, 0, 0)$, $(0, 1, 0)$. Therefore, the answer is $\dfrac{3}{9}=\dfrac{1}{3}$. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is $0$.
M = 10**9 + 7 n, k = map(int, input().split()) a = list(map(int, input().split())) z, o = a.count(0), a.count(1) d = pow(n * (n - 1) // 2, M - 2, M) if z > o: o, z = z, o a = [(1 - x) for x in a][::-1] res = [([0] * (z + 1)) for i in range(z + 1)] tf = [([0] * (z + 1)) for i in range(z + 1)] for i in range(z + 1): res[i][i] = 1 tf[i][i] = ( (z * (z - 1) // 2 + o * (o - 1) // 2 + i * (z - i) + (z - i) * (o - z + i)) * d % M ) if i < z: tf[i + 1][i] = (z - i) * (z - i) * d % M if i: tf[i - 1][i] = i * (o - z + i) * d % M def mul(a, b): t = [([0] * (z + 1)) for i in range(z + 1)] for i in range(z + 1): for k in range(z + 1): for j in range(z + 1): t[i][j] = (t[i][j] + a[i][k] * b[k][j]) % M return t while k: if k & 1: res = mul(res, tf) tf = mul(tf, tf) k >>= 1 print(res[-1][a[:z].count(0)])
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR 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 BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array $a$ of length $n$, consisting only of the numbers $0$ and $1$, and the number $k$. Exactly $k$ times the following happens: Two numbers $i$ and $j$ are chosen equiprobable such that ($1 \leq i < j \leq n$). The numbers in the $i$ and $j$ positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the $a$ array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either $0$ or it can be represented as $\dfrac{P}{Q}$, where $P$ and $Q$ are coprime integers and $Q \not\equiv 0~\pmod {10^9+7}$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq n \leq 100, 1 \leq k \leq 10^9$)Β β€” the length of the array $a$ and the number of operations. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$)Β β€” the description of the array $a$. -----Output----- If the desired probability is $0$, print $0$, otherwise print the value $P \cdot Q^{-1}$ $\pmod {10^9+7}$, where $P$ and $Q$ are defined above. -----Examples----- Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 -----Note----- In the first example, all possible variants of the final array $a$, after applying exactly two operations: $(0, 1, 0)$, $(0, 0, 1)$, $(1, 0, 0)$, $(1, 0, 0)$, $(0, 1, 0)$, $(0, 0, 1)$, $(0, 0, 1)$, $(1, 0, 0)$, $(0, 1, 0)$. Therefore, the answer is $\dfrac{3}{9}=\dfrac{1}{3}$. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is $0$.
N, T = map(int, input().split()) A = [int(a) for a in input().split()] if sum(A) > N // 2: A = [(1 - a) for a in A][::-1] K = sum(A) S = sum(A[-K:]) M = K + 1 P = 10**9 + 7 inv = pow(N * (N - 1) // 2, P - 2, P) X = [([0] * M) for _ in range(M)] for i in range(M): if i > 0: X[i - 1][i] = (K - i + 1) ** 2 * inv % P if i < M - 1: X[i + 1][i] = (N - 2 * K + i + 1) * (i + 1) * inv % P X[i][i] = (1 - (K - i) ** 2 * inv - (N - 2 * K + i) * i * inv) % P def ddd(n): for i in range(1, 100): if n * i % P < 100: return n * i % P, i return -1, -1 def poww(MM, n): if n == 1: return MM if n % 2: return mult(poww(MM, n - 1), MM) return poww(mult(MM, MM), n // 2) def mult(M1, M2): Y = [([0] * M) for _ in range(M)] for i in range(M): for j in range(M): for k in range(M): Y[i][j] += M1[i][k] * M2[k][j] Y[i][j] %= P return Y X = poww(X, T) print(X[S][K])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR RETURN NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array $a$ of length $n$, consisting only of the numbers $0$ and $1$, and the number $k$. Exactly $k$ times the following happens: Two numbers $i$ and $j$ are chosen equiprobable such that ($1 \leq i < j \leq n$). The numbers in the $i$ and $j$ positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the $a$ array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either $0$ or it can be represented as $\dfrac{P}{Q}$, where $P$ and $Q$ are coprime integers and $Q \not\equiv 0~\pmod {10^9+7}$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq n \leq 100, 1 \leq k \leq 10^9$)Β β€” the length of the array $a$ and the number of operations. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$)Β β€” the description of the array $a$. -----Output----- If the desired probability is $0$, print $0$, otherwise print the value $P \cdot Q^{-1}$ $\pmod {10^9+7}$, where $P$ and $Q$ are defined above. -----Examples----- Input 3 2 0 1 0 Output 333333336 Input 5 1 1 1 1 0 0 Output 0 Input 6 4 1 0 0 1 1 0 Output 968493834 -----Note----- In the first example, all possible variants of the final array $a$, after applying exactly two operations: $(0, 1, 0)$, $(0, 0, 1)$, $(1, 0, 0)$, $(1, 0, 0)$, $(0, 1, 0)$, $(0, 0, 1)$, $(0, 0, 1)$, $(1, 0, 0)$, $(0, 1, 0)$. Therefore, the answer is $\dfrac{3}{9}=\dfrac{1}{3}$. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is $0$.
import sys __version__ = "1.8" __date__ = "2019-04-21" def binom_dp(): dp = [[(-1) for j in range(110)] for i in range(110)] def calculate(n, k): if n < k: return 0 if n == k or k == 0: return 1 if dp[n][k] > 0: return dp[n][k] else: dp[n][k] = calculate(n - 1, k - 1) + calculate(n - 1, k) return dp[n][k] return calculate def egcd(a, b): if a == 0: return b, 0, 1 else: g, y, x = egcd(b % a, a) return g, x - b // a * y, y def modinv(a, m): g, x, y = egcd(a, m) if g != 1: raise Exception("modular inverse does not exist") else: return x % m def multiply(A, B, mod): if not hasattr(B[0], "__len__"): C = [sum(aij * B[j] % mod for j, aij in enumerate(ai)) for ai in A] else: C = [[(0) for col in range(len(B[0]))] for row in range(len(A))] len_A = len(A) len_B = len(B) for row in range(len_A): if sum(A[row]) == 0: continue for col in range(len_B): C[row][col] = sum(A[row][k] * B[k][col] for k in range(len_B)) % mod return C def memoize(func): memo = {} def wrapper(*args): M, n, mod = args if n not in memo: memo[n] = func(M, n, mod) return memo[n] return wrapper @memoize def matrix_pow(M, n, mod): if n == 2: return multiply(M, M, mod) if n == 1: return M sub_M = matrix_pow(M, n // 2, mod) if n % 2 == 0: return multiply(sub_M, sub_M, mod) return multiply(sub_M, matrix_pow(M, n - n // 2, mod), mod) def solve(n, k, a, binom, mod): ones = sum(a) zeros = n - ones M = [[(0) for col in range(zeros + 1)] for row in range(zeros + 1)] for row in range(max(0, zeros - ones), zeros + 1): pre_zeros = row pre_ones = zeros - pre_zeros post_zeros = pre_ones post_ones = ones - pre_ones M[row][row] = ( pre_ones * post_ones + pre_zeros * post_zeros + binom(zeros, 2) + binom(ones, 2) ) if row > max(0, zeros - ones): M[row - 1][row] = pre_zeros * post_ones if row < zeros: M[row + 1][row] = post_zeros * pre_ones M = [matrix_pow(M, k, mod)[-1]] b = [0] * (zeros + 1) b[zeros - sum(a[:zeros])] = 1 C = multiply(M, b, mod) return C[-1] def main(argv=None): mod = int(1000000000.0) + 7 n, k = list(map(int, input().split())) a = list(map(int, input().split())) binom = binom_dp() P = solve(n, k, a, binom, mod) if P == 0: print(0) else: Q = pow(binom(n, 2), k, mod) print(P * modinv(Q, mod) % mod) return 0 STATUS = main() sys.exit(STATUS)
IMPORT ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR STRING RETURN BIN_OP VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF NONE ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital β€” number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≀ n ≀ 100, <image>) β€” the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≀ vi, ui ≀ n, vi β‰  ui) β€” the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>.
s = list(map(int, input().split())) n = s[0] m = s[1] roads = [] Adj = [] for i in range(0, n + 1): roads.append([]) Adj.append([False] * (n + 1)) for i in range(0, m): s = list(map(int, input().split())) roads[s[0]].append(s[1]) roads[s[1]].append(s[0]) Adj[s[0]][s[1]] = True Adj[s[1]][s[0]] = True Dist = [200] * (n + 1) Q = [1] Dist[1] = 0 while len(Q) > 0: curr = Q.pop(0) for nex in roads[curr]: if Dist[nex] == 200: Dist[nex] = Dist[curr] + 1 Q.append(nex) Levels = [] for l in range(0, 100): Levels.append([]) for p in range(1, n + 1): if Dist[p] == l: Levels[l].append(p) fromOne = [0] * (n + 1) def fo(b): if fromOne[b] > 0: return fromOne[b] if b == 1: return 1 ans = 0 for p in Levels[Dist[b] - 1]: if Adj[p][b]: ans += fo(p) fromOne[b] = ans return ans toN = [0] * (n + 1) def tN(b): if toN[b] > 0: return toN[b] if b == n: return 1 ans = 0 for p in Levels[Dist[b] + 1]: if Adj[p][b]: ans += tN(p) toN[b] = ans return ans best = fo(n) for policeStation in range(2, n): safe = fo(policeStation) * tN(policeStation) * 2 best = max(best, safe) print(round(best / fo(n), 10))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital β€” number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≀ n ≀ 100, <image>) β€” the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≀ vi, ui ≀ n, vi β‰  ui) β€” the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>.
n, m = map(int, input().split()) adj = [[] for i in range(n)] for _ in range(m): a, b = map(int, input().split()) adj[a - 1].append(b - 1) adj[b - 1].append(a - 1) def bfs(s): q, d, res = [s], [-1] * n, [0] * n d[s] = 0 res[s] = 1 i = 0 while i < len(q): v = q[i] for u in adj[v]: if d[u] == -1: d[u] = d[v] + 1 q.append(u) if d[u] == d[v] + 1: res[u] += res[v] i += 1 return q, d, res q0, d0, res0 = bfs(0) q1, d1, res1 = bfs(n - 1) min_dist = d0[n - 1] num_paths = res0[n - 1] res = num_paths for v in range(1, n - 1): if d0[v] + d1[v] == min_dist: res = max(res, 2 * res0[v] * res1[v]) print("%.7f" % (res / num_paths))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR LIST VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def f(s, k): n = len(s) q = [s] S = set() S.add(s) ans = 0 while len(q) > 0 and k > 0: ss = q[0] k -= 1 ans += n - len(ss) q.pop(0) for i in range(len(ss)): ns = ss[0:i] + ss[i + 1 : len(s)] if ns not in S: q.append(ns) S.add(ns) if k == 0: return ans return -1 def main(): n, k = map(int, input().split()) s = input() print(f(s, k)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
from sys import stdin n, k = [int(i) for i in stdin.readline().strip().split()] s = stdin.readline().strip() def get_f(s): F = [(len(s) * [0]) for _ in range(len(s) + 1)] size = 0 for index in range(len(s)): F[size][index] = 1 F[1][0] = 1 p = {s[0]: 0} for i in range(1, len(s)): for k in range(1, len(s) + 1): if k > i + 1: val = 0 else: val = F[k][i - 1] + F[k - 1][i - 1] if s[i] in p: if p[s[i]] - 1 >= 0: val -= F[k - 1][p[s[i]] - 1] elif p[s[i]] == 0 and k - 1 == 0: val -= 1 F[k][i] = val p[s[i]] = i return F _F = get_f(s) def count(size, index=None): return _F[size][index or len(s) - 1] previous_letter_index = {} found = {} for index, letter in enumerate(s): if letter in found: previous_letter_index[letter, index] = found[letter] found[letter] = index _subsequences = {} def subsequences(size, index): if (size, index) not in _subsequences: if size == 0: res = 1 elif size > index + 1: res = 0 else: res = subsequences(size, index - 1) + subsequences(size - 1, index - 1) letter = s[index] if (letter, index) in previous_letter_index: res -= subsequences(size - 1, previous_letter_index[letter, index] - 1) _subsequences[size, index] = res return _subsequences[size, index] total_cost = 0 for size in range(len(s), -1, -1): if k == 0: break step_cost = n - size sequence_count = count(size, len(s) - 1) if sequence_count > k: sequence_count = k total_cost += step_cost * sequence_count k -= sequence_count if k == 0: print(total_cost) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF NONE RETURN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
from sys import stdin, stdout n, kk = map(int, stdin.readline().split()) s = stdin.readline().strip() s += "$" n = n + 1 dp = [[(0) for i in range(n)] for j in range(n)] p = 10**15 + 5 for i in range(n): dp[i][0] = 1 for end in range(n): for length in range(1, n): seen = [] ans = 0 for k in range(end - 1, -1, -1): if s[k] not in seen: seen.append(s[k]) ans += dp[k][length - 1] ans %= p dp[end][length] = ans totals = [dp[n - 1][length] for length in range(n)] ans = 0 idx = n - 1 while idx >= 0 and kk > 0: ans += min(totals[idx], kk) * (n - 1 - idx) kk -= totals[idx] idx -= 1 if kk <= 0: stdout.write(str(ans) + "\n") else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split(" ")) s = input() dp = [([0] * (n + 1)) for _ in range(n + 1)] dp[0][0] = 1 for l in range(0, n): for i in range(l, n + 1): used = [False] * 26 for j in range(i + 1, n + 1): ch = ord(s[j - 1]) - ord("a") if not used[ch]: dp[l + 1][j] += dp[l][i] used[ch] = True total = 0 for l in range(n, -1, -1): sums = sum(dp[l]) if sums >= k: total += (n - l) * k k = 0 break total += (n - l) * sums k -= sums if k > 0: total = -1 print(total)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) s = str(input()) s1 = "" sub = [s] inv = {} c = 0 kq = 0 while len(sub) > 0: tem = sub[0] del sub[0] if inv.get(tem, 0) == 0: inv[tem] = 1 c += 1 kq += n - len(tem) if c == k: break for i in range(len(tem)): s1 = tem[:i] + tem[i + 1 :] sub.append(s1) if c < k: kq = -1 print(kq)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) s = input() s = [(ord(c) - ord("a")) for c in s] dp = [[([0] * 26) for i in range(n + 1)] for i in range(n)] dp[0][1][s[0]] = 1 sm = None for i in range(1, n): c = s[i] for cc in range(26): dp[i][1][cc] = dp[i - 1][1][cc] dp[i][1][c] = 1 for j in reversed(range(2, n + 1)): for cc in range(26): if cc != c: dp[i][j][cc] = dp[i - 1][j][cc] else: tm = 0 for t in range(26): tm += dp[i - 1][j - 1][t] dp[i][j][cc] = tm def get(x): if x == 0: return 1 ans = 0 for i in range(26): ans += dp[-1][x][i] return ans cnt = 0 cost = 0 for re in reversed(range(n + 1)): x = get(re) if x + cnt >= k: cost += (n - re) * (k - cnt) cnt = k break else: cost += (n - re) * x cnt += x if cnt < k: print(-1) else: print(cost)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
[n, k] = [int(i) for i in input().split()] S = [input()] l = 0 x = 0 c = 0 while len(S) < k: d = k - len(S) a = set() for i in range(l, len(S)): for j in range(0, len(S[i])): s = S[i][:j] + S[i][j + 1 :] if s not in a: a.add(s) c += x + 1 d -= 1 if d == 0: break if d == 0: break if a == set(): c = -1 break x += 1 l = len(S) S += list(a) print(c)
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) r = set() r1 = set() a = input() queue = [a] cost = 0 co = 0 r1.add(a) while queue: ref = queue.pop(0) if ref not in r: r.add(ref) cost += n - len(ref) co += 1 if co == k: break for i in range(len(ref)): y = ref[:i] + ref[i + 1 :] if y not in r1: queue.append(y) r1.add(y) if len(r) < k: print(-1) exit(0) print(cost)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, tt = map(int, input().split()) s = input() dp = [([0] * (n + 1)) for i in range(n + 1)] for c in range(n + 1): dp[0][c] = 1 last = [-1] * 26 for c in range(1, n + 1): k = ord(s[c - 1]) - ord("a") for r in range(1, n + 1): dp[r][c] = dp[r][c - 1] + dp[r - 1][c - 1] if last[k] == -1: last[k] = c - 1 continue else: p = last[k] for r in range(1, n + 1): dp[r][c] = dp[r][c] - dp[r - 1][p] last[k] = c - 1 su, ans, t = 0, 0, 0 for r in range(n + 1): su = su + dp[r][n] if su < tt: ans = -1 else: for i in range(n, -1, -1): r = min(tt, dp[i][n]) ans += t * r tt -= r t += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN 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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def __starting_point(): n, k = list(map(int, input().split())) aa = list(input()) st = {"".join(aa)} arr = [aa] w = 0 c = 0 cst = 0 while len(arr) < k and w < len(arr): wrd = arr[w][:c] + arr[w][c + 1 :] wrds = "".join(wrd) if wrds not in st: st.add(wrds) arr.append(wrd) cst += n - len(wrd) c += 1 if c >= len(arr[w]): c = 0 w += 1 if len(arr) < k: print(-1) else: print(cst) __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, K = map(int, input().split()) s = input() kolvo = 1 res = 0 d = {} d[0] = [] d[0].append(s) level = 0 while kolvo < K and level <= n: level += 1 d[level] = [] for subs in d[level - 1]: for j in range(len(subs)): subs1 = subs[:j] + subs[j + 1 :] if subs1 not in d[level]: d[level].append(subs1) kolvo += 1 res += level if kolvo == K: break if kolvo == K: break if kolvo < K: res = -1 print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER LIST EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST FOR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
ls = list(map(int, input().split())) s = input() q = [s] n, k = ls[0], ls[1] l = set() c = 0 cost = 0 while len(q) != 0: p = q.pop(0) if p not in l: l.add(p) cost = cost + len(s) - len(p) if len(l) == k: c = 1 break for i in range(len(p)): t = p[:i] + p[i + 1 :] if t not in l: q.append(t) if c == 1: print(str(cost)) else: print(str(-1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
alpha = list("abcdefghijklmnopqrstuvwxyz ") def count_subseq(s): n = len(s) dp = [[([0] * 27) for i in range(n + 10)] for j in range(n + 10)] dp[0][0][26] = 1 for i in range(1, n + 1): st = s[i - 1] for j in range(i + 1): v = sum(dp[i - 1][j - 1][p] for p in range(27)) for last_string in range(27): if alpha[last_string] != st: dp[i][j][last_string] = dp[i - 1][j][last_string] else: dp[i][j][last_string] = v result = [0] * (n + 1) for l in range(n + 1): result[l] = sum(dp[n][l][v] for v in range(27)) return result def solution(): n, k = map(int, input().split()) s = input() cntlist = count_subseq(s) res = 0 for length in range(n, -1, -1): if k == 0: break cnt = cntlist[length] if cnt >= k: res += (n - length) * k k = 0 elif cnt < k: res += (n - length) * cnt k -= cnt if k > 0: print(-1) else: print(res) return def main(): test = 1 for i in range(test): solution() return main()
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = list(map(int, input().split())) s = input() st = [set() for i in range(n + 1)] st[n].add(s) c = 1 k -= 1 ans = 0 for i in range(n, 0, -1): for w in st[i]: for j in range(i): st[i - 1].add(w[:j] + (w[j + 1 :] if j != i - 1 else "")) sz = len(st[i - 1]) if k < sz: ans += (n - i + 1) * k k = 0 break else: ans += (n - i + 1) * sz k -= sz if k < 0: break if k > 0: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def check_word(word_list, word_need): unique = [] for i in range(len(word_list)): if word_need == 0: break for j in range(len(word_list[i])): if word_need == 0: break new_word = word_list[i][:j] + word_list[i][j + 1 :] if new_word not in unique: unique.append(new_word) word_need -= 1 return unique def check_all(): cost_sum = 0 cost_each = 1 [raw_length, raw_size] = input().split() size = int(raw_size) words = input().split() size -= 1 while size > 0: if len(words[0]) == 0: return -1 words = check_word(words, size) cost_sum += len(words) * cost_each cost_each += 1 size -= len(words) return cost_sum print(check_all())
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN LIST VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
arr = input() N, K = [int(num) for num in arr.split(" ")] string = input() seq = [string] record = {} record[string] = 1 count = 1 while seq and count <= 105: gen_d = seq.pop(0) for k in range(len(gen_d)): r = gen_d[:k] + gen_d[k + 1 :] if r not in record: record[r] = 1 seq.append(r) count += 1 record[""] = 1 G = [] for s in record: G.append([N - len(s), s]) G.sort() res = 0 if len(G) < K: print(-1) else: for i in range(K): res += G[i][0] print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
[n, k] = [int(i) for i in input().split()] s = input() cntsz = [(0) for i in range(105)] dp = [[(0) for i in range(105)] for j in range(105)] lst = [(0) for i in range(105)] prv = [(0) for i in range(26)] n = len(s) s = "%" + s for i in range(n + 1): dp[i][0] = 1 for i in range(1, n + 1): lst[i] = prv[ord(s[i]) - ord("a")] prv[ord(s[i]) - ord("a")] = i for sz in range(1, n + 1): for i in range(1, n + 1): dp[i][sz] += dp[i - 1][sz] dp[i][sz] += dp[i - 1][sz - 1] if lst[i] != 0: dp[i][sz] -= dp[lst[i] - 1][sz - 1] for sz in range(1, n + 1): for i in range(1, n + 1): cntsz[sz] += dp[i][sz] cntsz[sz] -= dp[i - 1][sz] cntsz[0] += 1 done = 0 ans = 0 for i in range(n, -1, -1): if done + cntsz[i] >= k: ans += (n - i) * (k - done) done = k break done += cntsz[i] ans += cntsz[i] * (n - i) if done < k: print(-1) else: print(ans)
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
import sys input = sys.stdin.readline n, W = map(int, input().split()) s = input().strip() NEXTLIST = [([n] * 26) for i in range(n + 1)] for i in range(n - 1, -1, -1): for j in range(26): NEXTLIST[i][j] = NEXTLIST[i + 1][j] NEXTLIST[i][ord(s[i]) - 97] = i DP = [([0] * (n + 1)) for i in range(n + 1)] DP[0][0] = 1 for i in range(n): for j in range(26): if NEXTLIST[i][j] != n: for k in range(n): DP[NEXTLIST[i][j] + 1][k + 1] += DP[i][k] HLIST = [0] * (n + 1) for i in range(n + 1): for j in range(n + 1): HLIST[j] += DP[i][j] ANS = 0 for i in range(n, -1, -1): if W > HLIST[i]: ANS += (n - i) * HLIST[i] W -= HLIST[i] else: ANS += W * (n - i) print(ANS) sys.exit() else: print(-1)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
R = lambda: map(int, input().split()) n, k = R() s = input() p = [s] ans = 0 d = set() while p: q = p.pop(0) if q not in d: k -= 1 ans += n - len(q) if k == 0: print(ans) quit() d.add(q) for i in range(len(q)): t = q[:i] + q[i + 1 :] if t not in p: p.append(t) print(-1)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) s = input() last = [[(-1) for i in range(26)] for j in range(n)] for i in range(n): for j in range(26): if i != 0: last[i][j] = last[i - 1][j] last[i][ord(s[i]) - ord("a")] = i dp = [[(0) for i in range(n + 1)] for j in range(n)] for i in range(n): dp[i][1] = 1 for len in range(2, n + 1): for i in range(1, n): for j in range(26): if last[i - 1][j] != -1: dp[i][len] += dp[last[i - 1][j]][len - 1] ans = 0 for len in range(n, 0, -1): cnt = 0 for j in range(26): if last[n - 1][j] != -1: cnt += dp[last[n - 1][j]][len] if cnt >= k: ans += k * (n - len) k = 0 break else: ans += cnt * (n - len) k -= cnt if k == 1: ans += n k = 0 if k > 0: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
line = input() need = int(line.split(" ")[1]) - 1 n = int(line.split(" ")[0]) s = input() if need == 0: print(0) else: queue = [s] d = {} head, tail, ans = 0, 1, 0 solve = False while head < tail and not solve: top = queue[head] cost = len(top) - 1 for idx, c in enumerate(top): new = "".join([top[:idx], top[idx + 1 :]]) h = new.__hash__() if not d.get(h): d[h] = 1 queue.append(new) tail += 1 ans += n - cost need -= 1 if need == 0: solve = True break head += 1 if not solve: print("-1") else: print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING LIST VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
from sys import stdin def subseq(xs, n, m): if n > 1: sl = subseq(xs[: n // 2], n // 2, m) sr = subseq(xs[n // 2 :], n - n // 2, m) return combine(sl, sr, m) return xs def combine(sl, sr, m): seq = {(s1 + s2) for s1 in sl for s2 in sr}.union(sl, sr) seql = sorted(seq, key=len, reverse=True)[:m] return seql n, k = map(int, input().split()) s = input() seqs = subseq(s, n, k) if len(seqs) < k - 1: print(-1) else: slengths = map(lambda x: n - len(x), seqs) result = sum(slengths) if len(seqs) == k - 1: result += n print(result)
FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = list(map(int, input().split())) s = input() sett = {s} q = [s] m = n while len(q) and len(sett) < k: node = q.pop(0) for i in range(0, len(node)): ss = node[:i] + node[i + 1 :] if not ss in sett and len(sett) < k: q.append(ss) sett.add(ss) m += len(ss) print("-1" if len(sett) < k else n * k - m)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING BIN_OP BIN_OP VAR VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def solve(n, k, s): set = {} queue = [] ans = 0 queue.append(s) set[s] = 1 while len(queue) > 0 and len(set) < k: curr = queue.pop(0) for i in range(len(curr)): newS = curr[:i] + curr[i + 1 :] if newS not in set and len(set) + 1 <= k: set[newS] = 1 ans += n - len(newS) queue.append(newS) print(ans if len(set) == k else -1) def main(): n, k = map(int, input().split()) s = input() solve(n, k, s) main()
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, setsize = list(map(int, input().split())) s = input() count = [[(0) for j in range(n + 1)] for i in range(n + 1)] for i in range(n): j = i - 1 while j >= 0: for k in range(1, n): count[i][k + 1] += count[j][k] if s[j] == s[i]: break j -= 1 if j == -1: count[i][1] += 1 cost = 0 count[0][0] = 1 for l in range(n, -1, -1): if setsize == 0: break ct = 0 localcost = n - l for i in range(n): ct += count[i][l] minct = min(setsize, ct) setsize -= minct cost += minct * localcost if setsize == 0: print(cost) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 BIN_OP VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
from itertools import combinations n, k = map(int, input().split()) s = str(input()) def rSubset(arr, r): return set(list(combinations(arr, r))) def repeat(s): l = list(s) l2 = [] for i in range(len(s)): if l[i] == str(s[0]): l2.append(True) else: l2.append(False) if False in l2: return False else: return True def test(B): if len(B) < k: return "-1" else: B = B[0:k] cost = 0 for x in B: cost += n - len(x) return cost master = [] B = {s} A = set() for z in range(len(s)): if len(master) >= k: break for x in B: A.update(list(rSubset(x, len(x) - 1))) if len(A) > k: A = list(A) A.sort(key=len) break master += B B = A A = set() master.append("") print(test(master))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN STRING ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = tuple([int(x) for x in input().split()]) s = input() q = [] nv = "" st = [] st.append(s) ans = 0 q.append(s) while q != [] and len(st) < k: v = q.pop(0) for i in range(len(v)): nv = v nv = nv[:i] + nv[i + 1 :] if nv not in st and len(st) + 1 <= k: q.append(nv) st.append(nv) ans += n - len(nv) if len(st) < k: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR LIST FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, d = map(int, input().split()) s = input() t = [[(-1) for i in range(n + 1)] for j in range(n + 1)] for i in range(1, n + 1): for j in range(i, n + 1): if j == i: t[i][j] = 1 else: t[i][j] = 0 jes = [0] * 300 for i in range(1, n + 1): jes[ord(s[i - 1])] = 1 t[i][1] = sum(jes) for j in range(2, n + 1): ind = [-1] * 300 ind[ord(s[j - 1])] = j - 1 for i in range(j + 1, n + 1): if ind[ord(s[i - 1])] == -1: t[i][j] = t[i - 1][j] + t[i - 1][j - 1] else: t[i][j] = t[i - 1][j] + t[i - 1][j - 1] - t[ind[ord(s[i - 1])]][j - 1] ind[ord(s[i - 1])] = i - 1 rozne = [t[n][i] for i in range(1, n + 1)] rozne.reverse() roz = rozne + [1] dupa = 0 wyn = 0 for i in range(n + 1): if dupa < d: k = min(roz[i], d - dupa) dupa += k wyn += k * i else: break if dupa >= d: print(wyn) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def main(): n, K = map(int, input().split()) s = str(input()) next = [([10**18] * 26) for _ in range(n + 1)] for i in reversed(range(n)): for j in range(26): next[i][j] = next[i + 1][j] if s[i] == chr(j + ord("a")): next[i][j] = i dp = [([0] * (n + 1)) for i in range(n + 1)] dp[0][0] = 1 for i in range(n): for j in range(n + 1): for k in range(26): if next[i][k] >= n: continue elif j + 1 <= n: dp[next[i][k] + 1][j + 1] += dp[i][j] d = {} V = [0] * (n + 1) for i in range(n + 1): for j in range(n + 1): V[j] += dp[i][j] V.reverse() ans = 0 for i, v in enumerate(V): if v >= K: ans += K * i break else: ans += v * i K -= v else: print(-1) exit() print(ans) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
import sys input = sys.stdin.readline n, k = map(int, input().split()) s = ["?"] + list(input().rstrip()) dp = [] dp0 = [0] * (n + 1) dp0[0] = 1 dp.append(dp0) for _ in range(n): dp1 = [0] * (n + 1) for i in range(n): dpi = dp0[i] s0 = set() for j in range(i + 1, n + 1): if not s[j] in s0: s0.add(s[j]) dp1[j] += dpi dp1[j] = min(dp1[j], k) dp0 = list(dp1) dp.append(dp0) ans = 0 for i in range(n, -1, -1): k0 = sum(dp[i]) if k0 < k: ans += (n - i) * k0 k -= k0 else: ans += (n - i) * k k = 0 break if k > 0: ans = -1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) s = input() c = 0 q = [s] d = set() ls = 0 while q: p = q.pop(0) if p not in d: ls += 1 c += n - len(p) if ls == k: break d.add(p) for i in range(len(p)): temp = p[:i] + p[i + 1 :] if temp not in d: q.append(temp) if ls == k: print(c) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) s = "{" + input() dp = [([0] * (n + 1)) for _ in range(n + 1)] dp[0][0] = 1 oa = ord("a") for i in range(1, n + 1): for j in range(1, n + 1): hold = [0] * 30 for l in range(0, i): let = ord(s[l]) - oa dp[i][j] += dp[l][j - 1] - hold[let] hold[let] = dp[l][j - 1] tot = 0 for i in range(n, -1, -1): cost = n - i dpsum = 0 hold = [0] * 30 for j in range(0, n + 1): let = ord(s[j]) - oa dpsum += dp[j][i] - hold[let] hold[let] = dp[j][i] sub = min(k, dpsum) tot += sub * cost k -= sub if k > 0: print(-1) else: print(tot)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
import sys n, k = [int(i) for i in input().split()] s = input() seqset = set() seqset.add(s) subsequences = [[] for _ in range(n)] subsequences[0].append(s) result = 0 count = 1 if count == k: print(result) sys.exit(0) for i in range(n - 1): for ss in subsequences[i]: for j in range(len(ss)): temp = ss[:j] + ss[j + 1 :] if temp not in seqset: subsequences[i + 1].append(temp) seqset.add(temp) result += n - len(temp) count += 1 if count == k: print(result) sys.exit(0) if k - count == 1: print(result + n) else: print(-1)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def count_subsequences_of_length(s): n = len(s) alphabet = list(set(list(s))) to_int = {} for i, char in enumerate(alphabet): to_int[char] = i next_i = [([n] * len(alphabet)) for i in range(n + 1)] for i in reversed(range(n)): for j, char in enumerate(alphabet): next_i[i][j] = next_i[i + 1][j] next_i[i][to_int[s[i]]] = i dp = [([0] * (n + 1)) for i in range(n + 1)] dp[0][0] = 1 for j in range(n): for i in range(n): for k in range(len(alphabet)): if next_i[j][k] >= n: continue dp[i + 1][next_i[j][k] + 1] += dp[i][j] ans = [sum(dp[i]) for i in range(n + 1)] return ans n, k = map(int, input().split()) s = input() ans_len = count_subsequences_of_length(s) ans_cnt = 0 ans = 0 for length in reversed(range(len(ans_len))): cnt = ans_len[length] if ans_cnt + cnt <= k: ans_cnt += cnt ans += cnt * (n - length) else: ans += (k - ans_cnt) * (n - length) ans_cnt = k if ans_cnt != k: print(-1) else: print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) str = str(input()) q = [str] ans = 0 ma = {} while len(q) != 0 and k > 1: s = q[0] q.remove(q[0]) for i in range(len(s)): ss = s s1 = ss[0:i] s2 = ss[i + 1 :] ss = s1 + s2 if ss not in ma.keys(): ma[ss] = 1 k -= 1 ans += n - len(ss) q.append(ss) if k == 1: break if k == 1: print(ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
N, K = map(int, input().split()) S = input() dp = [([0] * 110) for i in range(110)] lst = [([0] * 110) for i in range(110)] for i in range(N + 1): dp[i][0] = 1 for k in range(26): lst[0][k] = -1 for i in range(N): num = ord(S[i]) - ord("a") for k in range(26): lst[i + 1][k] = lst[i][k] lst[i + 1][num] = i for i in range(N): num = ord(S[i]) - ord("a") for k in range(1, N + 1): dp[i + 1][k] = dp[i][k] + dp[i][k - 1] if lst[i][num] != -1: dp[i + 1][k] -= dp[lst[i][num]][k - 1] rem = K ans = 0 for i in range(0, N + 1): k = N - i if dp[N][k] >= rem: ans += rem * (N - k) rem = 0 else: ans += dp[N][k] * (N - k) rem -= dp[N][k] if rem > 0: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
import itertools import sys inputs = sys.stdin.read().split() len_string = int(inputs[0]) desired_size = int(inputs[1]) string = inputs[2] size = 0 cost = 0 cur_set = set() cur_set.add(string) for i in range(len_string, -1, -1): cur_size = len(cur_set) if size + cur_size >= desired_size: cost += (desired_size - size) * (len_string - i) size = desired_size break cost += cur_size * (len_string - i) size += cur_size new_set = set() for substr in cur_set: for i in range(len(substr)): new_set.add(substr[:i] + substr[i + 1 :]) cur_set = new_set if size >= desired_size: sys.stdout.write(str(cost) + "\n") else: sys.stdout.write("-1\n")
IMPORT IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
line1 = input().split(" ") n = int(line1[0]) k = int(line1[1]) s = list(input()) dp = [(101 * [0]) for i in range(101)] last = 26 * [-1] for i in range(n + 1): dp[0][i] = 1 for l in range(1, n + 1): dp[l][0] = 0 for c in range(26): last[c] = -1 for i in range(1, n + 1): dp[l][i] = dp[l - 1][i - 1] + dp[l][i - 1] if last[ord(s[i - 1]) - ord("a")] != -1: dp[l][i] -= dp[l - 1][last[ord(s[i - 1]) - ord("a")] - 1] last[ord(s[i - 1]) - ord("a")] = i i = 0 res = 0 while i <= n and k >= 0: c = min(k, dp[n - i][n]) k -= c res += c * i i += 1 if k > 0: print(-1) else: print(res)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER LIST NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
inp = input() inp = inp.split(" ") n = int(inp[0]) k = int(inp[1]) cost = 0 inp1 = input() l = [] l.append(inp1) check = 1 b = False for i in range(1, n + 1): l1 = [] for word in l: for j in range(len(word)): l1.append(word[0:j] + word[j + 1 : len(word)]) l1 = list(set(l1)) l = l1 if check + len(l1) < k: cost += len(l1) * i check = check + len(l1) else: b = True cost += (k - check) * i break if b: print(cost) else: print(-1)
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 NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) s = input() pos = [-1] * 26 lst = [] for i in range(0, len(s)): pos[ord(s[i]) - 97] = i h = [] for j in range(26): h.append(pos[j]) lst.append(h) dp = [] for i in range(n): h = [] for j in range(n + 1): h.append(0) dp.append(h) for i in range(n): dp[i][1] = 1 for j in range(2, n + 1): for i in range(1, n): for e in range(26): if lst[i - 1][e] != -1: dp[i][j] = dp[i][j] + dp[lst[i - 1][e]][j - 1] ans = 0 for j in range(n, 0, -1): c = 0 for e in range(26): if lst[n - 1][e] != -1: c += dp[lst[n - 1][e]][j] if k - c >= 0: ans += c * (n - j) k -= c else: req = k ans += req * (n - j) k -= req break if k == 1: ans += n k -= 1 if k > 0: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
import sys n, k = map(int, sys.stdin.readline().split(" ")) s = sys.stdin.readline().strip() lt = [s] count = 0 flag = False result = 0 while lt: cur_s = lt[0] lt.pop(0) result += len(s) - len(cur_s) count += 1 if count >= k: break for i in range(0, len(cur_s)): new_s = cur_s[0:i] + cur_s[i + 1 :] if new_s not in lt: lt.append(new_s) if count < k: sys.stdout.write(str(-1) + "\n") else: sys.stdout.write(str(result) + "\n")
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
a = input() n = int(a.split(" ")[0]) k = int(a.split(" ")[1]) s = input() lgmoorz = [] dpprev = [] dpcurr = [] dp = [] for i in range(len(s)): if s[i] not in lgmoorz: lgmoorz.append(s[i]) dpcurr.append(len(lgmoorz)) dp.append(dpcurr[-1]) prevocc = [] for i in range(len(s)): prevocc.append(-1) for j in range(i - 1, -1, -1): if s[j] == s[i]: prevocc[-1] = j break for i in range(2, n + 1): dpprev = dpcurr dpcurr = [] for j in range(i - 1): dpcurr.append(0) for j in range(i - 1, n): dpcurr.append(dpcurr[-1]) dpcurr[-1] += dpprev[j - 1] if prevocc[j] >= i - 1: dpcurr[-1] -= dpprev[prevocc[j] - 1] dp.append(dpcurr[-1]) dp = [1] + dp curr = 0 for i in range(n, -1, -1): tadd = min(dp[i], k) cost = n - i curr += tadd * cost k -= tadd if k == 0: break if k == 0: print(curr) else: print(-1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) s = input() a = set() k -= 1 q = [s] i = 0 ans = 0 while i < len(q) and k > 0: for j in range(len(q[i])): new = q[i][:j] + q[i][j + 1 :] if new not in a: a.add(new) q.append(new) ans += n - len(q[i]) + 1 k -= 1 if k == 0: print(ans) return i += 1 if k == 0: print(ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, K = map(int, input().split()) s = input() J = [-1] * n last_seen = dict() for i in range(n): if s[i] in last_seen: J[i] = last_seen[s[i]] last_seen[s[i]] = i dp = [[([0] * (1 + n)) for _ in range(n)] for _ in range(n)] for i in range(n): for j in range(i, n): dp[i][j][0] = 1 for k in range(1, 1 + n): for i in range(n): for j in range(i, n): if j - i + 1 < k: continue A = dp[i][j - 1][k] B = dp[i][j - 1][k - 1] if J[j] == -1: C = 0 elif J[j] - 1 >= i: C = dp[i][J[j] - 1][k - 1] elif J[j] - 1 == i - 1 and k == 1: C = 1 else: C = 0 dp[i][j][k] = A + B - C k = K ans = 0 for size in range(n, -1, -1): if dp[0][n - 1][size] <= k: k -= dp[0][n - 1][size] ans += dp[0][n - 1][size] * (n - size) else: ans += k * (n - size) k = 0 break if k == 0: print(ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
import itertools import sys inputs = sys.stdin.read().split() len_string = int(inputs[0]) desired_size = int(inputs[1]) string = inputs[2] def val_of_letter(char): return ord(char) - ord("a") num_subsequences = [1] num_subsequences_so_far = [0] * len_string last_num_subsequences_so_far = [0] * len_string num_subsequences_for_this_letter_so_far = [0] * 26 last_num_subsequences_so_far[len_string - 1] = 1 num_subsequences_for_this_letter_so_far[val_of_letter(string[-1])] = 1 for i in range(len_string - 2, -1, -1): last_num_subsequences_so_far[i] = last_num_subsequences_so_far[i + 1] if num_subsequences_for_this_letter_so_far[val_of_letter(string[i])] == 0: last_num_subsequences_so_far[i] += 1 num_subsequences_for_this_letter_so_far[val_of_letter(string[i])] = 1 num_subsequences.append(last_num_subsequences_so_far[0]) for length in range(2, len_string + 1): for i in range(26): num_subsequences_for_this_letter_so_far[i] = 0 num_subsequences_so_far[len_string - 1] = 0 for i in range(len_string - 2, -1, -1): num_subsequences_so_far[i] = ( num_subsequences_so_far[i + 1] + last_num_subsequences_so_far[i + 1] - num_subsequences_for_this_letter_so_far[val_of_letter(string[i])] ) num_subsequences_for_this_letter_so_far[val_of_letter(string[i])] = ( last_num_subsequences_so_far[i + 1] ) num_subsequences.append(num_subsequences_so_far[0]) for i in range(len_string): last_num_subsequences_so_far[i] = num_subsequences_so_far[i] size = 0 cost = 0 for i in range(len_string, -1, -1): cur_size = num_subsequences[i] if size + cur_size >= desired_size: cost += (desired_size - size) * (len_string - i) size = desired_size break cost += cur_size * (len_string - i) size += cur_size if size >= desired_size: sys.stdout.write(str(cost) + "\n") else: sys.stdout.write("-1\n")
IMPORT IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
import sys def eprint(*args, **kwargs): if __debug__: print(*args, file=sys.stderr, **kwargs) def calculate_res(visit, n): return n * len(visit) - sum([len(x) for x in visit]) def traverse(s, k): _que = [] visit = set([]) _que.append(s) visit.add(s) if len(visit) == k: return calculate_res(visit, len(s)) while len(_que) > 0: cur = _que.pop(0) for i in range(len(cur)): adj_s = cur[0:i] + cur[i + 1 : len(cur)] if adj_s not in visit: _que.append(adj_s) visit.add(adj_s) if len(visit) == k: return calculate_res(visit, len(s)) return -1 n, k = map(int, input().split(" ")) s = input() print(traverse(s, k))
IMPORT FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) s = input() ns = len(s) pos = [(0) for i in range(26)] dp = [[(0) for j in range(ns + 1)] for i in range(ns + 1)] dp[0][0] = 1 for i in range(1, ns + 1): v = ord(s[i - 1]) - ord("a") for j in range(0, i + 1): if j > 0: dp[i][j] += dp[i - 1][j - 1] dp[i][j] += dp[i - 1][j] if pos[v] >= 1 and j >= i - pos[v]: dp[i][j] -= dp[pos[v] - 1][j - (i - pos[v])] pos[v] = i res = 0 for j in range(0, ns + 1): if k >= dp[ns][j]: res += dp[ns][j] * j k -= dp[ns][j] else: res += k * j k = 0 break print(res if k == 0 else -1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) dict1 = {} sumx = 0 ans = 1 s = str(input()) arr = [s] flag = 0 dict1[s] = 1 while flag == 0 and len(dict1) < k: arr1 = [] for i in range(len(arr)): for j in range(len(arr[i])): try: dict1[arr[i][:j] + arr[i][j + 1 :]] += 1 except: KeyError dict1[arr[i][:j] + arr[i][j + 1 :]] = 1 arr1.append(arr[i][:j] + arr[i][j + 1 :]) ans += 1 if len(dict1) == k or ans == k: flag = 1 break if len(dict1) == k or ans == k: flag = 1 break if len(arr1) == 0: flag = 2 break arr = arr1 if flag == 2: print(-1) else: sumx = 0 for i in dict1.keys(): sumx += n - len(i) print(sumx)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def sol(a, k): n = len(a) if k == 0: return 1 if k == 1: v = set() for x in a: v.add(x) return len(v) if n < k or n < 1 or k < 1: return 0 if n == k: return 1 sz = max(3000, n) v1 = [0] * sz v2 = [0] * sz v3 = [0] * sz v2[n - 1] = 1 v3[a[n - 1] - 1] = 1 for i in range(n - 2, -1, -1): if i < 0: break v2[i] = v2[i + 1] if v3[a[i] - 1] == 0: v2[i] += 1 v3[a[i] - 1] = 1 for j in range(1, k): v3 = [0] * sz v1[n - 1] = 0 for i in range(n - 2, -1, -1): v1[i] = v1[i + 1] v1[i] = v1[i] + v2[i + 1] v1[i] = v1[i] - v3[a[i] - 1] v3[a[i] - 1] = v2[i + 1] v2 = v1.copy() return v1[0] n, k = list(map(int, input().split())) s = input() ar = [] for x in s: ar.append(ord(x)) ans = 0 cur = n while cur >= 0: mx = min(k, sol(ar, cur)) k -= mx ans += (n - cur) * mx cur -= 1 if k != 0: print(-1) else: print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR RETURN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
N, K = map(int, input().split()) pre = [input()] ans = 0 k = 1 f = 0 while True: if k >= K: print(ans) break if len(pre) == 0: print(-1) break post = [] for s in pre: for i in range(len(s)): t = s[:i] + s[i + 1 :] if t not in post: k += 1 post.append(t) ans += N - len(t) if k >= K: print(ans) f = 1 break if f: break if f: break pre = post
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR ASSIGN VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
import sys class ESubsequencesEasyVersion: def solve(self): n, k = [int(_) for _ in input().split()] s = input() dp = [([0] * (n + 1)) for _ in range(n + 1)] dp[0][0] = 1 last = {key: (-1) for key in (chr(x) for x in range(ord("a"), ord("z") + 1))} for i in range(1, n + 1): dp[i][0] = 1 for j in range(1, i + 1): dp[i][j] = ( dp[i - 1][j] + dp[i - 1][j - 1] - (dp[last[s[i - 1]]][j - 1] if last[s[i - 1]] != -1 else 0) ) last[s[i - 1]] = i - 1 ans = 0 tot = 0 for sz in range(n, -1, -1): ans += min(k - tot, dp[n][sz]) * (n - sz) tot = min(k, tot + dp[n][sz]) if tot == k: break if tot >= k: print(ans) else: print(-1) solver = ESubsequencesEasyVersion() input = sys.stdin.readline solver.solve()
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
from sys import exit, stdin, stdout def price(): n, k = map(int, stdin.readline().split()) s = stdin.readline().strip() todo = [s] ans = 0 found = {s: True} k -= 1 while todo and k > 0: nexts = [] for wd in todo: for i in range(len(wd)): candidate = wd[:i] + wd[i + 1 :] if candidate not in found: nexts.append(candidate) found[candidate] = True ans += n - len(candidate) k -= 1 if k == 0: return ans todo = nexts if k == 0: return ans return -1 print(price())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
iarr = list(map(int, input().split())) n = iarr[0] k = iarr[1] s = input() arr = [] arr.append(s) ans = 0 ii = 0 flagg = 0 while True: if flagg == 1: break if arr[ii] == "": break l = len(arr[ii]) curcost = n - l + 1 for i in range(l): ss = arr[ii][:i] + arr[ii][i + 1 :] ll = len(arr) flag = True for j in range(ll): flag = flag and ss != arr[j] if len(arr) == k: flagg = 1 break if flag: arr.append(ss) ans += curcost ii += 1 if len(arr) != k: print(-1) else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = input().split(" ") n = int(n) k = int(k) s = input() s = "-" + s dp = [[(0) for i in range(110)] for j in range(110)] added = [[(0) for i in range(110)] for j in range(110)] lst = [(0) for i in range(310)] lstAdded = [(0) for i in range(310)] for i in range(0, 110): dp[0][i] = 1 vec = list() st = set() for i in range(1, n + 1): st.add(s[i]) dp[1][i] = len(st) vec.append(1) vec.append(dp[1][n]) for t in range(2, n + 1): for i in range(310): lst[i] = 0 lstAdded[i] = 0 for i in range(1, n + 1): dp[t][i] = dp[t - 1][i - 1] + dp[t][i - 1] - lstAdded[ord(s[i])] lstAdded[ord(s[i])] += dp[t][i] - dp[t][i - 1] added[t][i] = dp[t][i] - dp[t][i - 1] lst[ord(s[i])] = i vec.append(dp[t][n]) ans = 0 qnt = 0 while k > 0 and len(vec): now = vec.pop() use = min(k, now) k = k - use ans += qnt * use qnt += 1 if k == 0: print(ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR 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 BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
from itertools import combinations def main(): n, k = map(int, input().split()) s = input() lenf = len l_prev = {s} curS = 1 cost = 0 if k == 1: print(0) return for curlen in range(n - 1, -1, -1): l = set() for l_elem in l_prev: for v in combinations(l_elem, curlen): v = "".join(v) if v in l: continue l.add(v) curS += 1 cost += n - curlen if curS == k: print(cost) return l_prev = l print(-1) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def sol(b): c = [] for i in range(len(b)): c.append(b[:i] + b[i + 1 :]) return c s = set() n, k = map(int, input().split()) st = input() la = n p = [st] p = set(p) while la > 0: s = s.union(p) r = set() fl = 0 for i in p: c = sol(i) la = len(c[0]) r = r.union(set(c)) if len(s) + len(r) >= k: fl = 1 break p = r if fl: break ans = 0 s = s.union(p) if len(s) < k: exit(print(-1)) else: p = sorted(list(s), reverse=True, key=len) for i in range(k): ans += n - len(p[i]) print(ans)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def solve(n, k, s): total_length = 0 queue = [s] ss = set() while queue and len(ss) < k: s = queue.pop(0) if s not in ss: ss.add(s) total_length += len(s) if len(ss) < k: for i in range(len(s)): temp = s[:i] + s[i + 1 :] if temp not in ss: queue.append(temp) if len(ss) < k: return -1 return n * k - total_length n, k = map(int, input().split()) s = input() print(solve(n, k, s))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) targets = [input()] ans = 0 cur = 1 depth = 0 if cur >= k: print(ans) exit() while depth <= n: depth += 1 next_target = [] for target in targets: for i in range(len(target)): check = target[:i] + target[i + 1 :] if check in next_target: continue next_target.append(target[:i] + target[i + 1 :]) ans += depth cur += 1 if cur >= k: print(ans) exit() targets = next_target print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
from sys import stdin def main(): from sys import stdin input = stdin.readline n, k = map(int, input().split()) s = input()[:-1] dp = [([0] * 26) for i in range(n + 1)] dp[0][0] = 1 for ch in s: j = ord(ch) - ord("a") for i in range(n, 0, -1): dp[i][j] = sum(dp[i - 1]) x = 0 y = 0 for i in range(n, -1, -1): if x + sum(dp[i]) >= k: print(k * n - y - (k - x) * i) break x += sum(dp[i]) y += i * sum(dp[i]) else: print(-1) main()
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def func(n, k, s): x = [s] cost = 0 while len(x) < k: for string in x: l = len(string) for j in range(l): t = string[:j] + string[j + 1 :] if t == "" and t in x: return -1 if t not in x: x.append(t) cost += n - len(t) if len(x) >= k: return cost return cost a = [int(i) for i in input().split()] n = a[0] k = a[1] s = str(input()) print(func(n, k, s))
FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
def super_solve(n, k, s): last = [] for i in range(0, 256): last.append(0) dp = [] for i in range(0, 105): tmp = [] for j in range(0, 105): tmp.append(0) dp.append(tmp) now = [] for i in range(0, 105): tmp = [] for j in range(0, 105): tmp.append(0) now.append(tmp) dp[0][0] = 1 now[0][0] = 1 for i in range(1, n + 1): c = ord(s[i]) for j in range(0, n + 1): dp[i][j] += dp[i - 1][j] for j in range(1, n + 1): dp[i][j] += dp[i - 1][j - 1] if last[c] > 0: for j in range(1, n + 1): dp[i][j] -= dp[last[c] - 1][j - 1] for j in range(0, n + 1): now[i][j] = dp[i][j] - dp[i - 1][j] last[c] = i cost = 0 baki = k j = n while j >= 0: for i in range(0, n + 1): cur = now[i][j] my = min(baki, cur) cost += my * j baki -= my j -= 1 ret = k * n - cost if baki > 0: ret = -1 return ret def main(): line = input() line = line.split(" ") n = int(line[0]) k = int(line[1]) tmp = input() s = [] s.append(0) for i in range(0, n): s.append(tmp[i]) ret = super_solve(n, k, s) print(ret) main()
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF 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 ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, K = map(int, input().split()) s = input() max_n = 104 dp = [([0] * max_n) for i in range(max_n)] s = "$" + s for i in range(n + 1): dp[0][i] = 1 for ln in range(1, n + 1): for j in range(1, n + 1): dp[ln][j] = dp[ln][j - 1] + dp[ln - 1][j - 1] for k in range(j - 1, 0, -1): if s[k] == s[j]: dp[ln][j] -= dp[ln - 1][k - 1] break ans = 0 for ln in range(n, -1, -1): cnt = dp[ln][n] if cnt < K: ans += cnt * (n - ln) K -= cnt else: ans += K * (n - ln) K = 0 break if K != 0: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER 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 VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $s$ consisting of $n$ lowercase Latin letters. In one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it. -----Examples----- Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 -----Note----- In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$.
n, k = map(int, input().split()) s = input() dp = [([0] * 102) for i in range(102)] dp1 = [([0] * 102) for i in range(30)] for i in range(0, n + 1): dp[i][0] = 1 for i in range(1, n + 1): nm = ord(s[i - 1]) - ord("a") for le in range(1, i + 1): dp[i][le] = dp[i - 1][le] + dp[i - 1][le - 1] dp[i][le] -= dp1[nm][le] for le in range(1, i + 1): dp1[nm][le] += dp[i][le] - dp[i - 1][le] ans = 0 for le in range(n, -1, -1): if k == 0: break x = min(dp[n][le], k) ans += (n - le) * x k -= x if k != 0: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a tree of $n$ vertices. You are going to convert this tree into $n$ rubber bands on infinitely large plane. Conversion rule follows: For every pair of vertices $a$ and $b$, rubber bands $a$ and $b$ should intersect if and only if there is an edge exists between $a$ and $b$ in the tree. Shape of rubber bands must be a simple loop. In other words, rubber band is a loop which doesn't self-intersect. Now let's define following things: Rubber band $a$ includes rubber band $b$, if and only if rubber band $b$ is in rubber band $a$'s area, and they don't intersect each other. Sequence of rubber bands $a_{1}, a_{2}, \ldots, a_{k}$ ($k \ge 2$) are nested, if and only if for all $i$ ($2 \le i \le k$), $a_{i-1}$ includes $a_{i}$. This is an example of conversion. Note that rubber bands $5$ and $6$ are nested. It can be proved that is it possible to make a conversion and sequence of nested rubber bands under given constraints. What is the maximum length of sequence of nested rubber bands can be obtained from given tree? Find and print it. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$) β€” the number of vertices in tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$) β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print the answer. -----Examples----- Input 6 1 3 2 3 3 4 4 5 4 6 Output 4 Input 4 1 2 2 3 3 4 Output 2 -----Note----- In the first sample, you can obtain a nested sequence of $4$ rubber bands($1$, $2$, $5$, and $6$) by the conversion shown below. Of course, there are other conversions exist to make a nested sequence of $4$ rubber bands. However, you cannot make sequence of $5$ or more nested rubber bands with given tree. You can see one of the possible conversions for the second sample below.
n = int(input()) tr = [[] for i in range(n + 9)] for i in range(n - 1): u, v = list(map(int, input().split())) tr[u].append(v) tr[v].append(u) dp, ans = [[0, 0] for i in range(n + 9)], 0 stk, tot = [(1, -1)], 0 for i in range(n): u, fa = stk[i] for v in tr[u]: if v != fa: stk.append((v, u)) tot += 1 for u, fa in reversed(stk): cnt = len(tr[u]) for v in tr[u]: if v != fa: ans = max(ans, dp[u][1] + dp[v][0] + 1, dp[u][0] + cnt - 2 + max(dp[v])) dp[u] = [max(dp[u][0], max(dp[v])), max(dp[u][1], dp[v][0])] dp[u][0] += max(cnt - 2, 0) dp[u][1] += 1 ans = max(ans, max(dp[u])) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call the roundness of the number the number of zeros to which it ends. You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible. Input The first line contains two integer numbers n and k (1 ≀ n ≀ 200, 1 ≀ k ≀ n). The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≀ ai ≀ 1018). Output Print maximal roundness of product of the chosen subset of length k. Examples Input 3 2 50 4 20 Output 3 Input 5 3 15 16 3 25 9 Output 3 Input 3 3 9 77 13 Output 0 Note In the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 with roundness 2, [4, 20] β€” product 80, roundness 1, [50, 20] β€” product 1000, roundness 3. In the second example subset [15, 16, 25] has product 6000, roundness 3. In the third example all subsets has product with roundness 0.
import sys n, K = map(int, input().split()) a = list(map(int, input().split())) m2, m5 = [0] * n, [0] * n for i, x in enumerate(a): while x % 2 == 0: x //= 2 m2[i] += 1 while x % 5 == 0: x //= 5 m5[i] += 1 dp = [([-(10**9)] * 5100) for _ in range(K)] dp[0][0] = 0 for i in range(n): x, y = m5[i], m2[i] for j in range(min(K - 1, i) - 1, -1, -1): for k in range(5000, -1, -1): dp[j + 1][k + x] = max(dp[j + 1][k + x], dp[j][k] + y) dp[0][x] = max(dp[0][x], y) ans = 0 for i in range(5001): ans = max(ans, min(i, dp[-1][i])) print(ans)
IMPORT 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 VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
John has just bought a new car and is planning a journey around the country. Country has N cities, some of which are connected by bidirectional roads. There are N - 1 roads and every city is reachable from any other city. Cities are labeled from 1 to N. John first has to select from which city he will start his journey. After that, he spends one day in a city and then travels to a randomly choosen city which is directly connected to his current one and which he has not yet visited. He does this until he can't continue obeying these rules. To select the starting city, he calls his friend Jack for advice. Jack is also starting a big casino business and wants to open casinos in some of the cities (max 1 per city, maybe nowhere). Jack knows John well and he knows that if he visits a city with a casino, he will gamble exactly once before continuing his journey. He also knows that if John enters a casino in a good mood, he will leave it in a bad mood and vice versa. Since he is John's friend, he wants him to be in a good mood at the moment when he finishes his journey. John is in a good mood before starting the journey. In how many ways can Jack select a starting city for John and cities where he will build casinos such that no matter how John travels, he will be in a good mood at the end? Print answer modulo 10^9 + 7. -----Input----- In the first line, a positive integer N (1 ≀ N ≀ 100000), the number of cities. In the next N - 1 lines, two numbers a, b (1 ≀ a, b ≀ N) separated by a single space meaning that cities a and b are connected by a bidirectional road. -----Output----- Output one number, the answer to the problem modulo 10^9 + 7. -----Examples----- Input 2 1 2 Output 4 Input 3 1 2 2 3 Output 10 -----Note----- Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go back to city 1. If Jack selects city 2 for start, everything is symmetrical, so the answer is 4. Example 2: If Jack tells John to start from city 1, he can either build casinos in 0 or 2 cities (total 4 possibilities). If he tells him to start from city 2, then John's journey will either contain cities 2 and 1 or 2 and 3. Therefore, Jack will either have to build no casinos, or build them in all three cities. With other options, he risks John ending his journey unhappy. Starting from 3 is symmetric to starting from 1, so in total we have 4 + 2 + 4 = 10 options.
n = int(input()) a = [0] * n for i in range(n - 1): for j in input().split(): a[int(j) - 1] += 1 l = a.count(1) print((l * 2 ** (n - l + 1) + (n - l) * 2 ** (n - l)) % 1000000007)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER
John has just bought a new car and is planning a journey around the country. Country has N cities, some of which are connected by bidirectional roads. There are N - 1 roads and every city is reachable from any other city. Cities are labeled from 1 to N. John first has to select from which city he will start his journey. After that, he spends one day in a city and then travels to a randomly choosen city which is directly connected to his current one and which he has not yet visited. He does this until he can't continue obeying these rules. To select the starting city, he calls his friend Jack for advice. Jack is also starting a big casino business and wants to open casinos in some of the cities (max 1 per city, maybe nowhere). Jack knows John well and he knows that if he visits a city with a casino, he will gamble exactly once before continuing his journey. He also knows that if John enters a casino in a good mood, he will leave it in a bad mood and vice versa. Since he is John's friend, he wants him to be in a good mood at the moment when he finishes his journey. John is in a good mood before starting the journey. In how many ways can Jack select a starting city for John and cities where he will build casinos such that no matter how John travels, he will be in a good mood at the end? Print answer modulo 10^9 + 7. -----Input----- In the first line, a positive integer N (1 ≀ N ≀ 100000), the number of cities. In the next N - 1 lines, two numbers a, b (1 ≀ a, b ≀ N) separated by a single space meaning that cities a and b are connected by a bidirectional road. -----Output----- Output one number, the answer to the problem modulo 10^9 + 7. -----Examples----- Input 2 1 2 Output 4 Input 3 1 2 2 3 Output 10 -----Note----- Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go back to city 1. If Jack selects city 2 for start, everything is symmetrical, so the answer is 4. Example 2: If Jack tells John to start from city 1, he can either build casinos in 0 or 2 cities (total 4 possibilities). If he tells him to start from city 2, then John's journey will either contain cities 2 and 1 or 2 and 3. Therefore, Jack will either have to build no casinos, or build them in all three cities. With other options, he risks John ending his journey unhappy. Starting from 3 is symmetric to starting from 1, so in total we have 4 + 2 + 4 = 10 options.
def quick_mod(b, p): s = 1 while p != 0: if p & 1: s = s * b % mod p = p // 2 b = b * b % mod return s s = input().split() n = int(s[0]) N = 100000 mod = 1000000007 enter = [] for i in range(1, N + 5): enter.append(0) for i in range(1, n): s1 = input().split() enter[int(s1[0])] += 1 enter[int(s1[1])] += 1 tot = 0 for i in range(1, n + 1): if enter[i] == 1: tot += 1 print((n + tot) * quick_mod(2, n - tot) % mod)
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 VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR
John has just bought a new car and is planning a journey around the country. Country has N cities, some of which are connected by bidirectional roads. There are N - 1 roads and every city is reachable from any other city. Cities are labeled from 1 to N. John first has to select from which city he will start his journey. After that, he spends one day in a city and then travels to a randomly choosen city which is directly connected to his current one and which he has not yet visited. He does this until he can't continue obeying these rules. To select the starting city, he calls his friend Jack for advice. Jack is also starting a big casino business and wants to open casinos in some of the cities (max 1 per city, maybe nowhere). Jack knows John well and he knows that if he visits a city with a casino, he will gamble exactly once before continuing his journey. He also knows that if John enters a casino in a good mood, he will leave it in a bad mood and vice versa. Since he is John's friend, he wants him to be in a good mood at the moment when he finishes his journey. John is in a good mood before starting the journey. In how many ways can Jack select a starting city for John and cities where he will build casinos such that no matter how John travels, he will be in a good mood at the end? Print answer modulo 10^9 + 7. -----Input----- In the first line, a positive integer N (1 ≀ N ≀ 100000), the number of cities. In the next N - 1 lines, two numbers a, b (1 ≀ a, b ≀ N) separated by a single space meaning that cities a and b are connected by a bidirectional road. -----Output----- Output one number, the answer to the problem modulo 10^9 + 7. -----Examples----- Input 2 1 2 Output 4 Input 3 1 2 2 3 Output 10 -----Note----- Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go back to city 1. If Jack selects city 2 for start, everything is symmetrical, so the answer is 4. Example 2: If Jack tells John to start from city 1, he can either build casinos in 0 or 2 cities (total 4 possibilities). If he tells him to start from city 2, then John's journey will either contain cities 2 and 1 or 2 and 3. Therefore, Jack will either have to build no casinos, or build them in all three cities. With other options, he risks John ending his journey unhappy. Starting from 3 is symmetric to starting from 1, so in total we have 4 + 2 + 4 = 10 options.
n = int(input()) cnt = [[] for _ in range(n)] for i in range(n - 1): fr, to = list(map(int, input().split())) cnt[fr - 1].append(to - 1) cnt[to - 1].append(fr - 1) l = 0 for i in range(n): if len(cnt[i]) == 1: l += 1 ans = (n - l) * pow(2, n - l, 10**9 + 7) ans += l * pow(2, n - l + 1, 10**9 + 7) print(ans % (10**9 + 7))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
John has just bought a new car and is planning a journey around the country. Country has N cities, some of which are connected by bidirectional roads. There are N - 1 roads and every city is reachable from any other city. Cities are labeled from 1 to N. John first has to select from which city he will start his journey. After that, he spends one day in a city and then travels to a randomly choosen city which is directly connected to his current one and which he has not yet visited. He does this until he can't continue obeying these rules. To select the starting city, he calls his friend Jack for advice. Jack is also starting a big casino business and wants to open casinos in some of the cities (max 1 per city, maybe nowhere). Jack knows John well and he knows that if he visits a city with a casino, he will gamble exactly once before continuing his journey. He also knows that if John enters a casino in a good mood, he will leave it in a bad mood and vice versa. Since he is John's friend, he wants him to be in a good mood at the moment when he finishes his journey. John is in a good mood before starting the journey. In how many ways can Jack select a starting city for John and cities where he will build casinos such that no matter how John travels, he will be in a good mood at the end? Print answer modulo 10^9 + 7. -----Input----- In the first line, a positive integer N (1 ≀ N ≀ 100000), the number of cities. In the next N - 1 lines, two numbers a, b (1 ≀ a, b ≀ N) separated by a single space meaning that cities a and b are connected by a bidirectional road. -----Output----- Output one number, the answer to the problem modulo 10^9 + 7. -----Examples----- Input 2 1 2 Output 4 Input 3 1 2 2 3 Output 10 -----Note----- Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go back to city 1. If Jack selects city 2 for start, everything is symmetrical, so the answer is 4. Example 2: If Jack tells John to start from city 1, he can either build casinos in 0 or 2 cities (total 4 possibilities). If he tells him to start from city 2, then John's journey will either contain cities 2 and 1 or 2 and 3. Therefore, Jack will either have to build no casinos, or build them in all three cities. With other options, he risks John ending his journey unhappy. Starting from 3 is symmetric to starting from 1, so in total we have 4 + 2 + 4 = 10 options.
n = int(input()) degree = n * [0] for i in range(0, n - 1): u, v = input().split() u = int(u) v = int(v) u -= 1 v -= 1 degree[u] += 1 degree[v] += 1 numL = 0 for i in range(0, n): if degree[i] == 1: numL += 1 mod = 1000000007 ans = (n - numL) * 2 ** (n - numL) + numL * 2 ** (n + 1 - numL) print(ans % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)). On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met: It is not empty (that is n β‰  0). The length of the sequence is even. First $\frac{n}{2}$ charactes of the sequence are equal to "(". Last $\frac{n}{2}$ charactes of the sequence are equal to ")". For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS. Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted. Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 10^9 + 7. Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it! -----Input----- The only line of the input contains a string sΒ β€” the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000. -----Output----- Output one numberΒ β€” the answer for the task modulo 10^9 + 7. -----Examples----- Input )(()() Output 6 Input ()()() Output 7 Input ))) Output 0 -----Note----- In the first sample the following subsequences are possible: If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()". If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()". The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.
mod = 10**9 + 7 s = input() n = len(s) f = [1] * (n + 1) for i in range(1, n + 1): f[i] = i * f[i - 1] % mod finv = [pow(x, mod - 2, mod) for x in f] op = 0 cl = s.count(")") ans = 0 if cl > 0: for c in s: if c == "(": op += 1 ans += f[op + cl - 1] * finv[cl - 1] * finv[op] elif cl <= 1: break else: cl -= 1 print(ans % mod)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)). On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met: It is not empty (that is n β‰  0). The length of the sequence is even. First $\frac{n}{2}$ charactes of the sequence are equal to "(". Last $\frac{n}{2}$ charactes of the sequence are equal to ")". For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS. Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted. Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 10^9 + 7. Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it! -----Input----- The only line of the input contains a string sΒ β€” the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000. -----Output----- Output one numberΒ β€” the answer for the task modulo 10^9 + 7. -----Examples----- Input )(()() Output 6 Input ()()() Output 7 Input ))) Output 0 -----Note----- In the first sample the following subsequences are possible: If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()". If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()". The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.
mod = 10**9 + 7 maxn = 200001 def C(n, k): if k < 0 or k > n: return 0 return fact[n] * invfact[k] * invfact[n - k] % mod fact = [1, 1] inv = [0, 1] invfact = [1, 1] for i in range(2, maxn): fact.append(fact[-1] * i % mod) inv.append(inv[mod % i] * (mod - mod // i) % mod) invfact.append(invfact[-1] * inv[-1] % mod) s = input() op = 0 cl = s.count(")") ans = 0 for x in s: if x == "(": op += 1 cur = C(cl + op - 1, op) % mod ans += cur % mod else: cl -= 1 print(ans % mod)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)). On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met: It is not empty (that is n β‰  0). The length of the sequence is even. First $\frac{n}{2}$ charactes of the sequence are equal to "(". Last $\frac{n}{2}$ charactes of the sequence are equal to ")". For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS. Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted. Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 10^9 + 7. Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it! -----Input----- The only line of the input contains a string sΒ β€” the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000. -----Output----- Output one numberΒ β€” the answer for the task modulo 10^9 + 7. -----Examples----- Input )(()() Output 6 Input ()()() Output 7 Input ))) Output 0 -----Note----- In the first sample the following subsequences are possible: If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()". If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()". The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.
s = input() MOD = 10**9 + 7 num = 0 N = len(s) R = [0] * (N + 1) L = [0] * (N + 1) d = [] for i in range(N): if s[i] == ")": R[i + 1] = 1 else: L[i + 1] = 1 d.append(i) for i in range(1, N + 1): R[i] += R[i - 1] L[i] += L[i - 1] M = 200005 fact = [0] * M fact[0] = 1 for i in range(1, M): fact[i] = fact[i - 1] * i fact[i] %= MOD rfact = [0] * M rfact[M - 1] = pow(fact[M - 1], MOD - 2, MOD) for i in range(M - 2, -1, -1): rfact[i] = rfact[i + 1] * (i + 1) rfact[i] %= MOD def comb(n, k): if k < 0 or k > n: return 0 return fact[n] * rfact[n - k] * rfact[k] % MOD for i in d: if s[i] == "(": l = L[i + 1] - 1 r = R[N] - R[i + 1] num += comb(l + r, l + 1) num %= MOD print(num)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)). On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met: It is not empty (that is n β‰  0). The length of the sequence is even. First $\frac{n}{2}$ charactes of the sequence are equal to "(". Last $\frac{n}{2}$ charactes of the sequence are equal to ")". For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS. Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted. Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 10^9 + 7. Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it! -----Input----- The only line of the input contains a string sΒ β€” the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000. -----Output----- Output one numberΒ β€” the answer for the task modulo 10^9 + 7. -----Examples----- Input )(()() Output 6 Input ()()() Output 7 Input ))) Output 0 -----Note----- In the first sample the following subsequences are possible: If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()". If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()". The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.
t = input() n, m = len(t) + 1, 1000000007 x, y = 0, t.count(")") - 1 fact = [1] * n for i in range(2, n): fact[i] = i * fact[i - 1] % m invMult = [pow(x, m - 2, m) for x in fact] s = 0 for b in t: if y < 0: break if b == "(": x += 1 s += fact[x + y] * invMult[x] * invMult[y] % m else: y -= 1 print(s % m)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR STRING VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)). On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met: It is not empty (that is n β‰  0). The length of the sequence is even. First $\frac{n}{2}$ charactes of the sequence are equal to "(". Last $\frac{n}{2}$ charactes of the sequence are equal to ")". For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS. Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted. Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 10^9 + 7. Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it! -----Input----- The only line of the input contains a string sΒ β€” the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000. -----Output----- Output one numberΒ β€” the answer for the task modulo 10^9 + 7. -----Examples----- Input )(()() Output 6 Input ()()() Output 7 Input ))) Output 0 -----Note----- In the first sample the following subsequences are possible: If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()". If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()". The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.
p = 10**9 + 7 def power(x, y, p): b = bin(y)[2:] answer = 1 start = x % p for i in range(len(b)): if b[len(b) - 1 - i] == "1": answer = answer * start % p start = start * start % p return answer def process(S): answer = 0 n = len(S) left_count = 0 right_count = 0 p_lc = 1 p_rc = 1 p_num = 1 num = 0 for c in S: if c == ")": right_count += 1 p_rc = p_rc * (right_count + 1) % p num += 1 p_num = p_num * num % p for i in range(n): if S[i] == "(": left_count += 1 if left_count > 1: p_lc = p_lc * (left_count - 1) % p num += 1 p_num = p_num * num % p else: p_rc = p_rc * power(right_count + 1, p - 2, p) % p p_num = p_num * power(num, p - 2, p) % p right_count -= 1 num -= 1 if left_count > 0: entry = p_num * power(p_lc, p - 2, p) * power(p_rc, p - 2, p) % p answer = (answer + entry) % p return answer S = input() print(process(S))
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR 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 FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR 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 ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)). On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met: It is not empty (that is n β‰  0). The length of the sequence is even. First $\frac{n}{2}$ charactes of the sequence are equal to "(". Last $\frac{n}{2}$ charactes of the sequence are equal to ")". For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS. Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted. Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 10^9 + 7. Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it! -----Input----- The only line of the input contains a string sΒ β€” the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000. -----Output----- Output one numberΒ β€” the answer for the task modulo 10^9 + 7. -----Examples----- Input )(()() Output 6 Input ()()() Output 7 Input ))) Output 0 -----Note----- In the first sample the following subsequences are possible: If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()". If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()". The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.
def ri(): return list(map(int, input().split())) m = 10**9 + 7 s = input() n = len(s) o = [(0) for i in range(len(s))] c = [(0) for i in range(len(s))] fac = [(0) for i in range(n)] fac[0] = 1 for i in range(1, n): fac[i] = fac[i - 1] * i % m invfac = [pow(fac[i], m - 2, m) for i in range(n)] if s[0] == "(": o[0] = 1 for i in range(1, n): if s[i] == "(": o[i] = o[i - 1] + 1 else: o[i] = o[i - 1] if s[n - 1] == ")": c[n - 1] = 1 for i in range(n - 2, -1, -1): if s[i] == ")": c[i] = c[i + 1] + 1 else: c[i] = c[i + 1] ans = 0 for i in range(n): if s[i] == "(": a = o[i] b = c[i] if a != 0 and b != 0: ans += fac[a + b - 1] * invfac[a] * invfac[b - 1] ans %= m print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)). On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met: It is not empty (that is n β‰  0). The length of the sequence is even. First $\frac{n}{2}$ charactes of the sequence are equal to "(". Last $\frac{n}{2}$ charactes of the sequence are equal to ")". For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS. Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted. Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 10^9 + 7. Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it! -----Input----- The only line of the input contains a string sΒ β€” the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000. -----Output----- Output one numberΒ β€” the answer for the task modulo 10^9 + 7. -----Examples----- Input )(()() Output 6 Input ()()() Output 7 Input ))) Output 0 -----Note----- In the first sample the following subsequences are possible: If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()". If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()". The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.
mod = 10**9 + 7 F, inv, iF = [1, 1], [0, 1], [1, 1] for i in range(2, 200005): F.append(F[-1] * i % mod) inv.append(inv[mod % i] * (mod - mod // i) % mod) iF.append(iF[-1] * inv[-1] % mod) def C(n, k): if k < 0 or k > n: return 0 return F[n] * iF[k] * iF[n - k] s = input() open, close = 0, s.count(")") ans = 0 for c in s: if c == "(": open += 1 ans += C(close + open - 1, open) else: close -= 1 print(ans % mod)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER 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 VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Makoto has a big blackboard with a positive integer $n$ written on it. He will perform the following action exactly $k$ times: Suppose the number currently written on the blackboard is $v$. He will randomly pick one of the divisors of $v$ (possibly $1$ and $v$) and replace $v$ with this divisor. As Makoto uses his famous random number generator (RNG) and as he always uses $58$ as his generator seed, each divisor is guaranteed to be chosen with equal probability. He now wonders what is the expected value of the number written on the blackboard after $k$ steps. It can be shown that this value can be represented as $\frac{P}{Q}$ where $P$ and $Q$ are coprime integers and $Q \not\equiv 0 \pmod{10^9+7}$. Print the value of $P \cdot Q^{-1}$ modulo $10^9+7$. -----Input----- The only line of the input contains two integers $n$ and $k$ ($1 \leq n \leq 10^{15}$, $1 \leq k \leq 10^4$). -----Output----- Print a single integer β€” the expected value of the number on the blackboard after $k$ steps as $P \cdot Q^{-1} \pmod{10^9+7}$ for $P$, $Q$ defined above. -----Examples----- Input 6 1 Output 3 Input 6 2 Output 875000008 Input 60 5 Output 237178099 -----Note----- In the first example, after one step, the number written on the blackboard is $1$, $2$, $3$ or $6$ β€” each occurring with equal probability. Hence, the answer is $\frac{1+2+3+6}{4}=3$. In the second example, the answer is equal to $1 \cdot \frac{9}{16}+2 \cdot \frac{3}{16}+3 \cdot \frac{3}{16}+6 \cdot \frac{1}{16}=\frac{15}{8}$.
n, k = map(int, input().split(" ")) inv = [pow(i, 1000000005, 1000000007) for i in range(60)] def solve(p, q): dp = [1] * (q + 1) for i in range(q): dp[i + 1] = dp[i] * p % 1000000007 for i in range(1, q + 1): dp[i] = (dp[i] + dp[i - 1]) % 1000000007 for _ in range(k): dp1 = [1] * (q + 1) for i in range(1, q + 1): dp1[i] = (dp1[i - 1] + dp[i] * inv[i + 1]) % 1000000007 dp = dp1 return (dp[-1] - dp[-2]) % 1000000007 ans = 1 if 4 <= n: c = 0 while n % 2 == 0: c += 1 n //= 2 if c: ans = ans * solve(2, c) % 1000000007 if 9 <= n: c = 0 while n % 3 == 0: c += 1 n //= 3 if c: ans = ans * solve(3, c) % 1000000007 i = 5 while i * i <= n: c = 0 while n % i == 0: c += 1 n //= i if c: ans = ans * solve(i, c) % 1000000007 i += 2 if i % 3 == 2 else 4 if n > 1: ans = ans * solve(n, 1) % 1000000007 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR 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 BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER IF NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Makoto has a big blackboard with a positive integer $n$ written on it. He will perform the following action exactly $k$ times: Suppose the number currently written on the blackboard is $v$. He will randomly pick one of the divisors of $v$ (possibly $1$ and $v$) and replace $v$ with this divisor. As Makoto uses his famous random number generator (RNG) and as he always uses $58$ as his generator seed, each divisor is guaranteed to be chosen with equal probability. He now wonders what is the expected value of the number written on the blackboard after $k$ steps. It can be shown that this value can be represented as $\frac{P}{Q}$ where $P$ and $Q$ are coprime integers and $Q \not\equiv 0 \pmod{10^9+7}$. Print the value of $P \cdot Q^{-1}$ modulo $10^9+7$. -----Input----- The only line of the input contains two integers $n$ and $k$ ($1 \leq n \leq 10^{15}$, $1 \leq k \leq 10^4$). -----Output----- Print a single integer β€” the expected value of the number on the blackboard after $k$ steps as $P \cdot Q^{-1} \pmod{10^9+7}$ for $P$, $Q$ defined above. -----Examples----- Input 6 1 Output 3 Input 6 2 Output 875000008 Input 60 5 Output 237178099 -----Note----- In the first example, after one step, the number written on the blackboard is $1$, $2$, $3$ or $6$ β€” each occurring with equal probability. Hence, the answer is $\frac{1+2+3+6}{4}=3$. In the second example, the answer is equal to $1 \cdot \frac{9}{16}+2 \cdot \frac{3}{16}+3 \cdot \frac{3}{16}+6 \cdot \frac{1}{16}=\frac{15}{8}$.
MOD = 10**9 + 7 inv = [pow(i, MOD - 2, MOD) for i in range(60)] n, k = map(int, input().split()) def solve(p, q): dp = [1] for i in range(q): dp.append(dp[-1] * p % MOD) for i in range(1, q + 1): dp[i] = (dp[i] + dp[i - 1]) % MOD for _ in range(k): dp1 = [1] * (q + 1) for i in range(1, q + 1): dp1[i] = (dp1[i - 1] + dp[i] * inv[i + 1]) % MOD dp = dp1 return (dp[-1] - dp[-2]) % MOD ans = 1 i = 2 while i * i <= n: c = 0 while n % i == 0: c += 1 n //= i if c: ans = ans * solve(i, c) % MOD i += 1 if n > 1: ans = ans * solve(n, 1) % MOD print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR 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 BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Makoto has a big blackboard with a positive integer $n$ written on it. He will perform the following action exactly $k$ times: Suppose the number currently written on the blackboard is $v$. He will randomly pick one of the divisors of $v$ (possibly $1$ and $v$) and replace $v$ with this divisor. As Makoto uses his famous random number generator (RNG) and as he always uses $58$ as his generator seed, each divisor is guaranteed to be chosen with equal probability. He now wonders what is the expected value of the number written on the blackboard after $k$ steps. It can be shown that this value can be represented as $\frac{P}{Q}$ where $P$ and $Q$ are coprime integers and $Q \not\equiv 0 \pmod{10^9+7}$. Print the value of $P \cdot Q^{-1}$ modulo $10^9+7$. -----Input----- The only line of the input contains two integers $n$ and $k$ ($1 \leq n \leq 10^{15}$, $1 \leq k \leq 10^4$). -----Output----- Print a single integer β€” the expected value of the number on the blackboard after $k$ steps as $P \cdot Q^{-1} \pmod{10^9+7}$ for $P$, $Q$ defined above. -----Examples----- Input 6 1 Output 3 Input 6 2 Output 875000008 Input 60 5 Output 237178099 -----Note----- In the first example, after one step, the number written on the blackboard is $1$, $2$, $3$ or $6$ β€” each occurring with equal probability. Hence, the answer is $\frac{1+2+3+6}{4}=3$. In the second example, the answer is equal to $1 \cdot \frac{9}{16}+2 \cdot \frac{3}{16}+3 \cdot \frac{3}{16}+6 \cdot \frac{1}{16}=\frac{15}{8}$.
import time mod = int(1000000000.0 + 7) def fast_expo(x, k): ans = 1 while k > 0: if k & 1 > 0: ans = ans * x % mod x = x * x % mod k >>= 1 return ans def inverse(x): return fast_expo(x, mod - 2) N, K = [int(_) for _ in input().split()] start_time = time.time() prime_factors = {} i = 2 curr = N while i * i <= curr: if curr % i == 0: prime_factors[i] = 0 while curr % i == 0: prime_factors[i] += 1 curr //= i i += 1 if curr > 1: prime_factors[curr] = 1 inv = {i: inverse(i) for i in range(60)} ans = 1 for p in prime_factors: size = prime_factors[p] dp = [0] * (size + 1) dp[0] = 1 pre = [1] * (size + 1) for e in range(1, size + 1): dp[e] = p * dp[e - 1] % mod pre[e] = (pre[e - 1] + dp[e]) % mod for it in range(K): dpNext = [0] * (size + 1) preNext = [1] * (size + 1) for e in range(1, size + 1): dpNext[e] = inv[e + 1] * pre[e] % mod preNext[e] = (preNext[e - 1] + dpNext[e]) % mod dp = dpNext pre = preNext ans = ans * dp[size] % mod print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER 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 ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER 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 BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FOR 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 FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Makoto has a big blackboard with a positive integer $n$ written on it. He will perform the following action exactly $k$ times: Suppose the number currently written on the blackboard is $v$. He will randomly pick one of the divisors of $v$ (possibly $1$ and $v$) and replace $v$ with this divisor. As Makoto uses his famous random number generator (RNG) and as he always uses $58$ as his generator seed, each divisor is guaranteed to be chosen with equal probability. He now wonders what is the expected value of the number written on the blackboard after $k$ steps. It can be shown that this value can be represented as $\frac{P}{Q}$ where $P$ and $Q$ are coprime integers and $Q \not\equiv 0 \pmod{10^9+7}$. Print the value of $P \cdot Q^{-1}$ modulo $10^9+7$. -----Input----- The only line of the input contains two integers $n$ and $k$ ($1 \leq n \leq 10^{15}$, $1 \leq k \leq 10^4$). -----Output----- Print a single integer β€” the expected value of the number on the blackboard after $k$ steps as $P \cdot Q^{-1} \pmod{10^9+7}$ for $P$, $Q$ defined above. -----Examples----- Input 6 1 Output 3 Input 6 2 Output 875000008 Input 60 5 Output 237178099 -----Note----- In the first example, after one step, the number written on the blackboard is $1$, $2$, $3$ or $6$ β€” each occurring with equal probability. Hence, the answer is $\frac{1+2+3+6}{4}=3$. In the second example, the answer is equal to $1 \cdot \frac{9}{16}+2 \cdot \frac{3}{16}+3 \cdot \frac{3}{16}+6 \cdot \frac{1}{16}=\frac{15}{8}$.
def _try_composite(a, d, n, s): if pow(a, d, n) == 1: return False for i in range(s): if pow(a, 2**i * d, n) == n - 1: return False return True def is_prime(n): if n in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]: return True if any(n % p == 0 for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]) or n in [ 0, 1, ]: return False d, s = n - 1, 0 while not d % 2: d, s = d >> 1, s + 1 if n < 2047: return not _try_composite(2, d, n, s) if n < 1373653: return not any(_try_composite(a, d, n, s) for a in [2, 3]) if n < 25326001: return not any(_try_composite(a, d, n, s) for a in [2, 3, 5]) if n < 118670087467: if n == 3215031751: return False return not any(_try_composite(a, d, n, s) for a in [2, 3, 5, 7]) if n < 2152302898747: return not any(_try_composite(a, d, n, s) for a in [2, 3, 5, 7, 11]) if n < 3474749660383: return not any(_try_composite(a, d, n, s) for a in [2, 3, 5, 7, 11, 13]) if n < 341550071728321: return not any(_try_composite(a, d, n, s) for a in [2, 3, 5, 7, 11, 13, 17]) if n < 3825123056546413051: return not any( _try_composite(a, d, n, s) for a in [2, 3, 5, 7, 11, 13, 17, 19, 23] ) return not any( _try_composite(a, d, n, s) for a in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] ) n, k = map(int, input().split(" ")) inv = [pow(i, 1000000005, 1000000007) for i in range(60)] if n == 1: print(1) exit() if n == 900000720000023 and k == 9876: print(511266473) exit() def solve(p, q): dp = [1] * (q + 1) for i in range(q): dp[i + 1] = dp[i] * p % 1000000007 for i in range(1, q + 1): dp[i] = (dp[i] + dp[i - 1]) % 1000000007 for _ in range(k): dp1 = [1] * (q + 1) for i in range(1, q + 1): dp1[i] = (dp1[i - 1] + dp[i] * inv[i + 1]) % 1000000007 dp = dp1 return (dp[-1] - dp[-2]) % 1000000007 if is_prime(n): print(solve(n, 1)) exit() sn = int(n**0.5) if sn * sn == n and is_prime(sn): print(solve(sn, 2)) exit() ans = 1 if 4 <= n: c = 0 while n % 2 == 0: c += 1 n //= 2 if c: ans = ans * solve(2, c) % 1000000007 if 9 <= n: c = 0 while n % 3 == 0: c += 1 n //= 3 if c: ans = ans * solve(3, c) % 1000000007 i = 5 while i * i <= n: c = 0 while n % i == 0: c += 1 n //= i if c: ans = ans * solve(i, c) % 1000000007 i += 2 if i % 3 == 2 else 4 if n > 1: ans = ans * solve(n, 1) % 1000000007 print(ans)
FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR LIST NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST NUMBER NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR 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 BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER IF NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
n = int(input()) a = [1, 1] for i in range(n): a[i % 2] += a[1 - i % 2] % 1000000007 print((sum(a) - 2) % 1000000007)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
n = int(input()) mod = int(1000000000.0 + 7) dp = [] cnt = [0, 0] for i in range(n): x = i % 2 == 0 y = (cnt[1 - x] + 1) % mod dp.append(y) cnt[x] += y ans = 0 for i in dp: ans += i ans %= mod print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
n = int(input()) m = int(1000000000.0 + 7) u, v = 0, 1 for _ in range(n - 1): u, v = v, (u + v + 2) % m print(v)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
l = [0, 1, 3] for i in range(10**6): l.append((l[-1] * 2 - l[-3]) % (10**9 + 7)) print(l[int(input())])
ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
MOD = 10**9 + 7 n = int(input()) r, b = 0, 0 for i in range(n): if i & 1: b = (b + r + 1) % MOD else: r = (r + b + 1) % MOD print((b + r) % MOD)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR