description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32. Example 1: Input: n = 8, m = 2 Output: Yes Explanation: Possible in base 3 as 8 in base 3 is 22. Example 2: Input: n = 8, m = 3 Output: No Explanation: Not possible in any base. Your Task: You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes.. Expected Time Complexity: O(logN). Expected Auxiliary Space: O(1) Constraints: 1 <= n <=10^{9} 1 <= m <=32
class Solution: def baseEquiv(self, n, m): def chkpos(num, digit, base): if digit == 1 and num < base: return True if digit > 1 and num >= base: return chkpos(int(num / base), digit - 1, base) return False for i in range(2, 33): if chkpos(n, m, i): return "Yes" return "No"
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN STRING RETURN STRING
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32. Example 1: Input: n = 8, m = 2 Output: Yes Explanation: Possible in base 3 as 8 in base 3 is 22. Example 2: Input: n = 8, m = 3 Output: No Explanation: Not possible in any base. Your Task: You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes.. Expected Time Complexity: O(logN). Expected Auxiliary Space: O(1) Constraints: 1 <= n <=10^{9} 1 <= m <=32
class Solution: def baseEquiv(self, n, m): def getRep(num, base): count = 0 while num: num //= base count += 1 return count l = 2 r = 32 while l <= r: b = (l + r) // 2 rep = getRep(n, b) if rep == m: return "Yes" elif rep < m: r = b - 1 else: l = b + 1 return "No"
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN STRING IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN STRING
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32. Example 1: Input: n = 8, m = 2 Output: Yes Explanation: Possible in base 3 as 8 in base 3 is 22. Example 2: Input: n = 8, m = 3 Output: No Explanation: Not possible in any base. Your Task: You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes.. Expected Time Complexity: O(logN). Expected Auxiliary Space: O(1) Constraints: 1 <= n <=10^{9} 1 <= m <=32
class Solution: def baseEquiv(self, n, m): if n <= 32 and m == 1: return "Yes" for i in range(32): cnt = 0 x = 1 while n > x: x *= i cnt += 1 if cnt > m: break if cnt == m: return "Yes" return "No"
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR RETURN STRING RETURN STRING
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32. Example 1: Input: n = 8, m = 2 Output: Yes Explanation: Possible in base 3 as 8 in base 3 is 22. Example 2: Input: n = 8, m = 3 Output: No Explanation: Not possible in any base. Your Task: You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes.. Expected Time Complexity: O(logN). Expected Auxiliary Space: O(1) Constraints: 1 <= n <=10^{9} 1 <= m <=32
class Solution: def baseEquiv(self, n, m): l = [] if m == 1: for i in range(2, 33): l.append(i - 1) else: for i in range(2, 33): l.append(i ** (m - 1)) ans = -1 low = 0 high = len(l) for i in range(len(l)): if l[i] > n: ans = i - 1 break if ans == -1: ans = len(l) - 1 f = l[ans] ans = ans + 2 h = ans**m if n >= f and n < h: return "Yes" else: return "No"
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN STRING RETURN STRING
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32. Example 1: Input: n = 8, m = 2 Output: Yes Explanation: Possible in base 3 as 8 in base 3 is 22. Example 2: Input: n = 8, m = 3 Output: No Explanation: Not possible in any base. Your Task: You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes.. Expected Time Complexity: O(logN). Expected Auxiliary Space: O(1) Constraints: 1 <= n <=10^{9} 1 <= m <=32
class Solution: def solve(self, num, base): if num == 0: return 0 d = [] while num: d.append(num % base) num = num // base return len(d) def baseEquiv(self, n, m): for i in range(2, 33): a = self.solve(n, i) if a == m: return "Yes" return "No"
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN STRING RETURN STRING
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32. Example 1: Input: n = 8, m = 2 Output: Yes Explanation: Possible in base 3 as 8 in base 3 is 22. Example 2: Input: n = 8, m = 3 Output: No Explanation: Not possible in any base. Your Task: You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes.. Expected Time Complexity: O(logN). Expected Auxiliary Space: O(1) Constraints: 1 <= n <=10^{9} 1 <= m <=32
class Solution: def baseEquiv(self, n, m): for i in range(2, 32 + 1): temp = n count = 0 while temp != 0: count += 1 temp //= i if count == m: return "Yes" return "No"
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR RETURN STRING RETURN STRING
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32. Example 1: Input: n = 8, m = 2 Output: Yes Explanation: Possible in base 3 as 8 in base 3 is 22. Example 2: Input: n = 8, m = 3 Output: No Explanation: Not possible in any base. Your Task: You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes.. Expected Time Complexity: O(logN). Expected Auxiliary Space: O(1) Constraints: 1 <= n <=10^{9} 1 <= m <=32
class Solution: def baseEquiv(self, n, m): def check(n, i): s = [] while n >= i: s.append(n % i) n = n // i s.append(n) return len(s) if m == 1: if n <= 32: return "Yes" else: return "No" for i in range(2, 33): if check(n, i) == m: return "Yes" else: return "No"
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN STRING RETURN STRING
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32. Example 1: Input: n = 8, m = 2 Output: Yes Explanation: Possible in base 3 as 8 in base 3 is 22. Example 2: Input: n = 8, m = 3 Output: No Explanation: Not possible in any base. Your Task: You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes.. Expected Time Complexity: O(logN). Expected Auxiliary Space: O(1) Constraints: 1 <= n <=10^{9} 1 <= m <=32
class Solution: def baseEquiv(self, n, m): def solve(n, a): ans = 0 while n > 0: n //= a ans += 1 return ans for i in range(2, 33): if solve(n, i) == m: return "Yes" return "No"
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN STRING RETURN STRING
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32. Example 1: Input: n = 8, m = 2 Output: Yes Explanation: Possible in base 3 as 8 in base 3 is 22. Example 2: Input: n = 8, m = 3 Output: No Explanation: Not possible in any base. Your Task: You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes.. Expected Time Complexity: O(logN). Expected Auxiliary Space: O(1) Constraints: 1 <= n <=10^{9} 1 <= m <=32
class Solution: def baseEquiv(self, n, m): def get(num, b, t): if num < b: if t + 1 == m: return True else: return False elif t >= m: return False else: return get(num // b, b, t + 1) for i in range(2, 33): if get(n, i, 0): return "Yes" return "No"
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR IF BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN STRING RETURN STRING
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a card game called Blackjack. He starts with a deck of $N$ cards (numbered $1$ through $N$), where for each valid $i$, the $i$-th card has an integer $A_{i}$ written on it. Then he starts dealing the cards one by one in the order from card $1$ to card $N$. Chef wins if at some moment in time, the sum of all the cards dealt so far is between $X$ and $Y$ inclusive; if this never happens, Chef loses. We want Chef to win the game, so without him noticing, we will swap some pairs of cards (possibly none) before the game starts. Find the smallest number of swaps we need so that Chef would win the game, or determine that it is impossible to make Chef win. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $X$ and $Y$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest required number of swaps or $-1$ if Chef cannot win. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N,X,Y ≤ 10^{3}$ $X ≤ Y$ $1 ≤ A_{i} ≤ 10^{3}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{4}$ ------ Subtasks ------ Subtask #1 (22 points): Chef can win if we make up to $2$ swaps Subtask #2 (34 points): $1 ≤ N,X,Y ≤ 400$ $1 ≤ A_{i} ≤ 400$ for each valid $i$ the sum of $N$ over all test cases does not exceed $4,000$ Subtask #3 (44 points): original constraints ----- Sample Input 1 ------ 3 3 4 5 1 2 3 3 3 4 1 2 3 2 20 30 40 10 ----- Sample Output 1 ------ 1 0 -1 ----- explanation 1 ------ Example case 1: We can swap the last card with one of the first two cards. Example case 2: No swaps are necessary, Chef wins after dealing the first two cards. Example case 3: The order of the cards in the deck does not matter, Chef cannot win.
def solveCase(N, X, Y, a): left_cur, left_next = [0] * (N + 1), [0] * (N + 1) right_cur, right_next = [0] * (N + 1), [0] * (N + 1) left_cur[0] = 1 right_cur[N] = (2 << Y) - (1 << X) max_mask = (2 << Y) - 1 for missed in range(N // 2 + 1): for i in range(N): left_cur[i + 1] |= left_cur[i] << a[i] & max_mask left_next[i + 1] = left_cur[i] for i in range(N, 0, -1): right_next[i - 1] = right_cur[i] >> a[i - 1] right_cur[i - 1] |= right_cur[i] if any(right_cur[i] & left_cur[i] for i in range(1, N + 1)): return missed left_cur, left_next = left_next, left_cur right_cur, right_next = right_next, right_cur return -1 T = int(input()) for _ in range(T): N, X, Y = map(int, input().split()) a = list(map(int, input().split())) print(solveCase(N, X, Y, a))
FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a card game called Blackjack. He starts with a deck of $N$ cards (numbered $1$ through $N$), where for each valid $i$, the $i$-th card has an integer $A_{i}$ written on it. Then he starts dealing the cards one by one in the order from card $1$ to card $N$. Chef wins if at some moment in time, the sum of all the cards dealt so far is between $X$ and $Y$ inclusive; if this never happens, Chef loses. We want Chef to win the game, so without him noticing, we will swap some pairs of cards (possibly none) before the game starts. Find the smallest number of swaps we need so that Chef would win the game, or determine that it is impossible to make Chef win. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $X$ and $Y$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest required number of swaps or $-1$ if Chef cannot win. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N,X,Y ≤ 10^{3}$ $X ≤ Y$ $1 ≤ A_{i} ≤ 10^{3}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{4}$ ------ Subtasks ------ Subtask #1 (22 points): Chef can win if we make up to $2$ swaps Subtask #2 (34 points): $1 ≤ N,X,Y ≤ 400$ $1 ≤ A_{i} ≤ 400$ for each valid $i$ the sum of $N$ over all test cases does not exceed $4,000$ Subtask #3 (44 points): original constraints ----- Sample Input 1 ------ 3 3 4 5 1 2 3 3 3 4 1 2 3 2 20 30 40 10 ----- Sample Output 1 ------ 1 0 -1 ----- explanation 1 ------ Example case 1: We can swap the last card with one of the first two cards. Example case 2: No swaps are necessary, Chef wins after dealing the first two cards. Example case 3: The order of the cards in the deck does not matter, Chef cannot win.
left = [([0] * 1024) for _ in range(1024)] right = [([0] * 1024) for _ in range(1024)] def solveCase(N, X, Y, a): for i in range(N + 2): left[0][i] = right[0][i] = 0 left[0][0] = 1 right[0][N] = (1 << Y + 1) - (1 << X) max_mask = (1 << Y + 1) - 1 for missed in range(N // 2 + 1): left[missed + 1][0] = 0 for i in range(N): left[missed][i + 1] |= left[missed][i] << a[i] & max_mask left[missed + 1][i + 1] = left[missed][i] right[missed + 1][N] = 0 for i in range(N, 0, -1): right[missed + 1][i - 1] = right[missed][i] >> a[i - 1] right[missed][i - 1] |= right[missed][i] if any(right[missed][i] & left[missed][i] for i in range(1, N + 1)): return missed return -1 T = int(input()) for _ in range(T): N, X, Y = map(int, input().split()) a = list(map(int, input().split())) print(solveCase(N, X, Y, a))
ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given N integers: A_{1}, A_{2}, ..., A_{N}. You need to count the number of pairs of indices (i, j) such that 1 ≤ i < j ≤ N and A_{i} | A_{j} ≤ max(A_{i}, A_{j}). Note: A_{i} | A_{j} refers to bitwise OR. ------ Input ------ The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows. The first line of each testcase contains a single integer: N The second line of each testcase contains N integers: A_{1}, A_{2}, ..., A_{N}. ------ Output ------ For each test case, output a single line containing the answer for that test case. ------ Constraints ------ $1 ≤ T ≤ 20$ $1 ≤ N ≤ 10^{6} $ $0 ≤ A_{i} ≤ 10^{6}$ $1 ≤ Sum of N over all test cases ≤ 10^{6} $ ------ Subtasks ------ Subtask #1 (20 points): $1 ≤ N ≤ 10^{3} $ Subtask #2 (80 points): $Original constraints$ ----- Sample Input 1 ------ 1 3 1 2 3 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ There are three possible pairs of indices which satisfy 1 ? i j ? N: (1, 2), (1, 3) and (2, 3). Let us see which of those satisfy Ai | Aj ? max(Ai, Aj): (1, 2): A1 | A2 = 1 | 2 = (01)2 | (10)2 = (11)2 = 3. But max(A1, A2) = max(1, 2) = 2, and 3 ? 2 is not correct. Hence this is not a valid pair. (1, 3): A1 | A3 = 1 | 3 = (01)2 | (11)2 = (11)2 = 3. And max(A1, A3) = max(1, 3) = 3, and 3 ? 3 is correct. Hence this is a valid pair. (2, 3): A2 | A3 = 2 | 3 = (10)2 | (11)2 = (11)2 = 3. And max(A2, A3) = max(2, 3) = 3, and 3 ? 3 is correct. Hence this is a valid pair. So there are a total of 2 valid pairs, and hence the answer is 2.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().strip().split())) d = {} for i in range(n): if arr[i] not in d: d[arr[i]] = 1 else: d[arr[i]] += 1 ans = 0 for A1 in d.keys(): ans += d[A1] * (d[A1] - 1) for A2 in d.keys(): if A1 != A2 and A1 | A2 <= max(A1, A2): ans += d[A1] * d[A2] print(ans // 2)
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 FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Count the number of distinct sequences a_1, a_2, ..., a_{n} (1 ≤ a_{i}) consisting of positive integers such that gcd(a_1, a_2, ..., a_{n}) = x and $\sum_{i = 1}^{n} a_{i} = y$. As this number could be large, print the answer modulo 10^9 + 7. gcd here means the greatest common divisor. -----Input----- The only line contains two positive integers x and y (1 ≤ x, y ≤ 10^9). -----Output----- Print the number of such sequences modulo 10^9 + 7. -----Examples----- Input 3 9 Output 3 Input 5 8 Output 0 -----Note----- There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test.
import sys mod = 1000000007 def modpow(a, x): if x == 0: return 1 if x == 1: return a b = modpow(a, x // 2) return b * b * a ** (x % 2) % mod n, m = map(int, input().split()) if m % n != 0: print(0) sys.exit(0) if n == m: print(1) sys.exit(0) ans = m // n arr = [] i = 2 while i * i < ans: if ans % i == 0: arr.append(i) arr.append(ans // i) i += 1 if int(ans**0.5) ** 2 == ans: arr.append(int(ans**0.5)) arr.append(ans) arr.sort() result = modpow(2, ans - 1) muls = [(0) for _ in arr] muls[0] = 1 for xid, d in enumerate(arr): prevs = 0 for id, d2 in enumerate(arr): if d2 < d and d % d2 == 0: prevs += muls[id] muls[xid] = 1 - prevs result += (prevs - 1) * modpow(2, ans // d - 1) result = (result + 1000 * mod) % mod print(result)
IMPORT ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Count the number of distinct sequences a_1, a_2, ..., a_{n} (1 ≤ a_{i}) consisting of positive integers such that gcd(a_1, a_2, ..., a_{n}) = x and $\sum_{i = 1}^{n} a_{i} = y$. As this number could be large, print the answer modulo 10^9 + 7. gcd here means the greatest common divisor. -----Input----- The only line contains two positive integers x and y (1 ≤ x, y ≤ 10^9). -----Output----- Print the number of such sequences modulo 10^9 + 7. -----Examples----- Input 3 9 Output 3 Input 5 8 Output 0 -----Note----- There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test.
def bin_pow(num, degree, module): if degree == 0: return 1 if degree == 1: return num % module if degree % 2 == 0: val = bin_pow(num, degree // 2, module) return val * val % module return num * bin_pow(num, degree - 1, module) % module x, y = map(int, input().split()) if y % x != 0: print(0) exit(0) y //= x divs = set() to_gen = [] num = 2 val = y while num * num <= val: degree = 0 while y % num == 0: degree += 1 y //= num if degree != 0: to_gen.append((num, degree)) if num == 2: num += 1 else: num += 2 if y != 1: to_gen.append((y, 1)) to_gen_len = len(to_gen) def generate(ind): if ind == to_gen_len: yield 1 return gen_val = to_gen[ind][0] for deg in range(1 + to_gen[ind][1]): for each in generate(ind + 1): yield gen_val**deg * each for each in generate(0): divs.add(each) divs = list(divs) divs.sort() divs_answers = {} mod = 10**9 + 7 ans = bin_pow(2, val - 1, mod) for el in divs: if el == 1: divs_answers[el] = 1 ans -= 1 else: curr_val = bin_pow(2, el - 1, mod) for other_el in divs: if other_el >= el: break if el % other_el != 0: continue curr_val -= divs_answers[other_el] divs_answers[el] = curr_val % mod ans -= curr_val print(divs_answers[val])
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR EXPR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Count the number of distinct sequences a_1, a_2, ..., a_{n} (1 ≤ a_{i}) consisting of positive integers such that gcd(a_1, a_2, ..., a_{n}) = x and $\sum_{i = 1}^{n} a_{i} = y$. As this number could be large, print the answer modulo 10^9 + 7. gcd here means the greatest common divisor. -----Input----- The only line contains two positive integers x and y (1 ≤ x, y ≤ 10^9). -----Output----- Print the number of such sequences modulo 10^9 + 7. -----Examples----- Input 3 9 Output 3 Input 5 8 Output 0 -----Note----- There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test.
import sys x, y = list(map(int, input().strip().split())) if y % x != 0: print(0) return MOD = 10**9 + 7 K = y // x def multiply(A, b, MOD): n, m = len(A), len(b[0]) matrika = [[(0) for _ in range(m)] for _ in range(n)] for i in range(n): for j in range(m): vsota = 0 for k in range(len(A[0])): vsota += A[i][k] * b[k][j] matrika[i][j] = vsota % MOD return matrika def copy(mat): return [e[:] for e in mat] def fib(n): if n == 0: return 0 if n == 1: return 1 if n == 2: return 1 matrika = [[1, 1], [1, 0]] pripravi = dict() pripravi[1] = copy(matrika) s = 1 pot = [1] working = copy(matrika) while s <= n: working = multiply(working, working, MOD) s *= 2 pripravi[s] = copy(working) pot.append(s) manjka = n - 2 pointer = len(pot) - 1 while manjka > 0: if pot[pointer] > manjka: pointer -= 1 else: matrika = multiply(matrika, pripravi[pot[pointer]], MOD) manjka -= pot[pointer] v = [[1], [0]] return multiply(matrika, v, MOD)[0][0] memo2 = dict() def find(y): if y in memo2: return memo2[y] ALL = pow(2, y - 1, MOD) % MOD k = 2 while k * k <= y: if y % k == 0: if k * k != y: ALL -= find(y // k) ALL -= find(k) else: ALL -= find(k) k += 1 if y != 1: ALL -= 1 memo2[y] = ALL % MOD return ALL % MOD print(find(K) % MOD) def gcd(x, y): if x == 0: return y if y > x: return gcd(y, x) if x % y == 0: return y return gcd(y, x % y) memo = dict() def brute(k, gc): if (k, gc) in memo: return memo[k, gc] if k == 0: if gc: return 1 else: return 0 ALL = 0 for i in range(1, k + 1): ALL += brute(k - i, gcd(gc, i)) memo[k, gc] = ALL return ALL
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST LIST NUMBER LIST NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Count the number of distinct sequences a_1, a_2, ..., a_{n} (1 ≤ a_{i}) consisting of positive integers such that gcd(a_1, a_2, ..., a_{n}) = x and $\sum_{i = 1}^{n} a_{i} = y$. As this number could be large, print the answer modulo 10^9 + 7. gcd here means the greatest common divisor. -----Input----- The only line contains two positive integers x and y (1 ≤ x, y ≤ 10^9). -----Output----- Print the number of such sequences modulo 10^9 + 7. -----Examples----- Input 3 9 Output 3 Input 5 8 Output 0 -----Note----- There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test.
modulo = 10**9 + 7 num1, num2 = list(map(int, input().split())) if num2 % num1: print("0") else: num2 = num2 // num1 arr = set() for i in range(1, int(pow(num2, 0.5) + 1)): if num2 % i == 0: arr.add(i) arr.add(num2 // i) arr = sorted(list(arr)) cop2 = arr[:] for i in range(len(cop2)): cop2[i] = pow(2, arr[i] - 1, modulo) for j in range(i): if arr[i] % arr[j] == 0: cop2[i] -= cop2[j] print(cop2[-1] % modulo)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Count the number of distinct sequences a_1, a_2, ..., a_{n} (1 ≤ a_{i}) consisting of positive integers such that gcd(a_1, a_2, ..., a_{n}) = x and $\sum_{i = 1}^{n} a_{i} = y$. As this number could be large, print the answer modulo 10^9 + 7. gcd here means the greatest common divisor. -----Input----- The only line contains two positive integers x and y (1 ≤ x, y ≤ 10^9). -----Output----- Print the number of such sequences modulo 10^9 + 7. -----Examples----- Input 3 9 Output 3 Input 5 8 Output 0 -----Note----- There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test.
import sys mod = 1000000007 def mul(x, y): res = 1 while y > 0: if y % 2 == 1: res = res * x % mod x = x * x % mod y //= 2 return res x, y = map(int, sys.stdin.readline().split()) if y % x != 0: print(0) else: y /= x d = set([]) i = 1 while i * i <= y: if y % i == 0: d.add(i) d.add(y / i) i += 1 d = sorted(list(d)) dp = d[:] for i in range(len(d)): dp[i] = mul(2, d[i] - 1) for j in range(i): if d[i] % d[j] == 0: dp[i] -= dp[j] print(dp[-1] % mod)
IMPORT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Count the number of distinct sequences a_1, a_2, ..., a_{n} (1 ≤ a_{i}) consisting of positive integers such that gcd(a_1, a_2, ..., a_{n}) = x and $\sum_{i = 1}^{n} a_{i} = y$. As this number could be large, print the answer modulo 10^9 + 7. gcd here means the greatest common divisor. -----Input----- The only line contains two positive integers x and y (1 ≤ x, y ≤ 10^9). -----Output----- Print the number of such sequences modulo 10^9 + 7. -----Examples----- Input 3 9 Output 3 Input 5 8 Output 0 -----Note----- There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test.
lista = input().split(" ") def prueba(f, *params): i = 1 for a in params: if f(a[0], a[1]) != a[2]: print( "El caso", i, "es incorrecto se respondio", f(a[0], a[1]), "y se esperaba", a[2], ) else: print("El caso", i, "es correcto") i += 1 def comparacion(f, g, *casos): for a in casos: print( "La primera responde", f(a[0], a[1]), "y la segunda funcion responde", g(a[0], a[1]), ) def gcd(a, b): if a < b: return gcd(b, a) if b == 0: return a resto = a % b if resto > b // 2: return gcd(b, b - resto) else: return gcd(b, resto) def Unusual2(x, y): a = set() o = 10**9 + 7 if y < x or y % x != 0: return 0 y //= x for i in range(1, int(pow(y, 0.5) + 1)): if y % i == 0: a.add(i) a.add(y // i) lista = sorted(list(a)) copia = lista[:] for i in range(len(lista)): copia[i] = int(pow(2, lista[i] - 1, o)) for j in range(i): if lista[i] % lista[j] == 0: copia[i] -= copia[j] return copia[len(copia) - 1] % o print(Unusual2(int(lista[0]), int(lista[1])))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING VAR STRING FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR STRING VAR STRING VAR NUMBER FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR STRING FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
Count the number of distinct sequences a_1, a_2, ..., a_{n} (1 ≤ a_{i}) consisting of positive integers such that gcd(a_1, a_2, ..., a_{n}) = x and $\sum_{i = 1}^{n} a_{i} = y$. As this number could be large, print the answer modulo 10^9 + 7. gcd here means the greatest common divisor. -----Input----- The only line contains two positive integers x and y (1 ≤ x, y ≤ 10^9). -----Output----- Print the number of such sequences modulo 10^9 + 7. -----Examples----- Input 3 9 Output 3 Input 5 8 Output 0 -----Note----- There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test.
mod = 1000000007 x, y = map(int, input().split()) if y % x: print(0) exit(0) y //= x f = {(1): 1} def quick_power(a, b): r = 1 while b: if b & 1: r = r * a % mod a = a * a % mod b >>= 1 return r def dfs(s): if s in f.keys(): return f[s] r = quick_power(2, s - 1) for g in range(2, int(s**0.5)): if s % g == 0: r = (r - dfs(s // g) - dfs(g)) % mod g = int(s**0.5) if g >= 2 and s % g == 0: if g * g == s: r = (r - dfs(g)) % mod else: r = (r - dfs(s // g) - dfs(g)) % mod f[s] = (r - 1) % mod return f[s] print(dfs(y))
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Count the number of distinct sequences a_1, a_2, ..., a_{n} (1 ≤ a_{i}) consisting of positive integers such that gcd(a_1, a_2, ..., a_{n}) = x and $\sum_{i = 1}^{n} a_{i} = y$. As this number could be large, print the answer modulo 10^9 + 7. gcd here means the greatest common divisor. -----Input----- The only line contains two positive integers x and y (1 ≤ x, y ≤ 10^9). -----Output----- Print the number of such sequences modulo 10^9 + 7. -----Examples----- Input 3 9 Output 3 Input 5 8 Output 0 -----Note----- There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test.
x, y = map(int, input().split()) b = y // x if y % x != 0: print(0) return ds = set() M = 10**9 + 7 for i in range(1, int(pow(b, 0.5) + 1)): if b % i == 0: ds.add(i) ds.add(b // i) ds = sorted(list(ds)) ans = pow(2, b - 1, M) f = ds[:] for i in range(len(ds)): f[i] = pow(2, ds[i] - 1, M) for j in range(i): if ds[i] % ds[j] == 0: f[i] -= f[j] print(f[-1] % M)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
You are given a boolean function of three variables which is defined by its truth table. You need to find an expression of minimum length that equals to this function. The expression may consist of: Operation AND ('&', ASCII code 38) Operation OR ('|', ASCII code 124) Operation NOT ('!', ASCII code 33) Variables x, y and z (ASCII codes 120-122) Parentheses ('(', ASCII code 40, and ')', ASCII code 41) If more than one expression of minimum length exists, you should find the lexicographically smallest one. Operations have standard priority. NOT has the highest priority, then AND goes, and OR has the lowest priority. The expression should satisfy the following grammar: E ::= E '|' T | T T ::= T '&' F | F F ::= '!' F | '(' E ')' | 'x' | 'y' | 'z' -----Input----- The first line contains one integer n — the number of functions in the input (1 ≤ n ≤ 10 000). The following n lines contain descriptions of functions, the i-th of them contains a string of length 8 that consists of digits 0 and 1 — the truth table of the i-th function. The digit on position j (0 ≤ j < 8) equals to the value of the function in case of $x = \lfloor \frac{j}{4} \rfloor$, $y = \lfloor \frac{j}{2} \rfloor \operatorname{mod} 2$ and $z = j \operatorname{mod} 2$. -----Output----- You should output n lines, the i-th line should contain the expression of minimum length which equals to the i-th function. If there is more than one such expression, output the lexicographically smallest of them. Expressions should satisfy the given grammar and shouldn't contain white spaces. -----Example----- Input 4 00110011 00000111 11110000 00011111 Output y (y|z)&x !x x|y&z -----Note----- The truth table for the second function: [Image]
x = int("00001111", 2) y = int("00110011", 2) z = int("01010101", 2) E = set() T = set() F = {("x", x), ("y", y), ("z", z)} prv = set(), set(), set() fam = 2**8 tmpl = "#" * 99 ans = [tmpl] * fam def cmpr(E): nonlocal ans ans = [tmpl] * fam for e in E: if ( len(ans[e[1]]) > len(e[0]) or len(ans[e[1]]) == len(e[0]) and ans[e[1]] > e[0] ): ans[e[1]] = e[0] return set((j, i) for i, j in enumerate(ans) if j != tmpl) while prv != (E, T, F): prv = E.copy(), T.copy(), F.copy() for f in prv[2]: F.add(("!" + f[0], ~f[1] & fam - 1)) T.add(f) for t in prv[1]: T.add((t[0] + "&" + f[0], t[1] & f[1])) for t in prv[1]: E.add(t) for e in prv[0]: if e not in F: F.add(("(" + e[0] + ")", e[1])) for t in prv[1]: E.add((e[0] + "|" + t[0], e[1] | t[1])) E, T, F = cmpr(E), cmpr(T), cmpr(F) cmpr(E) n = int(input()) for i in range(n): print(ans[int(input(), 2)])
ASSIGN VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING VAR STRING VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP STRING NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER STRING VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER
You are given a boolean function of three variables which is defined by its truth table. You need to find an expression of minimum length that equals to this function. The expression may consist of: Operation AND ('&', ASCII code 38) Operation OR ('|', ASCII code 124) Operation NOT ('!', ASCII code 33) Variables x, y and z (ASCII codes 120-122) Parentheses ('(', ASCII code 40, and ')', ASCII code 41) If more than one expression of minimum length exists, you should find the lexicographically smallest one. Operations have standard priority. NOT has the highest priority, then AND goes, and OR has the lowest priority. The expression should satisfy the following grammar: E ::= E '|' T | T T ::= T '&' F | F F ::= '!' F | '(' E ')' | 'x' | 'y' | 'z' -----Input----- The first line contains one integer n — the number of functions in the input (1 ≤ n ≤ 10 000). The following n lines contain descriptions of functions, the i-th of them contains a string of length 8 that consists of digits 0 and 1 — the truth table of the i-th function. The digit on position j (0 ≤ j < 8) equals to the value of the function in case of $x = \lfloor \frac{j}{4} \rfloor$, $y = \lfloor \frac{j}{2} \rfloor \operatorname{mod} 2$ and $z = j \operatorname{mod} 2$. -----Output----- You should output n lines, the i-th line should contain the expression of minimum length which equals to the i-th function. If there is more than one such expression, output the lexicographically smallest of them. Expressions should satisfy the given grammar and shouldn't contain white spaces. -----Example----- Input 4 00110011 00000111 11110000 00011111 Output y (y|z)&x !x x|y&z -----Note----- The truth table for the second function: [Image]
val = """!x&x x&y&z !z&x&y x&y !y&x&z x&z !y&x&z|!z&x&y (y|z)&x !y&!z&x !y&!z&x|x&y&z !z&x !z&x|x&y !y&x !y&x|x&z !(y&z)&x x !x&y&z y&z !x&y&z|!z&x&y (x|z)&y !x&y&z|!y&x&z (x|y)&z !x&y&z|!y&x&z|!z&x&y (x|y)&z|x&y !x&y&z|!y&!z&x !y&!z&x|y&z !x&y&z|!z&x !z&x|y&z !x&y&z|!y&x !y&x|y&z !(y&z)&x|!x&y&z x|y&z !x&!z&y !x&!z&y|x&y&z !z&y !z&y|x&y !x&!z&y|!y&x&z !x&!z&y|x&z !y&x&z|!z&y !z&y|x&z !(!x&!y|x&y|z) !(!x&!y|x&y|z)|x&y&z !z&(x|y) !z&(x|y)|x&y !x&!z&y|!y&x !x&!z&y|!y&x|x&z !y&x|!z&y !z&y|x !x&y !x&y|y&z !(x&z)&y y !x&y|!y&x&z !x&y|x&z !(x&z)&y|!y&x&z x&z|y !x&y|!y&!z&x !x&y|!y&!z&x|y&z !x&y|!z&x !z&x|y !x&y|!y&x !x&y|!y&x|x&z !(x&z)&y|!y&x x|y !x&!y&z !x&!y&z|x&y&z !x&!y&z|!z&x&y !x&!y&z|x&y !y&z !y&z|x&z !y&z|!z&x&y !y&z|x&y !(!x&!z|x&z|y) !(!x&!z|x&z|y)|x&y&z !x&!y&z|!z&x !x&!y&z|!z&x|x&y !y&(x|z) !y&(x|z)|x&z !y&z|!z&x !y&z|x !x&z !x&z|y&z !x&z|!z&x&y !x&z|x&y !(x&y)&z z !(x&y)&z|!z&x&y x&y|z !x&z|!y&!z&x !x&z|!y&!z&x|y&z !x&z|!z&x !x&z|!z&x|x&y !x&z|!y&x !y&x|z !(x&y)&z|!z&x x|z !(!y&!z|x|y&z) !(!y&!z|x|y&z)|x&y&z !x&!y&z|!z&y !x&!y&z|!z&y|x&y !x&!z&y|!y&z !x&!z&y|!y&z|x&z !y&z|!z&y !y&z|!z&y|x&y !(!x&!y|x&y|z)|!x&!y&z !(!x&!y|x&y|z)|!x&!y&z|x&y&z !x&!y&z|!z&(x|y) !x&!y&z|!z&(x|y)|x&y !x&!z&y|!y&(x|z) !x&!z&y|!y&(x|z)|x&z !y&(x|z)|!z&y !y&z|!z&y|x !x&(y|z) !x&(y|z)|y&z !x&z|!z&y !x&z|y !x&y|!y&z !x&y|z !(x&y)&z|!z&y y|z !x&(y|z)|!y&!z&x !x&(y|z)|!y&!z&x|y&z !x&(y|z)|!z&x !x&z|!z&x|y !x&(y|z)|!y&x !x&y|!y&x|z !x&y|!y&z|!z&x x|y|z !(x|y|z) !(x|y|z)|x&y&z !(!x&y|!y&x|z) !(x|y|z)|x&y !(!x&z|!z&x|y) !(x|y|z)|x&z !(!x&y|!y&x|z)|!y&x&z !(x|y|z)|(y|z)&x !y&!z !y&!z|x&y&z !(!x&y|z) !y&!z|x&y !(!x&z|y) !y&!z|x&z !(!x&y|z)|!y&x !y&!z|x !(!y&z|!z&y|x) !(x|y|z)|y&z !(!x&y|!y&x|z)|!x&y&z !(x|y|z)|(x|z)&y !(!x&z|!z&x|y)|!x&y&z !(x|y|z)|(x|y)&z !(!x&y|!y&x|z)|!x&y&z|!y&x&z !(x|y|z)|(x|y)&z|x&y !x&y&z|!y&!z !y&!z|y&z !(!x&y|z)|!x&y&z !(!x&y|z)|y&z !(!x&z|y)|!x&y&z !(!x&z|y)|y&z !(!x&y|z)|!x&y&z|!y&x !y&!z|x|y&z !x&!z !x&!z|x&y&z !(!y&x|z) !x&!z|x&y !x&!z|!y&x&z !x&!z|x&z !(!y&x|z)|!y&x&z !(!y&x|z)|x&z !(x&y|z) !(x&y|z)|x&y&z !z !z|x&y !x&!z|!y&x !(x&y|z)|x&z !y&x|!z !z|x !(!y&z|x) !x&!z|y&z !(!y&x|z)|!x&y !x&!z|y !(!y&z|x)|!y&x&z !(!y&z|x)|x&z !(!y&x|z)|!x&y|!y&x&z !x&!z|x&z|y !x&y|!y&!z !(x&y|z)|y&z !x&y|!z !z|y !(!x&!y&z|x&y) !x&!z|!y&x|y&z !x&y|!y&x|!z !z|x|y !x&!y !x&!y|x&y&z !x&!y|!z&x&y !x&!y|x&y !(!z&x|y) !x&!y|x&z !(!z&x|y)|!z&x&y !(!z&x|y)|x&y !(x&z|y) !(x&z|y)|x&y&z !x&!y|!z&x !(x&z|y)|x&y !y !y|x&z !y|!z&x !y|x !(!z&y|x) !x&!y|y&z !(!z&y|x)|!z&x&y !(!z&y|x)|x&y !(!z&x|y)|!x&z !x&!y|z !(!z&x|y)|!x&z|!z&x&y !x&!y|x&y|z !x&z|!y&!z !(x&z|y)|y&z !(!x&!z&y|x&z) !x&!y|!z&x|y&z !x&z|!y !y|z !x&z|!y|!z&x !y|x|z !(x|y&z) !(x|y&z)|x&y&z !x&!y|!z&y !(x|y&z)|x&y !x&!z|!y&z !(x|y&z)|x&z !(!y&!z&x|y&z) !x&!y|!z&y|x&z !((x|y)&z|x&y) !((x|y)&z|x&y)|x&y&z !x&!y|!z !x&!y|!z|x&y !x&!z|!y !x&!z|!y|x&z !y|!z !y|!z|x !x !x|y&z !x|!z&y !x|y !x|!y&z !x|z !x|!y&z|!z&y !x|y|z !x|!y&!z !x|!y&!z|y&z !x|!z !x|!z|y !x|!y !x|!y|z !(x&y&z) !x|x""".split() for _ in range(int(input())): n = 0 for b in input(): n <<= 1 if b == "1": n |= 1 print(val[n])
ASSIGN VAR FUNC_CALL STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given a boolean function of three variables which is defined by its truth table. You need to find an expression of minimum length that equals to this function. The expression may consist of: Operation AND ('&', ASCII code 38) Operation OR ('|', ASCII code 124) Operation NOT ('!', ASCII code 33) Variables x, y and z (ASCII codes 120-122) Parentheses ('(', ASCII code 40, and ')', ASCII code 41) If more than one expression of minimum length exists, you should find the lexicographically smallest one. Operations have standard priority. NOT has the highest priority, then AND goes, and OR has the lowest priority. The expression should satisfy the following grammar: E ::= E '|' T | T T ::= T '&' F | F F ::= '!' F | '(' E ')' | 'x' | 'y' | 'z' -----Input----- The first line contains one integer n — the number of functions in the input (1 ≤ n ≤ 10 000). The following n lines contain descriptions of functions, the i-th of them contains a string of length 8 that consists of digits 0 and 1 — the truth table of the i-th function. The digit on position j (0 ≤ j < 8) equals to the value of the function in case of $x = \lfloor \frac{j}{4} \rfloor$, $y = \lfloor \frac{j}{2} \rfloor \operatorname{mod} 2$ and $z = j \operatorname{mod} 2$. -----Output----- You should output n lines, the i-th line should contain the expression of minimum length which equals to the i-th function. If there is more than one such expression, output the lexicographically smallest of them. Expressions should satisfy the given grammar and shouldn't contain white spaces. -----Example----- Input 4 00110011 00000111 11110000 00011111 Output y (y|z)&x !x x|y&z -----Note----- The truth table for the second function: [Image]
table = "!x&x x&y&z !z&x&y x&y !y&x&z x&z !y&x&z|!z&x&y (y|z)&x !y&!z&x !y&!z&x|x&y&z !z&x !z&x|x&y !y&x !y&x|x&z !(y&z)&x x !x&y&z y&z !x&y&z|!z&x&y (x|z)&y !x&y&z|!y&x&z (x|y)&z !x&y&z|!y&x&z|!z&x&y (x|y)&z|x&y !x&y&z|!y&!z&x !y&!z&x|y&z !x&y&z|!z&x !z&x|y&z !x&y&z|!y&x !y&x|y&z !(y&z)&x|!x&y&z x|y&z !x&!z&y !x&!z&y|x&y&z !z&y !z&y|x&y !x&!z&y|!y&x&z !x&!z&y|x&z !y&x&z|!z&y !z&y|x&z !(!x&!y|x&y|z) !(!x&!y|x&y|z)|x&y&z !z&(x|y) !z&(x|y)|x&y !x&!z&y|!y&x !x&!z&y|!y&x|x&z !y&x|!z&y !z&y|x !x&y !x&y|y&z !(x&z)&y y !x&y|!y&x&z !x&y|x&z !(x&z)&y|!y&x&z x&z|y !x&y|!y&!z&x !x&y|!y&!z&x|y&z !x&y|!z&x !z&x|y !x&y|!y&x !x&y|!y&x|x&z !(x&z)&y|!y&x x|y !x&!y&z !x&!y&z|x&y&z !x&!y&z|!z&x&y !x&!y&z|x&y !y&z !y&z|x&z !y&z|!z&x&y !y&z|x&y !(!x&!z|x&z|y) !(!x&!z|x&z|y)|x&y&z !x&!y&z|!z&x !x&!y&z|!z&x|x&y !y&(x|z) !y&(x|z)|x&z !y&z|!z&x !y&z|x !x&z !x&z|y&z !x&z|!z&x&y !x&z|x&y !(x&y)&z z !(x&y)&z|!z&x&y x&y|z !x&z|!y&!z&x !x&z|!y&!z&x|y&z !x&z|!z&x !x&z|!z&x|x&y !x&z|!y&x !y&x|z !(x&y)&z|!z&x x|z !(!y&!z|x|y&z) !(!y&!z|x|y&z)|x&y&z !x&!y&z|!z&y !x&!y&z|!z&y|x&y !x&!z&y|!y&z !x&!z&y|!y&z|x&z !y&z|!z&y !y&z|!z&y|x&y !(!x&!y|x&y|z)|!x&!y&z !(!x&!y|x&y|z)|!x&!y&z|x&y&z !x&!y&z|!z&(x|y) !x&!y&z|!z&(x|y)|x&y !x&!z&y|!y&(x|z) !x&!z&y|!y&(x|z)|x&z !y&(x|z)|!z&y !y&z|!z&y|x !x&(y|z) !x&(y|z)|y&z !x&z|!z&y !x&z|y !x&y|!y&z !x&y|z !(x&y)&z|!z&y y|z !x&(y|z)|!y&!z&x !x&(y|z)|!y&!z&x|y&z !x&(y|z)|!z&x !x&z|!z&x|y !x&(y|z)|!y&x !x&y|!y&x|z !x&y|!y&z|!z&x x|y|z !(x|y|z) !(x|y|z)|x&y&z !(!x&y|!y&x|z) !(x|y|z)|x&y !(!x&z|!z&x|y) !(x|y|z)|x&z !(!x&y|!y&x|z)|!y&x&z !(x|y|z)|(y|z)&x !y&!z !y&!z|x&y&z !(!x&y|z) !y&!z|x&y !(!x&z|y) !y&!z|x&z !(!x&y|z)|!y&x !y&!z|x !(!y&z|!z&y|x) !(x|y|z)|y&z !(!x&y|!y&x|z)|!x&y&z !(x|y|z)|(x|z)&y !(!x&z|!z&x|y)|!x&y&z !(x|y|z)|(x|y)&z !(!x&y|!y&x|z)|!x&y&z|!y&x&z !(x|y|z)|(x|y)&z|x&y !x&y&z|!y&!z !y&!z|y&z !(!x&y|z)|!x&y&z !(!x&y|z)|y&z !(!x&z|y)|!x&y&z !(!x&z|y)|y&z !(!x&y|z)|!x&y&z|!y&x !y&!z|x|y&z !x&!z !x&!z|x&y&z !(!y&x|z) !x&!z|x&y !x&!z|!y&x&z !x&!z|x&z !(!y&x|z)|!y&x&z !(!y&x|z)|x&z !(x&y|z) !(x&y|z)|x&y&z !z !z|x&y !x&!z|!y&x !(x&y|z)|x&z !y&x|!z !z|x !(!y&z|x) !x&!z|y&z !(!y&x|z)|!x&y !x&!z|y !(!y&z|x)|!y&x&z !(!y&z|x)|x&z !(!y&x|z)|!x&y|!y&x&z !x&!z|x&z|y !x&y|!y&!z !(x&y|z)|y&z !x&y|!z !z|y !(!x&!y&z|x&y) !x&!z|!y&x|y&z !x&y|!y&x|!z !z|x|y !x&!y !x&!y|x&y&z !x&!y|!z&x&y !x&!y|x&y !(!z&x|y) !x&!y|x&z !(!z&x|y)|!z&x&y !(!z&x|y)|x&y !(x&z|y) !(x&z|y)|x&y&z !x&!y|!z&x !(x&z|y)|x&y !y !y|x&z !y|!z&x !y|x !(!z&y|x) !x&!y|y&z !(!z&y|x)|!z&x&y !(!z&y|x)|x&y !(!z&x|y)|!x&z !x&!y|z !(!z&x|y)|!x&z|!z&x&y !x&!y|x&y|z !x&z|!y&!z !(x&z|y)|y&z !(!x&!z&y|x&z) !x&!y|!z&x|y&z !x&z|!y !y|z !x&z|!y|!z&x !y|x|z !(x|y&z) !(x|y&z)|x&y&z !x&!y|!z&y !(x|y&z)|x&y !x&!z|!y&z !(x|y&z)|x&z !(!y&!z&x|y&z) !x&!y|!z&y|x&z !((x|y)&z|x&y) !((x|y)&z|x&y)|x&y&z !x&!y|!z !x&!y|!z|x&y !x&!z|!y !x&!z|!y|x&z !y|!z !y|!z|x !x !x|y&z !x|!z&y !x|y !x|!y&z !x|z !x|!y&z|!z&y !x|y|z !x|!y&!z !x|!y&!z|y&z !x|!z !x|!z|y !x|!y !x|!y|z !(x&y&z) !x|x".split() n = int(input()) for i in range(n): print(table[int(input(), 2)]) return E = set() T = set() F = set("xyz") prv = 0 x = int("00001111", 2) y = int("00110011", 2) z = int("01010101", 2) fam = 2**8 tmpl = "#" * 99 ans = [tmpl] * fam def cmpr(E): nonlocal ans ans = [tmpl] * fam for e in E: res = eval(e.replace("!", "~")) & fam - 1 if len(ans[res]) > len(e) or len(ans[res]) == len(e) and ans[res] > e: ans[res] = e return set(ans) - {tmpl} def cmpr3(E, T, F): return cmpr(E), cmpr(T), cmpr(F) while prv != (E, T, F): prv = E.copy(), T.copy(), F.copy() for f in prv[2]: F.add("!" + f) T.add(f) for t in prv[1]: T.add(t + "&" + f) for t in prv[1]: E.add(t) for e in prv[0]: if e not in F: F.add("(" + e + ")") for t in prv[1]: E.add(e + "|" + t) E, T, F = cmpr3(E, T, F) cmpr(E) for f in ans: print(f) print(ans.count(tmpl), fam - ans.count(tmpl))
ASSIGN VAR FUNC_CALL STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP STRING NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR STRING STRING BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR STRING FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR
You have a password which you often type — a string s of length n. Every character of this string is one of the first m lowercase Latin letters. Since you spend a lot of time typing it, you want to buy a new keyboard. A keyboard is a permutation of the first m Latin letters. For example, if m = 3, then there are six possible keyboards: abc, acb, bac, bca, cab and cba. Since you type your password with one finger, you need to spend time moving your finger from one password character to the next. The time to move from character s_i to character s_{i+1} is equal to the distance between these characters on keyboard. The total time you have to spend typing the password with a keyboard is called the slowness of this keyboard. More formaly, the slowness of keyboard is equal to ∑_{i=2}^{n} |pos_{s_{i-1}} - pos_{s_i} |, where pos_x is position of letter x in keyboard. For example, if s is aacabc and the keyboard is bac, then the total time of typing this password is |pos_a - pos_a| + |pos_a - pos_c| + |pos_c - pos_a| + |pos_a - pos_b| + |pos_b - pos_c| = |2 - 2| + |2 - 3| + |3 - 2| + |2 - 1| + |1 - 3| = 0 + 1 + 1 + 1 + 2 = 5. Before buying a new keyboard you want to know the minimum possible slowness that the keyboard can have. Input The first line contains two integers n and m (1 ≤ n ≤ 10^5, 1 ≤ m ≤ 20). The second line contains the string s consisting of n characters. Each character is one of the first m Latin letters (lowercase). Output Print one integer – the minimum slowness a keyboard can have. Examples Input 6 3 aacabc Output 5 Input 6 4 aaaaaa Output 0 Input 15 4 abacabadabacaba Output 16 Note The first test case is considered in the statement. In the second test case the slowness of any keyboard is 0. In the third test case one of the most suitable keyboards is bacd.
n, m = map(int, input().split()) a = list(map(str, input().strip())) dp = [10**10] * (1 << 20) cnt = [0] * (1 << 20) def get(x): return 1 << ord(x) - ord("a") for i, v in enumerate(a): if i: cnt[get(a[i]) | get(a[i - 1])] += 1 for i in range(m): for j in range(1 << m): if 1 << i & j: cnt[j] += cnt[j ^ 1 << i] dp[0] = 0 for i in range(1 << m): for j in range(m): if not i & 1 << j: dp[i | 1 << j] = min( dp[i | 1 << j], dp[i] + n - 1 - cnt[i | 1 << j] - cnt[(1 << m) - 1 - (i | 1 << j)], ) print(dp[(1 << m) - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FUNC_DEF RETURN BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER
You have a password which you often type — a string s of length n. Every character of this string is one of the first m lowercase Latin letters. Since you spend a lot of time typing it, you want to buy a new keyboard. A keyboard is a permutation of the first m Latin letters. For example, if m = 3, then there are six possible keyboards: abc, acb, bac, bca, cab and cba. Since you type your password with one finger, you need to spend time moving your finger from one password character to the next. The time to move from character s_i to character s_{i+1} is equal to the distance between these characters on keyboard. The total time you have to spend typing the password with a keyboard is called the slowness of this keyboard. More formaly, the slowness of keyboard is equal to ∑_{i=2}^{n} |pos_{s_{i-1}} - pos_{s_i} |, where pos_x is position of letter x in keyboard. For example, if s is aacabc and the keyboard is bac, then the total time of typing this password is |pos_a - pos_a| + |pos_a - pos_c| + |pos_c - pos_a| + |pos_a - pos_b| + |pos_b - pos_c| = |2 - 2| + |2 - 3| + |3 - 2| + |2 - 1| + |1 - 3| = 0 + 1 + 1 + 1 + 2 = 5. Before buying a new keyboard you want to know the minimum possible slowness that the keyboard can have. Input The first line contains two integers n and m (1 ≤ n ≤ 10^5, 1 ≤ m ≤ 20). The second line contains the string s consisting of n characters. Each character is one of the first m Latin letters (lowercase). Output Print one integer – the minimum slowness a keyboard can have. Examples Input 6 3 aacabc Output 5 Input 6 4 aaaaaa Output 0 Input 15 4 abacabadabacaba Output 16 Note The first test case is considered in the statement. In the second test case the slowness of any keyboard is 0. In the third test case one of the most suitable keyboards is bacd.
import sys sys.setrecursionlimit(10**7) input = sys.stdin.readline INF = 10**9 n, m = [int(item) for item in input().split()] s = input().rstrip() count = [([0] * m) for _ in range(m)] ord_a = ord("a") for c1, c2 in zip(s, s[1:]): c1 = ord(c1) - ord_a c2 = ord(c2) - ord_a if c1 != c2: count[c1][c2] += 1 sum_of_subset = [([0] * (1 << m)) for _ in range(m)] for i in range(m): for j in range(1 << m): if j == 0: continue lsb = j & -j sum_of_subset[i][j] = sum_of_subset[i][j ^ lsb] + count[i][lsb.bit_length() - 1] adj_in_subset = [0] * (1 << m) for i in range(1 << m): for j in range(m): if i & 1 << j: adj_in_subset[i] += sum_of_subset[j][i] total_adj = adj_in_subset[-1] dp = [INF] * (1 << m) dp[0] = 0 for i in range(1 << m): for j in range(m): if i & 1 << j: continue cost = total_adj - adj_in_subset[i] - adj_in_subset[~i] dp[i | 1 << j] = min(dp[i | 1 << j], dp[i] + cost) print(dp[-1])
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Vasya has a sequence a consisting of n integers a_1, a_2, ..., a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (... 00000000110_2) into 3 (... 00000000011_2), 12 (... 000000001100_2), 1026 (... 10000000010_2) and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence. Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with [bitwise exclusive or](https://en.wikipedia.org/wiki/Exclusive_or) of all elements equal to 0. For the given sequence a_1, a_2, …, a_n Vasya'd like to calculate number of integer pairs (l, r) such that 1 ≤ l ≤ r ≤ n and sequence a_l, a_{l + 1}, ..., a_r is good. Input The first line contains a single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — length of the sequence. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{18}) — the sequence a. Output Print one integer — the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and the sequence a_l, a_{l + 1}, ..., a_r is good. Examples Input 3 6 7 14 Output 2 Input 4 1 2 1 16 Output 4 Note In the first example pairs (2, 3) and (1, 3) are valid. Pair (2, 3) is valid since a_2 = 7 → 11, a_3 = 14 → 11 and 11 ⊕ 11 = 0, where ⊕ — bitwise exclusive or. Pair (1, 3) is valid since a_1 = 6 → 3, a_2 = 7 → 13, a_3 = 14 → 14 and 3 ⊕ 13 ⊕ 14 = 0. In the second example pairs (1, 2), (2, 3), (3, 4) and (1, 4) are valid.
import sys def rint(): return map(int, sys.stdin.readline().split()) def get_num1(i): cnt = 0 while i: if i % 2: cnt += 1 i //= 2 return cnt n = int(input()) a = list(rint()) b = [get_num1(aa) for aa in a] ans = 0 S0 = [0] * n S0[0] = b[0] % 2 for i in range(1, n): S0[i] = (S0[i - 1] + b[i]) % 2 even_cnt = S0.count(0) ans = even_cnt for i in range(1, n): if b[i - 1] % 2: even_cnt = n - i - even_cnt else: even_cnt -= 1 ans += even_cnt for i in range(n): max_value = 0 sum_value = 0 for j in range(1, 62): if i + j > n: break sum_value += b[i + j - 1] max_value = max(max_value, b[i + j - 1]) if 2 * max_value > sum_value and sum_value % 2 == 0: ans -= 1 print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has a sequence a consisting of n integers a_1, a_2, ..., a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (... 00000000110_2) into 3 (... 00000000011_2), 12 (... 000000001100_2), 1026 (... 10000000010_2) and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence. Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with [bitwise exclusive or](https://en.wikipedia.org/wiki/Exclusive_or) of all elements equal to 0. For the given sequence a_1, a_2, …, a_n Vasya'd like to calculate number of integer pairs (l, r) such that 1 ≤ l ≤ r ≤ n and sequence a_l, a_{l + 1}, ..., a_r is good. Input The first line contains a single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — length of the sequence. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{18}) — the sequence a. Output Print one integer — the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and the sequence a_l, a_{l + 1}, ..., a_r is good. Examples Input 3 6 7 14 Output 2 Input 4 1 2 1 16 Output 4 Note In the first example pairs (2, 3) and (1, 3) are valid. Pair (2, 3) is valid since a_2 = 7 → 11, a_3 = 14 → 11 and 11 ⊕ 11 = 0, where ⊕ — bitwise exclusive or. Pair (1, 3) is valid since a_1 = 6 → 3, a_2 = 7 → 13, a_3 = 14 → 14 and 3 ⊕ 13 ⊕ 14 = 0. In the second example pairs (1, 2), (2, 3), (3, 4) and (1, 4) are valid.
n = int(input()) cnt = [[(0) for _ in range(n + 1)] for _ in range(2)] b = [bin(_).count("1") for _ in list(map(int, input().split()))] res = 0 suf_sum = 0 cnt[0][n] = 1 for i in range(n)[::-1]: _sum, mx = 0, 0 lst_j = i add = 0 for j in range(i, min(n, i + 65)): _sum += b[j] mx = max(mx, b[j]) if mx > _sum - mx and _sum % 2 == 0: add -= 1 lst_j = j suf_sum += b[i] add += cnt[suf_sum & 1][i + 1] res += add cnt[0][i] = cnt[0][i + 1] cnt[1][i] = cnt[1][i + 1] if suf_sum & 1: cnt[1][i] += 1 else: cnt[0][i] += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has a sequence a consisting of n integers a_1, a_2, ..., a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (... 00000000110_2) into 3 (... 00000000011_2), 12 (... 000000001100_2), 1026 (... 10000000010_2) and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence. Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with [bitwise exclusive or](https://en.wikipedia.org/wiki/Exclusive_or) of all elements equal to 0. For the given sequence a_1, a_2, …, a_n Vasya'd like to calculate number of integer pairs (l, r) such that 1 ≤ l ≤ r ≤ n and sequence a_l, a_{l + 1}, ..., a_r is good. Input The first line contains a single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — length of the sequence. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{18}) — the sequence a. Output Print one integer — the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and the sequence a_l, a_{l + 1}, ..., a_r is good. Examples Input 3 6 7 14 Output 2 Input 4 1 2 1 16 Output 4 Note In the first example pairs (2, 3) and (1, 3) are valid. Pair (2, 3) is valid since a_2 = 7 → 11, a_3 = 14 → 11 and 11 ⊕ 11 = 0, where ⊕ — bitwise exclusive or. Pair (1, 3) is valid since a_1 = 6 → 3, a_2 = 7 → 13, a_3 = 14 → 14 and 3 ⊕ 13 ⊕ 14 = 0. In the second example pairs (1, 2), (2, 3), (3, 4) and (1, 4) are valid.
N = int(input()) a = list(map(int, input().split())) cnt = [([0] * (N + 1)) for i in range(2)] b = [0] * N for i in range(N): b[i] = bin(a[i]).count("1") num = 0 suf = 0 cnt[0][N] = 1 for i in range(N - 1, -1, -1): s = 0 ma = 0 add = 0 for j in range(i, min(N, i + 65)): s += b[j] ma = max(ma, b[j]) if ma * 2 > s and s % 2 == 0: add -= 1 suf += b[i] add += cnt[suf & 1][i + 1] num += add cnt[0][i] = cnt[0][i + 1] cnt[1][i] = cnt[1][i + 1] if suf & 1: cnt[1][i] += 1 else: cnt[0][i] += 1 print(num)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has a sequence a consisting of n integers a_1, a_2, ..., a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (... 00000000110_2) into 3 (... 00000000011_2), 12 (... 000000001100_2), 1026 (... 10000000010_2) and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence. Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with [bitwise exclusive or](https://en.wikipedia.org/wiki/Exclusive_or) of all elements equal to 0. For the given sequence a_1, a_2, …, a_n Vasya'd like to calculate number of integer pairs (l, r) such that 1 ≤ l ≤ r ≤ n and sequence a_l, a_{l + 1}, ..., a_r is good. Input The first line contains a single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — length of the sequence. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{18}) — the sequence a. Output Print one integer — the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and the sequence a_l, a_{l + 1}, ..., a_r is good. Examples Input 3 6 7 14 Output 2 Input 4 1 2 1 16 Output 4 Note In the first example pairs (2, 3) and (1, 3) are valid. Pair (2, 3) is valid since a_2 = 7 → 11, a_3 = 14 → 11 and 11 ⊕ 11 = 0, where ⊕ — bitwise exclusive or. Pair (1, 3) is valid since a_1 = 6 → 3, a_2 = 7 → 13, a_3 = 14 → 14 and 3 ⊕ 13 ⊕ 14 = 0. In the second example pairs (1, 2), (2, 3), (3, 4) and (1, 4) are valid.
n = int(input()) a = [int(x) for x in input().split()] c = [bin(x).count("1") for x in a] d = [(0) for i in range(n + 1)] even = 1 for i in range(1, n + 1): d[i] = d[i - 1] + c[i - 1] if d[i] % 2 == 0: even += 1 odd = n + 1 - even temp = even * (even - 1) // 2 + odd * (odd - 1) // 2 for i in range(n): C = c[i] if C == 1: continue if C == 2: temp -= 1 continue for a in range(C): if i - a < 0: break if d[i] - d[i - a] >= C: break for b in range(C - a): if i + b > n - 1: break if d[i + b + 1] - d[i - a] < 2 * C: if (d[i + b + 1] - d[i - a]) % 2 == 0: temp -= 1 else: break print(temp)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN 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 IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has a sequence a consisting of n integers a_1, a_2, ..., a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (... 00000000110_2) into 3 (... 00000000011_2), 12 (... 000000001100_2), 1026 (... 10000000010_2) and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence. Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with [bitwise exclusive or](https://en.wikipedia.org/wiki/Exclusive_or) of all elements equal to 0. For the given sequence a_1, a_2, …, a_n Vasya'd like to calculate number of integer pairs (l, r) such that 1 ≤ l ≤ r ≤ n and sequence a_l, a_{l + 1}, ..., a_r is good. Input The first line contains a single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — length of the sequence. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{18}) — the sequence a. Output Print one integer — the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and the sequence a_l, a_{l + 1}, ..., a_r is good. Examples Input 3 6 7 14 Output 2 Input 4 1 2 1 16 Output 4 Note In the first example pairs (2, 3) and (1, 3) are valid. Pair (2, 3) is valid since a_2 = 7 → 11, a_3 = 14 → 11 and 11 ⊕ 11 = 0, where ⊕ — bitwise exclusive or. Pair (1, 3) is valid since a_1 = 6 → 3, a_2 = 7 → 13, a_3 = 14 → 14 and 3 ⊕ 13 ⊕ 14 = 0. In the second example pairs (1, 2), (2, 3), (3, 4) and (1, 4) are valid.
import sys input = sys.stdin.readline n = int(input()) A = [int(x) for x in input().split()] B = [bin(a).count("1") for a in A] Bcumsum = [0] for b in B: Bcumsum.append((Bcumsum[-1] + b) % 2) count0 = [0] * (n + 1) for i in reversed(range(n)): count0[i] = count0[i + 1] + (Bcumsum[i + 1] == 0) numb = 65 sol = 0 for i in range(n): mini = B[i] maxi = mini for j in range(i + 1, min(i + 1 + numb, n)): if mini <= B[j] <= maxi: if (mini + B[j]) % 2 == 0: mini = 0 else: mini = 1 elif B[j] < mini: mini = mini - B[j] else: mini = B[j] - maxi maxi = B[j] + maxi if mini == 0: sol += 1 if i + 1 + numb < n: count = count0[i + 1 + numb] if (mini + Bcumsum[i + 1 + numb]) % 2 == 1: count = n - (i + 1 + numb) - count sol += count print(sol)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a sequence a consisting of n integers a_1, a_2, ..., a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (... 00000000110_2) into 3 (... 00000000011_2), 12 (... 000000001100_2), 1026 (... 10000000010_2) and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence. Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with [bitwise exclusive or](https://en.wikipedia.org/wiki/Exclusive_or) of all elements equal to 0. For the given sequence a_1, a_2, …, a_n Vasya'd like to calculate number of integer pairs (l, r) such that 1 ≤ l ≤ r ≤ n and sequence a_l, a_{l + 1}, ..., a_r is good. Input The first line contains a single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — length of the sequence. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{18}) — the sequence a. Output Print one integer — the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and the sequence a_l, a_{l + 1}, ..., a_r is good. Examples Input 3 6 7 14 Output 2 Input 4 1 2 1 16 Output 4 Note In the first example pairs (2, 3) and (1, 3) are valid. Pair (2, 3) is valid since a_2 = 7 → 11, a_3 = 14 → 11 and 11 ⊕ 11 = 0, where ⊕ — bitwise exclusive or. Pair (1, 3) is valid since a_1 = 6 → 3, a_2 = 7 → 13, a_3 = 14 → 14 and 3 ⊕ 13 ⊕ 14 = 0. In the second example pairs (1, 2), (2, 3), (3, 4) and (1, 4) are valid.
n = int(input()) a = list(map(int, input().split())) for i in range(n): x = bin(a[i]) a[i] = x.count("1") even = 0 odd = 0 cnt = 0 sofar = 0 for i in range(n): sofar += a[i] if sofar % 2: odd += 1 else: even += 1 even += 1 sofar = 0 for i in range(n): if sofar % 2: odd -= 1 cnt += odd else: even -= 1 cnt += even sofar += a[i] for i in range(n): sum1 = 0 max1 = a[i] for j in range(i, min(i + 100, n)): sum1 += a[j] max1 = max(max1, a[j]) if sum1 % 2 == 0 and sum1 < 2 * max1: cnt -= 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Sam has been teaching Jon the Game of Stones to sharpen his mind and help him devise a strategy to fight the white walkers. The rules of this game are quite simple: The game starts with n piles of stones indexed from 1 to n. The i-th pile contains s_{i} stones. The players make their moves alternatively. A move is considered as removal of some number of stones from a pile. Removal of 0 stones does not count as a move. The player who is unable to make a move loses. Now Jon believes that he is ready for battle, but Sam does not think so. To prove his argument, Sam suggested that they play a modified version of the game. In this modified version, no move can be made more than once on a pile. For example, if 4 stones are removed from a pile, 4 stones cannot be removed from that pile again. Sam sets up the game and makes the first move. Jon believes that Sam is just trying to prevent him from going to battle. Jon wants to know if he can win if both play optimally. -----Input----- First line consists of a single integer n (1 ≤ n ≤ 10^6) — the number of piles. Each of next n lines contains an integer s_{i} (1 ≤ s_{i} ≤ 60) — the number of stones in i-th pile. -----Output----- Print a single line containing "YES" (without quotes) if Jon wins, otherwise print "NO" (without quotes) -----Examples----- Input 1 5 Output NO Input 2 1 2 Output YES -----Note----- In the first case, Sam removes all the stones and Jon loses. In second case, the following moves are possible by Sam: $\{1,2 \} \rightarrow \{0,2 \}, \{1,2 \} \rightarrow \{1,0 \}, \{1,2 \} \rightarrow \{1,1 \}$ In each of these cases, last move can be made by Jon to win the game as follows: $\{0,2 \} \rightarrow \{0,0 \}, \{1,0 \} \rightarrow \{0,0 \}, \{1,1 \} \rightarrow \{0,1 \}$
memo = {} def get_reachable_states(k, max_allowed): states = [] for i in range(1, min(k, max_allowed) + 1): new_k = k - i states.append((new_k, i - 1)) return states def Grundy(k, max_allowed): if k == 0: return 0 if (k, max_allowed) in memo: return memo[k, max_allowed] reachable_states = get_reachable_states(k, max_allowed) if len(reachable_states) == 0: memo[k, max_allowed] = 0 return 0 s = set() for state in reachable_states: s.add(Grundy(*state)) i = 0 while i in s: i += 1 memo[k, max_allowed] = i return memo[k, max_allowed] n = int(input()) GrundyTotal = 0 for i in range(n): k = int(input()) GrundyTotal ^= Grundy(k, k) print("YES" if GrundyTotal == 0 else "NO")
ASSIGN VAR DICT FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING STRING
Sam has been teaching Jon the Game of Stones to sharpen his mind and help him devise a strategy to fight the white walkers. The rules of this game are quite simple: The game starts with n piles of stones indexed from 1 to n. The i-th pile contains s_{i} stones. The players make their moves alternatively. A move is considered as removal of some number of stones from a pile. Removal of 0 stones does not count as a move. The player who is unable to make a move loses. Now Jon believes that he is ready for battle, but Sam does not think so. To prove his argument, Sam suggested that they play a modified version of the game. In this modified version, no move can be made more than once on a pile. For example, if 4 stones are removed from a pile, 4 stones cannot be removed from that pile again. Sam sets up the game and makes the first move. Jon believes that Sam is just trying to prevent him from going to battle. Jon wants to know if he can win if both play optimally. -----Input----- First line consists of a single integer n (1 ≤ n ≤ 10^6) — the number of piles. Each of next n lines contains an integer s_{i} (1 ≤ s_{i} ≤ 60) — the number of stones in i-th pile. -----Output----- Print a single line containing "YES" (without quotes) if Jon wins, otherwise print "NO" (without quotes) -----Examples----- Input 1 5 Output NO Input 2 1 2 Output YES -----Note----- In the first case, Sam removes all the stones and Jon loses. In second case, the following moves are possible by Sam: $\{1,2 \} \rightarrow \{0,2 \}, \{1,2 \} \rightarrow \{1,0 \}, \{1,2 \} \rightarrow \{1,1 \}$ In each of these cases, last move can be made by Jon to win the game as follows: $\{0,2 \} \rightarrow \{0,0 \}, \{1,0 \} \rightarrow \{0,0 \}, \{1,1 \} \rightarrow \{0,1 \}$
s = 0 for t in range(int(input())): s ^= int((8 * int(input()) + 1) ** 0.5 - 1) // 2 print(["YES", "NO"][s > 0])
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST STRING STRING VAR NUMBER
Sam has been teaching Jon the Game of Stones to sharpen his mind and help him devise a strategy to fight the white walkers. The rules of this game are quite simple: The game starts with n piles of stones indexed from 1 to n. The i-th pile contains s_{i} stones. The players make their moves alternatively. A move is considered as removal of some number of stones from a pile. Removal of 0 stones does not count as a move. The player who is unable to make a move loses. Now Jon believes that he is ready for battle, but Sam does not think so. To prove his argument, Sam suggested that they play a modified version of the game. In this modified version, no move can be made more than once on a pile. For example, if 4 stones are removed from a pile, 4 stones cannot be removed from that pile again. Sam sets up the game and makes the first move. Jon believes that Sam is just trying to prevent him from going to battle. Jon wants to know if he can win if both play optimally. -----Input----- First line consists of a single integer n (1 ≤ n ≤ 10^6) — the number of piles. Each of next n lines contains an integer s_{i} (1 ≤ s_{i} ≤ 60) — the number of stones in i-th pile. -----Output----- Print a single line containing "YES" (without quotes) if Jon wins, otherwise print "NO" (without quotes) -----Examples----- Input 1 5 Output NO Input 2 1 2 Output YES -----Note----- In the first case, Sam removes all the stones and Jon loses. In second case, the following moves are possible by Sam: $\{1,2 \} \rightarrow \{0,2 \}, \{1,2 \} \rightarrow \{1,0 \}, \{1,2 \} \rightarrow \{1,1 \}$ In each of these cases, last move can be made by Jon to win the game as follows: $\{0,2 \} \rightarrow \{0,0 \}, \{1,0 \} \rightarrow \{0,0 \}, \{1,1 \} \rightarrow \{0,1 \}$
n = int(input()) arr = [int(input()) for i in range(n)] b = [(0) for i in range(n)] s = 0 for i in range(n): j = int((arr[i] << 1) ** 0.5) if j * (j + 1) > arr[i] << 1: j -= 1 s ^= j if s != 0: print("NO") else: print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Sam has been teaching Jon the Game of Stones to sharpen his mind and help him devise a strategy to fight the white walkers. The rules of this game are quite simple: The game starts with n piles of stones indexed from 1 to n. The i-th pile contains s_{i} stones. The players make their moves alternatively. A move is considered as removal of some number of stones from a pile. Removal of 0 stones does not count as a move. The player who is unable to make a move loses. Now Jon believes that he is ready for battle, but Sam does not think so. To prove his argument, Sam suggested that they play a modified version of the game. In this modified version, no move can be made more than once on a pile. For example, if 4 stones are removed from a pile, 4 stones cannot be removed from that pile again. Sam sets up the game and makes the first move. Jon believes that Sam is just trying to prevent him from going to battle. Jon wants to know if he can win if both play optimally. -----Input----- First line consists of a single integer n (1 ≤ n ≤ 10^6) — the number of piles. Each of next n lines contains an integer s_{i} (1 ≤ s_{i} ≤ 60) — the number of stones in i-th pile. -----Output----- Print a single line containing "YES" (without quotes) if Jon wins, otherwise print "NO" (without quotes) -----Examples----- Input 1 5 Output NO Input 2 1 2 Output YES -----Note----- In the first case, Sam removes all the stones and Jon loses. In second case, the following moves are possible by Sam: $\{1,2 \} \rightarrow \{0,2 \}, \{1,2 \} \rightarrow \{1,0 \}, \{1,2 \} \rightarrow \{1,1 \}$ In each of these cases, last move can be made by Jon to win the game as follows: $\{0,2 \} \rightarrow \{0,0 \}, \{1,0 \} \rightarrow \{0,0 \}, \{1,1 \} \rightarrow \{0,1 \}$
import sys input = sys.stdin.readline def large(x): for i in range(10, -1, -1): if i * (i + 1) // 2 <= x: return i x = int(input()) l = [] for i in range(x): l.append(int(input())) a = [large(i) for i in range(0, 61)] lol = 0 for i in l: i = a[i] lol = lol ^ i if lol == 0: print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Sam has been teaching Jon the Game of Stones to sharpen his mind and help him devise a strategy to fight the white walkers. The rules of this game are quite simple: The game starts with n piles of stones indexed from 1 to n. The i-th pile contains s_{i} stones. The players make their moves alternatively. A move is considered as removal of some number of stones from a pile. Removal of 0 stones does not count as a move. The player who is unable to make a move loses. Now Jon believes that he is ready for battle, but Sam does not think so. To prove his argument, Sam suggested that they play a modified version of the game. In this modified version, no move can be made more than once on a pile. For example, if 4 stones are removed from a pile, 4 stones cannot be removed from that pile again. Sam sets up the game and makes the first move. Jon believes that Sam is just trying to prevent him from going to battle. Jon wants to know if he can win if both play optimally. -----Input----- First line consists of a single integer n (1 ≤ n ≤ 10^6) — the number of piles. Each of next n lines contains an integer s_{i} (1 ≤ s_{i} ≤ 60) — the number of stones in i-th pile. -----Output----- Print a single line containing "YES" (without quotes) if Jon wins, otherwise print "NO" (without quotes) -----Examples----- Input 1 5 Output NO Input 2 1 2 Output YES -----Note----- In the first case, Sam removes all the stones and Jon loses. In second case, the following moves are possible by Sam: $\{1,2 \} \rightarrow \{0,2 \}, \{1,2 \} \rightarrow \{1,0 \}, \{1,2 \} \rightarrow \{1,1 \}$ In each of these cases, last move can be made by Jon to win the game as follows: $\{0,2 \} \rightarrow \{0,0 \}, \{1,0 \} \rightarrow \{0,0 \}, \{1,1 \} \rightarrow \{0,1 \}$
import sys input = sys.stdin.readline dp = {} def cal(x, y): x, y = int(x), int(y) a = set() if (x, y) in dp: return dp[x, y] for i in range(min(int(60), x)): p = y & 1 << i if p > 0: continue r = cal(x - i - 1, y | 1 << i) a.add(r) cn = 0 for i in range(len(a) + 2): if cn in a: cn += 1 else: break dp[x, y] = cn return cn n = int(input()) ans = 0 for i in range(n): x = int(input()) ans ^= cal(x, 0) if ans == 0: print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number $6$ $(\dots 00000000110_2)$ into $3$ $(\dots 00000000011_2)$, $12$ $(\dots 000000001100_2)$, $1026$ $(\dots 10000000010_2)$ and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence. Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with bitwise exclusive or of all elements equal to $0$. For the given sequence $a_1, a_2, \ldots, a_n$ Vasya'd like to calculate number of integer pairs $(l, r)$ such that $1 \le l \le r \le n$ and sequence $a_l, a_{l + 1}, \dots, a_r$ is good. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$) — length of the sequence. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{18}$) — the sequence $a$. -----Output----- Print one integer — the number of pairs $(l, r)$ such that $1 \le l \le r \le n$ and the sequence $a_l, a_{l + 1}, \dots, a_r$ is good. -----Examples----- Input 3 6 7 14 Output 2 Input 4 1 2 1 16 Output 4 -----Note----- In the first example pairs $(2, 3)$ and $(1, 3)$ are valid. Pair $(2, 3)$ is valid since $a_2 = 7 \rightarrow 11$, $a_3 = 14 \rightarrow 11$ and $11 \oplus 11 = 0$, where $\oplus$ — bitwise exclusive or. Pair $(1, 3)$ is valid since $a_1 = 6 \rightarrow 3$, $a_2 = 7 \rightarrow 13$, $a_3 = 14 \rightarrow 14$ and $3 \oplus 13 \oplus 14 = 0$. In the second example pairs $(1, 2)$, $(2, 3)$, $(3, 4)$ and $(1, 4)$ are valid.
import sys def rint(): return list(map(int, sys.stdin.readline().split())) def get_num1(i): cnt = 0 while i: if i % 2: cnt += 1 i //= 2 return cnt n = int(input()) a = list(rint()) b = [get_num1(aa) for aa in a] ans = 0 S0 = [0] * n S0[0] = b[0] % 2 for i in range(1, n): S0[i] = (S0[i - 1] + b[i]) % 2 even_cnt = S0.count(0) ans = even_cnt for i in range(1, n): if b[i - 1] % 2: even_cnt = n - i - even_cnt else: even_cnt -= 1 ans += even_cnt for i in range(n): max_value = 0 sum_value = 0 for j in range(1, 62): if i + j > n: break sum_value += b[i + j - 1] max_value = max(max_value, b[i + j - 1]) if 2 * max_value > sum_value and sum_value % 2 == 0: ans -= 1 print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number $6$ $(\dots 00000000110_2)$ into $3$ $(\dots 00000000011_2)$, $12$ $(\dots 000000001100_2)$, $1026$ $(\dots 10000000010_2)$ and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence. Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with bitwise exclusive or of all elements equal to $0$. For the given sequence $a_1, a_2, \ldots, a_n$ Vasya'd like to calculate number of integer pairs $(l, r)$ such that $1 \le l \le r \le n$ and sequence $a_l, a_{l + 1}, \dots, a_r$ is good. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$) — length of the sequence. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{18}$) — the sequence $a$. -----Output----- Print one integer — the number of pairs $(l, r)$ such that $1 \le l \le r \le n$ and the sequence $a_l, a_{l + 1}, \dots, a_r$ is good. -----Examples----- Input 3 6 7 14 Output 2 Input 4 1 2 1 16 Output 4 -----Note----- In the first example pairs $(2, 3)$ and $(1, 3)$ are valid. Pair $(2, 3)$ is valid since $a_2 = 7 \rightarrow 11$, $a_3 = 14 \rightarrow 11$ and $11 \oplus 11 = 0$, where $\oplus$ — bitwise exclusive or. Pair $(1, 3)$ is valid since $a_1 = 6 \rightarrow 3$, $a_2 = 7 \rightarrow 13$, $a_3 = 14 \rightarrow 14$ and $3 \oplus 13 \oplus 14 = 0$. In the second example pairs $(1, 2)$, $(2, 3)$, $(3, 4)$ and $(1, 4)$ are valid.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) b = [0] * n for i in range(n): ai = a[i] for j in range(61): if ai >> j & 1: b[i] += 1 cnt = [([0] * (n + 1)) for i in range(2)] cnt[0][n] = 1 res = 0 suf = 0 for i in range(n)[::-1]: s = 0 MAX = 0 add = 0 for j in range(i, n): if j - i >= 65: break s += b[j] MAX = max(MAX, b[j]) if MAX > s - MAX and s % 2 == 0: add -= 1 suf += b[i] add += cnt[suf & 1][i + 1] res += add cnt[0][i] = cnt[0][i + 1] cnt[1][i] = cnt[1][i + 1] if suf & 1: cnt[1][i] += 1 else: cnt[0][i] += 1 print(res)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number $6$ $(\dots 00000000110_2)$ into $3$ $(\dots 00000000011_2)$, $12$ $(\dots 000000001100_2)$, $1026$ $(\dots 10000000010_2)$ and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence. Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with bitwise exclusive or of all elements equal to $0$. For the given sequence $a_1, a_2, \ldots, a_n$ Vasya'd like to calculate number of integer pairs $(l, r)$ such that $1 \le l \le r \le n$ and sequence $a_l, a_{l + 1}, \dots, a_r$ is good. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$) — length of the sequence. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{18}$) — the sequence $a$. -----Output----- Print one integer — the number of pairs $(l, r)$ such that $1 \le l \le r \le n$ and the sequence $a_l, a_{l + 1}, \dots, a_r$ is good. -----Examples----- Input 3 6 7 14 Output 2 Input 4 1 2 1 16 Output 4 -----Note----- In the first example pairs $(2, 3)$ and $(1, 3)$ are valid. Pair $(2, 3)$ is valid since $a_2 = 7 \rightarrow 11$, $a_3 = 14 \rightarrow 11$ and $11 \oplus 11 = 0$, where $\oplus$ — bitwise exclusive or. Pair $(1, 3)$ is valid since $a_1 = 6 \rightarrow 3$, $a_2 = 7 \rightarrow 13$, $a_3 = 14 \rightarrow 14$ and $3 \oplus 13 \oplus 14 = 0$. In the second example pairs $(1, 2)$, $(2, 3)$, $(3, 4)$ and $(1, 4)$ are valid.
N = int(input()) a = list(map(int, input().split())) cnt = [([0] * (N + 1)) for i in range(2)] b = [0] * N for i in range(N): b[i] = bin(a[i]).count("1") num = 0 suf = 0 cnt[0][N] = 1 for i in range(N - 1, -1, -1): s = 0 ma = 0 suf += b[i] num += cnt[suf & 1][i + 1] for j in range(i, min(N, i + 65)): s += b[j] ma = max(ma, b[j]) if ma * 2 > s and s % 2 == 0: num -= 1 cnt[0][i] = cnt[0][i + 1] cnt[1][i] = cnt[1][i + 1] if suf & 1: cnt[1][i] += 1 else: cnt[0][i] += 1 print(num)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers. There are $k$ boxes numbered from $1$ to $k$. The $i$-th box contains $n_i$ integer numbers. The integers can be negative. All of the integers are distinct. Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, $k$ integers in total. Then he will insert the chosen numbers — one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box. Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be? -----Input----- The first line contains a single integer $k$ ($1 \leq k \leq 15$), the number of boxes. The $i$-th of the next $k$ lines first contains a single integer $n_i$ ($1 \leq n_i \leq 5\,000$), the number of integers in box $i$. Then the same line contains $n_i$ integers $a_{i,1}, \ldots, a_{i,n_i}$ ($|a_{i,j}| \leq 10^9$), the integers in the $i$-th box. It is guaranteed that all $a_{i,j}$ are distinct. -----Output----- If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output $k$ lines. The $i$-th of these lines should contain two integers $c_i$ and $p_i$. This means that Ujan should pick the integer $c_i$ from the $i$-th box and place it in the $p_i$-th box afterwards. If there are multiple solutions, output any of those. You can print each letter in any case (upper or lower). -----Examples----- Input 4 3 1 7 4 2 3 2 2 8 5 1 10 Output Yes 7 2 2 3 5 1 10 4 Input 2 2 3 -2 2 -1 5 Output No Input 2 2 -10 10 2 0 -20 Output Yes -10 2 -20 1 -----Note----- In the first sample, Ujan can put the number $7$ in the $2$nd box, the number $2$ in the $3$rd box, the number $5$ in the $1$st box and keep the number $10$ in the same $4$th box. Then the boxes will contain numbers $\{1,5,4\}$, $\{3, 7\}$, $\{8,2\}$ and $\{10\}$. The sum in each box then is equal to $10$. In the second sample, it is not possible to pick and redistribute the numbers in the required way. In the third sample, one can swap the numbers $-20$ and $-10$, making the sum in each box equal to $-10$.
from itertools import accumulate from sys import stdin, stdout def main(): k = int(stdin.readline()) a = [tuple(map(int, stdin.readline().split()[1:])) for _ in range(k)] a2ij = {aij: (i, j) for i, ai in enumerate(a) for j, aij in enumerate(ai)} plena = [0] + list(accumulate(map(len, a))) suma = tuple(map(sum, a)) totala = sum(suma) if totala % k != 0: stdout.write("No\n") else: needle = totala // k mask2i2cp = compute_mask2i2cp(a, a2ij, needle, plena, suma) dp = compute_previous_mask(mask2i2cp) output(dp, mask2i2cp) def compute_mask2i2cp(a, a2ij, needle, plena, suma): used = [False] * plena[-1] number_of_masks = 1 << len(a) mask2i2cp = [-1] * number_of_masks for i, ai in enumerate(a): for j, aij in enumerate(ai): if not used[plena[i] + j]: mask, i2cp = compute_mask_i2cp(a2ij, aij, i, j, needle, suma) if i2cp != -1: mask2i2cp[mask] = i2cp for cp in i2cp: if cp != -1: c, p = cp ii, jj = a2ij[c] used[plena[ii] + jj] = True return mask2i2cp def output(dp, mask2i2cp): mask = len(mask2i2cp) - 1 if dp[mask] == -1: stdout.write("No\n") else: answer = [-1] * len(mask2i2cp[dp[mask]]) while mask > 0: current_mask = dp[mask] for i, cp in enumerate(mask2i2cp[current_mask]): if 1 == current_mask >> i & 1: c, p = cp answer[i] = c, p mask ^= current_mask stdout.write("Yes\n" + "\n".join("{} {}".format(c, 1 + p) for c, p in answer)) def compute_mask_i2cp(a2ij, aij, i, j, needle, suma): i2cp = [-1] * len(suma) mask = 0 current_a = aij current_i = i try: while True: next_a = needle - (suma[current_i] - current_a) next_i, next_j = a2ij[next_a] if mask >> next_i & 1 == 1: return mask, -1 mask |= 1 << next_i i2cp[next_i] = next_a, current_i if next_i == i: if next_j == j: return mask, i2cp return mask, -1 if next_i == current_i: return mask, -1 current_a = next_a current_i = next_i except KeyError: return mask, -1 def compute_previous_mask(mask2cp): number_of_masks = len(mask2cp) dp = [-1] * number_of_masks dp[0] = 0 for mask, cp in enumerate(mask2cp): if cp != -1: complement_mask = number_of_masks - 1 & ~mask previous_mask = complement_mask while previous_mask > 0: if dp[previous_mask] != -1 and dp[previous_mask | mask] == -1: dp[previous_mask | mask] = mask previous_mask = previous_mask - 1 & complement_mask if dp[mask] == -1: dp[mask] = mask return dp main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING FUNC_CALL STRING VAR BIN_OP NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR IF VAR VAR RETURN VAR VAR RETURN VAR NUMBER IF VAR VAR RETURN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers. There are $k$ boxes numbered from $1$ to $k$. The $i$-th box contains $n_i$ integer numbers. The integers can be negative. All of the integers are distinct. Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, $k$ integers in total. Then he will insert the chosen numbers — one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box. Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be? -----Input----- The first line contains a single integer $k$ ($1 \leq k \leq 15$), the number of boxes. The $i$-th of the next $k$ lines first contains a single integer $n_i$ ($1 \leq n_i \leq 5\,000$), the number of integers in box $i$. Then the same line contains $n_i$ integers $a_{i,1}, \ldots, a_{i,n_i}$ ($|a_{i,j}| \leq 10^9$), the integers in the $i$-th box. It is guaranteed that all $a_{i,j}$ are distinct. -----Output----- If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output $k$ lines. The $i$-th of these lines should contain two integers $c_i$ and $p_i$. This means that Ujan should pick the integer $c_i$ from the $i$-th box and place it in the $p_i$-th box afterwards. If there are multiple solutions, output any of those. You can print each letter in any case (upper or lower). -----Examples----- Input 4 3 1 7 4 2 3 2 2 8 5 1 10 Output Yes 7 2 2 3 5 1 10 4 Input 2 2 3 -2 2 -1 5 Output No Input 2 2 -10 10 2 0 -20 Output Yes -10 2 -20 1 -----Note----- In the first sample, Ujan can put the number $7$ in the $2$nd box, the number $2$ in the $3$rd box, the number $5$ in the $1$st box and keep the number $10$ in the same $4$th box. Then the boxes will contain numbers $\{1,5,4\}$, $\{3, 7\}$, $\{8,2\}$ and $\{10\}$. The sum in each box then is equal to $10$. In the second sample, it is not possible to pick and redistribute the numbers in the required way. In the third sample, one can swap the numbers $-20$ and $-10$, making the sum in each box equal to $-10$.
def main(): k = int(input()) n = [] a = [] for i in range(k): line = [int(x) for x in input().split()] ni = line[0] ai = [] n.append(ni) a.append(ai) for j in range(ni): ai.append(line[1 + j]) answer, c, p = solve(k, n, a) if answer: print("Yes") for i in range(k): print(c[i], p[i] + 1) else: print("No") def solve(k, n, a): asum, sums = calc_sums(k, n, a) if asum % k != 0: return False, None, None tsum = asum / k num_map = build_num_map(k, n, a) masks = [None] * (1 << k) answer = [False] * (1 << k) left = [0] * (1 << k) right = [0] * (1 << k) for i in range(k): for j in range(n[i]): found, mask, path = find_cycle( i, j, i, j, k, n, a, sums, tsum, num_map, 0, dict() ) if found: answer[mask] = True masks[mask] = path for mask_right in range(1 << k): if not masks[mask_right]: continue zeroes_count = 0 for u in range(k): if 1 << u > mask_right: break if mask_right & 1 << u == 0: zeroes_count += 1 for mask_mask in range(1 << zeroes_count): mask_left = 0 c = 0 for u in range(k): if 1 << u > mask_right: break if mask_right & 1 << u == 0: if mask_mask & 1 << c != 0: mask_left = mask_left | 1 << u c += 1 joint_mask = mask_left | mask_right if answer[mask_left] and not answer[joint_mask]: answer[joint_mask] = True left[joint_mask] = mask_left right[joint_mask] = mask_right if joint_mask == (1 << k) - 1: return build_answer(k, masks, left, right) if answer[(1 << k) - 1]: return build_answer(k, masks, left, right) return False, None, None def build_answer(k, masks, left, right): c = [-1] * k p = [-1] * k pos = (1 << k) - 1 while not masks[pos]: for key, val in masks[right[pos]].items(): c[key] = val[0] p[key] = val[1] pos = left[pos] for key, val in masks[pos].items(): c[key] = val[0] p[key] = val[1] return True, c, p def build_num_map(k, n, a): result = dict() for i in range(k): for j in range(n[i]): result[a[i][j]] = i, j return result def find_cycle(i_origin, j_origin, i, j, k, n, a, sums, tsum, num_map, mask, path): if mask & 1 << i != 0: if i == i_origin and j == j_origin: return True, mask, path else: return False, None, None mask = mask | 1 << i a_needed = tsum - (sums[i] - a[i][j]) if a_needed not in num_map: return False, None, None i_next, j_next = num_map[a_needed] path[i_next] = a[i_next][j_next], i return find_cycle( i_origin, j_origin, i_next, j_next, k, n, a, sums, tsum, num_map, mask, path ) def calc_sums(k, n, a): sums = [0] * k for i in range(k): for j in range(n[i]): sums[i] = sums[i] + a[i][j] asum = 0 for i in range(k): asum = asum + sums[i] return asum, sums main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER NONE NONE ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER NONE NONE FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER WHILE VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER VAR VAR RETURN NUMBER NONE NONE ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER NONE NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers. There are $k$ boxes numbered from $1$ to $k$. The $i$-th box contains $n_i$ integer numbers. The integers can be negative. All of the integers are distinct. Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, $k$ integers in total. Then he will insert the chosen numbers — one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box. Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be? -----Input----- The first line contains a single integer $k$ ($1 \leq k \leq 15$), the number of boxes. The $i$-th of the next $k$ lines first contains a single integer $n_i$ ($1 \leq n_i \leq 5\,000$), the number of integers in box $i$. Then the same line contains $n_i$ integers $a_{i,1}, \ldots, a_{i,n_i}$ ($|a_{i,j}| \leq 10^9$), the integers in the $i$-th box. It is guaranteed that all $a_{i,j}$ are distinct. -----Output----- If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output $k$ lines. The $i$-th of these lines should contain two integers $c_i$ and $p_i$. This means that Ujan should pick the integer $c_i$ from the $i$-th box and place it in the $p_i$-th box afterwards. If there are multiple solutions, output any of those. You can print each letter in any case (upper or lower). -----Examples----- Input 4 3 1 7 4 2 3 2 2 8 5 1 10 Output Yes 7 2 2 3 5 1 10 4 Input 2 2 3 -2 2 -1 5 Output No Input 2 2 -10 10 2 0 -20 Output Yes -10 2 -20 1 -----Note----- In the first sample, Ujan can put the number $7$ in the $2$nd box, the number $2$ in the $3$rd box, the number $5$ in the $1$st box and keep the number $10$ in the same $4$th box. Then the boxes will contain numbers $\{1,5,4\}$, $\{3, 7\}$, $\{8,2\}$ and $\{10\}$. The sum in each box then is equal to $10$. In the second sample, it is not possible to pick and redistribute the numbers in the required way. In the third sample, one can swap the numbers $-20$ and $-10$, making the sum in each box equal to $-10$.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ k = int(input()) d = {} aa = [] sa = [] for i in range(k): ni, *a = map(int, input().split()) for ai in a: d[ai] = i aa.append(a) sa.append(sum(a)) s = sum(sa) if s % k != 0: print("No") exit() s //= k def calc_next(i, aij): bij = s - sa[i] + aij if bij not in d: return -1, bij else: return d[bij], bij def loop_to_num(loop): ret = 0 for i in reversed(range(k)): ret <<= 1 ret += loop[i] return ret loop_dict = {} used = set() for i in range(k): for aij in aa[i]: if aij in used: continue loop = [0] * k num = [float("Inf")] * k start_i = i start_aij = aij j = i loop[j] = 1 num[j] = aij used.add(aij) exist = False for _ in range(100): j, aij = calc_next(j, aij) if j == -1: break if loop[j] == 0: loop[j] = 1 num[j] = aij else: if j == start_i and aij == start_aij: exist = True break if exist: m = loop_to_num(loop) loop_dict[m] = tuple(num) for numi in num: if numi != float("inf"): used.add(numi) mask = 1 << k for state in range(1, mask): if state in loop_dict: continue j = state - 1 & state while j: i = state ^ j if i in loop_dict and j in loop_dict: tp = tuple(min(loop_dict[i][l], loop_dict[j][l]) for l in range(k)) loop_dict[state] = tp break j = j - 1 & state if mask - 1 not in loop_dict: print("No") else: print("Yes") t = loop_dict[mask - 1] ns = [(sa[i] - t[i]) for i in range(k)] need = [(s - ns[i]) for i in range(k)] for i in range(k): print(t[i], need.index(t[i]) + 1)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers. There are $k$ boxes numbered from $1$ to $k$. The $i$-th box contains $n_i$ integer numbers. The integers can be negative. All of the integers are distinct. Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, $k$ integers in total. Then he will insert the chosen numbers — one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box. Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be? -----Input----- The first line contains a single integer $k$ ($1 \leq k \leq 15$), the number of boxes. The $i$-th of the next $k$ lines first contains a single integer $n_i$ ($1 \leq n_i \leq 5\,000$), the number of integers in box $i$. Then the same line contains $n_i$ integers $a_{i,1}, \ldots, a_{i,n_i}$ ($|a_{i,j}| \leq 10^9$), the integers in the $i$-th box. It is guaranteed that all $a_{i,j}$ are distinct. -----Output----- If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output $k$ lines. The $i$-th of these lines should contain two integers $c_i$ and $p_i$. This means that Ujan should pick the integer $c_i$ from the $i$-th box and place it in the $p_i$-th box afterwards. If there are multiple solutions, output any of those. You can print each letter in any case (upper or lower). -----Examples----- Input 4 3 1 7 4 2 3 2 2 8 5 1 10 Output Yes 7 2 2 3 5 1 10 4 Input 2 2 3 -2 2 -1 5 Output No Input 2 2 -10 10 2 0 -20 Output Yes -10 2 -20 1 -----Note----- In the first sample, Ujan can put the number $7$ in the $2$nd box, the number $2$ in the $3$rd box, the number $5$ in the $1$st box and keep the number $10$ in the same $4$th box. Then the boxes will contain numbers $\{1,5,4\}$, $\{3, 7\}$, $\{8,2\}$ and $\{10\}$. The sum in each box then is equal to $10$. In the second sample, it is not possible to pick and redistribute the numbers in the required way. In the third sample, one can swap the numbers $-20$ and $-10$, making the sum in each box equal to $-10$.
import sys n = sys.stdin.readline() n = int(n) def get_graph(n): graph = [] for _ in range(n): entries = list(map(lambda x: int(x), sys.stdin.readline().split(" ")[1:])) graph.append(entries) return graph def chain(target, buckets, reverse_bucket, sum_bucket, bucket_num, val): mask = 2**bucket_num mem = [] buckets_seen = set({bucket_num}) og_bucket = bucket_num og_val = val for _ in range(len(buckets)): rem = target - sum_bucket[bucket_num] + val if rem not in reverse_bucket: return None, [] new_bucket = reverse_bucket[rem] if new_bucket == og_bucket and rem != og_val: return None, [] elif new_bucket == og_bucket and rem == og_val: mem.append((rem, bucket_num)) return mask | 2**new_bucket, mem elif new_bucket in buckets_seen: return None, [] buckets_seen.add(new_bucket) mask = mask | 2**new_bucket mem.append((rem, bucket_num)) bucket_num = new_bucket val = rem return None, [] def helper(chains, mask, mem): if mask == 0: return [] if mask in mem: return mem[mask] for i, chain in enumerate(chains): if mask >> i & 0: continue for key in chain: if key | mask != mask: continue future = helper(chains, ~key & mask, mem) if future is not None: mem[mask] = chain[key] + future return mem[mask] mem[mask] = None return None def solve(n): buckets = get_graph(n) reverse_bucket = {} sum_bucket = [0] * len(buckets) total_sum = 0 for i, bucket in enumerate(buckets): for x in bucket: total_sum += x sum_bucket[i] += x reverse_bucket[x] = i target = total_sum / len(buckets) chains = [] for i, bucket in enumerate(buckets): seto = {} for x in bucket: key, val = chain(target, buckets, reverse_bucket, sum_bucket, i, x) if key is not None: seto[key] = val chains.append(seto) return helper(chains, 2 ** len(buckets) - 1, {}), reverse_bucket def result(n): res, reverse_bucket = solve(n) if res is None: sys.stdout.write("No\n") else: res = sorted(res, key=lambda x: reverse_bucket[x[0]]) sys.stdout.write("Yes\n") for x, y in res: x = int(x) y = int(y) + 1 stuff = " ".join([str(x), str(y), "\n"]) sys.stdout.write(stuff) result(n)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NONE LIST ASSIGN VAR VAR VAR IF VAR VAR VAR VAR RETURN NONE LIST IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR VAR RETURN NONE LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN NONE LIST FUNC_DEF IF VAR NUMBER RETURN LIST IF VAR VAR RETURN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR NONE RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER DICT VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer m. Let M = 2^{m} - 1. You are also given a set of n integers denoted as the set T. The integers will be provided in base 2 as n binary strings of length m. A set of integers S is called "good" if the following hold. If $a \in S$, then [Image]. If $a, b \in S$, then $a \text{AND} b \in S$ $T \subseteq S$ All elements of S are less than or equal to M. Here, $XOR$ and $\text{AND}$ refer to the bitwise XOR and bitwise AND operators, respectively. Count the number of good sets S, modulo 10^9 + 7. -----Input----- The first line will contain two integers m and n (1 ≤ m ≤ 1 000, 1 ≤ n ≤ min(2^{m}, 50)). The next n lines will contain the elements of T. Each line will contain exactly m zeros and ones. Elements of T will be distinct. -----Output----- Print a single integer, the number of good sets modulo 10^9 + 7. -----Examples----- Input 5 3 11010 00101 11000 Output 4 Input 30 2 010101010101010010101010101010 110110110110110011011011011011 Output 860616440 -----Note----- An example of a valid set S is {00000, 00101, 00010, 00111, 11000, 11010, 11101, 11111}.
MOD = 10**9 + 7 m, N = list(map(int, input().split())) binom = [([1] + [(0) for i in range(m)]) for j in range(m + 1)] for n in range(1, m + 1): for k in range(1, n + 1): binom[n][k] = (binom[n - 1][k] + binom[n - 1][k - 1]) % MOD bell = [(0) for n in range(m + 1)] bell[0] = bell[1] = 1 for n in range(1, m): for k in range(n + 1): bell[n + 1] += bell[k] * binom[n][k] bell[n + 1] %= MOD bags = [(0) for i in range(m)] for it in range(N): for i, z in enumerate(input()): if z == "1": bags[i] |= 1 << it difs = set(bags) sol = 1 for mask in difs: sol = sol * bell[bags.count(mask)] % MOD print(sol)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR 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 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 BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR STRING VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian here Chef has a special affection for sets of binary strings of equal length which have same numbers of 1's. Given three integers n, k and m, your task is to find the the lexicographically m^{th} smallest string among strings which have length n and have k 1's. If no such string exists output -1. ------ Tips: ------ To see what lexicographic order means . See http://en.wikipedia.org/wiki/Lexicographical_{order} ------ Input ------ Input description. The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows: The first and only line of each test case contains three space separated integers N , K and M ------ Output ------ For each test case output the answer on a separate line . ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 350$ $1 ≤ K ≤ N$ ------ Example ------ Input: 1 3 2 2 Output: 101 ------ Explanation ------ Example case 1. The set of strings in lexicographic order is "011", "101", and "110" ------ Scoring ------ Subtask 1 (41 point): $1 ≤ N ≤ 20$ Subtask 2 (59 points): $1 ≤ N ≤ 350$
tests = int(input()) MAXN = 400 c = [[(-1) for _ in range(MAXN)] for _ in range(MAXN)] def cnk(n, k): if k > n: return 0 if k == 0 or n == k: return 1 if c[n][k] != -1: return c[n][k] c[n][k] = cnk(n - 1, k) + cnk(n - 1, k - 1) return c[n][k] for it in range(tests): n, k, m = map(int, input().split()) x = m left = k s = "" if cnk(n, k) < m: print(-1) continue for i in range(n): if cnk(n - i - 1, left) < x: s += "1" x -= cnk(n - i - 1, left) left -= 1 else: s += "0" print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Russian here Chef has a special affection for sets of binary strings of equal length which have same numbers of 1's. Given three integers n, k and m, your task is to find the the lexicographically m^{th} smallest string among strings which have length n and have k 1's. If no such string exists output -1. ------ Tips: ------ To see what lexicographic order means . See http://en.wikipedia.org/wiki/Lexicographical_{order} ------ Input ------ Input description. The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows: The first and only line of each test case contains three space separated integers N , K and M ------ Output ------ For each test case output the answer on a separate line . ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 350$ $1 ≤ K ≤ N$ ------ Example ------ Input: 1 3 2 2 Output: 101 ------ Explanation ------ Example case 1. The set of strings in lexicographic order is "011", "101", and "110" ------ Scoring ------ Subtask 1 (41 point): $1 ≤ N ≤ 20$ Subtask 2 (59 points): $1 ≤ N ≤ 350$
def combi(n, k): res = 1 for i in range(k): res *= n - i for i in range(k): res //= i + 1 return res T = int(input()) for case in range(T): n, k, m = map(int, input().split()) if combi(n, k) < m: print(-1) continue b = [] for i in range(n): rem_bits = n - i if rem_bits == k or combi(rem_bits - 1, k) < m: b.append("1") m -= combi(rem_bits - 1, k) k -= 1 else: b.append("0") print("".join(b))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The only difference between easy and hard versions is constraints. Polycarp loves to listen to music, so he never leaves the player, even on the way home from the university. Polycarp overcomes the distance from the university to the house in exactly $T$ minutes. In the player, Polycarp stores $n$ songs, each of which is characterized by two parameters: $t_i$ and $g_i$, where $t_i$ is the length of the song in minutes ($1 \le t_i \le 15$), $g_i$ is its genre ($1 \le g_i \le 3$). Polycarp wants to create such a playlist so that he can listen to music all the time on the way from the university to his home, and at the time of his arrival home, the playlist is over. Polycarp never interrupts songs and always listens to them from beginning to end. Thus, if he started listening to the $i$-th song, he would spend exactly $t_i$ minutes on its listening. Polycarp also does not like when two songs of the same genre play in a row (i.e. successively/adjacently) or when the songs in his playlist are repeated. Help Polycarpus count the number of different sequences of songs (their order matters), the total duration is exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. -----Input----- The first line of the input contains two integers $n$ and $T$ ($1 \le n \le 15, 1 \le T \le 225$) — the number of songs in the player and the required total duration, respectively. Next, the $n$ lines contain descriptions of songs: the $i$-th line contains two integers $t_i$ and $g_i$ ($1 \le t_i \le 15, 1 \le g_i \le 3$) — the duration of the $i$-th song and its genre, respectively. -----Output----- Output one integer — the number of different sequences of songs, the total length of exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. Since the answer may be huge, output it modulo $10^9 + 7$ (that is, the remainder when dividing the quantity by $10^9 + 7$). -----Examples----- Input 3 3 1 1 1 2 1 3 Output 6 Input 3 3 1 1 1 1 1 3 Output 2 Input 4 10 5 3 2 1 3 2 5 1 Output 10 -----Note----- In the first example, Polycarp can make any of the $6$ possible playlist by rearranging the available songs: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$ and $[3, 2, 1]$ (indices of the songs are given). In the second example, the first and second songs cannot go in succession (since they have the same genre). Thus, Polycarp can create a playlist in one of $2$ possible ways: $[1, 3, 2]$ and $[2, 3, 1]$ (indices of the songs are given). In the third example, Polycarp can make the following playlists: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$, $[1, 4]$, $[4, 1]$, $[2, 3, 4]$ and $[4, 3, 2]$ (indices of the songs are given).
def popcount(i): assert 0 <= i < 4294967296 i = i - (i >> 1 & 1431655765) i = (i & 858993459) + (i >> 2 & 858993459) return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24 N, T = map(int, input().split()) TG = [list(map(int, input().split())) for _ in range(N)] mod = 10**9 + 7 dp = [([0] * 2**N) for _ in range(4)] for i in range(1, 4): dp[i][0] = 1 for S in range(2**N): if popcount(S) == 1: dp[TG[(S & -S).bit_length() - 1][1]][S] = 1 for i in range(1, 4): for j in range(N): if S & 2**j or i == TG[j][1]: continue dp[TG[j][1]][S | 2**j] = (dp[TG[j][1]][S | 2**j] + dp[i][S]) % mod table = [0] * 2**N for S in range(2**N): table[S] = sum(TG[j][0] for j in range(N) if 2**j & S) ans = 0 for S in range(2**N): if table[S] == T: for i in range(1, 4): ans = (ans + dp[i][S]) % mod print(ans)
FUNC_DEF NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. Polycarp loves to listen to music, so he never leaves the player, even on the way home from the university. Polycarp overcomes the distance from the university to the house in exactly $T$ minutes. In the player, Polycarp stores $n$ songs, each of which is characterized by two parameters: $t_i$ and $g_i$, where $t_i$ is the length of the song in minutes ($1 \le t_i \le 15$), $g_i$ is its genre ($1 \le g_i \le 3$). Polycarp wants to create such a playlist so that he can listen to music all the time on the way from the university to his home, and at the time of his arrival home, the playlist is over. Polycarp never interrupts songs and always listens to them from beginning to end. Thus, if he started listening to the $i$-th song, he would spend exactly $t_i$ minutes on its listening. Polycarp also does not like when two songs of the same genre play in a row (i.e. successively/adjacently) or when the songs in his playlist are repeated. Help Polycarpus count the number of different sequences of songs (their order matters), the total duration is exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. -----Input----- The first line of the input contains two integers $n$ and $T$ ($1 \le n \le 15, 1 \le T \le 225$) — the number of songs in the player and the required total duration, respectively. Next, the $n$ lines contain descriptions of songs: the $i$-th line contains two integers $t_i$ and $g_i$ ($1 \le t_i \le 15, 1 \le g_i \le 3$) — the duration of the $i$-th song and its genre, respectively. -----Output----- Output one integer — the number of different sequences of songs, the total length of exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. Since the answer may be huge, output it modulo $10^9 + 7$ (that is, the remainder when dividing the quantity by $10^9 + 7$). -----Examples----- Input 3 3 1 1 1 2 1 3 Output 6 Input 3 3 1 1 1 1 1 3 Output 2 Input 4 10 5 3 2 1 3 2 5 1 Output 10 -----Note----- In the first example, Polycarp can make any of the $6$ possible playlist by rearranging the available songs: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$ and $[3, 2, 1]$ (indices of the songs are given). In the second example, the first and second songs cannot go in succession (since they have the same genre). Thus, Polycarp can create a playlist in one of $2$ possible ways: $[1, 3, 2]$ and $[2, 3, 1]$ (indices of the songs are given). In the third example, Polycarp can make the following playlists: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$, $[1, 4]$, $[4, 1]$, $[2, 3, 4]$ and $[4, 3, 2]$ (indices of the songs are given).
n, tnow = map(int, input().split()) left = int("".join(["1" for i in range(n)]), 2) arr = [] dp = {} for i in range(n): a, b = map(int, input().split()) arr.append([a, b]) def recur(tnow, prevgenre, left): key = str(left) + "_" + str(prevgenre) if tnow == 0: return 1 elif key in dp: return dp[key] else: ans = 0 for i in range(n): if left & 1 << i != 0: if arr[i][0] <= tnow and arr[i][1] != prevgenre: left = left & ~(1 << i) ans += recur(tnow - arr[i][0], arr[i][1], left) left = left | 1 << i dp[key] = ans return ans print(recur(tnow, 4, left) % (10**9 + 7))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING STRING VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT 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 FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
The only difference between easy and hard versions is constraints. Polycarp loves to listen to music, so he never leaves the player, even on the way home from the university. Polycarp overcomes the distance from the university to the house in exactly $T$ minutes. In the player, Polycarp stores $n$ songs, each of which is characterized by two parameters: $t_i$ and $g_i$, where $t_i$ is the length of the song in minutes ($1 \le t_i \le 15$), $g_i$ is its genre ($1 \le g_i \le 3$). Polycarp wants to create such a playlist so that he can listen to music all the time on the way from the university to his home, and at the time of his arrival home, the playlist is over. Polycarp never interrupts songs and always listens to them from beginning to end. Thus, if he started listening to the $i$-th song, he would spend exactly $t_i$ minutes on its listening. Polycarp also does not like when two songs of the same genre play in a row (i.e. successively/adjacently) or when the songs in his playlist are repeated. Help Polycarpus count the number of different sequences of songs (their order matters), the total duration is exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. -----Input----- The first line of the input contains two integers $n$ and $T$ ($1 \le n \le 15, 1 \le T \le 225$) — the number of songs in the player and the required total duration, respectively. Next, the $n$ lines contain descriptions of songs: the $i$-th line contains two integers $t_i$ and $g_i$ ($1 \le t_i \le 15, 1 \le g_i \le 3$) — the duration of the $i$-th song and its genre, respectively. -----Output----- Output one integer — the number of different sequences of songs, the total length of exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. Since the answer may be huge, output it modulo $10^9 + 7$ (that is, the remainder when dividing the quantity by $10^9 + 7$). -----Examples----- Input 3 3 1 1 1 2 1 3 Output 6 Input 3 3 1 1 1 1 1 3 Output 2 Input 4 10 5 3 2 1 3 2 5 1 Output 10 -----Note----- In the first example, Polycarp can make any of the $6$ possible playlist by rearranging the available songs: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$ and $[3, 2, 1]$ (indices of the songs are given). In the second example, the first and second songs cannot go in succession (since they have the same genre). Thus, Polycarp can create a playlist in one of $2$ possible ways: $[1, 3, 2]$ and $[2, 3, 1]$ (indices of the songs are given). In the third example, Polycarp can make the following playlists: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$, $[1, 4]$, $[4, 1]$, $[2, 3, 4]$ and $[4, 3, 2]$ (indices of the songs are given).
import sys input = sys.stdin.readline mod = 10**9 + 7 n, t = map(int, input().split()) a = [] for i in range(n): time, genre = map(int, input().split()) genre -= 1 a.append((time, genre)) dp = [[(0) for j in range(3)] for i in range(1 << n)] for i in range(n): dp[1 << i][a[i][1]] = 1 for i in range(1 << n): for j in range(3): if dp[i][j] == 0: continue mask = 1 for k in range(n): if i & mask or a[k][1] == j: mask <<= 1 continue dp[i | mask][a[k][1]] = (dp[i | mask][a[k][1]] + dp[i][j]) % mod mask <<= 1 ans = 0 for i in range(1 << n): mask = 1 duration = 0 for j in range(n): if i & mask: duration += a[j][0] mask <<= 1 if duration == t: ans = (ans + sum(dp[i])) % mod print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. Polycarp loves to listen to music, so he never leaves the player, even on the way home from the university. Polycarp overcomes the distance from the university to the house in exactly $T$ minutes. In the player, Polycarp stores $n$ songs, each of which is characterized by two parameters: $t_i$ and $g_i$, where $t_i$ is the length of the song in minutes ($1 \le t_i \le 15$), $g_i$ is its genre ($1 \le g_i \le 3$). Polycarp wants to create such a playlist so that he can listen to music all the time on the way from the university to his home, and at the time of his arrival home, the playlist is over. Polycarp never interrupts songs and always listens to them from beginning to end. Thus, if he started listening to the $i$-th song, he would spend exactly $t_i$ minutes on its listening. Polycarp also does not like when two songs of the same genre play in a row (i.e. successively/adjacently) or when the songs in his playlist are repeated. Help Polycarpus count the number of different sequences of songs (their order matters), the total duration is exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. -----Input----- The first line of the input contains two integers $n$ and $T$ ($1 \le n \le 15, 1 \le T \le 225$) — the number of songs in the player and the required total duration, respectively. Next, the $n$ lines contain descriptions of songs: the $i$-th line contains two integers $t_i$ and $g_i$ ($1 \le t_i \le 15, 1 \le g_i \le 3$) — the duration of the $i$-th song and its genre, respectively. -----Output----- Output one integer — the number of different sequences of songs, the total length of exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. Since the answer may be huge, output it modulo $10^9 + 7$ (that is, the remainder when dividing the quantity by $10^9 + 7$). -----Examples----- Input 3 3 1 1 1 2 1 3 Output 6 Input 3 3 1 1 1 1 1 3 Output 2 Input 4 10 5 3 2 1 3 2 5 1 Output 10 -----Note----- In the first example, Polycarp can make any of the $6$ possible playlist by rearranging the available songs: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$ and $[3, 2, 1]$ (indices of the songs are given). In the second example, the first and second songs cannot go in succession (since they have the same genre). Thus, Polycarp can create a playlist in one of $2$ possible ways: $[1, 3, 2]$ and $[2, 3, 1]$ (indices of the songs are given). In the third example, Polycarp can make the following playlists: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$, $[1, 4]$, $[4, 1]$, $[2, 3, 4]$ and $[4, 3, 2]$ (indices of the songs are given).
from itertools import combinations def findsum(comb): sum = 0 for song in comb: sum += song[0] return sum def finda(a, b, c): if a == 0: return 0 if a == 1 and b == 0 and c == 0: return 1 else: return a * findb(a - 1, b, c) + a * findc(a - 1, b, c) def findb(a, b, c): if b == 0: return 0 if b == 1 and a == 0 and c == 0: return 1 else: return b * finda(a, b - 1, c) + b * findc(a, b - 1, c) def findc(a, b, c): if c == 0: return 0 if c == 1 and a == 0 and b == 0: return 1 else: return c * finda(a, b, c - 1) + c * findb(a, b, c - 1) n, T = map(int, input().split()) songs = [] total_combinations = 0 for i in range(n): t, g = map(int, input().split()) songs.append([t, g]) for i in range(1, n + 1): allcomb = list(combinations(songs, i)) for comb in allcomb: sum = findsum(comb) if sum == T: a = 0 b = 0 c = 0 for song in comb: if song[1] == 1: a += 1 elif song[1] == 2: b += 1 else: c += 1 total_combinations += finda(a, b, c) + findb(a, b, c) + findc(a, b, c) total_combinations = total_combinations % 1000000007 print(total_combinations)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST 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 FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. Polycarp loves to listen to music, so he never leaves the player, even on the way home from the university. Polycarp overcomes the distance from the university to the house in exactly $T$ minutes. In the player, Polycarp stores $n$ songs, each of which is characterized by two parameters: $t_i$ and $g_i$, where $t_i$ is the length of the song in minutes ($1 \le t_i \le 15$), $g_i$ is its genre ($1 \le g_i \le 3$). Polycarp wants to create such a playlist so that he can listen to music all the time on the way from the university to his home, and at the time of his arrival home, the playlist is over. Polycarp never interrupts songs and always listens to them from beginning to end. Thus, if he started listening to the $i$-th song, he would spend exactly $t_i$ minutes on its listening. Polycarp also does not like when two songs of the same genre play in a row (i.e. successively/adjacently) or when the songs in his playlist are repeated. Help Polycarpus count the number of different sequences of songs (their order matters), the total duration is exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. -----Input----- The first line of the input contains two integers $n$ and $T$ ($1 \le n \le 15, 1 \le T \le 225$) — the number of songs in the player and the required total duration, respectively. Next, the $n$ lines contain descriptions of songs: the $i$-th line contains two integers $t_i$ and $g_i$ ($1 \le t_i \le 15, 1 \le g_i \le 3$) — the duration of the $i$-th song and its genre, respectively. -----Output----- Output one integer — the number of different sequences of songs, the total length of exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. Since the answer may be huge, output it modulo $10^9 + 7$ (that is, the remainder when dividing the quantity by $10^9 + 7$). -----Examples----- Input 3 3 1 1 1 2 1 3 Output 6 Input 3 3 1 1 1 1 1 3 Output 2 Input 4 10 5 3 2 1 3 2 5 1 Output 10 -----Note----- In the first example, Polycarp can make any of the $6$ possible playlist by rearranging the available songs: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$ and $[3, 2, 1]$ (indices of the songs are given). In the second example, the first and second songs cannot go in succession (since they have the same genre). Thus, Polycarp can create a playlist in one of $2$ possible ways: $[1, 3, 2]$ and $[2, 3, 1]$ (indices of the songs are given). In the third example, Polycarp can make the following playlists: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$, $[1, 4]$, $[4, 1]$, $[2, 3, 4]$ and $[4, 3, 2]$ (indices of the songs are given).
from itertools import combinations def out1(a, b, c): if a < 0 or b < 0 or c < 0: return 0 if a == 1 and b == 0 and c == 0: return 1 return a * (out2(a - 1, b, c) + out3(a - 1, b, c)) def out2(a, b, c): if a < 0 or b < 0 or c < 0: return 0 if a == 0 and b == 1 and c == 0: return 1 return b * (out1(a, b - 1, c) + out3(a, b - 1, c)) def out3(a, b, c): if a < 0 or b < 0 or c < 0: return 0 if a == 0 and b == 0 and c == 1: return 1 return c * (out2(a, b, c - 1) + out1(a, b, c - 1)) def column(matrix, i): return [row[i] for row in matrix] N, T = [int(x) for x in input().split()] A = [] s = 0 for i in range(N): A.append([int(x) for x in input().split()]) for i in range(1, N + 1): comb = list(combinations(A, i)) for x in comb: if sum(column(x, 0)) == T: a = column(x, 1).count(1) b = column(x, 1).count(2) c = column(x, 1).count(3) s += out1(a, b, c) + out2(a, b, c) + out3(a, b, c) print(s % 1000000007)
FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
def f(n): if n == 0: return 0 return 2 ** (n - 1) + 2 * f(n - 1) f = [0] * 60 for n in range(1, 60): f[n] = 2 ** (n - 1) + 2 * f[n - 1] n = int(input()) ans = 0 for k in range(60, 0, -1): if n >= 2**k: ans += f[k] n -= 2**k if n > 0: ans += 2**k print(int(ans))
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
def slow_cal(x): if x == 0: return 0 else: return slow_cal(x - 1) + min(x ^ v for v in range(x)) def med_cal(x): if x == 0: return 0 return sum(min(i ^ j for j in range(i)) for i in range(1, x + 1)) def fast_cal(x): res = 0 for bit in range(50): res += (x // (1 << bit) - x // (1 << bit + 1)) * (1 << bit) return res n = int(input()) - 1 print(fast_cal(n))
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input()) def PotenciaDosMasCercana(n): if n == 0: return 0, 0 p = 1 i = 0 while p <= n: p = p << 1 i += 1 return p >> 1, i - 1 def C_Conexas(b): dp = [int] * 2 dp[0] = 0 for i in range(1, b + 1): dp[1] = 2 * dp[0] + 2 ** (i - 1) dp[0] = dp[1] return dp[0] def main(n): a, b = PotenciaDosMasCercana(n) Ans = 0 if n == a: Ans = C_Conexas(b) else: c, d = PotenciaDosMasCercana(n - a) Ans = C_Conexas(b) + main(n - a) + a return Ans print(main(n))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
from sys import stdin, stdout n = int(stdin.readline()) psze = 41 ans = 0 def trans(s): cnt = 0 for i in range(psze * 2): if i < len(s) and s[i] == "1": cnt += 1 << i return cnt n -= 1 for i in range(psze): if 1 << i > n: break s = "0" * i + "1" cnt = 1 l, r = 0, n + 1 while r - l > 1: m = l + r >> 1 f = bin(m)[2:][::-1] if trans(s + f) <= n: l = m else: r = m ans += (1 << i) * (l + 1) stdout.write(str(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR STRING VAR BIN_OP NUMBER VAR RETURN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input()) - 1 r = 0 while n != 0: zz = 1 a = 1 z = 1 while zz <= n: z <<= 1 zz <<= 1 zz += 1 a <<= 1 a += z zz >>= 1 a -= z a >>= 1 n -= zz r += a if n: n -= 1 r += z print(r)
ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input()) a, t = n - 1, 1 n -= 1 i = 2 while i <= n: a += n // i * t i *= 2 t *= 2 print(a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
def main(): n = int(input()) - 1 res = n power = 1 while n // 2**power > 0: res += n // 2**power * 2 ** (power - 1) power += 1 print(res) main()
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
def func(n): if n == 0: return 0 if n == 1: return 1 if n % 2: return func(n // 2) * 2 + n // 2 + 1 else: return func(n // 2) * 2 + n // 2 n = int(input()) a = [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1] print(func(n - 1))
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
L = input().split() n = int(L[0]) - 1 k = 0 while 1 << k < n: k += 1 ans = 0 for i in range(0, k + 1): ans += (1 << i) * ((n + (1 << i + 1) - (1 << i)) // (1 << i + 1)) print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
ans = 0 n = int(input()) k = 2**63 lpre = [0] for i in range(50): lpre.append(lpre[-1] * 2 + 2**i) lmax = -1 for i in range(42, -1, -1): if n & 2**i: ans += lpre[i] if lmax != -1: ans += 2**lmax lmax = i print(ans)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
import sys n = int(sys.stdin.readline()) L = [0, 1] for i in range(1, 48): L.append(2 * L[i] + 2**i) B = bin(n - 1)[2:] result = 0 l = len(B) for i in range(0, len(B)): if B[i] == "1": result = result + 2 ** (l - 1 - i) + L[l - 1 - i] print(result) L
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input()) - 1 dp = [0] * 40 for i in range(1, 40): dp[i] = 2 * dp[i - 1] + 2 ** (i - 1) c = 0 for i in range(40): if 1 << i & n: c += dp[i] + 2**i print(c)
ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
a = [0] cur = 1 while cur < 10**13: x = a[-1] a.append(x * 2 + cur) cur *= 2 n = int(input()) n -= 1 ans = 0 i = 1 cur = 1 while n > 0: x = n % 2 n = n // 2 if x > 0: ans += cur + a[i - 1] i += 1 cur *= 2 print(ans)
ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input().strip()) res = 0 i = 1 n -= 1 while i <= n: res += ((n - i) // (i << 1) + 1) * i i <<= 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
nn = input() n = int(nn) st = 0 dv = 1 step = [] while dv <= n: step.append(dv) dv = dv * 2 st += 1 step.append(dv) step.append(dv * 2) otv = 0 f = 1 q = 0 while f != 0: if n == 0: break if n == 1: break for i in range(len(step)): if step[i] > n: q = i - 1 break if n == step[q]: otv += step[q - 1] * q n -= step[q] else: otv += step[q] + step[q - 1] * q n -= step[q] print(otv)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input()) ans2 = [0] for i in range(1, 64): ans2.append(ans2[i - 1] + ans2[i - 1] + pow(2, i - 1)) def ans(n): if n == 0: return 0 else: length = n.bit_length() - 1 if n == pow(2, length): return ans2[length] else: return ans2[length] + ans(n - pow(2, length)) + pow(2, length) print(ans(n))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
a = int(input()) def ans(n, p=1): return ans((n + 1) // 2, p * 2) + n // 2 * p if n > 1 else 0 print(ans(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF NUMBER RETURN VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input()) n -= 1 tot = n // 2 ans = 0 ans += n - tot mask = 1 while mask <= 40: curr = 1 << mask strt = curr // 2 cnt = (tot - strt) // curr + 1 ans += curr * cnt mask += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input()) def pow2(x): r = 1 while x > 0: x -= 1 r *= 2 return r def val_pow_2(x): lst = [] lst.append(0) for i in range(x): lst.append(2 * lst[i] + pow2(i)) return lst def lg2(x): r = 0 ans = 1 while x > 1: x = int(x / 2) ans *= 2 r += 1 return r def max_pow_2(x): ans = 1 while x > 1: x = int(x / 2) ans *= 2 return ans def func(x, lst): if bin(x).count("1") == 1: return lst[lg2(x)] elif x == 0: return 0 else: y = max_pow_2(x) return lst[lg2(y)] + y + func(x - y, lst) lst = val_pow_2(int(lg2(n))) print(func(n, lst))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER RETURN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
import sys input = sys.stdin.readline n = int(input()) n -= 1 ans = 0 for i in range(50): ans += (n // (1 << i) - n // (1 << i + 1)) * (1 << i) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
import sys input = sys.stdin.readline n = int(input()) ans = 0 w = 1 while n > 1: cnt = n // 2 ans += w * cnt n = (n + 1) // 2 w <<= 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input()) ans = 0 _pow = 2 while 2 * n > _pow: ans += _pow // 2 * (n // _pow + (1 if n % _pow > _pow // 2 else 0)) _pow *= 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
import sys def main(): import sys input = sys.stdin.readline def rec(n, k): if n == 1: return 0 return n // 2 * k + rec((n + 1) // 2, 2 * k) print(rec(int(input()), 1)) return 0 main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
def f(n): if n == 2: return 1 if n == 3: return 3 if n % 2 == 0: return 2 * f(n // 2) + n // 2 else: return 2 * f(n // 2 + 1) + n // 2 n = int(input()) print(f(n))
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
def gao(n): if n == 1: return 1 else: return 2 * gao(n // 2) + (n + 1) // 2 while True: try: n = int(input()) print(gao(n - 1)) except BaseException: break
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input()) n = n - 1 ans = 0 dp = [] dp.append(1) for i in range(1, 41): dp.append(2 * dp[i - 1] + pow(2, i)) for i in range(40, -1, -1): if n & 1 << i > 0: ans += pow(2, i) if i != 0: ans += dp[i - 1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
n = int(input()) c = 0 for i in range(39, 0, -1): s = pow(2, i) if s <= n: n -= s c += s // 2 * i if n != 0: c += s print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Bakry faced a problem, but since he's lazy to solve it, he asks for your help. You are given a tree of $n$ nodes, the $i$-th node has value $a_i$ assigned to it for each $i$ from $1$ to $n$. As a reminder, a tree on $n$ nodes is a connected graph with $n-1$ edges. You want to delete at least $1$, but at most $k-1$ edges from the tree, so that the following condition would hold: For every connected component calculate the bitwise XOR of the values of the nodes in it. Then, these values have to be the same for all connected components. Is it possible to achieve this condition? -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ $(1 \leq t \leq 5 \cdot 10^4)$. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ $(2 \leq k \leq n \leq 10^5)$. The second line of each test case contains $n$ integers $a_1, a_2, ..., a_n$ $(1 \leq a_i \leq 10^9)$. The $i$-th of the next $n-1$ lines contains two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$, $u_i\neq v_i$), which means that there's an edge between nodes $u_i$ and $v_i$. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, you should output a single string. If you can delete the edges according to the conditions written above, output "YES" (without quotes). Otherwise, output "NO" (without quotes). You can print each letter of "YES" and "NO" in any case (upper or lower). -----Examples----- Input 5 2 2 1 3 1 2 5 5 3 3 3 3 3 1 2 2 3 1 4 4 5 5 2 1 7 2 3 5 1 2 2 3 1 4 4 5 5 3 1 6 4 1 2 1 2 2 3 1 4 4 5 3 3 1 7 4 1 2 2 3 Output NO YES NO YES NO -----Note----- It can be shown that the objection is not achievable for first, third, and fifth test cases. In the second test case, you can just remove all the edges. There will be $5$ connected components, each containing only one node with value $3$, so the bitwise XORs will be $3$ for all of them. In the fourth test case, this is the tree: . You can remove an edge $(4,5)$ The bitwise XOR of the first component will be, $a_1 \oplus a_2 \oplus a_3 \oplus a_4 = 1 \oplus 6 \oplus 4 \oplus 1 = 2$ (where $\oplus$ denotes the bitwise XOR). The bitwise XOR of the second component will be, $a_5 = 2$.
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) (*a,) = map(int, input().split()) X = 0 for i in a: X ^= i g = [[] for i in range(n)] for i in range(n - 1): u, v = map(int, input().split()) g[u - 1].append(v - 1) g[v - 1].append(u - 1) if X == 0: print("YES") return elif k == 2: print("NO") return p = [None] * n q = [0] p[0] = -1 i = 0 while i < len(q): x = q[i] i += 1 for v in g[x]: if p[v] is None: p[v] = x q.append(v) d = [None] * n dx = [None] * n for i in range(len(q) - 1, -1, -1): x = q[i] P = p[x] z = a[x] for v in g[x]: if v != P: z ^= dx[v] c = 0 for v in g[x]: if v != P: if d[v]: c += 1 if z == 0: print("YES") return if c > 1: print("YES") return dx[x] = z d[x] = z == X or c > 0 print("NO") for i in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
The Bubble Cup hypothesis stood unsolved for 130 years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem: Given a number m, how many polynomials P with coefficients in set {\{0,1,2,3,4,5,6,7\}} have: P(2)=m? Help Jerry Mao solve the long standing problem! Input The first line contains a single integer t (1 ≤ t ≤ 5⋅ 10^5) - number of test cases. On next line there are t numbers, m_i (1 ≤ m_i ≤ 10^{18}) - meaning that in case i you should solve for number m_i. Output For each test case i, print the answer on separate lines: number of polynomials P as described in statement such that P(2)=m_i, modulo 10^9 + 7. Example Input 2 2 4 Output 2 4 Note In first case, for m=2, polynomials that satisfy the constraint are x and 2. In second case, for m=4, polynomials that satisfy the constraint are x^2, x + 2, 2x and 4.
T = input() mod = int(1000000000.0 + 7) a = map(int, input().split()) c = [] for n in a: b = n // 2 + 2 b = b * b b //= 4 c.append(str(b % mod)) print(" ".join(c))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The Bubble Cup hypothesis stood unsolved for 130 years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem: Given a number m, how many polynomials P with coefficients in set {\{0,1,2,3,4,5,6,7\}} have: P(2)=m? Help Jerry Mao solve the long standing problem! Input The first line contains a single integer t (1 ≤ t ≤ 5⋅ 10^5) - number of test cases. On next line there are t numbers, m_i (1 ≤ m_i ≤ 10^{18}) - meaning that in case i you should solve for number m_i. Output For each test case i, print the answer on separate lines: number of polynomials P as described in statement such that P(2)=m_i, modulo 10^9 + 7. Example Input 2 2 4 Output 2 4 Note In first case, for m=2, polynomials that satisfy the constraint are x and 2. In second case, for m=4, polynomials that satisfy the constraint are x^2, x + 2, 2x and 4.
t = int(input()) a = list(map(int, input().split())) out = [] for n in a: ans = n // 2 + 2 ans = ans * ans ans //= 4 out.append(ans % 1000000007) print(" ".join(str(x) for x in out))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
There is the following puzzle popular among nuclear physicists. A reactor contains a set of n atoms of some chemical elements. We shall understand the phrase "atomic number" as the number of this atom's element in the periodic table of the chemical elements. You are allowed to take any two different atoms and fuse a new one from them. That results in a new atom, whose number is equal to the sum of the numbers of original atoms. The fusion operation can be performed several times. The aim is getting a new pregiven set of k atoms. The puzzle's difficulty is that it is only allowed to fuse two atoms into one, it is not allowed to split an atom into several atoms. You are suggested to try to solve the puzzle. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 17). The second line contains space-separated symbols of elements of n atoms, which are available from the start. The third line contains space-separated symbols of elements of k atoms which need to be the result of the fusion. The symbols of the elements coincide with the symbols from the periodic table of the chemical elements. The atomic numbers do not exceed 100 (elements possessing larger numbers are highly unstable). Some atoms can have identical numbers (that is, there can be several atoms of the same element). The sum of numbers of initial atoms is equal to the sum of numbers of the atoms that need to be synthesized. Output If it is impossible to synthesize the required atoms, print "NO" without the quotes. Otherwise, print on the first line «YES», and on the next k lines print the way of synthesizing each of k atoms as equations. Each equation has the following form: "x1+x2+...+xt->yi", where xj is the symbol of the element of some atom from the original set, and yi is the symbol of the element of some atom from the resulting set. Each atom from the input data should occur in the output data exactly one time. The order of summands in the equations, as well as the output order does not matter. If there are several solutions, print any of them. For a better understanding of the output format, see the samples. Examples Input 10 3 Mn Co Li Mg C P F Zn Sc K Sn Pt Y Output YES Mn+C+K-&gt;Sn Co+Zn+Sc-&gt;Pt Li+Mg+P+F-&gt;Y Input 2 1 H H He Output YES H+H-&gt;He Input 2 2 Bk Fm Cf Es Output NO Note The reactions from the first example possess the following form (the atomic number is written below and to the left of the element): <image> <image> <image> To find a periodic table of the chemical elements, you may use your favorite search engine. The pretest set contains each of the first 100 elements of the periodic table at least once. You can use that information to check for misprints.
import itertools element_to_value = { "H": 1, "He": 2, "Li": 3, "Be": 4, "B": 5, "C": 6, "N": 7, "O": 8, "F": 9, "Ne": 10, "Na": 11, "Mg": 12, "Al": 13, "Si": 14, "P": 15, "S": 16, "Cl": 17, "Ar": 18, "K": 19, "Ca": 20, "Sc": 21, "Ti": 22, "V": 23, "Cr": 24, "Mn": 25, "Fe": 26, "Co": 27, "Ni": 28, "Cu": 29, "Zn": 30, "Ga": 31, "Ge": 32, "As": 33, "Se": 34, "Br": 35, "Kr": 36, "Rb": 37, "Sr": 38, "Y": 39, "Zr": 40, "Nb": 41, "Mo": 42, "Tc": 43, "Ru": 44, "Rh": 45, "Pd": 46, "Ag": 47, "Cd": 48, "In": 49, "Sn": 50, "Sb": 51, "Te": 52, "I": 53, "Xe": 54, "Cs": 55, "Ba": 56, "La": 57, "Ce": 58, "Pr": 59, "Nd": 60, "Pm": 61, "Sm": 62, "Eu": 63, "Gd": 64, "Tb": 65, "Dy": 66, "Ho": 67, "Er": 68, "Tm": 69, "Yb": 70, "Lu": 71, "Hf": 72, "Ta": 73, "W": 74, "Re": 75, "Os": 76, "Ir": 77, "Pt": 78, "Au": 79, "Hg": 80, "Tl": 81, "Pb": 82, "Bi": 83, "Po": 84, "At": 85, "Rn": 86, "Fr": 87, "Ra": 88, "Ac": 89, "Th": 90, "Pa": 91, "U": 92, "Np": 93, "Pu": 94, "Am": 95, "Cm": 96, "Bk": 97, "Cf": 98, "Es": 99, "Fm": 100, } value_to_element = dict() for element, value in element_to_value.items(): value_to_element[value] = element n, k = map(int, input().split()) products_start_str = input().split() products_end_str = input().split() products_start = [element_to_value[elem] for elem in products_start_str] products_end = [element_to_value[elem] for elem in products_end_str] products_start.sort() ingredient_value = [] ingredient_count = [] for key, lst in itertools.groupby(products_start): ingredient_value.append(key) ingredient_count.append(len(list(lst))) nr_ingredients = len(ingredient_value) construction_options = [[] for i in range(k)] for combination in itertools.product(*[range(l + 1) for l in ingredient_count]): value = sum(combination[i] * ingredient_value[i] for i in range(nr_ingredients)) if value in products_end: for i in range(k): if products_end[i] == value: construction_options[i].append(combination) solution = [None for i in range(k)] def find_solution(used=[(0) for i in range(nr_ingredients)], next=0): if next == k: return all(used[i] == ingredient_count[i] for i in range(nr_ingredients)) else: for option in construction_options[next]: usage = [(used[i] + option[i]) for i in range(nr_ingredients)] if all(used[i] <= ingredient_count[i] for i in range(nr_ingredients)): possible = find_solution(usage, next + 1) if possible: solution[next] = option return True return False possible = find_solution() if not possible: print("NO") exit() def combination_to_recipe(combination): recipe = [] for i in range(nr_ingredients): for j in range(combination[i]): recipe.append(value_to_element[ingredient_value[i]]) return recipe print("YES") for i in range(k): recipe = combination_to_recipe(solution[i]) print("%s->%s" % ("+".join(recipe), products_end_str[i]))
IMPORT ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING VAR VAR VAR
Monocarp has arranged $n$ colored marbles in a row. The color of the $i$-th marble is $a_i$. Monocarp likes ordered things, so he wants to rearrange marbles in such a way that all marbles of the same color form a contiguos segment (and there is only one such segment for each color). In other words, Monocarp wants to rearrange marbles so that, for every color $j$, if the leftmost marble of color $j$ is $l$-th in the row, and the rightmost marble of this color has position $r$ in the row, then every marble from $l$ to $r$ has color $j$. To achieve his goal, Monocarp can do the following operation any number of times: choose two neighbouring marbles, and swap them. You have to calculate the minimum number of operations Monocarp has to perform to rearrange the marbles. Note that the order of segments of marbles having equal color does not matter, it is only required that, for every color, all the marbles of this color form exactly one contiguous segment. -----Input----- The first line contains one integer $n$ $(2 \le n \le 4 \cdot 10^5)$ — the number of marbles. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 20)$, where $a_i$ is the color of the $i$-th marble. -----Output----- Print the minimum number of operations Monocarp has to perform to achieve his goal. -----Examples----- Input 7 3 4 2 3 4 2 2 Output 3 Input 5 20 1 14 10 2 Output 0 Input 13 5 5 4 4 3 5 7 6 5 4 4 6 5 Output 21 -----Note----- In the first example three operations are enough. Firstly, Monocarp should swap the third and the fourth marbles, so the sequence of colors is $[3, 4, 3, 2, 4, 2, 2]$. Then Monocarp should swap the second and the third marbles, so the sequence is $[3, 3, 4, 2, 4, 2, 2]$. And finally, Monocarp should swap the fourth and the fifth marbles, so the sequence is $[3, 3, 4, 4, 2, 2, 2]$. In the second example there's no need to perform any operations.
n = int(input()) a = [int(x) for x in input().split()] d = {} for i in range(1, 21): for j in range(1, 21): d[i, j] = 0 cv = [(0) for i in range(21)] for j in a: for i in range(1, 21): d[i, j] += cv[i] cv[j] += 1 s = 0 for i in range(1, 21): for j in range(i + 1, 21): s += min(d[i, j], d[j, i]) print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp has arranged $n$ colored marbles in a row. The color of the $i$-th marble is $a_i$. Monocarp likes ordered things, so he wants to rearrange marbles in such a way that all marbles of the same color form a contiguos segment (and there is only one such segment for each color). In other words, Monocarp wants to rearrange marbles so that, for every color $j$, if the leftmost marble of color $j$ is $l$-th in the row, and the rightmost marble of this color has position $r$ in the row, then every marble from $l$ to $r$ has color $j$. To achieve his goal, Monocarp can do the following operation any number of times: choose two neighbouring marbles, and swap them. You have to calculate the minimum number of operations Monocarp has to perform to rearrange the marbles. Note that the order of segments of marbles having equal color does not matter, it is only required that, for every color, all the marbles of this color form exactly one contiguous segment. -----Input----- The first line contains one integer $n$ $(2 \le n \le 4 \cdot 10^5)$ — the number of marbles. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 20)$, where $a_i$ is the color of the $i$-th marble. -----Output----- Print the minimum number of operations Monocarp has to perform to achieve his goal. -----Examples----- Input 7 3 4 2 3 4 2 2 Output 3 Input 5 20 1 14 10 2 Output 0 Input 13 5 5 4 4 3 5 7 6 5 4 4 6 5 Output 21 -----Note----- In the first example three operations are enough. Firstly, Monocarp should swap the third and the fourth marbles, so the sequence of colors is $[3, 4, 3, 2, 4, 2, 2]$. Then Monocarp should swap the second and the third marbles, so the sequence is $[3, 3, 4, 2, 4, 2, 2]$. And finally, Monocarp should swap the fourth and the fifth marbles, so the sequence is $[3, 3, 4, 4, 2, 2, 2]$. In the second example there's no need to perform any operations.
import sys n = int(sys.stdin.readline().strip()) a = list(map(int, sys.stdin.readline().strip().split())) M = [[(0) for i in range(0, 21)] for j in range(0, 21)] F = [(0) for i in range(0, 21)] for i in range(0, n): x = int(a[i]) for j in range(0, 21): if j != x: M[j][x] = M[j][x] + F[j] F[x] = F[x] + 1 ans = 0 for i in range(0, 21): for j in range(0, i): ans = ans + min(M[i][j], M[j][i]) print(ans)
IMPORT 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 NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: if len(s) == 0: return 0 count = 0 counterLeft = collections.Counter() counterRight = collections.Counter(s) for element in s: counterLeft[element] += 1 counterRight[element] -= 1 if counterRight[element] == 0: counterRight.pop(element) if len(counterLeft) == len(counterRight): count += 1 return count
CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left_count = collections.Counter() right_count = collections.Counter(s) ans = 0 for ch in s: left_count[ch] += 1 right_count[ch] -= 1 if right_count[ch] == 0: del right_count[ch] if len(left_count) == len(right_count): ans += 1 return ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: dic_b = {} for i in s[1:]: if i not in dic_b: dic_b[i] = 1 else: dic_b[i] += 1 b_count = len(dic_b) a = list(s[:1]) a_count = 1 count = int(b_count == a_count) ix = 1 while ix < len(s): if s[ix] not in a: a_count += 1 a.append(s[ix]) dic_b[s[ix]] -= 1 if dic_b[s[ix]] == 0: b_count -= 1 count += int(a_count == b_count) ix += 1 return count
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: index_one = 0 index_two = 0 set_one = set() set_two = set() s_r = s[::-1] for i in range(len(s)): print((i, len(set(s[: i + 1])), len(set(s[i + 1 :])))) if len(set(s[: i + 1])) == len(set(s[i + 1 :])): index_one = i break else: set_one.add(s[i]) for i in range(len(s_r)): print((i, len(set(s_r[: i + 1])), len(set(s_r[i + 1 :])))) if len(set(s_r[: i + 1])) == len(set(s_r[i + 1 :])): print(i) index_two = len(s) - i - 1 break else: set_two.add(s_r[i]) print((index_one, index_two)) return index_two - index_one
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: counter = 0 left = defaultdict(int) right = defaultdict(int) for c in s: right[c] += 1 for i in range(len(s)): left[s[i]] += 1 right[s[i]] -= 1 if right[s[i]] == 0: right.pop(s[i]) if len(left.keys()) == len(right.keys()): counter += 1 return counter
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, string: str) -> int: def isValid(string, i): return 0 <= i < len(string) def getLength(string, unique, i): if isValid(string, i): unique.add(string[i]) return len(unique) return len(unique) left, right = [0] * len(string), [0] * len(string) left_unique, right_unique = set(), set() reversed_string = string[::-1] for i in range(len(left)): left[i] = getLength(string, left_unique, i) right[len(right) - 1 - i] = getLength(reversed_string, right_unique, i) good_splits = 0 for i in range(len(left) - 1): if right[i + 1] == left[i]: good_splits += 1 return good_splits
CLASS_DEF FUNC_DEF VAR FUNC_DEF RETURN NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left = sorted([s.find(l) for l in set(s)]) + [len(s)] right = sorted([s.rfind(l) for l in set(s)], reverse=True) + [0] ind = max(ind for ind in range(len(left)) if left[ind] <= right[ind]) return min(right[ind], left[ind + 1]) - max(left[ind], right[ind + 1])
CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: set_f = set() map_b = defaultdict(int) for c in s: map_b[c] += 1 num = 0 for c in s: map_b[c] -= 1 if not map_b[c]: del map_b[c] set_f.add(c) if len(set_f) == len(map_b): num += 1 return num
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: if len(s) == 1: return 0 ln = len(s) ans = 0 t1 = {} t2 = {} l1, l2 = 0, 0 t1[s[0]] = 1 for i in range(1, ln): if s[i] in list(t2.keys()): t2[s[i]] += 1 else: t2[s[i]] = 1 if len(list(t1.keys())) == len(list(t2.keys())): ans += 1 for i in range(1, ln): if s[i] in list(t1.keys()): t1[s[i]] += 1 else: t1[s[i]] = 1 t2[s[i]] -= 1 if t2[s[i]] == 0: del t2[s[i]] if len(t1) == len(t2): ans += 1 return ans
CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left_unique = [(0) for x in range(len(s))] right_unique = [(0) for x in range(len(s))] left_set = set() right_set = set() for x in range(len(s)): if s[x] not in left_set: left_set.add(s[x]) if x > 0: left_unique[x] = left_unique[x - 1] + 1 else: left_unique[x] += 1 else: left_unique[x] = left_unique[x - 1] if s[len(s) - 1 - x] not in right_set: right_set.add(s[len(s) - x - 1]) if x > 0: right_unique[len(s) - x - 1] = right_unique[len(s) - x] + 1 else: right_unique[len(s) - x - 1] += 1 else: right_unique[len(s) - x - 1] = right_unique[len(s) - x] min_splits = 0 for x in range(1, len(s)): if left_unique[x - 1] == right_unique[x]: min_splits += 1 return min_splits
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: if not s: return 0 l_unique = set([]) r_unique = set(s) good_splits = 0 for i, x in enumerate(s): l_unique.add(x) if x not in s[i + 1 :]: r_unique.discard(x) if len(l_unique) == len(r_unique): good_splits += 1 return good_splits
CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left = {s[0]: 1} right = {} for i in range(1, len(s)): right[s[i]] = right.get(s[i], 0) + 1 middle_i = 1 count = 0 while middle_i < len(s): if len(left) == len(right): count += 1 middle = s[middle_i] right[middle] -= 1 if right[middle] == 0: right.pop(middle) left[middle] = left.get(middle, 0) + 1 middle_i += 1 return count
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: unique_chars = {} for i, char in enumerate(s): if char in unique_chars: first, last = unique_chars[char] unique_chars[char] = first, i else: unique_chars[char] = i, i n_unique_chars = len(list(unique_chars.keys())) events = [] for start, end in list(unique_chars.values()): events.append((start, 1)) events.append((end, -1)) events.sort(key=lambda x: x[0]) left_num = 0 right_num = n_unique_chars total = 0 start_good = None for i, event_type in events: if start_good is not None: return i - start_good if event_type == 1: left_num += 1 else: right_num -= 1 if left_num == right_num: start_good = i
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR VAR IF VAR NONE RETURN BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left = Counter() right = Counter(s) res = 0 for ch in s: if right[ch] == 1: del right[ch] else: right[ch] -= 1 left[ch] += 1 if len(list(left.keys())) == len(list(right.keys())): res += 1 return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: p_dict = {s[0]: 1} q_dict = {} for char in s[1:]: self.addToDict(q_dict, char) if len(p_dict.keys()) == len(q_dict.keys()): count = 1 else: count = 0 for i in range(1, len(s) - 1): char = s[i] self.removeFromDict(q_dict, char) self.addToDict(p_dict, char) if len(p_dict.keys()) == len(q_dict.keys()): count += 1 return count def addToDict(self, my_dict, value): if my_dict.get(value) is None: my_dict[value] = 1 else: my_dict[value] += 1 def removeFromDict(self, my_dict, value): if my_dict.get(value) == 1: my_dict.pop(value) elif my_dict.get(value) > 1: my_dict[value] -= 1
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: res = 0 fs = set() d = {} for x in s: if x not in d: d[x] = 1 else: d[x] += 1 i = 0 while i < len(s): fs.add(s[i]) if d[s[i]] > 1: d[s[i]] -= 1 else: del d[s[i]] if len(fs) == len(set(d.keys())): res += 1 i += 1 return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR