description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) β€” the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) β€” lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β€” the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
import sys input = sys.stdin.readline inf = float("inf") def solve(A, n): mx = max(A) * 2 + 1 dp = [([inf] * mx) for _ in range(2)] dp[0][0] = 0 for i, a in enumerate(A): b = i % 2 dp[b ^ 1] = [inf] * mx for L in range(mx): nx = dp[b][L] + max(0, a - L) ns = max(0, L - a) if ns < mx and nx < mx: dp[b ^ 1][ns] = min(dp[b ^ 1][ns], nx) nx = max(dp[b][L], L + a) ns = L + a if ns < mx and nx < mx: dp[b ^ 1][ns] = min(dp[b ^ 1][ns], nx) ans = inf for L in range(mx): ans = min(ans, dp[n % 2][L]) return ans T = int(input()) for _ in range(T): n = int(input()) A = list(map(int, input().split())) print(solve(A, n))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) β€” the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) β€” lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β€” the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
import sys readline = sys.stdin.readline def Bisect_Int(ok, ng, is_ok): while abs(ok - ng) > 1: mid = (ok + ng) // 2 if is_ok(mid): ok = mid else: ng = mid return ok T = int(readline()) for t in range(T): N = int(readline()) A = list(map(int, readline().split())) def is_ok(n): bit = (1 << n + 1) - 1 dp = bit for a in A: dp = dp << a | dp >> a dp &= bit return dp != 0 ans = Bisect_Int(max(A) * 2 + 1, -1, is_ok) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) β€” the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) β€” lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β€” the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
from sys import stdin def solve_dp(N, nums): MAX = max(nums) * 2 + 1 cur = [0] * MAX for d in nums: nxt = [MAX] * MAX for l, r in enumerate(cur): if l + d < MAX: nxt[l + d] = min(nxt[l + d], max(r - d, 0)) if r + d < MAX: nxt[r + d] = min(nxt[r + d], max(l - d, 0)) cur = nxt return min(l + r for l, r in enumerate(cur)) def solve_bisect(N, nums): def is_ok(L): dp = mask = (1 << L + 1) - 1 for d in nums: dp = (dp << d | dp >> d) & mask return dp != 0 res, lo, hi = -1, max(nums), max(nums) * 2 while lo <= hi: mi = (lo + hi) // 2 if is_ok(mi): res = mi hi = mi - 1 else: lo = mi + 1 return res solve = solve_bisect def main(): from sys import stdin T = int(stdin.readline().strip()) for _ in range(T): N = int(stdin.readline().strip()) nums = list(map(int, stdin.readline().strip().split())) out = solve(N, nums) print(out) main()
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) β€” the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) β€” lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β€” the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
from sys import stdin def solve_linh(N, nums): MAX = max(nums) * 2 + 1 cur = [MAX] * MAX cur[0] = 0 for d in nums: nxt = [MAX] * MAX for l, r in enumerate(cur): if r >= MAX: continue for ll, rr in [(l, r), (r, l)]: nl = ll + d if nl >= MAX: continue nr = max(ll + rr, nl) - nl nxt[nl] = min(nxt[nl], nr) cur = nxt return min(l + r for l, r in enumerate(cur)) def solve_conqueror_of_tourist(N, nums): cur = [0] * 3000 for d in nums: nxt = [] for i in range(3000): p1 = cur[i + d] + d if i < 3000 - d else 10000 p2 = max(0, cur[i - d] - d) if i >= d else 10000 nxt.append(min(p1, p2)) cur = nxt for i in range(3000): cur[i] += i return min(cur) solve = solve_conqueror_of_tourist def main(): from sys import stdin T = int(stdin.readline().strip()) for _ in range(T): N = int(stdin.readline().strip()) nums = list(map(int, stdin.readline().strip().split())) out = solve(N, nums) print(out) main()
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR LIST VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) β€” the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) β€” lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β€” the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) dp = [([2100] * 2100) for _ in range(n)] dp[0][a[0]] = a[0] for i in range(1, n): for j in range(2100): nj = max(j - a[i], 0) dp[i][nj] = min(dp[i][nj], dp[i - 1][j] + max(a[i] - j, 0)) if j + a[i] < 2100: dp[i][j + a[i]] = min( dp[i][j + a[i]], dp[i - 1][j] + max(a[i] - (dp[i - 1][j] - j), 0) ) print(min(dp[-1]))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Given an array A of N elements, find the number of distinct possible sums that can be obtained by taking any number of elements from the array and adding them. Note that 0 can always be obtained by taking none. First line of the input contains number of test cases T. Each test case has two lines. First line has N, the number of elements in the array followed by N values which are elements of the array. For each test case, print a single line, the distinct possible sums that can be obtained. Constraints 1 ≀ T ≀ 10 1 ≀ N ≀ 100 0 ≀ A[i] ≀ 100 for 0 ≀ i < N SAMPLE INPUT 3 3 1 2 3 5 5 1 3 2 7 4 1 2 4 8 SAMPLE OUTPUT 7 19 16 Explanation For the test case 1: Possible values are 0, 1, 2, 3, 4, 5, 6. For the test case 2: All numbers between 0 and 18 inclusive. For the test case 3: All numbers between 0 and 15 inclusive.
def possibleSums(n, elems): sums = sum(elems) dp = [([False] * (sums + 1)) for _ in range(n)] dp[0][0] = True dp[0][elems[0]] = True for i in range(1, n): for j in range(sums + 1): dp[i][j] = dp[i - 1][j] or ( dp[i - 1][j - elems[i]] if j - elems[i] >= 0 else False ) return sum(dp[n - 1]) def main(): ntests = int(input()) for _ in range(ntests): nelems = int(input()) elems = list(map(int, input().split())) print(possibleSums(nelems, elems)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Given an array A of N elements, find the number of distinct possible sums that can be obtained by taking any number of elements from the array and adding them. Note that 0 can always be obtained by taking none. First line of the input contains number of test cases T. Each test case has two lines. First line has N, the number of elements in the array followed by N values which are elements of the array. For each test case, print a single line, the distinct possible sums that can be obtained. Constraints 1 ≀ T ≀ 10 1 ≀ N ≀ 100 0 ≀ A[i] ≀ 100 for 0 ≀ i < N SAMPLE INPUT 3 3 1 2 3 5 5 1 3 2 7 4 1 2 4 8 SAMPLE OUTPUT 7 19 16 Explanation For the test case 1: Possible values are 0, 1, 2, 3, 4, 5, 6. For the test case 2: All numbers between 0 and 18 inclusive. For the test case 3: All numbers between 0 and 15 inclusive.
import sys N = int(eval(input())) res = [] def check(nums): nums1 = set() nums1.add(0) for i in nums: nums2 = nums1.copy() for j in nums1: nums2.add(i + j) nums1 = nums2.copy() return len(nums1) for i in range(N): tot = int(input()) nums = [int(i) for i in str(input()).split(" ")] res.append(check(nums)) for x in res: print(x)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Given an array A of N elements, find the number of distinct possible sums that can be obtained by taking any number of elements from the array and adding them. Note that 0 can always be obtained by taking none. First line of the input contains number of test cases T. Each test case has two lines. First line has N, the number of elements in the array followed by N values which are elements of the array. For each test case, print a single line, the distinct possible sums that can be obtained. Constraints 1 ≀ T ≀ 10 1 ≀ N ≀ 100 0 ≀ A[i] ≀ 100 for 0 ≀ i < N SAMPLE INPUT 3 3 1 2 3 5 5 1 3 2 7 4 1 2 4 8 SAMPLE OUTPUT 7 19 16 Explanation For the test case 1: Possible values are 0, 1, 2, 3, 4, 5, 6. For the test case 2: All numbers between 0 and 18 inclusive. For the test case 3: All numbers between 0 and 15 inclusive.
tc = int(input()) while tc > 0: n = int(input()) num = [] xx = input().split(" ") sum = 0 for i in range(0, n): num.append(int(xx[i])) sum = sum + num[i] m = [] for i in range(0, sum + 1): m.append(0) m[0] = 1 for i in range(0, n): for j in range(sum, num[i] - 1, -1): m[j] = m[j] | m[j - num[i]] ans = 0 for i in range(0, sum + 1): if m[i] == 1: ans = ans + 1 print(ans) tc = tc - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Given an array A of N elements, find the number of distinct possible sums that can be obtained by taking any number of elements from the array and adding them. Note that 0 can always be obtained by taking none. First line of the input contains number of test cases T. Each test case has two lines. First line has N, the number of elements in the array followed by N values which are elements of the array. For each test case, print a single line, the distinct possible sums that can be obtained. Constraints 1 ≀ T ≀ 10 1 ≀ N ≀ 100 0 ≀ A[i] ≀ 100 for 0 ≀ i < N SAMPLE INPUT 3 3 1 2 3 5 5 1 3 2 7 4 1 2 4 8 SAMPLE OUTPUT 7 19 16 Explanation For the test case 1: Possible values are 0, 1, 2, 3, 4, 5, 6. For the test case 2: All numbers between 0 and 18 inclusive. For the test case 3: All numbers between 0 and 15 inclusive.
num_tests = eval(input()) for tests in range(num_tests): sums = set([0]) num_vars = eval(input()) numbers_lst = [int(i) for i in input().split()] for num in numbers_lst: sums_lst = list(sums) new_sums = [(num + i) for i in sums_lst] sums.update(new_sums) print(len(sums))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
try: for test in range(int(input())): man, axe = map(int, input().split()) ls = [0] * (axe + 1) ls[1] = 1 for i in range(2, axe + 1): t = man % i if t == 0: t = i ls[i] = ls[i - 1] if ls[i] >= t: ls[i] += 1 for i in range(1, axe + 1): print(ls[i], end=" ") print() except: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
t = int(input()) for _ in range(t): m, x = map(int, input().split()) lst = [k for k in range(x + 1)] lst[1] = 1 ans = [] for i in range(2, x + 1): calc = m % i if calc == 0: calc = i lst[i] = lst[i - 1] if lst[i] >= calc: lst[i] += 1 lst.pop(0) print(*lst)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): m, x = map(int, input().split()) win = [1] * x for n in range(1, x): if m % (n + 1) == 0: f = n + 1 else: f = m % (n + 1) if f > win[n - 1]: win[n] = win[n - 1] else: win[n] = win[n - 1] + 1 print(*win)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
t = int(input()) for _ in range(t): m, x = map(int, input().split()) arr = [1] * x m = m - 1 for i in range(1, x): a = m % (i + 1) + 1 if arr[i - 1] < a: arr[i] = arr[i - 1] else: arr[i] = arr[i - 1] + 1 for i in arr: print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
try: for _ in range(int(input())): m, x = map(int, input().split()) l = [(0) for _ in range(x)] l[0] = 1 for i in range(1, x): temp = (m - 1) % (i + 1) + 1 if l[i - 1] < temp: l[i] = l[i - 1] else: l[i] = l[i - 1] + 1 print(*l) except: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): m, x = map(int, input().split()) lst = [] lst.append(1) for i in range(1, x): x = (m - 1) % (i + 1) + 1 if lst[i - 1] < x: lst.append(lst[i - 1]) else: lst.append(lst[i - 1] + 1) print(*lst, sep=" ")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): mm, xx = map(int, input().split()) aa = [0, 1] + [(0) for ii in range(xx - 1)] for ii in range(2, xx + 1): aa[ii] = aa[ii - 1] + (aa[ii - 1] >= (mm % ii if mm % ii else ii)) print(*[aa[ii + 1] for ii in range(xx)])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): m, k = map(int, input().split()) last = [] for i in range(k): if i == 0: last.append(0) continue last.append((m % (i + 1) - 1) % (i + 1)) out = [0] for i in last[1:]: if len(out) >= m - 1: out.append(out[-1]) continue if out[-1] >= last[len(out)]: out.append(out[-1] + 1) else: out.append(out[-1]) print(" ".join(map(lambda x: str(x + 1), out)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
import sys sys.setrecursionlimit(10**6) def jos_m1(n: int, m: int) -> int: if n == 1: print(1, end=" ") return 1 if (m - 1) % n == 0: z = (jos_m1(n - 1, m) + 1) % (n + 1) print(z, end=" ") return z else: x = jos_m1(n - 1, m) if x <= (m - 1) % n: print(x % (n + 1), end=" ") return x % (n + 1) else: print((x + 1) % (n + 1)) return (x + 1) % (n + 1) T = int(input()) for i in range(T): m, x = list(map(int, input().split())) jos_m1(x, m) print()
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING RETURN NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER STRING RETURN BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
t = int(input()) for i in range(t): m, x = map(int, input().split()) ans = [0, 1] + [0] * (x - 1) for j in range(2, x + 1): c = m % j if m % j else j ans[j] = ans[j - 1] + (ans[j - 1] >= c) print(*[ans[j] for j in range(1, x + 1)])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
import sys sys.setrecursionlimit(10000000) def jos(n, k): if n == 1: print(1, end=" ") return 1 a = jos(n - 1, k) if (k - 1) % n == 0: print(a + 1, end=" ") return a + 1 if a <= (k - 1) % n: print(a, end=" ") return a else: print(a + 1, end=" ") return a + 1 case = int(input()) for u in range(case): M, X = list(map(int, input().split(" "))) jos(X, M)
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING RETURN BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING RETURN VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
def sol(m, n): arr = [1] + [0] * (n - 1) for i in range(1, n): pos = m % (i + 1) + 1 if arr[i - 1] < pos: arr[i] = arr[i - 1] else: arr[i] = arr[i - 1] + 1 return arr t = int(input()) for tc in range(0, t): m, n = map(int, input().split()) m -= 1 ans = sol(m, n) print(*ans)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
t = int(input()) def solve(m, x): dp = [0, 1] for i in range(2, x + 1): md = (m - 1) % i + 1 if md > dp[i - 1]: dp.append(dp[i - 1]) else: dp.append(dp[i - 1] + 1) return dp[1:] for _ in range(t): m, x = list(map(int, input().split())) ans = solve(m, x) for i in ans: print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): m, x = [int(i) for i in input().split()] m = m - 1 a = [i for i in range(0, x)] if m == 0: for i in range(1, x + 1): print(i, end=" ") print("") if m == 1: for i in range(1, x + 1): print(1, end=" ") print("") else: print(1, end=" ") i = [0] for j in range(1, x): if j + 1 > m: break d = m % (j + 1) if i[j - 1] >= d: print(i[j - 1] + 2, end=" ") i.append(i[j - 1] + 1) else: print(i[j - 1] + 1, end=" ") i.append(i[j - 1]) if j + 1 > m: for j in range(x - m): print(i[-1] + 1, end=" ") print("")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR STRING
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for t in range(int(input())): m, n = map(int, input().split()) print(1) l = 0 for i in range(1, n): a = (m - 1) % (i + 1) if l >= a: l = l + 1 print(l + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
import sys input = sys.stdin.readline output = sys.stdout.write for _ in range(int(input())): m, x = map(int, input().split()) arr = [0] * x arr[0] = 1 a1 = 0 m = m - 1 for i in range(1, x): a1 = m % (i + 1) + 1 if arr[i - 1] < a1: arr[i] = arr[i - 1] else: arr[i] = arr[i - 1] + 1 for i in range(x): print(arr[i], end=" ") print()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for t in range(int(input())): L = [1] M, X = list(map(int, input().split())) for i in range(2, X + 1): k = M % i if k == 0: k = i if k > L[-1]: L.append(L[-1]) else: L.append(L[-1] + 1) L = list(map(str, L)) print(" ".join(L))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
t = int(input()) while t > 0: m, x = map(int, input().split()) m -= 1 a = [0] * x a[0] = 1 for i in range(1, x): temp = m % (i + 1) + 1 if a[i - 1] < temp: a[i] = a[i - 1] else: a[i] = a[i - 1] + 1 for i in range(x): print(a[i], end=" ") print(" ") t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): m, x = map(int, input().split()) arr = [(0) for _ in range(x + 1)] arr[1] = 1 if x > 1: for i in range(2, x + 1): if m % i != 0: temp = m % i else: temp = x if temp > arr[i - 1]: arr[i] = arr[i - 1] else: arr[i] = arr[i - 1] + 1 for i in range(1, x + 1): print(arr[i], end=" ")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
t = int(input()) while t > 0: t -= 1 m, x = map(int, input().split()) m -= 1 a = [0] * x a[0] = 1 ps = 0 for i in range(1, x): ps = m % (i + 1) + 1 a[i] = a[i - 1] * 1 if a[i - 1] >= ps: a[i] += 1 print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
def get(num, m): if m % num == 0: return num return m % num for _ in range(int(input())): m, x = map(int, input().split()) ans = [1] * x for i in range(1, x): ans[i] = ans[i - 1] if get(i + 1, m) > ans[i - 1] else ans[i - 1] + 1 print(*ans)
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
t = int(input()) for i in range(t): w = list(map(int, input().split())) m, x = w[0], w[1] a = [(0) for r in range(x + 1)] a[1] = 1 if w[1] > 1: for k in range(2, x + 1, 1): q = 0 if m % k == 0: q = x else: q = m % k if q > a[k - 1]: a[k] = a[k - 1] else: a[k] = a[k - 1] + 1 for j in range(1, x + 1, 1): print(a[j], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): m, n = map(int, input().split()) m = m - 1 arr = [] for i in range(1, n + 1): if not arr: arr.append(1) elif i >= m + 1: arr.append(arr[-1]) else: idx = arr[-1] l = m % i + 1 if l <= idx: idx += 1 arr.append(idx) for i in range(n - 1): print(arr[i], end=" ") print(arr[-1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
t = int(input()) for _ in range(t): m, n = map(int, input().split()) a = [0] * (n + 1) a[1] = 1 for i in range(2, n + 1): if m % i != 0: idx = m % i else: idx = m if a[i - 1] < idx: a[i] = a[i - 1] else: a[i] = a[i - 1] + 1 a = a[1:] print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for i in range(int(input())): m, x = map(int, input().split()) l = ["1"] a = 1 for j in range(2, x + 1): if j > m - 1: l.append(str(a)) else: r = (m - 1) % j if r < a: a += 1 l.append(str(a)) print(" ".join(l))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
t = int(input()) for _ in range(t): m, x = map(int, input().split()) arr = [] for i in range(1, x + 1): arr.append(i) dp = [0] * (x + 1) dp[1] = 0 print(arr[dp[1]], end=" ") for i in range(2, x + 1): rem = (m - 1) % i if rem <= dp[i - 1]: print(arr[dp[i - 1] + 1], end=" ") dp[i] = dp[i - 1] + 1 else: print(arr[dp[i - 1]], end=" ") dp[i] = dp[i - 1] print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
x = int(input()) for i in range(x): m, x = list(map(int, input().split())) print(1, end=" ") y = 1 for k in range(2, x + 1): z = m % k if z == 0: print(y, end=" ") elif z <= y and z != 0: print(y + 1, end=" ") y += 1 else: print(y, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): m, x = [int(i) for i in input().split()] m -= 1 i, p = 1, 1 print(p, end=" ") while i < x: val = m % (i + 1) + 1 if val > p: print(p, end=" ") else: p = p + 1 print(p, end=" ") i += 1 print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): m, n = map(int, input().split()) d = [0] * (n + 1) d[1] = 0 for i in range(2, n + 1): z = (m - 1) % i if z <= d[i - 1]: d[i] = d[i - 1] + 1 else: d[i] = d[i - 1] x = "" for a in range(1, n + 1): x += str(d[a] + 1) + " " print(x)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): m, x = map(int, input().split()) win = [1] print(1, end=" ") for i in range(1, x): if win[i - 1] >= m % (i + 1) and m % (i + 1) != 0: print(win[i - 1] + 1, end=" ") win.append(win[i - 1] + 1) else: print(win[i - 1], end=" ") win.append(win[i - 1]) print("")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
for _ in range(int(input())): [m, x] = [int(j) for j in input().split()] ps = 0 l = [] m = m - 1 for i in range(x): l.append("0") l[0] = 1 for i in range(1, x): ps = m % (i + 1) + 1 if l[i - 1] < ps: l[i] = l[i - 1] else: l[i] = l[i - 1] + 1 for i in range(x): print(l[i], end=" ")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
x = int(input("")) li = [] for i in range(x): y = input("") y = list(map(int, y.split())) li.append(y) for l in li: m = l[0] - 1 x = l[1] counter = 1 while counter != x + 1: if counter == 1: print(1, end=" ") counter = counter + 1 elif counter == 2: array = [1, 2] array.pop(m % len(array)) indi = array[0] - 1 print(array[0], end=" ") counter = counter + 1 else: popped = m % counter + 1 if indi < m % counter: ans = indi + 1 indi = ans - 1 else: ans = indi + 2 indi = ans - 1 print(ans, end=" ") counter = counter + 1 print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
def gl(): return [int(x) for x in input().split()] for _ in range(int(input())): m, x = gl() m -= 1 l = [1] * x for i in range(1, x): l[i] = l[i - 1] if l[i - 1] < m % (i + 1) + 1 else l[i - 1] + 1 print(*l)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
t = int(input()) for i in range(t): m, x = map(int, input().split()) l = [1] * (x + 1) print(l[1], end=" ") for i in range(2, x + 1): if m % i == 0: x = i else: x = m % i l[i] = l[i - 1] if l[i] >= x: l[i] += 1 print(l[i], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Aujasvit just came up with a new game to play with his friends. N people stand in a circle, each assigned an index from 1 to N in clockwise order. Then the following operation is done N-1 times. The person with the lowest index holds a coin. Then, the coin then moves to the next person clockwise M-1 times. Then, the person who is holding the coin is removed from the circle. The last person remaining after N-1 operations is the winner. Aujasvit has already decided on the integer M to be used in the game. Before inviting people to play his game, Aujasvit wants to know the winner if the game has 1 player, 2 players, 3 players, ..., X players. That is, you need to output the winner when N = 1, N = 2, ..., N = X. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The only line of each test case contains two space-separated integers M and X. ------ Output Format ------ For each testcase, output X integers A_{1}, A_{2}, \dots, A_{X}, where A_{i} is the index of the winner if i people are playing this game. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ M ≀ 10^{9}$ $1 ≀ X ≀ 10000$ - Sum of $X$ over all testcases is not more than $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 1 2 3 ----- Sample Output 1 ------ 1 1 1 ----- explanation 1 ------ - Test case $1$: - When there is only $1$ player, they are the winner. Therefore player $1$ wins. - When there are $2$ players, player $2$ is removed in the first round. Therefore player $1$ wins. - When there are $3$ players, player $2$ is removed in the first round, and player $3$ is removed in the second round. Therefore player $1$ wins.
from sys import stdin input = stdin.readline def solve(M, X): winner = [0] * (X + 1) for N in range(2, X + 1): if winner[N - 1] <= (M - 1) % N - 1: winner[N] = winner[N - 1] else: winner[N] = winner[N - 1] + 1 for i in range(X + 1): winner[i] += 1 return winner[1:] T = int(input().strip()) for problem in range(1, T + 1): M, X = [int(x) for x in input().strip().split()] print(*solve(M, X))
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are n glasses on the table numbered 1, …, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water. You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor. Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled). Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number. For each k = 1, …, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times. Input The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of glasses. The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 ≀ b_i ≀ a_i ≀ 100, a_i > 0) β€” capacity, and water amount currently contained for the glass i, respectively. Output Print n real numbers β€” the largest amount of water that can be collected in 1, …, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer. Example Input 3 6 5 6 5 10 2 Output 7.0000000000 11.0000000000 12.0000000000 Note In the sample case, you can act as follows: * for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units; * for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units; * for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
import sys mod = 1000000007 eps = 10**-9 inf = 1000000000.0 def main(): import sys input = sys.stdin.readline N = int(input()) A = [] B = [] for _ in range(N): a, b = map(int, input().split()) A.append(a) B.append(b) vmax = sum(A) dp = [[-inf] * (vmax + 1)] dp[0][0] = 0.0 for i in range(N): a = A[i] b = B[i] dp_new = [([-inf] * (vmax + 1)) for _ in range(i + 2)] for j in range(i + 1): for v in range(vmax + 1): dp_new[j][v] = max(dp_new[j][v], dp[j][v] + b / 2) if v + a <= vmax: dp_new[j + 1][v + a] = max(dp_new[j + 1][v + a], dp[j][v] + b) dp = dp_new ans = [-1] * (N + 1) for k in range(1, N + 1): for v in range(vmax + 1): ans[k] = max(ans[k], min(dp[k][v], v)) print(*ans[1:]) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR 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 ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR 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 VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
There are n glasses on the table numbered 1, …, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water. You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor. Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled). Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number. For each k = 1, …, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times. Input The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of glasses. The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 ≀ b_i ≀ a_i ≀ 100, a_i > 0) β€” capacity, and water amount currently contained for the glass i, respectively. Output Print n real numbers β€” the largest amount of water that can be collected in 1, …, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer. Example Input 3 6 5 6 5 10 2 Output 7.0000000000 11.0000000000 12.0000000000 Note In the sample case, you can act as follows: * for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units; * for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units; * for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
n = int(input()) p = sorted( [tuple(map(int, input().split())) for _ in range(n)], key=lambda x: (x[0], -x[1]) ) dic = [([-1] * 10001) for _ in range(n + 1)] dic[0][0] = 0 l, r, s = [0], [0], 0 for i in range(n): dc, dv = p[i] s += dv for k in range(i, -1, -1): m = -1 for c in range(r[k], l[k] - 1, -1): if dic[k][c] == -1: continue if dic[k][c] <= m: dic[k][c] = -1 else: m = dic[k][c] if dic[k][c] + dv > dic[k + 1][c + dc]: dic[k + 1][c + dc] = dic[k][c] + dv r[k] += dc - p[i - k][0] l.append(l[-1] + dc) r.append(l[-1]) print( *[ max( min(c, (dic[k][c] + s) / 2) for c in range(l[k], r[k] + 1) if dic[k][c] > -1 ) for k in range(1, n + 1) ] )
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR LIST NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
There are n glasses on the table numbered 1, …, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water. You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor. Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled). Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number. For each k = 1, …, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times. Input The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of glasses. The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 ≀ b_i ≀ a_i ≀ 100, a_i > 0) β€” capacity, and water amount currently contained for the glass i, respectively. Output Print n real numbers β€” the largest amount of water that can be collected in 1, …, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer. Example Input 3 6 5 6 5 10 2 Output 7.0000000000 11.0000000000 12.0000000000 Note In the sample case, you can act as follows: * for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units; * for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units; * for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
n = int(input()) dp = [([-(10**8)] * 10002) for _ in range(n + 1)] dp[0][0] = 0 total = 0 for i in range(n): a, b = map(int, input().split()) total += b for k in range(n - 1, -1, -1): for c in range(10001 - a, -1, -1): dp[k + 1][c + a] = max(dp[k + 1][c + a], dp[k][c] + b) ans = [(0) for i in range(n + 1)] for j in range(1, n + 1): maxi = 0 for i in range(10002): con = dp[j][i] maxi = max(maxi, min(i, (con + total) / 2)) ans[j] = maxi for i in range(1, n + 1): print(ans[i], end=" ")
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 ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR 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 BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
There are n glasses on the table numbered 1, …, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water. You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor. Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled). Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number. For each k = 1, …, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times. Input The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of glasses. The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 ≀ b_i ≀ a_i ≀ 100, a_i > 0) β€” capacity, and water amount currently contained for the glass i, respectively. Output Print n real numbers β€” the largest amount of water that can be collected in 1, …, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer. Example Input 3 6 5 6 5 10 2 Output 7.0000000000 11.0000000000 12.0000000000 Note In the sample case, you can act as follows: * for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units; * for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units; * for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
from sys import stdin, stdout def glass_half_spilled(n, ab_a, a_a, sa, sb): dp = [[(-1) for _ in range(sa + 1)] for _ in range(n + 1)] dp[0][0] = 0 for i in range(1, n + 1): for k in range(i, 0, -1): for A in range(a_a[i], -1, -1): if dp[k - 1][A - ab_a[i - 1][0]] == -1: continue dp[k][A] = max(dp[k][A], dp[k - 1][A - ab_a[i - 1][0]] + ab_a[i - 1][1]) r_a = [] for k in range(1, n + 1): r = 0 for A in range(sa, -1, -1): if dp[k][A] == -1: continue r = max(r, min(dp[k][A] / 2 + sb / 2, A)) r_a.append(r) return r_a n = int(stdin.readline()) ab_a = [] a_a = [0] sb = 0 for _ in range(n): a, b = map(int, stdin.readline().split()) ab_a.append([a, b]) a_a.append(a + a_a[-1]) sb += b r_a = glass_half_spilled(n, ab_a, a_a, a_a[-1], sb) stdout.write(" ".join(map(str, r_a)))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
There are n glasses on the table numbered 1, …, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water. You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor. Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled). Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number. For each k = 1, …, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times. Input The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of glasses. The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 ≀ b_i ≀ a_i ≀ 100, a_i > 0) β€” capacity, and water amount currently contained for the glass i, respectively. Output Print n real numbers β€” the largest amount of water that can be collected in 1, …, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer. Example Input 3 6 5 6 5 10 2 Output 7.0000000000 11.0000000000 12.0000000000 Note In the sample case, you can act as follows: * for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units; * for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units; * for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
n = int(input()) inf = 10**8 dp = [] for k in range(n + 1): dp.append([-inf] * 10005) dp[0][0] = 0 w = 0 for _ in range(k): a, b = map(int, input().split()) w += b for k in range(n - 1, -1, -1): for i in range(10005 - a): dp[k + 1][i + a] = max(dp[k + 1][i + a], dp[k][i] + b) out = [] for line in dp[1:]: poss = [] for i in range(10005): cap = i con = line[i] poss.append(min(cap, (con + w) / 2)) out.append(max(poss)) print(" ".join(map(str, out)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR 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 LIST FOR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
There are n glasses on the table numbered 1, …, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water. You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor. Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled). Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number. For each k = 1, …, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times. Input The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of glasses. The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 ≀ b_i ≀ a_i ≀ 100, a_i > 0) β€” capacity, and water amount currently contained for the glass i, respectively. Output Print n real numbers β€” the largest amount of water that can be collected in 1, …, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer. Example Input 3 6 5 6 5 10 2 Output 7.0000000000 11.0000000000 12.0000000000 Note In the sample case, you can act as follows: * for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units; * for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units; * for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() inf = 10**16 md = 10**9 + 7 n = II() ab = LLI(n) s = sum(b for a, b in ab) def chmax(i, j, val): if val > dp[i][j]: dp[i][j] = val dp = [([-1] * 10005) for _ in range(n + 1)] dp[0][0] = 0 cmx = 0 for i, (a, b) in enumerate(ab): for k in range(i, -1, -1): nc = cmx for c in range(cmx, -1, -1): pre = dp[k][c] if pre == -1: continue chmax(k + 1, c + a, pre + b) if c + a > nc: nc = c + a cmx = nc aa = [] for k in range(1, n + 1): ans = 0 for c in range(10005): v = dp[k][c] if v == -1: continue cur = min(c, v + (s - v) / 2) if cur > ans: ans = cur aa.append(ans) print(*aa)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are n glasses on the table numbered 1, …, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water. You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor. Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled). Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number. For each k = 1, …, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times. Input The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of glasses. The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 ≀ b_i ≀ a_i ≀ 100, a_i > 0) β€” capacity, and water amount currently contained for the glass i, respectively. Output Print n real numbers β€” the largest amount of water that can be collected in 1, …, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer. Example Input 3 6 5 6 5 10 2 Output 7.0000000000 11.0000000000 12.0000000000 Note In the sample case, you can act as follows: * for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units; * for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units; * for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
n = int(input()) k = [] dics = [] for _ in range(n): a, b = map(int, input().split()) k.append([a, b]) dics.append({}) sums = 0 for i in range(n): sums += k[i][1] for i in range(n): r1 = k[i][0] r2 = k[i][1] for j in range(n - 1, -1, -1): for t in dics[j]: if t + r1 in dics[j + 1]: dics[j + 1][t + r1] = max(dics[j + 1][t + r1], dics[j][t] + r2) else: dics[j + 1][t + r1] = dics[j][t] + r2 if r1 in dics[0]: dics[0][r1] = max(dics[0][r1], r2) else: dics[0][r1] = r2 ans = [] for i in range(n): tmp = 0 for j in dics[i]: tmp = max(min((sums + dics[i][j]) / 2, j), tmp) ans.append(str(tmp)) print(" ".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR 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 BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are n glasses on the table numbered 1, …, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water. You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor. Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled). Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number. For each k = 1, …, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times. Input The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of glasses. The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 ≀ b_i ≀ a_i ≀ 100, a_i > 0) β€” capacity, and water amount currently contained for the glass i, respectively. Output Print n real numbers β€” the largest amount of water that can be collected in 1, …, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer. Example Input 3 6 5 6 5 10 2 Output 7.0000000000 11.0000000000 12.0000000000 Note In the sample case, you can act as follows: * for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units; * for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units; * for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
import sys def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) input = sys.stdin.buffer.readline n = int(input()) a = [] b = [] for _ in range(n): aa, bb = [int(x) for x in input().split()] a.append(aa) b.append(bb) total = sum(b) MAXCAP = sum(a) dp = [[(-float("inf")) for _ in range(MAXCAP + 1)] for __ in range(n + 1)] for glass in range(n): dp[0][0] = 0 for glass in range(n): for nGlassesTaken in range(glass + 1, 0, -1): for cap in range(MAXCAP, 0, -1): if cap - a[glass] >= 0: takeGlass = dp[nGlassesTaken - 1][cap - a[glass]] + b[glass] dp[nGlassesTaken][cap] = max(dp[nGlassesTaken][cap], takeGlass) ans = [(0) for _ in range(n + 1)] for nGlassesTaken in range(1, n + 1): for cap in range(MAXCAP + 1): amount = dp[nGlassesTaken][cap] if amount != -float("inf"): amount2 = (total - amount) / 2.0 amount3 = min(cap, amount + amount2) ans[nGlassesTaken] = max(ans[nGlassesTaken], amount3) oneLineArrayPrint(ans[1:])
IMPORT FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN 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 BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
You are given an r rows and c cols matrix grid representing a field of chocolates where grid[i][j] represents the number of chocolates that you can collect from the (i, j) cell. You have two robots that can collect chocolates for you: Robot #1 is located at the top-left corner (0, 0), and Robot #2 is located at the top-right corner (0, cols - 1). Return the maximum number of chocolates collection using both robots by following the rules below: From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1). When any robot passes through a cell, It picks up all chocolates, and the cell becomes an empty cell. When both robots stay in the same cell, only one takes the chocolates. Both robots cannot move outside of the grid at any moment. Both robots should reach the bottom row in grid. Example: Input: r = 3, c = 4 grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] Output: 24 Explanation: Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24. Your Task: You don't need to read input or print anything. Your task is to complete the function Solve() which takes r rows, c columns, and a matrix grid and returns the maximum number of chocolates that can be collected by two robots. Expected Time Complexity: O(r * c * c) Expected Space Complexity: O(c * c * c) Constraint: 2 <= r < = 70 0 <= grid[i][j] <= 100
class Solution: def solve(self, n, m, grid): dictio = {(1, -1), (1, 0), (1, 1)} m = len(grid) n = len(grid[0]) dp = {} def f(i, j, r, c): if (i, j, r, c) in dp: return dp[i, j, r, c] if i == m - 1: if j == c: return grid[-1][j] else: return grid[-1][j] + grid[-1][c] if j == c: cur = grid[i][j] else: cur = grid[i][j] + grid[r][c] maxi = 0 for di, dj in dictio: n_i, n_j = i + di, j + dj if n_i >= 0 and n_j >= 0 and n_i < m and n_j < n: for di, dj in dictio: nn_i, nn_j = r + di, c + dj if nn_i >= 0 and nn_j >= 0 and nn_i < m and nn_j < n: maxi = max(maxi, cur + f(n_i, n_j, nn_i, nn_j)) dp[i, j, r, c] = maxi return maxi return f(0, 0, 0, n - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR NUMBER VAR RETURN BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER
You are given an r rows and c cols matrix grid representing a field of chocolates where grid[i][j] represents the number of chocolates that you can collect from the (i, j) cell. You have two robots that can collect chocolates for you: Robot #1 is located at the top-left corner (0, 0), and Robot #2 is located at the top-right corner (0, cols - 1). Return the maximum number of chocolates collection using both robots by following the rules below: From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1). When any robot passes through a cell, It picks up all chocolates, and the cell becomes an empty cell. When both robots stay in the same cell, only one takes the chocolates. Both robots cannot move outside of the grid at any moment. Both robots should reach the bottom row in grid. Example: Input: r = 3, c = 4 grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] Output: 24 Explanation: Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24. Your Task: You don't need to read input or print anything. Your task is to complete the function Solve() which takes r rows, c columns, and a matrix grid and returns the maximum number of chocolates that can be collected by two robots. Expected Time Complexity: O(r * c * c) Expected Space Complexity: O(c * c * c) Constraint: 2 <= r < = 70 0 <= grid[i][j] <= 100
class Solution: def solve(self, n, m, grid): dp = [[[(-1) for _ in range(m)] for _ in range(m)] for _ in range(n)] def helper(i, j1, j2): if j1 < 0 or j1 >= m or j2 < 0 or j2 >= m: return -1000000000.0 if i == n - 1: if j1 == j2: return grid[i][j1] return grid[i][j1] + grid[i][j2] if dp[i][j1][j2] != -1: return dp[i][j1][j2] maxi = 0 for dj1 in range(-1, 2): for dj2 in range(-1, 2): if j1 == j2: value = grid[i][j1] else: value = grid[i][j1] + grid[i][j2] value += helper(i + 1, j1 + dj1, j2 + dj2) maxi = max(maxi, value) dp[i][j1][j2] = maxi return maxi return helper(0, 0, m - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER
You are given an r rows and c cols matrix grid representing a field of chocolates where grid[i][j] represents the number of chocolates that you can collect from the (i, j) cell. You have two robots that can collect chocolates for you: Robot #1 is located at the top-left corner (0, 0), and Robot #2 is located at the top-right corner (0, cols - 1). Return the maximum number of chocolates collection using both robots by following the rules below: From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1). When any robot passes through a cell, It picks up all chocolates, and the cell becomes an empty cell. When both robots stay in the same cell, only one takes the chocolates. Both robots cannot move outside of the grid at any moment. Both robots should reach the bottom row in grid. Example: Input: r = 3, c = 4 grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] Output: 24 Explanation: Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24. Your Task: You don't need to read input or print anything. Your task is to complete the function Solve() which takes r rows, c columns, and a matrix grid and returns the maximum number of chocolates that can be collected by two robots. Expected Time Complexity: O(r * c * c) Expected Space Complexity: O(c * c * c) Constraint: 2 <= r < = 70 0 <= grid[i][j] <= 100
class Solution: def solve(self, n, m, grid): dp = {} def recursion(row, c1, c2): if c1 < 0 or c1 >= m or c2 < 0 or c2 >= m: return -(10**9) if row == n - 1: if c1 == c2: return grid[row][c1] return grid[row][c1] + grid[row][c2] if (row, c1, c2) in dp: return dp[row, c1, c2] maxi = float("-inf") for dir1 in range(-1, 2): currentAns = float("-inf") for dir2 in range(-1, 2): if c1 == c2: currentAns = max( currentAns, grid[row][c1] + recursion(row + 1, c1 + dir1, c2 + dir2), ) else: currentAns = max( currentAns, grid[row][c2] + grid[row][c1] + recursion(row + 1, c1 + dir1, c2 + dir2), ) maxi = max(maxi, currentAns) dp[row, c1, c2] = maxi return maxi return recursion(0, 0, m - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN BIN_OP NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER
You are given an r rows and c cols matrix grid representing a field of chocolates where grid[i][j] represents the number of chocolates that you can collect from the (i, j) cell. You have two robots that can collect chocolates for you: Robot #1 is located at the top-left corner (0, 0), and Robot #2 is located at the top-right corner (0, cols - 1). Return the maximum number of chocolates collection using both robots by following the rules below: From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1). When any robot passes through a cell, It picks up all chocolates, and the cell becomes an empty cell. When both robots stay in the same cell, only one takes the chocolates. Both robots cannot move outside of the grid at any moment. Both robots should reach the bottom row in grid. Example: Input: r = 3, c = 4 grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] Output: 24 Explanation: Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24. Your Task: You don't need to read input or print anything. Your task is to complete the function Solve() which takes r rows, c columns, and a matrix grid and returns the maximum number of chocolates that can be collected by two robots. Expected Time Complexity: O(r * c * c) Expected Space Complexity: O(c * c * c) Constraint: 2 <= r < = 70 0 <= grid[i][j] <= 100
class Solution: def solve(self, n, m, grid): memo = {} def dp(i, j1, j2): if j1 >= m or j1 < 0 or j2 >= m or j2 < 0: return 0 k = i, j1, j2 if k in memo: return memo[k] ans = grid[i][j1] if j2 != j1: ans += grid[i][j2] if i == n - 1: return ans bestNext = 0 for dj1 in range(-1, 2): for dj2 in range(-1, 2): bestNext = max(bestNext, dp(i + 1, j1 + dj1, j2 + dj2)) ans += bestNext memo[k] = ans return ans return dp(0, 0, m - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER
You are given an r rows and c cols matrix grid representing a field of chocolates where grid[i][j] represents the number of chocolates that you can collect from the (i, j) cell. You have two robots that can collect chocolates for you: Robot #1 is located at the top-left corner (0, 0), and Robot #2 is located at the top-right corner (0, cols - 1). Return the maximum number of chocolates collection using both robots by following the rules below: From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1). When any robot passes through a cell, It picks up all chocolates, and the cell becomes an empty cell. When both robots stay in the same cell, only one takes the chocolates. Both robots cannot move outside of the grid at any moment. Both robots should reach the bottom row in grid. Example: Input: r = 3, c = 4 grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] Output: 24 Explanation: Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24. Your Task: You don't need to read input or print anything. Your task is to complete the function Solve() which takes r rows, c columns, and a matrix grid and returns the maximum number of chocolates that can be collected by two robots. Expected Time Complexity: O(r * c * c) Expected Space Complexity: O(c * c * c) Constraint: 2 <= r < = 70 0 <= grid[i][j] <= 100
class Solution: def solve(self, n, m, grid): dp = [[[(-1) for j in range(m)] for i in range(m)] for k in range(n)] n = len(grid) m = len(grid[0]) return self.func(0, 0, m - 1, n, m, grid, dp) def func(self, i, j1, j2, n, m, grid, dp): if j1 < 0 or j1 >= m or j2 < 0 or j2 >= m: return float("-inf") if i == n - 1: if j1 == j2: return grid[i][j1] else: return grid[i][j1] + grid[i][j2] if dp[i][j1][j2] != -1: return dp[i][j1][j2] maxi = 0 for d1 in range(-1, 2): for d2 in range(-1, 2): rb = 0 if j1 == j2: rb = grid[i][j1] + self.func( i + 1, j1 + d1, j2 + d2, n, m, grid, dp ) else: rb = ( grid[i][j1] + grid[i][j2] + self.func(i + 1, j1 + d1, j2 + d2, n, m, grid, dp) ) maxi = max(maxi, rb) dp[i][j1][j2] = maxi return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR
You are given an r rows and c cols matrix grid representing a field of chocolates where grid[i][j] represents the number of chocolates that you can collect from the (i, j) cell. You have two robots that can collect chocolates for you: Robot #1 is located at the top-left corner (0, 0), and Robot #2 is located at the top-right corner (0, cols - 1). Return the maximum number of chocolates collection using both robots by following the rules below: From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1). When any robot passes through a cell, It picks up all chocolates, and the cell becomes an empty cell. When both robots stay in the same cell, only one takes the chocolates. Both robots cannot move outside of the grid at any moment. Both robots should reach the bottom row in grid. Example: Input: r = 3, c = 4 grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] Output: 24 Explanation: Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24. Your Task: You don't need to read input or print anything. Your task is to complete the function Solve() which takes r rows, c columns, and a matrix grid and returns the maximum number of chocolates that can be collected by two robots. Expected Time Complexity: O(r * c * c) Expected Space Complexity: O(c * c * c) Constraint: 2 <= r < = 70 0 <= grid[i][j] <= 100
class Solution: def dfs(self, ind, j1, j2, grid, N, M, dp): if ind >= N: return 0 if dp[ind][j1][j2] != -1: return dp[ind][j1][j2] ans = 0 for i in [j1 - 1, j1, j1 + 1]: for j in [j2 - 1, j2, j2 + 1]: if i < M and i >= 0 and j < M and j >= 0: if i == j: ans = max( ans, grid[ind][i] + self.dfs(ind + 1, i, j, grid, N, M, dp) ) else: ans = max( ans, grid[ind][i] + grid[ind][j] + self.dfs(ind + 1, i, j, grid, N, M, dp), ) dp[ind][j1][j2] = ans return ans def solve(self, n, m, grid): dp = [[[(-1) for _ in range(m)] for _ in range(m)] for _ in range(n)] if m - 1 == 0: return grid[0][0] + self.dfs(0, 0, m - 1, grid, n, m, dp) return grid[0][0] + grid[0][-1] + self.dfs(1, 0, m - 1, grid, n, m, dp)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR
You are given an r rows and c cols matrix grid representing a field of chocolates where grid[i][j] represents the number of chocolates that you can collect from the (i, j) cell. You have two robots that can collect chocolates for you: Robot #1 is located at the top-left corner (0, 0), and Robot #2 is located at the top-right corner (0, cols - 1). Return the maximum number of chocolates collection using both robots by following the rules below: From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1). When any robot passes through a cell, It picks up all chocolates, and the cell becomes an empty cell. When both robots stay in the same cell, only one takes the chocolates. Both robots cannot move outside of the grid at any moment. Both robots should reach the bottom row in grid. Example: Input: r = 3, c = 4 grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] Output: 24 Explanation: Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24. Your Task: You don't need to read input or print anything. Your task is to complete the function Solve() which takes r rows, c columns, and a matrix grid and returns the maximum number of chocolates that can be collected by two robots. Expected Time Complexity: O(r * c * c) Expected Space Complexity: O(c * c * c) Constraint: 2 <= r < = 70 0 <= grid[i][j] <= 100
class Solution: def helper(self, n, m, grid, dp, i, j1, j2): if i < 0 or j1 < 0 or j2 < 0 or i >= n or j1 >= m or j2 >= m: return float("-inf") if i == n - 1: if j1 == j2: return grid[i][j1] else: return grid[i][j1] + grid[i][j2] if dp[i][j1][j2] != -1: return dp[i][j1][j2] maxi = float("-inf") for r1 in range(-1, 2): for r2 in range(-1, 2): ans = 0 if j1 != j2: ans = ( grid[i][j1] + grid[i][j2] + self.helper(n, m, grid, dp, i + 1, j1 + r1, j2 + r2) ) else: ans = grid[i][j1] + self.helper( n, m, grid, dp, i + 1, j1 + r1, j2 + r2 ) maxi = max(ans, maxi) dp[i][j1][j2] = maxi return maxi def solve(self, n, m, grid): n = len(grid) m = len(grid[0]) dp = [[[(-1) for _ in range(m)] for _ in range(m)] for _ in range(n)] return self.helper(n, m, grid, dp, 0, 0, m - 1)
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER
You are given an r rows and c cols matrix grid representing a field of chocolates where grid[i][j] represents the number of chocolates that you can collect from the (i, j) cell. You have two robots that can collect chocolates for you: Robot #1 is located at the top-left corner (0, 0), and Robot #2 is located at the top-right corner (0, cols - 1). Return the maximum number of chocolates collection using both robots by following the rules below: From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1). When any robot passes through a cell, It picks up all chocolates, and the cell becomes an empty cell. When both robots stay in the same cell, only one takes the chocolates. Both robots cannot move outside of the grid at any moment. Both robots should reach the bottom row in grid. Example: Input: r = 3, c = 4 grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] Output: 24 Explanation: Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24. Your Task: You don't need to read input or print anything. Your task is to complete the function Solve() which takes r rows, c columns, and a matrix grid and returns the maximum number of chocolates that can be collected by two robots. Expected Time Complexity: O(r * c * c) Expected Space Complexity: O(c * c * c) Constraint: 2 <= r < = 70 0 <= grid[i][j] <= 100
class Solution: def solve(self, n, m, A): dp = [([0] * m) for _ in range(m)] dp[0][m - 1] = A[0][0] + A[0][-1] for i in range(1, n): dpn = [([0] * m) for _ in range(m)] for j in range(m): for k in range(m): if j > i or m - k - 1 > i: continue for dj in [-1, 0, 1]: for dk in [-1, 0, 1]: if 0 <= j + dj < m and 0 <= k + dk < m: dpn[j][k] = max(dpn[j][k], dp[j + dj][k + dk]) dpn[j][k] += A[i][j] + A[i][k] - (0 if j != k else A[i][j]) dp = dpn return max(x for r in dp for x in r)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR LIST NUMBER NUMBER NUMBER FOR VAR LIST NUMBER NUMBER NUMBER IF NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): dp = {} n = len(s) def helper(ind, sum): if ind == n: return 1 if (ind, sum) in dp: return dp[ind, sum] curr_sum = 0 ans = 0 for i in range(ind, n): curr_sum += ord(s[i]) - ord("0") if curr_sum >= sum: ans += helper(i + 1, curr_sum) dp[ind, sum] = ans return dp[ind, sum] return helper(0, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): n = len(s) maxl = 100 dp = [[(-1) for i in range(9 * maxl + 1)] for i in range(maxl)] def helper(p, psum): if p == n: return 1 if dp[p][psum] != -1: return dp[p][psum] ans = 0 sumd = 0 dp[p][psum] = 0 for i in range(p, n): sumd += int(s[i]) if sumd >= psum: ans += helper(i + 1, sumd) dp[p][psum] = ans return dp[p][psum] return helper(0, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def dfs(self, s, index, prevSum): if index >= len(s): return 1 if (index, prevSum) in self.memo: return self.memo[index, prevSum] currentSum = ans = 0 for i in range(index, len(s)): currentSum += int(s[i]) if currentSum >= prevSum: ans += self.dfs(s, i + 1, currentSum) self.memo[index, prevSum] = ans return ans def TotalCount(self, s): self.memo = {} return self.dfs(s, 0, -1)
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR NUMBER NUMBER
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
def f(idx, summ, dp, S): N = len(S) if idx == N: return 1 if dp[idx][summ] != -1: return dp[idx][summ] dp[idx][summ] = 0 ans = 0 tempsumm = 0 for i in range(idx, N): tempsumm += int(S[i]) if tempsumm >= summ: ans += f(i + 1, tempsumm, dp, S) dp[idx][summ] = ans return ans class Solution: def TotalCount(self, s): dp = [[(-1) for i in range(901)] for i in range(100)] return f(0, 0, dp, s)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): def task(n, i, ism, s, dp): if i == n: return 1 if dp[i][ism] != -1: return dp[i][ism] cm = 0 ans = 0 for j in range(i, n): cm += int(s[j]) if cm >= ism: ans += task(n, j + 1, cm, s, dp) dp[i][ism] = ans return dp[i][ism] n = len(s) sm = 0 for i in s: sm += int(i) dp = [[(-1) for _ in range(sm + 1)] for _ in range(n + 1)] return task(n, 0, 0, s, dp)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): def is_possible(i, prev, dp): if i == n: return 1 if dp[i][prev] != -1: return dp[i][prev] res = 0 for j in range(i, n): summ = pre_sum[j + 1] - pre_sum[i] if summ >= prev: res += is_possible(j + 1, summ, dp) dp[i][prev] = res return res n = len(s) pre_sum = [0] for i in range(n): pre_sum.append(pre_sum[-1] + int(s[i])) dp = [([-1] * 1000) for _ in range(n)] return is_possible(0, int(s[0]), dp)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): sum2 = 0 n = len(s) for i in s: sum2 += int(i) dp = [[(-1) for i in range(sum2 + 1)] for i in range(n + 1)] return self.group(n, 0, 0, s, dp) def group(self, n, index, sum1, s, dp): if index == n: return 1 if dp[index][sum1] != -1: return dp[index][sum1] curr_sum = 0 ans = 0 for i in range(index, n): curr_sum += int(s[i]) if curr_sum >= sum1: ans += self.group(n, i + 1, curr_sum, s, dp) dp[index][sum1] = ans return dp[index][sum1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): n = len(s) y = 0 for i in range(n): y += int(s[i]) dp = [] for i in range(n + 1): x = [] for j in range(y + 1): x.append(-1) dp.append(x) def solve(i, sm): if i == len(s): return 1 if dp[i][sm] != -1: return dp[i][sm] ans = 0 temp_sm = 0 for k in range(i, n): temp_sm += int(s[k]) if temp_sm >= sm: ans += solve(k + 1, temp_sm) dp[i][sm] = ans return ans return solve(0, 0) if __name__ == "__main__": T = int(input()) for i in range(T): s = input() ob = Solution() ans = ob.TotalCount(s) print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, num): dp = {} def count_parts(pre_sum, num_lis): if not num_lis: return 1 if (num_lis, pre_sum) in dp: return dp[num_lis, pre_sum] cur_sum = 0 count = 0 for i in range(len(num_lis)): cur_sum += num_lis[i] if cur_sum >= pre_sum: count += count_parts(cur_sum, num_lis[i + 1 :]) dp[num_lis, pre_sum] = count return count num_lis = [int(i) for i in str(num)] return count_parts(0, tuple(num_lis))
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def solve(self, s, i, prev, dp): n = len(s) if i == n: return 1 elif dp[i][prev] != -1: return dp[i][prev] cur = res = 0 for j in range(i, n): cur += int(s[j]) if cur >= prev: res += self.solve(s, j + 1, cur, dp) dp[i][prev] = res return dp[i][prev] def TotalCount(self, s): n = len(s) dp = [[(-1) for i in range(9 * n)] for j in range(n)] return self.solve(s, 0, 0, dp)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): dp = [[(-1) for _ in range(9 * len(s) + 1)] for _ in range(len(s))] def rec(i, sm): if i == len(s): return 1 if dp[i][sm] != -1: return dp[i][sm] dp[i][sm] = 0 sum_ = 0 for j in range(i, len(s)): sum_ += int(s[j]) if sum_ >= sm: dp[i][sm] += rec(j + 1, sum_) return dp[i][sm] return rec(0, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): n = len(s) dp = [[(-1) for i in range(9 * n)] for j in range(n)] return self.solve(s, 0, 0, dp) def solve(self, S, index, sum_, dp): if index == len(S): return 1 if dp[index][sum_] != -1: return dp[index][sum_] currsum = 0 ans = 0 for i in range(index, len(S)): currsum += int(S[i]) if currsum >= sum_: ans += self.solve(S, i + 1, currsum, dp) dp[index][sum_] = ans return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def solve(self, dp, s, n, Sum, idx): if idx >= n: return 1 if dp[idx][Sum] != -1: return dp[idx][Sum] currSum = 0 res = 0 for i in range(idx, n): currSum += int(s[i]) if currSum >= Sum: res += self.solve(dp, s, n, currSum, i + 1) dp[idx][Sum] = res return res def TotalCount(self, s): Sum = 0 for i in range(len(s)): Sum += int(s[i]) dp = [[(-1) for _ in range(Sum + 1)] for _ in range(len(s) + 1)] return self.solve(dp, s, len(s), 0, 0)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def sumi(self, s): i = 0 if s != "": for c in s: i += int(c) return i def solve(self, i, j, s, c): if i >= j: return 0 for k in range(i, j): left = self.solve(i, k, s, c) sl = self.sumi(left) right = self.solve(k + 1, j, s, c) sr = self.sumi(right) if sl <= sr: c.append(1) return c def TotalCount(self, s): n = len(s) maxl = 100 dp = [[(-1) for i in range(9 * maxl + 1)] for i in range(maxl)] def helper(p, psum): if p == n: return 1 if dp[p][psum] != -1: return dp[p][psum] ans = 0 sumd = 0 dp[p][psum] = 0 for i in range(p, n): sumd += int(s[i]) if sumd >= psum: ans += helper(i + 1, sumd) dp[p][psum] = ans return dp[p][psum] return helper(0, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
def rec(s, i, presum, dp): if i == len(s): return 1 elif dp[i][presum] != -1: return dp[i][presum] cursum = 0 res = 0 for j in range(i, len(s)): cursum += int(s[j]) if cursum >= presum: res += rec(s, j + 1, cursum, dp) dp[i][presum] = res return dp[i][presum] class Solution: def TotalCount(self, s): dp = [[(-1) for i in range(9 * len(s))] for j in range(len(s))] return rec(s, 0, 0, dp)
FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): arr = [int(ch) for ch in s] n, tot = len(s), sum(arr) dp = [([-1] * (tot + 1)) for _ in range(n)] return self.find_count(arr, 0, len(s), 0, dp) def find_count(self, arr, i, n, prev, dp): if i == n: return 1 elif dp[i][prev] >= 0: return dp[i][prev] cnt = sm = 0 for ind in range(i, n): sm += arr[ind] if sm >= prev: cnt += self.find_count(arr, ind + 1, n, sm, dp) dp[i][prev] = cnt return cnt
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): def helper(i, s): if dp[i][s] == -1: cs = 0 res = 0 for j in range(i, n): cs += l[j] if cs >= s: res += helper(j + 1, cs) dp[i][s] = res return dp[i][s] l = [int(i) for i in s] n = len(s) strsum = sum(l) dp = [[(-1) for j in range(strsum + 1)] for i in range(n + 1)] for i in range(strsum + 1): dp[n][i] = 1 return helper(0, 0)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def countSubgroups(self, n, position, num, previous_sum, dp): if position == n: return 1 if dp[position][previous_sum] != -1: return dp[position][previous_sum] dp[position][previous_sum] = 0 s = 0 res = 0 for i in range(position, n): s += int(num[i]) if s >= previous_sum: res += self.countSubgroups(n, i + 1, num, s, dp) dp[position][previous_sum] = res return res def TotalCount(self, s): n = len(s) dp = [[(-1) for i in range(n * 9)] for j in range(n)] ans = self.countSubgroups(n, 0, s, 0, dp) return ans
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR RETURN VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): dp = {} def countGroup(position, pre_sum, length, s): if position == length: return 1 if (position, pre_sum) in dp: return dp[position, pre_sum] res = 0 sum = 0 for i in range(position, length): sum += int(s[i]) if sum >= pre_sum: res = res + countGroup(i + 1, sum, length, s) dp[position, pre_sum] = res return dp[position, pre_sum] return countGroup(0, 0, len(s), s)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): dp = [([-1] * len(s)) for _ in range(1000)] def rec(i, su): if i >= len(s): return 1 if dp[su][i] != -1: return dp[su][i] sum = 0 c = 0 for j in range(i, len(s)): sum += int(s[j]) if sum >= su: c += rec(j + 1, sum) dp[su][i] = c return c return rec(0, 0) if __name__ == "__main__": T = int(input()) for i in range(T): s = input() ob = Solution() ans = ob.TotalCount(s) print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
[7, -1, -1, -1] [-1, 4, -1, -1] [-1, 2, 1, -1] [-1, 1, 1, 1] class Solution: def TotalCount(self, s): def haha(ind, sumi, s, dp, n): if ind == n: return 1 if dp[ind][sumi] != -1: return dp[ind][sumi] c, ans = 0, 0 for i in range(ind, n): c += int(s[i]) if c >= sumi: ans = ans + haha(i + 1, c, s, dp, n) dp[ind][sumi] = ans return dp[ind][sumi] n = len(s) dp = [([-1] * (n * 9)) for i in range(n)] x = haha(0, 0, s, dp, n) return x
EXPR LIST NUMBER NUMBER NUMBER NUMBER EXPR LIST NUMBER NUMBER NUMBER NUMBER EXPR LIST NUMBER NUMBER NUMBER NUMBER EXPR LIST NUMBER NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR RETURN VAR
Given a string str consisting of digits, one may group these digits into sub-groups while maintaining their original order. The task is to count number of groupings such that for every sub-group except the last one, sum of digits in a sub-group is less than or equal to sum of the digits in the sub-group immediately on its right. For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right. Example 1: Input: str = "1119" Output: 7 Explanation: [1-119], [1-1-19], [1-11-9], [1-1-1-9], [11-19], [111-9] and [1119] are seven sub-groups. Example 2: Input: str = "12" Output: 2 Explanation: [1-2] and [12] are two sub-groups. Your Task: You don't need to read or print anything. Your task is to complete the function TotalCount() which takes the string str as input parameter and returns total possible groupings. Expected Time Complexity: O(N * N ) where N is length of string. Expected Space Complexity: O(N * N) Constraints: 1 <= length of string <= 100
class Solution: def TotalCount(self, s): n = len(s) sums = 0 for i in s: sums += int(i) dp = [[(-1) for _ in range(sums + 1)] for _ in range(n)] def fun(i, pre): nonlocal n if i == n: return 1 if dp[i][pre] != -1: return dp[i][pre] ans = 0 sumi = 0 for j in range(i, n): sumi += int(s[j]) if sumi >= pre: ans += fun(j + 1, sumi) dp[i][pre] = ans return ans return fun(0, 0) n = len(s) summ = 0 for i in s: summ += int(i) dp = [[(-1) for _ in range(summ + 1)] for _ in range(n + 1)] return self.group(n, 0, 0, s, dp) def group(self, n, idx, initialsum, s, dp): if idx == n: return 1 if dp[idx][initialsum] != -1: return dp[idx][initialsum] cursum = 0 ans = 0 for i in range(idx, n): cursum += int(s[i]) if cursum >= initialsum: ans += self.group(n, i + 1, cursum, s, dp) dp[idx][initialsum] = ans return dp[idx][initialsum]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
w, b = map(int, input().split()) dp = [[([1, 0] if i and not j else [0, 0]) for j in range(b + 2)] for i in range(w + 2)] for i in range(1, w + 1): for j in range(1, b + 1): dp[i][j][0] = (i + j * dp[i][j - 1][1]) / (i + j) dp[i][j][1] = ( j * ((j - 1) * dp[i][j - 2][0] + i * dp[i - 1][j - 1][0]) / (i + j) / (i + j - 1) ) print(dp[w][b][0])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER LIST NUMBER 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 NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
def f(W, B): n = B - W + 1 d, p, t, r = [0] * B, [0] * B, [0] * B, [0] * B p[0], r[0], p[1] = 1, 1, 0.5 for b in range(2, n + 1): d[b] = (t[b - 1] + p[b - 2] * (b - 1)) / (1 + b) p[b] = (1 + d[b - 1] * b) / (1 + b) r[0], t[0] = 1, 1 t, r, p = p, t, r for w in range(2, W): p[1], d[1] = w / (w + 1), 1 / (w + 1) for b in range(2, n + w): d[b] = (t[b - 1] * w + p[b - 2] * (b - 1)) * b / ((w + b) * (w + b - 1)) p[b] = (w + d[b - 1] * b) / (w + b) t, r, p = p, t, r return t[B - 1] w, b = map(int, input().split()) if w == 0: print(0) elif b == 0: print(1) else: print(f(w + 1, b + 1))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER 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 BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
w, b = [int(i) for i in input().split()] a = [[(0) for i in range(w + 1)] for j in range(b + 1)] def cnt(w, b): if w <= 0: return 0 if b <= 0: return 1 if a[b][w] != 0: return a[b][w] c = w / (w + b) if w + b > 2: wjump = cnt(w - 1, b - 2) bjump = cnt(w, b - 3) c += b / (w + b) * ((b - 1) / (w + b - 1)) * ((b - 2) / (w + b - 2)) * bjump c += b / (w + b) * ((b - 1) / (w + b - 1)) * (w / (w + b - 2)) * wjump a[b][w] = c return c print(cnt(w, b))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
import sys sys.setrecursionlimit(10**6) w, b = map(int, input().split()) w += 1 b += 1 pr_win = [[None for k in range(b)] for k in range(w)] dr_win = [[None for k in range(b)] for k in range(w)] def f(w0, b0, princess): if w0 < 0 or b0 < 0: return 0 if w0 == 0 and b0 == 0: return 0 if b0 == 0: return 1 if princess else 0 if w0 == 0: return 0 if princess: if pr_win[w0][b0] is None: p_w = w0 / (w0 + b0) p_b = b0 / (w0 + b0) * f(w0, b0 - 1, not princess) pr_win[w0][b0] = p_w + p_b return pr_win[w0][b0] else: if dr_win[w0][b0] is None: p_bb = ( b0 / (w0 + b0) * (b0 - 1) / (w0 + b0 - 1) * f(w0, b0 - 2, not princess) ) p_bw = b0 / (w0 + b0) * w0 / (w0 + b0 - 1) * f(w0 - 1, b0 - 1, not princess) dr_win[w0][b0] = p_bb + p_bw return dr_win[w0][b0] print("%.10f" % f(w - 1, b - 1, True))
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR IF VAR VAR VAR NONE ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR NONE ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
dp = {} def prob(w, b): if (w, b) in dp: return dp[w, b] if w == 0 and b == 0: return 0 if b == 0: return 1 if w == 0: return 0 if b == 1: ans = w / (w + b) dp[w, b] = ans return ans if b == 2: ans = w / (w + 2) + 2 / (w + 2) * 1 / (w + 1) * prob(w - 1, b - 2) dp[w, b] = ans return ans sum = w + b if sum == 1 or sum == 2: print(w, b) ans = w / sum + b / sum * (b - 1) / (sum - 1) * ( w / (sum - 2) * prob(w - 1, b - 2) + (b - 2) / (sum - 2) * prob(w, b - 3) ) dp[w, b] = ans return ans w, b = tuple(map(int, input().split())) print(prob(w, b))
ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
w, b = map(float, input().split()) dic = {} def fun(w, b): if dic.get((w, b)): return dic[w, b] if w > 0 and b > 1: temp = b / (b + w) * (b - 1) / (w + b - 1) res = w / (w + b) + temp * ( w / (w + b - 2) * fun(w - 1, b - 2) + (b - 2) / (w + b - 2) * fun(w, b - 3) ) dic[w, b] = res return res elif w > 0 and b == 1: return w / (w + b) elif w > 0 and b == 0: return 1.0 else: return 0.0 pro = fun(w, b) print(pro)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
w, b = map(int, input().split(" ")) if w == 0: print(0) elif b == 0: print(1) else: solved = [([-1] * b) for i in range(w)] def solve(w, b): if solved[w - 1][b - 1] != -1: return solved[w - 1][b - 1] if w == 0: ans = 0 solved[w - 1][b - 1] = ans return ans if b == 0: ans = 1 solved[w - 1][b - 1] = ans return ans if b == 1: ans = w / (w + 1) solved[w - 1][b - 1] = ans return ans if b == 2: if w == 1: ans = 1 / 3 solved[w - 1][b - 1] = ans return ans else: ans = w / (w + 2) + 2 / (w + 2) * 1 / (w + 1) solved[w - 1][b - 1] = ans return ans x = solve(w - 1, b - 2) y = solve(w, b - 3) ans = w / (w + b) + b / (w + b) * (b - 1) / (w + b - 1) * ( w / (w + b - 2) * x + (b - 2) / (w + b - 2) * y ) solved[w - 1][b - 1] = ans return ans print(solve(w, b))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
MAX = 1002 w, b = map(int, input().split()) ans = 0 dp = [[(0) for _ in range(MAX)] for _ in range(MAX)] end = [[(0) for _ in range(MAX)] for _ in range(MAX)] dp[w][b] = 1 for i in range(w, -1, -1): end[i][b + 1] = 1 if w > 0: ans += w / (w + b) * dp[w][b] end[w - 1][b] = 1 if b > 0: for j in range(b - 1, -1, -1): dp[w][j] = (j + 1) / (w + j + 1) * dp[w][j + 1] for i in range(w - 1, -1, -1): for j in range(b - 1, -1, -1): if end[i + 1][j] and end[i][j + 1]: end[i][j] = 1 else: if not end[i + 1][j]: if (w + b - i - j) % 3 == 1: ans += (i + 1) / (i + j + 1) * dp[i + 1][j] if end[i][j + 1]: end[i][j] = 1 elif (w + b - i - j) % 3 == 2: if end[i][j + 1]: end[i][j] = 1 else: dp[i][j] += (i + 1) / (i + j + 1) * dp[i + 1][j] if not end[i][j + 1]: dp[i][j] += (j + 1) / (i + j + 1) * dp[i][j + 1] print(ans)
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
w, b = list(map(int, input().split())) p = [] for i in range(w + 1): p.append([0] * (b + 1)) for i in range(1, w + 1): p[i][0] = 1 for i in range(1, w + 1): for j in range(1, b + 1): p[i][j] = i / (i + j) if j >= 3: p[i][j] += ( j / (i + j) * ((j - 1) / (i + j - 1)) * ((j - 2) / (i + j - 2)) * p[i][j - 3] ) if j >= 2: p[i][j] += ( j / (i + j) * ((j - 1) / (i + j - 1)) * (i / (i + j - 2)) * p[i - 1][j - 2] ) print("%.9f" % p[w][b])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP 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 FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
import sys input = sys.stdin.readline memo = {} def prob(w, b): if w == 0 or b < 0: return 0 if b == 0: return 1 if (w, b) in memo: return memo[w, b] ans1 = w / (w + b) if b == 1: ans2 = 0 else: ans2 = ( b / (w + b) * ((b - 1) / (w + b - 1)) * ( prob(w - 1, b - 2) * (w / (b + w - 2)) + prob(w, b - 3) * ((b - 2) / (b + w - 2)) ) ) memo[w, b] = ans1 + ans2 return ans1 + ans2 w, b = map(int, input().split()) print(prob(w, b))
IMPORT ASSIGN VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
w, b = map(int, input().split()) dp = [([0] * (b + 1)) for _ in range(w + 1)] for i in range(1, w + 1): dp[i][0] = 1 for i in range(1, w + 1): for j in range(1, b + 1): dp[i][j] += i / (i + j) if j >= 3: dp[i][j] += ( dp[i][j - 3] * j / (i + j) * (j - 1) / (i + j - 1) * (j - 2) / (i + j - 2) ) if i >= 1 and j >= 2: dp[i][j] += ( dp[i - 1][j - 2] * j / (i + j) * (j - 1) / (i + j - 1) * i / (i + j - 2) ) print(dp[-1][-1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
w, b = [int(i) for i in input().split()] dp = [[(-1) for i in range(b + 1)] for j in range(w + 1)] def f(w, b): if w == 0 and b == 0: return 0 if b <= 0: return 1 if w == 0: return 0 if dp[w][b] != -1: return dp[w][b] ans = w / (w + b) if w >= 1 and b >= 2: ans += ( b / (w + b) * ((b - 1) / (w + b - 1)) * (w / (w + b - 2) * f(w - 1, b - 2)) ) if b >= 3: ans += ( b / (w + b) * ((b - 1) / (w + b - 1)) * ((b - 2) / (w + b - 2)) * f(w, b - 3) ) dp[w][b] = ans return ans print(f(w, b))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
w, b = map(int, input().split()) k, p, q, s = 0, 0, 1, w + b while q != 0 and k < s: d = q * w / s p += d q -= d s -= 1 d = q * w / s q -= d s -= 1 k += 1 print(p)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
w, b = map(int, input().split()) if w == 0: print(0) elif b == 0: print(1) else: ans = 0 dp = [[(0.0) for i in range(b + 1)] for j in range(w + 1)] dp[w][b - 1] = b / (w + b) ans += w / (w + b) i = w while i >= 0: j = b - 1 while j >= 0: if j + 3 <= b: dp[i][j] = dp[i][j] + (j + 1) * (j + 2) * (j + 3) * dp[i][j + 3] / ( (i + j + 1) * (i + j + 2) * (i + j + 3) ) if j + 2 <= b and i + 1 <= w: dp[i][j] = dp[i][j] + (j + 1) * (i + 1) * (j + 2) * dp[i + 1][j + 2] / ( (i + j + 1) * (i + j + 2) * (i + j + 3) ) ans += ( (i + 1) * (j + 1) * (j + 2) * dp[i + 1][j + 2] / ((i + j + 1) * (i + j + 2) * (i + j + 3)) ) if i + 2 <= w and j + 1 <= b: ans += ( (i + 1) * (i + 2) * (j + 1) * dp[i + 2][j + 1] / ((i + j + 1) * (i + j + 2) * (i + j + 3)) ) j += -1 i += -1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
save = {} def fun(w, b): global save key = str(w) + " " + str(b) if save.get(key) != None: return save[key] prob = 0 if w >= 0 and b >= 0 and w + b != 0: prob = w / (w + b) if w + b > 2: prob += ( fun(w - 1, b - 2) * (b / (w + b)) * ((b - 1) / (w + b - 1)) * (w / (w + b - 2)) ) prob += ( fun(w, b - 3) * (b / (w + b)) * ((b - 1) / (w + b - 1)) * ((b - 2) / (w + b - 2)) ) save[key] = prob return prob scr = input() mice_w, mice_b = map(int, scr.split()) prob = fun(mice_w, mice_b) print(prob)
ASSIGN VAR DICT FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NONE RETURN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≀ w, b ≀ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β€” one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
w, b = map(int, input().split()) cached = [([None] * (b + 1)) for i in range(w + 1)] def cal(w, b): if cached[w][b] is not None: return cached[w][b] if w == 0: return 0 if b == 0: return 1 total = w + b p = w / total bp = 1 - p dbp = (b - 1) / (total - 1) if b == 1: return p if b == 2: if w == 1: return p else: return p + bp * dbp cached[w][b] = p + bp * dbp * ( w / (total - 2) * cal(w - 1, b - 2) + (b - 2) / (total - 2) * cal(w, b - 3) ) return cached[w][b] print(cal(w, b))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER IF VAR NUMBER RETURN VAR RETURN BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Knight is at (start_x,start_y) in Geekland which is represented by an NxM 2D matrix. Each cell in the matrix contains some points. In the ith step, the knight can collect all the points from all the cells that can be visited in exactly i steps without revisiting any cell. Also, the knight has some magical powers that enable him to fetch coins from the future i.e. If the knight can collect y coins in the xth step he can fetch all the coins that he will collect in the (x + y)th step and if the knight can collect z coins in the yth step he can fetch all the coins that he will collect in the (y + z)th step and so on without increasing the step count i.e. knight will stay on xth step and will get all the coins of the future steps mentioned above((x + y)th step coins + (y+z)th steps + ...). Find the minimum number of steps required to collect the maximum points. Note: The knight moves exactly the same as the knight on a chess board. Please follow 0 indexing. Example 1: Input: n = 9 m = 10 start_x = 4, start_y = 5 arr = 0 0 0 2 0 2 0 2 0 0 0 0 2 0 2 0 2 0 2 0 0 2 0 0 1 2 0 0 0 2 0 0 2 0 2 0 2 0 2 0 0 2 0 2 0 0 0 2 0 2 0 0 2 0 2 0 2 0 2 0 0 2 0 0 0 2 0 0 0 2 0 0 2 0 2 0 2 0 2 0 0 0 0 2 0 2 0 2 0 0 Output: 1 Explanation: minimum knight have to take 1 steps to gain maximum points. Initially, the knight has 0 coins, he will take 1 step to collect 1 point (sum of cells denoted in red color). Now in the second step, he can collect points from all the cells colored green i.e. 64 points. But with his magical power, at the 1st step, he can fetch points from the (1 + 1)th step. Therefore he can collect 1 + 64 coins at step 1 only. Hence answer is 1. Example 2: Input: n = 3 m = 3 start_x = 2, start_y = 1 arr = 7 6 8 9 1 4 6 2 8 Output:0 Explanation: Initially, the knight has 2 points, or more formally we can say that at the 0th step knight has 2 points. In the first step, he can collect points from cells (0, 0) and (0, 2) i.e. 15 points. In the second step, he can collect points from cells (1, 0) and (1, 2) i.e. 13 coins. In the third step, he can collect points from cells (2, 0) and (2, 2) i.e. 14 points. In the fourth step, he can collect points from the cell (0, 1) i.e. 6 points. So in each step, he can collect coins like -You can see in the below image Knight can collect 15 coins in the 0th step only Your Task: You don't need to read input or print anything. Your task is to complete the function knightInGeekland() which takes 2-d array arr[][], starting coordinates of knight start_x, and start_y as input, and return an integer value as min steps to gain max points. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 <= len(arr), len(arr[0]) < 10^{3} 0 <= values in arr <=100
class Solution: def knightInGeekland(self, arr, start): x = [-2, -2, 2, 2, -1, 1, -1, 1] y = [-1, 1, -1, 1, -2, -2, 2, 2] n = len(arr) m = len(arr[0]) visit = [[(False) for _ in range(m)] for _ in range(n)] data = [] start = [start] visit[start[0][0]][start[0][1]] = True while start: count = 0 for s in range(len(start)): count += arr[start[0][0]][start[0][1]] prev_x, prev_y = start[0] del start[0] for i, j in zip(x, y): next_i = prev_x + i next_j = prev_y + j if ( 0 <= next_i < n and 0 <= next_j < m and visit[next_i][next_j] != True ): start.append([next_i, next_j]) visit[next_i][next_j] = True data.append(count) for i in range(len(data) - 1, -1, -1): try: data[i] += data[data[i] + i] except: pass return data.index(max(data))
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Knight is at (start_x,start_y) in Geekland which is represented by an NxM 2D matrix. Each cell in the matrix contains some points. In the ith step, the knight can collect all the points from all the cells that can be visited in exactly i steps without revisiting any cell. Also, the knight has some magical powers that enable him to fetch coins from the future i.e. If the knight can collect y coins in the xth step he can fetch all the coins that he will collect in the (x + y)th step and if the knight can collect z coins in the yth step he can fetch all the coins that he will collect in the (y + z)th step and so on without increasing the step count i.e. knight will stay on xth step and will get all the coins of the future steps mentioned above((x + y)th step coins + (y+z)th steps + ...). Find the minimum number of steps required to collect the maximum points. Note: The knight moves exactly the same as the knight on a chess board. Please follow 0 indexing. Example 1: Input: n = 9 m = 10 start_x = 4, start_y = 5 arr = 0 0 0 2 0 2 0 2 0 0 0 0 2 0 2 0 2 0 2 0 0 2 0 0 1 2 0 0 0 2 0 0 2 0 2 0 2 0 2 0 0 2 0 2 0 0 0 2 0 2 0 0 2 0 2 0 2 0 2 0 0 2 0 0 0 2 0 0 0 2 0 0 2 0 2 0 2 0 2 0 0 0 0 2 0 2 0 2 0 0 Output: 1 Explanation: minimum knight have to take 1 steps to gain maximum points. Initially, the knight has 0 coins, he will take 1 step to collect 1 point (sum of cells denoted in red color). Now in the second step, he can collect points from all the cells colored green i.e. 64 points. But with his magical power, at the 1st step, he can fetch points from the (1 + 1)th step. Therefore he can collect 1 + 64 coins at step 1 only. Hence answer is 1. Example 2: Input: n = 3 m = 3 start_x = 2, start_y = 1 arr = 7 6 8 9 1 4 6 2 8 Output:0 Explanation: Initially, the knight has 2 points, or more formally we can say that at the 0th step knight has 2 points. In the first step, he can collect points from cells (0, 0) and (0, 2) i.e. 15 points. In the second step, he can collect points from cells (1, 0) and (1, 2) i.e. 13 coins. In the third step, he can collect points from cells (2, 0) and (2, 2) i.e. 14 points. In the fourth step, he can collect points from the cell (0, 1) i.e. 6 points. So in each step, he can collect coins like -You can see in the below image Knight can collect 15 coins in the 0th step only Your Task: You don't need to read input or print anything. Your task is to complete the function knightInGeekland() which takes 2-d array arr[][], starting coordinates of knight start_x, and start_y as input, and return an integer value as min steps to gain max points. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 <= len(arr), len(arr[0]) < 10^{3} 0 <= values in arr <=100
class Solution: def knightInGeekland(self, arr, start): m, n = len(arr), len(arr[0]) q = [(start[0], start[1])] coins = [] visited = {(start[0], start[1])} ds = [(r, c) for r in (-2, 2) for c in (-1, 1)] + [ (r, c) for r in (-1, 1) for c in (-2, 2) ] while q: s = 0 nq = [] for r0, c0 in q: s += arr[r0][c0] for dr, dc in ds: r, c = r0 + dr, c0 + dc if (r, c) not in visited and 0 <= r < m and 0 <= c < n: nq.append((r, c)) visited.add((r, c)) coins.append(s) q = nq cnt = 0 ans = 0 for i in range(len(coins) - 1, -1, -1): if coins[i] + i < len(coins): coins[i] += coins[coins[i] + i] if coins[i] >= cnt: cnt = coins[i] ans = i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR