description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given an array a of size n, and q queries to it. There are queries of two types: 1 l_{i} r_{i} β€” perform a cyclic shift of the segment [l_{i}, r_{i}] to the right. That is, for every x such that l_{i} ≀ x < r_{i} new value of a_{x} + 1 becomes equal to old value of a_{x}, and new value of a_{l}_{i} becomes equal to old value of a_{r}_{i}; 2 l_{i} r_{i} β€” reverse the segment [l_{i}, r_{i}]. There are m important indices in the array b_1, b_2, ..., b_{m}. For each i such that 1 ≀ i ≀ m you have to output the number that will have index b_{i} in the array after all queries are performed. -----Input----- The first line contains three integer numbers n, q and m (1 ≀ n, q ≀ 2Β·10^5, 1 ≀ m ≀ 100). The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9). Then q lines follow. i-th of them contains three integer numbers t_{i}, l_{i}, r_{i}, where t_{i} is the type of i-th query, and [l_{i}, r_{i}] is the segment where this query is performed (1 ≀ t_{i} ≀ 2, 1 ≀ l_{i} ≀ r_{i} ≀ n). The last line contains m integer numbers b_1, b_2, ..., b_{m} (1 ≀ b_{i} ≀ n) β€” important indices of the array. -----Output----- Print m numbers, i-th of which is equal to the number at index b_{i} after all queries are done. -----Example----- Input 6 3 5 1 2 3 4 5 6 2 1 3 2 3 6 1 1 6 2 2 1 5 3 Output 3 3 1 5 2
from sys import stdin, stdout input = stdin.readline print = stdout.write n, q, m = map(int, input().split()) a = list(map(int, input().split())) ops = [list(map(int, input().split())) for _ in range(q)] b = list(map(int, input().split())) def solve(index, ops): def _solve(index, op): t, l, r = op if index < l or index > r: return index if t == 1: if index == l: return r else: return index - 1 else: return l + r - index for op in ops[::-1]: index = _solve(index, op) return index b = list(map(lambda x: solve(x, ops), b)) for i in b: print(str(a[i - 1]) + " ")
ASSIGN VAR VAR ASSIGN 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR NUMBER IF VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING
Anna Hazare is a well known social activist in India. On 5th April, 2011 he started "Lokpal Bill movement". Chef is very excited about this movement. He is thinking of contributing to it. He gathers his cook-herd and starts thinking about how our community can contribute to this. All of them are excited about this too, but no one could come up with any idea. Cooks were slightly disappointed with this and went to consult their friends. One of the geekiest friend gave them the idea of spreading knowledge through Facebook. But we do not want to spam people's wall. So our cook came up with the idea of dividing Facebook users into small friend groups and then identify the most popular friend in each group and post on his / her wall. They started dividing users into groups of friends and identifying the most popular amongst them. The notoriety of a friend is defined as the averaged distance from all the other friends in his / her group. This measure considers the friend himself, and the trivial distance of '0' that he / she has with himself / herself. The most popular friend in a group is the friend whose notoriety is least among all the friends in the group. Distance between X and Y is defined as follows: Minimum number of profiles that X needs to visit for reaching Y's profile(Including Y's profile). X can open only those profiles which are in the friend list of the current opened profile. For Example: Suppose A is friend of B. B has two friends C and D. E is a friend of D. Now, the distance between A and B is 1, A and C is 2, C and E is 3. So, one of our smart cooks took the responsibility of identifying the most popular friend in each group and others will go to persuade them for posting. This cheeky fellow knows that he can release his burden by giving this task as a long contest problem. Now, he is asking you to write a program to identify the most popular friend among all the friends in each group. Also, our smart cook wants to know the average distance of everyone from the most popular friend. ------ Input ------ Friends in a group are labelled with numbers to hide their Facebook identity. The first line of input contains the number of groups in which users are divided. First line of each group contains the number of friends that belong to that group. i^{th} line of each group contains the space separated friend list of 'i'. You are assured that each friend's profile can be accessed by the profile of every other friend by following some sequence of profile visits. ------ Output ------ Your output contains the most popular friend name label along with the average distance (space separated) from all other friends (including himself / herself) in six digits of precision. There might be multiple most popular friend, in that case output the friend labelled with least number. ------ Note: ------ Each person in a group have atleast one friend and he/she cannot be in his/her own friend list. Number of friends in a group cannot be more than 100. There are atmost 100 groups. ----- Sample Input 1 ------ 1 6 3 5 1 4 3 5 6 2 4 6 4 5 ----- Sample Output 1 ------ 4 1.166667
def bfs(start, friend_dict): visited = set() queue = [(start, 0)] visited.add(start) s = 0 while queue: friend, dist = queue.pop(0) s += dist for x in friend_dict[friend]: if x not in visited: queue.append((x, dist + 1)) visited.add(x) return s for _ in range(int(input())): n = int(input()) friend_dict = {} for i in range(1, n + 1): li = list(map(int, input().split())) friend_dict[i] = li mini = float("inf") at_source = None for i in range(1, n + 1): if bfs(i, friend_dict) < mini: mini = bfs(i, friend_dict) at_source = i res = mini / n res = round(res, 6) print(at_source, f"{res:.6f}")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
Anna Hazare is a well known social activist in India. On 5th April, 2011 he started "Lokpal Bill movement". Chef is very excited about this movement. He is thinking of contributing to it. He gathers his cook-herd and starts thinking about how our community can contribute to this. All of them are excited about this too, but no one could come up with any idea. Cooks were slightly disappointed with this and went to consult their friends. One of the geekiest friend gave them the idea of spreading knowledge through Facebook. But we do not want to spam people's wall. So our cook came up with the idea of dividing Facebook users into small friend groups and then identify the most popular friend in each group and post on his / her wall. They started dividing users into groups of friends and identifying the most popular amongst them. The notoriety of a friend is defined as the averaged distance from all the other friends in his / her group. This measure considers the friend himself, and the trivial distance of '0' that he / she has with himself / herself. The most popular friend in a group is the friend whose notoriety is least among all the friends in the group. Distance between X and Y is defined as follows: Minimum number of profiles that X needs to visit for reaching Y's profile(Including Y's profile). X can open only those profiles which are in the friend list of the current opened profile. For Example: Suppose A is friend of B. B has two friends C and D. E is a friend of D. Now, the distance between A and B is 1, A and C is 2, C and E is 3. So, one of our smart cooks took the responsibility of identifying the most popular friend in each group and others will go to persuade them for posting. This cheeky fellow knows that he can release his burden by giving this task as a long contest problem. Now, he is asking you to write a program to identify the most popular friend among all the friends in each group. Also, our smart cook wants to know the average distance of everyone from the most popular friend. ------ Input ------ Friends in a group are labelled with numbers to hide their Facebook identity. The first line of input contains the number of groups in which users are divided. First line of each group contains the number of friends that belong to that group. i^{th} line of each group contains the space separated friend list of 'i'. You are assured that each friend's profile can be accessed by the profile of every other friend by following some sequence of profile visits. ------ Output ------ Your output contains the most popular friend name label along with the average distance (space separated) from all other friends (including himself / herself) in six digits of precision. There might be multiple most popular friend, in that case output the friend labelled with least number. ------ Note: ------ Each person in a group have atleast one friend and he/she cannot be in his/her own friend list. Number of friends in a group cannot be more than 100. There are atmost 100 groups. ----- Sample Input 1 ------ 1 6 3 5 1 4 3 5 6 2 4 6 4 5 ----- Sample Output 1 ------ 4 1.166667
G = int(input()) tf = [0] * G m = [] for g in range(0, G): tf[g] = int(input()) m.append({}) for i in range(0, tf[g]): m[g].update({(i + 1): [int(p) for p in input().split()]}) for grp in range(0, G): dict1 = m[grp] u = [] mmember = 1 for member in range(0, tf[grp]): visited = [0] * tf[grp] visited[member] = 1 dist = 0 q = [] time = 1 q.append([member, time]) while 0 in visited: o, y = q.pop(0) for v in dict1[o + 1]: if visited[v - 1] == 0: visited[v - 1] = 1 q.append([v - 1, y + 1]) dist = dist + y u.append(dist) ans = u.index(min(u)) + 1 print(ans, "{:.6f}".format(round(min(u) / tf[grp], 6)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR DICT BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR WHILE NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER
Anna Hazare is a well known social activist in India. On 5th April, 2011 he started "Lokpal Bill movement". Chef is very excited about this movement. He is thinking of contributing to it. He gathers his cook-herd and starts thinking about how our community can contribute to this. All of them are excited about this too, but no one could come up with any idea. Cooks were slightly disappointed with this and went to consult their friends. One of the geekiest friend gave them the idea of spreading knowledge through Facebook. But we do not want to spam people's wall. So our cook came up with the idea of dividing Facebook users into small friend groups and then identify the most popular friend in each group and post on his / her wall. They started dividing users into groups of friends and identifying the most popular amongst them. The notoriety of a friend is defined as the averaged distance from all the other friends in his / her group. This measure considers the friend himself, and the trivial distance of '0' that he / she has with himself / herself. The most popular friend in a group is the friend whose notoriety is least among all the friends in the group. Distance between X and Y is defined as follows: Minimum number of profiles that X needs to visit for reaching Y's profile(Including Y's profile). X can open only those profiles which are in the friend list of the current opened profile. For Example: Suppose A is friend of B. B has two friends C and D. E is a friend of D. Now, the distance between A and B is 1, A and C is 2, C and E is 3. So, one of our smart cooks took the responsibility of identifying the most popular friend in each group and others will go to persuade them for posting. This cheeky fellow knows that he can release his burden by giving this task as a long contest problem. Now, he is asking you to write a program to identify the most popular friend among all the friends in each group. Also, our smart cook wants to know the average distance of everyone from the most popular friend. ------ Input ------ Friends in a group are labelled with numbers to hide their Facebook identity. The first line of input contains the number of groups in which users are divided. First line of each group contains the number of friends that belong to that group. i^{th} line of each group contains the space separated friend list of 'i'. You are assured that each friend's profile can be accessed by the profile of every other friend by following some sequence of profile visits. ------ Output ------ Your output contains the most popular friend name label along with the average distance (space separated) from all other friends (including himself / herself) in six digits of precision. There might be multiple most popular friend, in that case output the friend labelled with least number. ------ Note: ------ Each person in a group have atleast one friend and he/she cannot be in his/her own friend list. Number of friends in a group cannot be more than 100. There are atmost 100 groups. ----- Sample Input 1 ------ 1 6 3 5 1 4 3 5 6 2 4 6 4 5 ----- Sample Output 1 ------ 4 1.166667
class Graph: def __init__(self, V): self.V = V self.adj = [[] for i in range(V + 1)] def addEdge(self, v, w): self.adj[v].append(w) self.adj[w].append(v) def BFS(self, i): level = ["useless"] for j in range(1, self.V + 1): level.append(-1) Q = [] level[i] = 0 Q.append(i) while Q: j = Q[0] del Q[0] for k in self.adj[j]: if level[k] == -1: level[k] = 1 + level[j] Q.append(k) return level t = int(input()) for i in range(t): N = int(input()) g = Graph(N) for i in range(1, N + 1): arr = [int(p) for p in input().split()] g.adj[i] = arr countarr = ["useless"] for i in range(1, N + 1): countarr.append(0) for i in range(1, N + 1): arrlist = g.BFS(i) countarr[i] = sum(arrlist[1:]) / N maxx = min(countarr[1:]) ind = countarr.index(maxx) arr2 = g.BFS(ind) ans = sum(arr2[1:]) / N print(ind, "{:.6f}".format(ans))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_CALL STRING VAR
Anna Hazare is a well known social activist in India. On 5th April, 2011 he started "Lokpal Bill movement". Chef is very excited about this movement. He is thinking of contributing to it. He gathers his cook-herd and starts thinking about how our community can contribute to this. All of them are excited about this too, but no one could come up with any idea. Cooks were slightly disappointed with this and went to consult their friends. One of the geekiest friend gave them the idea of spreading knowledge through Facebook. But we do not want to spam people's wall. So our cook came up with the idea of dividing Facebook users into small friend groups and then identify the most popular friend in each group and post on his / her wall. They started dividing users into groups of friends and identifying the most popular amongst them. The notoriety of a friend is defined as the averaged distance from all the other friends in his / her group. This measure considers the friend himself, and the trivial distance of '0' that he / she has with himself / herself. The most popular friend in a group is the friend whose notoriety is least among all the friends in the group. Distance between X and Y is defined as follows: Minimum number of profiles that X needs to visit for reaching Y's profile(Including Y's profile). X can open only those profiles which are in the friend list of the current opened profile. For Example: Suppose A is friend of B. B has two friends C and D. E is a friend of D. Now, the distance between A and B is 1, A and C is 2, C and E is 3. So, one of our smart cooks took the responsibility of identifying the most popular friend in each group and others will go to persuade them for posting. This cheeky fellow knows that he can release his burden by giving this task as a long contest problem. Now, he is asking you to write a program to identify the most popular friend among all the friends in each group. Also, our smart cook wants to know the average distance of everyone from the most popular friend. ------ Input ------ Friends in a group are labelled with numbers to hide their Facebook identity. The first line of input contains the number of groups in which users are divided. First line of each group contains the number of friends that belong to that group. i^{th} line of each group contains the space separated friend list of 'i'. You are assured that each friend's profile can be accessed by the profile of every other friend by following some sequence of profile visits. ------ Output ------ Your output contains the most popular friend name label along with the average distance (space separated) from all other friends (including himself / herself) in six digits of precision. There might be multiple most popular friend, in that case output the friend labelled with least number. ------ Note: ------ Each person in a group have atleast one friend and he/she cannot be in his/her own friend list. Number of friends in a group cannot be more than 100. There are atmost 100 groups. ----- Sample Input 1 ------ 1 6 3 5 1 4 3 5 6 2 4 6 4 5 ----- Sample Output 1 ------ 4 1.166667
def bfs(source, adj): visited = set() visited.add(source) queue = [] queue.append((source, 0)) s = 0 while queue: curr, dist = queue.pop(0) for node in adj[curr]: if node not in visited: visited.add(node) queue.append((node, dist + 1)) s += dist + 1 return s for _ in range(int(input())): n = int(input()) friends = {} for i in range(1, n + 1): friends[i] = tuple(map(int, input().split())) distance = [] for i in range(1, n + 1): distance.append(bfs(i, friends)) most_popular = distance.index(min(distance)) + 1 f = min(distance) / n average = round(f, 6) print(most_popular, "{:.6f}".format(f))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL STRING VAR
Anna Hazare is a well known social activist in India. On 5th April, 2011 he started "Lokpal Bill movement". Chef is very excited about this movement. He is thinking of contributing to it. He gathers his cook-herd and starts thinking about how our community can contribute to this. All of them are excited about this too, but no one could come up with any idea. Cooks were slightly disappointed with this and went to consult their friends. One of the geekiest friend gave them the idea of spreading knowledge through Facebook. But we do not want to spam people's wall. So our cook came up with the idea of dividing Facebook users into small friend groups and then identify the most popular friend in each group and post on his / her wall. They started dividing users into groups of friends and identifying the most popular amongst them. The notoriety of a friend is defined as the averaged distance from all the other friends in his / her group. This measure considers the friend himself, and the trivial distance of '0' that he / she has with himself / herself. The most popular friend in a group is the friend whose notoriety is least among all the friends in the group. Distance between X and Y is defined as follows: Minimum number of profiles that X needs to visit for reaching Y's profile(Including Y's profile). X can open only those profiles which are in the friend list of the current opened profile. For Example: Suppose A is friend of B. B has two friends C and D. E is a friend of D. Now, the distance between A and B is 1, A and C is 2, C and E is 3. So, one of our smart cooks took the responsibility of identifying the most popular friend in each group and others will go to persuade them for posting. This cheeky fellow knows that he can release his burden by giving this task as a long contest problem. Now, he is asking you to write a program to identify the most popular friend among all the friends in each group. Also, our smart cook wants to know the average distance of everyone from the most popular friend. ------ Input ------ Friends in a group are labelled with numbers to hide their Facebook identity. The first line of input contains the number of groups in which users are divided. First line of each group contains the number of friends that belong to that group. i^{th} line of each group contains the space separated friend list of 'i'. You are assured that each friend's profile can be accessed by the profile of every other friend by following some sequence of profile visits. ------ Output ------ Your output contains the most popular friend name label along with the average distance (space separated) from all other friends (including himself / herself) in six digits of precision. There might be multiple most popular friend, in that case output the friend labelled with least number. ------ Note: ------ Each person in a group have atleast one friend and he/she cannot be in his/her own friend list. Number of friends in a group cannot be more than 100. There are atmost 100 groups. ----- Sample Input 1 ------ 1 6 3 5 1 4 3 5 6 2 4 6 4 5 ----- Sample Output 1 ------ 4 1.166667
tn = int(input()) for _ in range(tn): n = int(input()) ar = dict() for i in range(1, n + 1): ar[i] = list(map(int, input().split())) mvsm, midx = -1, -1 for i in range(1, n + 1): visli = {i} tdis = 0 queue = [(i, 0)] while queue: nd, dis = queue.pop(0) tdis += dis for x in ar[nd]: if x not in visli: visli.add(x) queue.append((x, dis + 1)) if mvsm == -1: mvsm = tdis midx = i elif mvsm > tdis: mvsm = tdis midx = i print(midx, "{:.6f}".format(mvsm / n))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL STRING BIN_OP VAR VAR
Anna Hazare is a well known social activist in India. On 5th April, 2011 he started "Lokpal Bill movement". Chef is very excited about this movement. He is thinking of contributing to it. He gathers his cook-herd and starts thinking about how our community can contribute to this. All of them are excited about this too, but no one could come up with any idea. Cooks were slightly disappointed with this and went to consult their friends. One of the geekiest friend gave them the idea of spreading knowledge through Facebook. But we do not want to spam people's wall. So our cook came up with the idea of dividing Facebook users into small friend groups and then identify the most popular friend in each group and post on his / her wall. They started dividing users into groups of friends and identifying the most popular amongst them. The notoriety of a friend is defined as the averaged distance from all the other friends in his / her group. This measure considers the friend himself, and the trivial distance of '0' that he / she has with himself / herself. The most popular friend in a group is the friend whose notoriety is least among all the friends in the group. Distance between X and Y is defined as follows: Minimum number of profiles that X needs to visit for reaching Y's profile(Including Y's profile). X can open only those profiles which are in the friend list of the current opened profile. For Example: Suppose A is friend of B. B has two friends C and D. E is a friend of D. Now, the distance between A and B is 1, A and C is 2, C and E is 3. So, one of our smart cooks took the responsibility of identifying the most popular friend in each group and others will go to persuade them for posting. This cheeky fellow knows that he can release his burden by giving this task as a long contest problem. Now, he is asking you to write a program to identify the most popular friend among all the friends in each group. Also, our smart cook wants to know the average distance of everyone from the most popular friend. ------ Input ------ Friends in a group are labelled with numbers to hide their Facebook identity. The first line of input contains the number of groups in which users are divided. First line of each group contains the number of friends that belong to that group. i^{th} line of each group contains the space separated friend list of 'i'. You are assured that each friend's profile can be accessed by the profile of every other friend by following some sequence of profile visits. ------ Output ------ Your output contains the most popular friend name label along with the average distance (space separated) from all other friends (including himself / herself) in six digits of precision. There might be multiple most popular friend, in that case output the friend labelled with least number. ------ Note: ------ Each person in a group have atleast one friend and he/she cannot be in his/her own friend list. Number of friends in a group cannot be more than 100. There are atmost 100 groups. ----- Sample Input 1 ------ 1 6 3 5 1 4 3 5 6 2 4 6 4 5 ----- Sample Output 1 ------ 4 1.166667
import sys if len(sys.argv) >= 2: sys.stdin = open(sys.argv[1]) def dijkstra(adj, src): n = len(adj) dist = [float("inf")] * n dist[src] = 0 q = [src] while q: u = q.pop(0) for v in adj[u]: if dist[v] > dist[u] + 1: dist[v] = dist[u] + 1 q.append(v) return dist def avg(adj, src): dist = dijkstra(adj, src) _sum = 0 for i in range(1, len(dist)): _sum += dist[i] return _sum / (len(dist) - 1) def solve(): n = int(input()) adj = [set() for _ in range(n + 1)] for i in range(1, n + 1): a = input().split() adj[i] = set(map(int, a)) _min = 1000000000.0 friend = 1 for i in range(1, n + 1): val = avg(adj, i) if val < _min: _min = val friend = i if friend else friend print(friend, f"{_min:.6f}") t = 1 t = int(input()) for _ in range(t): solve()
IMPORT IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Anna Hazare is a well known social activist in India. On 5th April, 2011 he started "Lokpal Bill movement". Chef is very excited about this movement. He is thinking of contributing to it. He gathers his cook-herd and starts thinking about how our community can contribute to this. All of them are excited about this too, but no one could come up with any idea. Cooks were slightly disappointed with this and went to consult their friends. One of the geekiest friend gave them the idea of spreading knowledge through Facebook. But we do not want to spam people's wall. So our cook came up with the idea of dividing Facebook users into small friend groups and then identify the most popular friend in each group and post on his / her wall. They started dividing users into groups of friends and identifying the most popular amongst them. The notoriety of a friend is defined as the averaged distance from all the other friends in his / her group. This measure considers the friend himself, and the trivial distance of '0' that he / she has with himself / herself. The most popular friend in a group is the friend whose notoriety is least among all the friends in the group. Distance between X and Y is defined as follows: Minimum number of profiles that X needs to visit for reaching Y's profile(Including Y's profile). X can open only those profiles which are in the friend list of the current opened profile. For Example: Suppose A is friend of B. B has two friends C and D. E is a friend of D. Now, the distance between A and B is 1, A and C is 2, C and E is 3. So, one of our smart cooks took the responsibility of identifying the most popular friend in each group and others will go to persuade them for posting. This cheeky fellow knows that he can release his burden by giving this task as a long contest problem. Now, he is asking you to write a program to identify the most popular friend among all the friends in each group. Also, our smart cook wants to know the average distance of everyone from the most popular friend. ------ Input ------ Friends in a group are labelled with numbers to hide their Facebook identity. The first line of input contains the number of groups in which users are divided. First line of each group contains the number of friends that belong to that group. i^{th} line of each group contains the space separated friend list of 'i'. You are assured that each friend's profile can be accessed by the profile of every other friend by following some sequence of profile visits. ------ Output ------ Your output contains the most popular friend name label along with the average distance (space separated) from all other friends (including himself / herself) in six digits of precision. There might be multiple most popular friend, in that case output the friend labelled with least number. ------ Note: ------ Each person in a group have atleast one friend and he/she cannot be in his/her own friend list. Number of friends in a group cannot be more than 100. There are atmost 100 groups. ----- Sample Input 1 ------ 1 6 3 5 1 4 3 5 6 2 4 6 4 5 ----- Sample Output 1 ------ 4 1.166667
class Connections: def __init__(self, n): self.num = n self.friends = {} self.lst = [] self.social() self.length() def social(self): for i in range(1, self.num + 1): self.friends[i] = tuple(map(int, input().split())) def search(self, vec): visited = set() visited.add(vec) queue = [(vec, 0)] s = 0 while queue: q = queue[0] queue.pop(0) for i in self.friends[q[0]]: if i not in visited: queue.append((i, q[1] + 1)) visited.add(i) s += q[1] + 1 return s def length(self): for i in range(1, self.num + 1): self.lst.append(self.search(i)) T = int(input()) for _ in range(T): con = Connections(int(input())) x = min(con.lst) i = con.lst.index(x) + 1 print(i, "{:.6f}".format(round(x / con.num, 6)))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
D = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} def chef(meals): freq = [0] * 32 for s in meals: temp = 0 for i in range(len(s)): temp |= 1 << D[s[i]] freq[temp] += 1 res = 0 for i in range(32): for j in range(i + 1, 32): if i | j == 31: res += freq[i] * freq[j] res += freq[31] * (freq[31] - 1) // 2 return res def main(): tc = int(input()) for t in range(tc): n = int(input()) meals = [] for _ in range(n): meals.append(input()) print(chef(meals)) main()
ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def solve(): n = int(input()) freq_comb = [0] * 32 for i in range(n): string = input() mask = 0 for s in string: if s == "a": mask = mask | 1 << 0 elif s == "e": mask = mask | 1 << 1 elif s == "i": mask = mask | 1 << 2 elif s == "o": mask = mask | 1 << 3 else: mask = mask | 1 << 4 freq_comb[mask] += 1 ans = 0 for i in range(1, 32): for j in range(i + 1, 32): if i | j == 31: ans += freq_comb[i] * freq_comb[j] ans += freq_comb[31] * (freq_comb[31] - 1) // 2 return ans t = int(input()) for _ in range(t): print(solve())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
shifts = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} for _ in range(int(input())): n = int(input()) bitmasks = [0] * 2**5 for __ in range(n): s = input() b = 0 for i in s: b = b | 1 << shifts[i] bitmasks[b] += 1 res = 0 for i in range(len(bitmasks)): for j in range(i + 1, len(bitmasks)): if i | j == 31: res += bitmasks[i] * bitmasks[j] res += bitmasks[-1] * (bitmasks[-1] - 1) // 2 print(res)
ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) for _ in range(1, t + 1): n = int(input()) fr = [0] * 32 for k in range(n): s = set(input()) mask = 0 for i in s: if i == "a": mask = mask | 1 << 0 if i == "e": mask = mask | 1 << 1 if i == "i": mask = mask | 1 << 2 if i == "o": mask = mask | 1 << 3 if i == "u": mask = mask | 1 << 4 fr[mask] += 1 ans = 0 for i in range(1, 32): for j in range(i + 1, 32): if i | j == 31: ans += fr[i] * fr[j] ans += fr[31] * (fr[31] - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
v = "aeiou" t = int(input()) for _ in range(t): n = int(input()) c = 0 l = [0] * 32 for i in range(n): s = set(input()) d = dict() for j in v: d[j] = 0 for j in s: if j in v: d[j] += 1 val = d["a"] * 16 | d["e"] * 8 | d["i"] * 4 | d["o"] * 2 | d["u"] l[val] += 1 for i in range(1, 31): for j in range(i + 1, 32): if (l[i] and l[j]) and i | j == 31: c += l[i] * l[j] c += l[31] * (l[31] - 1) // 2 print(c)
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR STRING NUMBER BIN_OP VAR STRING NUMBER BIN_OP VAR STRING NUMBER BIN_OP VAR STRING NUMBER VAR STRING VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) while t: n = int(input()) lis = [] s = "" a = [0] * n e = [0] * n i = [0] * n o = [0] * n u = [0] * n bt = [0] * n fr = [0] * 32 for x in range(n): s = input() l = len(s) for j in s: if j == "a": a[x] = 1 elif j == "e": e[x] = 1 elif j == "i": i[x] = 1 elif j == "o": o[x] = 1 elif j == "u": u[x] = 1 bt[x] = a[x] * 16 + e[x] * 8 + i[x] * 4 + o[x] * 2 + u[x] fr[bt[x]] = fr[bt[x]] + 1 c = 0 for l in range(31): for g in range(l + 1, 32): if (fr[l] != 0 and fr[g] != 0) and l | g == 31: c = c + fr[l] * fr[g] b = fr[31] c = c + b * (b - 1) // 2 print(c) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING ASSIGN VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
for _ in range(int(input())): n = int(input()) l = [] yt = (1 << 5) - 1 d = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} for _ in range(n): l.append(str(input())) v = {} for i in range(1, 32): v[i] = 0 for i in range(n): s = set() m = 0 for j in range(len(l[i])): s.add(d[l[i][j]]) for x in s: m += 1 << x v[m] += 1 ans = 0 for i in range(1, 32): for j in range(1, 32): if i | j == yt: if i == j: ans += v[i] * (v[j] - 1) else: ans += v[i] * v[j] print(int(ans / 2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def maskit(x): j = 0 for i in x: if i == "a": j = j | 1 << 4 elif i == "e": j = j | 1 << 3 elif i == "i": j = j | 1 << 2 elif i == "o": j = j | 1 << 1 elif i == "u": j = j | 1 << 0 return j t = int(input()) for _ in range(t): n = int(input()) dish = list() for i in range(n): x = input() dish.append(maskit(x)) c = 0 l = [0] * 32 for i in range(n): l[dish[i]] += 1 for i in range(32): for j in range(i + 1, 32): if i | j == 31: c += l[i] * l[j] c += l[31] * (l[31] - 1) // 2 print(c)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
T = int(input()) for i in range(T): N = int(input()) F = 1 << 5 l = [0] * F l1 = [] for j in range(N): x = input() l1.append(x) for j in range(len(l1)): mask = 0 for k in l1[j]: if k == "a": mask = mask | 1 << 0 elif k == "e": mask = mask | 1 << 1 elif k == "i": mask = mask | 1 << 2 elif k == "o": mask = mask | 1 << 3 elif k == "u": mask = mask | 1 << 4 l[mask] += 1 s = 0 for j in range(32): for k in range(j + 1, 32): if j | k == 31: s = s + l[j] * l[k] if l[31] != 0: s = s + l[31] * (l[31] - 1) // 2 print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
T = int(input()) for i in range(T): n = int(input()) lst = [0] * (1 << 5) for j in range(n): ans = 0 for k in input(): if k == "a": ans |= 1 << 0 elif k == "e": ans |= 1 << 1 elif k == "i": ans |= 1 << 2 elif k == "o": ans |= 1 << 3 else: ans |= 1 << 4 lst[ans] += 1 ans = 0 for j in range(1, 31): for k in range(j, 32): if j | k == 31: ans += lst[j] * lst[k] ans += lst[31] * (lst[31] - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def funct(li, n): f = [(0) for i in range(32)] for i in range(len(li)): mask = 0 for j in li[i]: if j == "a": mask = mask | 1 << 0 if j == "e": mask = mask | 1 << 1 if j == "i": mask = mask | 1 << 2 if j == "o": mask = mask | 1 << 3 if j == "u": mask = mask | 1 << 4 f[mask] += 1 count = 0 for i in range(32): for j in range(i + 1, 32): if i | j == 31: count += f[i] * f[j] count = count + f[31] * (f[31] - 1) // 2 return count t = int(input()) for i in range(t): n = int(input()) li = [] for i in range(n): s = input() li.append(set(s)) print(funct(li, n))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) while t: n = int(input()) arr = [0] * 32 for i in range(n): tmp = input() mask = 0 for j in tmp: if "a" is j: mask |= 16 continue if "e" is j: mask |= 8 continue if "i" is j: mask |= 4 continue if "o" is j: mask |= 2 continue if "u" is j: mask |= 1 arr[mask] += 1 mycount = arr[31] * (arr[31] - 1) // 2 for i in range(32): for j in range(i + 1, 32): mycount += (0, arr[i] * arr[j])[i | j == 31] print(mycount) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF STRING VAR VAR NUMBER IF STRING VAR VAR NUMBER IF STRING VAR VAR NUMBER IF STRING VAR VAR NUMBER IF STRING VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
for _ in range(int(input())): n = int(input()) l = [0] * 32 d = {"a": 1, "e": 2, "i": 4, "o": 8, "u": 16} for i in range(n): s = list(set(list(input()))) m = 0 for j in s: m |= d[j] l[m] += 1 ans = l[31] * (l[31] - 1) // 2 for i in range(1, 32): for j in range(i + 1, 32): if i | j == 31: ans += l[i] * l[j] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
p = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} for _ in range(int(input())): n = int(input()) l = [] d = {i: (0) for i in range(0, 32)} for i in range(n): s = input() s = set(s) val = 0 for i in s: val = val | 1 << p[i] d[val] += 1 l = [i for i in d] n = len(l) ans = 0 for i in range(n): for j in range(i, n): findor = l[i] | l[j] if findor == 31: if l[i] == l[j] == 31: ans = ans + d[31] * (d[31] - 1) // 2 else: ans += d[l[i]] * d[l[j]] print(ans)
ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def evaluatestring(dp, string): mask = 0 for s in string: if s == "a": mask |= 1 << 0 elif s == "e": mask |= 1 << 1 elif s == "i": mask |= 1 << 2 elif s == "o": mask |= 1 << 3 elif s == "u": mask |= 1 << 4 dp[mask] += 1 return dp testcase = int(input()) while testcase: dp = [0] * 32 number = int(input()) for _ in range(number): string = input().lower() dp = evaluatestring(dp, string) answer = 0 for i in range(32): for j in range(i + 1, 32): if i | j == 31: answer += dp[i] * dp[j] answer += dp[31] * (dp[31] - 1) // 2 print(answer) testcase -= 1
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
N = 1 << 5 for t in range(int(input())): n = int(input()) frq = [(0) for i in range(N)] for i in range(n): d = list(input()) index = 0 for j in d: if j == "a": index |= 1 elif j == "e": index |= 2 elif j == "i": index |= 4 elif j == "o": index |= 8 elif j == "u": index |= 16 frq[index] += 1 ans = 0 for i in range(1, N): for j in range(i + 1, N): if i | j == 31: ans += frq[i] * frq[j] ans += frq[31] * (frq[31] - 1) // 2 print(ans)
ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
import itertools t = int(input()) for i in range(t): vowels = ["a", "e", "i", "o", "u"] l = [] n = int(input()) for i in range(n): l.append(list(set(input()))) combinations = [] for i in range(len(vowels) + 1): combinations += list(itertools.combinations(vowels, i)) for i in range(len(combinations)): combinations[i] = list(combinations[i]) f = {} for i in combinations: a = "".join(i) f[a] = 0 for i in l: l2 = "" for j in range(len(i)): if i[j] in vowels: l2 += i[j] l2 = set(l2) l2 = list(l2) l2 = sorted(l2) l2 = "".join(l2) f[l2] += 1 sum1 = 0 for i in f: if i != "aeiou": sum1 += f[i] final = {} for i in f: if f[i] > 0: final[i] = f[i] f1 = list(final) ans = 0 for i in range(len(f1)): for j in range(i + 1, len(f1)): if f1[i] != "aeiou" and f1[j] != "aeiou": k = f1[i] + f1[j] k = set(k) k = list(k) k = sorted(k) k = "".join(k) if k == "aeiou": ans += final[f1[i]] * final[f1[j]] if f1[i] == "aeiou": ans += final[f1[i]] * sum1 + (final[f1[i]] - 1) * final[f1[i]] // 2 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR STRING VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def show(t): for _ in range(t): a = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ] n = int(input()) count = 0 count_num = 0 for lonavla in range(n): str1 = set(input()) res = 0 if "a" in str1: res = res | 16 if "e" in str1: res = res | 8 if "i" in str1: res = res | 4 if "o" in str1: res = res | 2 if "u" in str1: res = res | 1 a[res] += 1 for i in range(31): for j in range(i + 1, 32): if i | j >= 31: count = count + a[i] * a[j] if a[31] > 0: count = count + a[31] * (a[31] - 1) // 2 print(count) show(int(input()))
FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) while t: n = int(input()) d = [] mymap = {} for i in range(n): d.append(input()) x = 0 if "a" in d[i]: x += 16 if "e" in d[i]: x += 8 if "i" in d[i]: x += 4 if "o" in d[i]: x += 2 if "u" in d[i]: x += 1 mymap.setdefault(x, 0) mymap[x] += 1 mymap.setdefault(31, 0) mycount = int(mymap[31] * (mymap[31] - 1) // 2) for i in range(32): for j in range(i + 1, 32): mymap.setdefault(i, 0) mymap.setdefault(j, 0) if i | j == 31: mycount += mymap[i] * mymap[j] print(mycount) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF STRING VAR VAR VAR NUMBER IF STRING VAR VAR VAR NUMBER IF STRING VAR VAR VAR NUMBER IF STRING VAR VAR VAR NUMBER IF STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def decimal(bin_arr): return ( 2 * 2 * 2 * 2 * bin_arr["a"] + 2 * 2 * 2 * bin_arr["e"] + 2 * 2 * bin_arr["i"] + 2 * bin_arr["o"] + bin_arr["u"] ) T = int(input()) for i in range(T): count = 0 arr = [0] * 32 N = int(input()) list_D = [] for x in range(N): vowel_num = {"a": 0, "e": 0, "i": 0, "o": 0, "u": 0} for i in input(): vowel_num[i] = 1 list_D.append(vowel_num) arr[decimal(list_D[x])] += 1 count = arr[31] * (arr[31] - 1) // 2 for j in range(32): for k in range(j + 1, 32): if j | k == 31: count += arr[j] * arr[k] print(count)
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER VAR STRING BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR STRING BIN_OP BIN_OP NUMBER NUMBER VAR STRING BIN_OP NUMBER VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
for _ in range(int(input())): n = int(input()) dp = dict() maps = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} for __ in range(n): s = input() mask = 0 for i in s: mask = mask | 1 << maps[i] if mask not in dp: dp[mask] = 1 else: dp[mask] += 1 ans = 0 for i in range(1, 32): for j in range(i + 1, 32): if i | j == 31: if i in dp and j in dp: ans += dp[i] * dp[j] if 31 in dp: ans += dp[31] * (dp[31] - 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) di = {"a": 16, "e": 8, "i": 4, "o": 2, "u": 1} for i in range(t): n = int(input()) d = {} for j in range(n): te = set(input()) l = 0 for k in te: l = l + di[k] if l in d: d[l] = d[l] + 1 else: d[l] = 1 li = list(d.keys()) li.sort(reverse=True) count = 0 if li[0] == 31: count = sum(range(1, d[31])) for j in range(len(li)): for k in range(j + 1, len(li)): if li[j] | li[k] == 31: count = count + d[li[j]] * d[li[k]] print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
for _ in range(int(input())): N = int(input()) F = [0] * 32 for i in range(N): mask = 0 s = input() for j in s: if j == "a": mask = mask | 1 << 0 elif j == "e": mask = mask | 1 << 1 elif j == "i": mask = mask | 1 << 2 elif j == "o": mask = mask | 1 << 3 else: mask = mask | 1 << 4 F[mask] += 1 res = 0 for i in range(1, 32): if F[i] > 0: for j in range(i + 1, 32): if i | j == 31 and F[j] > 0: res = res + F[i] * F[j] res = res + F[31] * (F[31] - 1) // 2 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def li(): return list(map(int, input().split())) def si(): return input().split() def ii(): return int(input()) def ip(): return input() def make(): d = {} d["a"] = 0 d["e"] = 0 d["i"] = 0 d["o"] = 0 d["u"] = 0 return d ar = [ [""], ["u"], ["o"], ["o", "u"], ["i"], ["i", "u"], ["i", "o"], ["i", "o", "u"], ["e"], ["e", "u"], ["e", "o"], ["e", "o", "u"], ["e", "i"], ["e", "i", "u"], ["e", "i", "o"], ["e", "i", "o", "u"], ["a"], ["a", "u"], ["a", "o"], ["a", "o", "u"], ["a", "i"], ["a", "i", "u"], ["a", "i", "o"], ["a", "i", "o", "u"], ["a", "e"], ["a", "e", "u"], ["a", "e", "o"], ["a", "e", "o", "u"], ["a", "e", "i"], ["a", "e", "i", "u"], ["a", "e", "i", "o"], ["a", "e", "i", "o", "u"], ] for tastcas in range(int(input())): n = ii() c = [0] * 32 ans = 0 nn = n - 1 for i in range(n): s = input() v = make() w = "" for i in s: v[i] = 1 for k, v in sorted(v.items()): w += str(v) c[int(w, 2)] += 1 for i in range(1, 31): for j in range(i + 1, 31): if len(set(ar[i] + ar[j])) == 5: ans += c[i] * c[j] tt = nn - c[-1] ans += nn * (nn + 1) // 2 - tt * (tt + 1) // 2 print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER RETURN VAR ASSIGN VAR LIST LIST STRING LIST STRING LIST STRING LIST STRING STRING LIST STRING LIST STRING STRING LIST STRING STRING LIST STRING STRING STRING LIST STRING LIST STRING STRING LIST STRING STRING LIST STRING STRING STRING LIST STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING LIST STRING STRING LIST STRING STRING LIST STRING STRING STRING LIST STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
for _ in range(int(input())): n = int(input()) F = [0] * 32 res = 0 for i in range(n): s = set(input()) mask = 0 for ch in s: if ch == "a": mask |= 1 << 0 if ch == "e": mask |= 1 << 1 if ch == "i": mask |= 1 << 2 if ch == "o": mask |= 1 << 3 if ch == "u": mask |= 1 << 4 F[mask] += 1 for i in range(1, 32): for j in range(i + 1, 32): if i | j == 31: res += F[i] * F[j] res += F[31] * (F[31] - 1) // 2 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def solve(d, n): f = [(0) for i in range(32)] for string in d: m = 0 for ch in string: m = m | 1 << dct[ch] if m == 31: break f[m] += 1 res = 0 for i in range(1, 31): for j in range(i + 1, 32): if i | j == 31: res += f[i] * f[j] res += f[31] * (f[31] - 1) // 2 return res dct = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} for _ in range(int(input())): n = int(input()) d = [] for k in range(n): di = input() d.append(di) print(solve(d, n))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
from sys import stdin, stdout t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) arr = [0] * 32 l = [] for i in range(n): s = stdin.readline() f = 0 set_s = set(s) if "u" in set_s: f = f | 1 << 0 if "o" in set_s: f = f | 1 << 1 if "i" in set_s: f = f | 1 << 2 if "e" in set_s: f = f | 1 << 3 if "a" in set_s: f = f | 1 << 4 arr[f] += 1 ans = 0 for i in range(1, 32): for j in range(i + 1, 32): if i | j == 31: ans += arr[i] * arr[j] if arr[31] > 1: n = arr[31] ans += n * (n - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
dict = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} for _ in range(int(input())): N = int(input()) strs = [] arr = [] for i in range(N): strs.append(str(input())) for s in strs: temp = [0] * 5 for i in s: temp[dict[i]] = 1 arr.append(temp) br = [] for a in arr: s = [str(i) for i in a] res = str("".join(s)) br.append(res) s_dict = {} for a in br: if a in s_dict: s_dict[a] += 1 else: s_dict[a] = 1 f = [] for s in s_dict: f.append(s) f.append(s_dict[s]) key, value = f[::2], f[1::2] pairs = 0 if "11111" in s_dict: pairs = int(s_dict["11111"] * (s_dict["11111"] - 1) / 2) for i in range(len(key)): for j in range(i + 1, len(key)): flag = 1 for k in range(5): if key[i][k] == "0" and key[j][k] == "0": flag = 0 if flag == 1: pairs += value[i] * value[j] print(pairs)
ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING BIN_OP VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
T = int(input()) for _ in range(T): N = int(input()) l = [] a = [(0) for i in range(32)] for i in range(N): s = input() x = 0 a1 = 0 a2 = 0 a3 = 0 a4 = 0 a5 = 0 if "a" in s: a1 = 1 if "e" in s: a2 = 2 if "i" in s: a3 = 4 if "o" in s: a4 = 8 if "u" in s: a5 = 16 x = a1 + a2 + a3 + a4 + a5 a[x] += 1 c = 0 for k in range(1, 31): for l in range(k + 1, 32): if k | l == 31 and a[k] != 0 and a[l] != 0: c += a[k] * a[l] c = c + a[31] * (a[31] - 1) // 2 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP 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 IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def solve(s): l = ["0"] * 5 d = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} for i in s: if i in "aeiou" or i in "AEIOU": l[d[i.lower()]] = "1" l.reverse() return "".join(l) def main(): for _ in range(int(input())): n = int(input()) k = 1 << 5 l = [0] * k for i in range(n): s = input() a = solve(s) a = int(a, 2) l[a] += 1 c = 0 for i in range(k): for j in range(i + 1, k): if l[i] and l[j] and i | j == k - 1: c += l[i] * l[j] c += l[31] * (l[31] - 1) // 2 print(c) main()
FUNC_DEF ASSIGN VAR BIN_OP LIST STRING NUMBER ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR RETURN FUNC_CALL STRING VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def fun(): test = int(input()) for t in range(test): n = int(input()) d = dict() arr = set() for _ in range(n): s1 = set(input().strip()) vowels = ["a", "e", "i", "o", "u"] val = 0 for i in range(5): if vowels[i] in s1: val += 1 << i d[val] = d.get(val, 0) + 1 arr.add(val) ans = 0 m = len(arr) arr = list(arr) for i in range(m): for j in range(i + 1, m): if arr[i] | arr[j] == 31: ans += d[arr[i]] * d[arr[j]] x = d.get(31, 0) ans += x * (x - 1) // 2 print(ans) fun()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) for _ in range(t): n = int(input()) ans = [0] * 32 for _ in range(n): word = set(input()) mask = 0 for i in word: if i == "a": mask |= 1 if i == "e": mask |= 2 if i == "i": mask |= 4 if i == "o": mask |= 8 if i == "u": mask |= 16 ans[mask] += 1 count = ans[31] * (ans[31] - 1) // 2 for i in range(32): for j in range(i + 1, 32): if i | j == 31: count += ans[i] * ans[j] print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
import itertools power_set = [ "".join(map(str, k)) for k in map(list, itertools.product([0, 1], repeat=5)) ] def get_bitmask(S): indexes = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} bitmask = [0] * 5 for ch in S: bitmask[indexes[ch]] = 1 return def get_mask(S): indexes = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} mask = 0 for ch in S: mask += 2 ** indexes[ch] return mask def find_union(x, y): res = "" for p, q in zip(x, y): res += str(int(p) or int(q)) return res for i in range(int(input())): N = int(input()) di_set = [] dp = [0] * 32 for _ in range(N): mask = get_mask(set(input())) dp[mask] += 1 likes_dishes = 0 for i in range(32): for j in range(i + 1, 32): if i | j == 31: likes_dishes += dp[i] * dp[j] likes_dishes += dp[31] * (dp[31] - 1) // 2 print(likes_dishes)
IMPORT ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
for _ in range(int(input())): n = int(input()) count = 0 a1 = [0] * 32 for i in range(n): t = [0] * 5 s = set(input()) for j in s: if j == "a": t[0] = 1 elif j == "e": t[1] = 1 elif j == "i": t[2] = 1 elif j == "o": t[3] = 1 elif j == "u": t[4] = 1 value = t[0] * 16 + t[1] * 8 + t[2] * 4 + t[3] * 2 + t[4] * 1 a1[value] += 1 for i in range(31): for j in range(i, 32): if i | j == 31: count += a1[i] * a1[j] x = a1[31] count += int(x * (x - 1) / 2) print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def bit_dishes(str_dishes): dishes = [0] * 32 a, e, i, o, u = 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4 for str_dish in str_dishes: dish = 0 for c in str_dish: if c == "a": dish |= a elif c == "e": dish |= e elif c == "i": dish |= i elif c == "o": dish |= o elif c == "u": dish |= u if dish == 31: break dishes[dish] += 1 return dishes def count_good(dishes): res = 0 for i in range(1, 32): for j in range(i + 1, 32): if i | j == 31: res += dishes[i] * dishes[j] res += dishes[31] * (dishes[31] - 1) // 2 return res trials = int(input()) for _ in range(trials): n = int(input()) dishes = [""] * n for i in range(n): dishes[i] = input() dishes = bit_dishes(dishes) print(count_good(dishes))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR IF VAR STRING VAR VAR IF VAR STRING VAR VAR IF VAR STRING VAR VAR IF VAR STRING VAR VAR IF VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) for k in range(t): l = [0] * 32 n = int(input()) for z in range(n): s1 = list(set(input())) num = 0 for ch in s1: if ch == "a": num = num + 10000 elif ch == "e": num = num + 1000 elif ch == "i": num = num + 100 elif ch == "o": num = num + 10 elif ch == "u": num = num + 1 val = int(str(num), base=2) l[val] = l[val] + 1 c = 0 for i in range(1, 31): for j in range(i + 1, 32): if (l[i] and l[j]) and i | j == 31: c = c + l[i] * l[j] c = c + l[31] * (l[31] - 1) // 2 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) for _ in range(t): n = int(input()) freq = [0] * 32 for i in range(n): s = input() ans = 0 s = list(set(s)) for j in s: if j == "a": ans = ans | 1 if j == "e": ans = ans | 2 if j == "i": ans = ans | 4 if j == "o": ans = ans | 8 if j == "u": ans = ans | 16 freq[ans] += 1 ans = 0 for i in range(1, 32): for j in range(i + 1, 32): if i | j == 31: ans += freq[i] * freq[j] c = freq[31] ans += c * (c - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) for l1 in range(t): ans = 0 cmb = {} n = int(input()) for l2 in range(n): inp = str(input()) vwl = set(inp) vwl = "".join(list(sorted(vwl))) if vwl in cmb: cmb[vwl] += 1 else: cmb[vwl] = 1 for i in cmb.keys(): for j in cmb.keys(): if i != j: if len(set(i + j)) == 5: ans += cmb[i] * cmb[j] if "aeiou" in cmb: ans += cmb["aeiou"] * (cmb["aeiou"] - 1) print(int(ans / 2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR IF VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF STRING VAR VAR BIN_OP VAR STRING BIN_OP VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
for xxx in range(int(input())): n = int(input()) desc = list() count = 0 full = "aeiou" table = dict() for _ in range(n): desc.append("".join(sorted("".join(set(input()))))) desc.sort(key=lambda x: len(x)) for d in desc: if d in table.keys(): table[d] += 1 else: table[d] = 1 keys = list(table.keys()) if desc[-1] == full: count += sum(range(1, table[full])) for index, i in enumerate(keys): for j in keys[index + 1 :]: s = "".join(sorted("".join(set(i + j)))) if s == full: count += table[i] * table[j] print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) for _ in range(t): n = int(input()) l = [] for i in range(n): s = input() a, b, c, d, e = "0" * 5 for j in s: if j == "a": a = "1" if j == "e": b = "1" if j == "i": c = "1" if j == "o": d = "1" if j == "u": e = "1" x = a + b + c + d + e l.append(int(x, 2)) d = {} ans = 0 for i in range(32): d[i] = 0 for i in l: d[i] += 1 for i in range(31): for j in range(i + 1, 32): if i | j == 31: ans += d[i] * d[j] ans += d[31] * (d[31] - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP STRING NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) while t: freq = [0] * 32 n = int(input()) for _ in range(n): s = input() mask = 0 if "a" in s: mask = mask | 1 << 0 if "e" in s: mask = mask | 1 << 1 if "i" in s: mask = mask | 1 << 2 if "o" in s: mask = mask | 1 << 3 if "u" in s: mask = mask | 1 << 4 freq[mask] += 1 res = 0 for i in range(1, 32): for j in range(i + 1, 32): if i | j == 31: res += freq[i] * freq[j] res += freq[31] * (freq[31] - 1) // 2 print(res) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
for _ in range(int(input())): arr = [0] * 32 for _ in range(int(input())): val = 0 list1 = list(set(input())) for i in range(len(list1)): if list1[i] == "a": val += 10000 elif list1[i] == "e": val += 1000 elif list1[i] == "i": val += 100 elif list1[i] == "o": val += 10 elif list1[i] == "u": val += 1 val = int(str(val), base=2) arr[val] += 1 m = 0 for i in range(1, 31): for j in range(i + 1, 32): if (arr[i] and arr[j]) and i | j == 31: m += arr[i] * arr[j] m += arr[31] * (arr[31] - 1) // 2 print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def solve(): n = int(input()) freq = dict() for i in range(32): freq[i] = 0 for i in range(n): s = set(input()) si = 0 if "a" in s: si += 1 << 0 if "e" in s: si += 1 << 1 if "i" in s: si += 1 << 2 if "o" in s: si += 1 << 3 if "u" in s: si += 1 << 4 freq[si] += 1 ans = 0 for i in range(1, 32): for j in range(i + 1, 32): if i | j == 31: ans += freq[i] * freq[j] if freq[31]: ans += freq[31] * (freq[31] - 1) // 2 print(ans) for t in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF STRING VAR VAR BIN_OP NUMBER NUMBER IF STRING VAR VAR BIN_OP NUMBER NUMBER IF STRING VAR VAR BIN_OP NUMBER NUMBER IF STRING VAR VAR BIN_OP NUMBER NUMBER IF STRING VAR VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
d = {"a": 1, "e": 2, "i": 4, "o": 8, "u": 16} for ii in range(int(input())): n = int(input()) dd = {} for i in range(n): s = set(input()) k = 0 for j in s: k += d[j] if k in dd: dd[k] += 1 else: dd[k] = 1 c = 0 for i in dd: for j in dd: if i | j == 31: if i == j: c += dd[i] * (dd[j] - 1) else: c += dd[i] * dd[j] print(c // 2)
ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
def solve(st): n = len(st) new_st = [(0) for _ in range(n)] for i in range(n): for e in st[i]: if e == "a": new_st[i] |= 1 << 0 if e == "e": new_st[i] |= 1 << 1 if e == "i": new_st[i] |= 1 << 2 if e == "o": new_st[i] |= 1 << 3 if e == "u": new_st[i] |= 1 << 4 nn = 1 << 5 ans = [(0) for _ in range(nn)] for e in new_st: ans[e] += 1 final_ans = 0 for i in range(nn): for j in range(i + 1, nn): if i | j == 31: final_ans += ans[i] * ans[j] final_ans += ans[31] * (ans[31] - 1) // 2 return final_ans for _ in range(int(input())): n = int(input()) st = [input() for _ in range(n)] print(solve(st))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR STRING VAR VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR VAR BIN_OP NUMBER NUMBER IF VAR STRING VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
R = lambda: map(int, input().split()) def shrink(string): return "".join(sorted(list(set(string)))) def has_all_vowels(str1, str2): return set(str1 + str2) == {"a", "e", "i", "o", "u"} for i in range(int(input())): n = int(input()) map = {} for i in range(n): string = input() if shrink(string) in map: map[shrink(string)] += 1 else: map[shrink(string)] = 1 ans = 0 for i in map: for j in map: if i != j and has_all_vowels(i, j): ans += map[i] * map[j] ans //= 2 if "aeiou" in map: ans += map["aeiou"] * (map["aeiou"] - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF STRING VAR VAR BIN_OP BIN_OP VAR STRING BIN_OP VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
from sys import stdin class pair(object): def __init__(self, s1, s2): self.s1 = s1 self.s2 = s2 def possible(s1, s2): s = set() for itms in s1: s.add(itms) for items in s2: s.add(items) if len(s) is 5: return True else: return False for _ in range(int(stdin.readline())): n = int(stdin.readline()) m = dict() for _ in range(n): s = stdin.readline().rstrip() se = set() for items in s: se.add(items) se = sorted(se) see = "" for items in se: see += items if see not in m: m.update({see: 1}) else: c = m.get(see) c = c + 1 m.update({see: c}) lis = list() for k, v in m.items(): lis.append(pair(k, v)) ans = 0 for i in range(len(lis)): if len(lis[i].s1) is 5: continue for j in range(i + 1, len(lis)): if len(lis[j].s1) is 5: continue if possible(lis[i].s1, lis[j].s1) is True: ans = ans + lis[i].s2 * lis[j].s2 add = 0 ok = "aeiou" in m.keys() if ok is True: add = add + m.get("aeiou") * (m.get("aeiou") - 1) / 2 mew = m.get("aeiou") var = n - mew add = add + var * m.get("aeiou") print(int(ans + add))
CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR DICT VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR DICT VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) for _ in range(t): f = [0] * 32 ip = int(input()) li = list() for i in range(ip): li.append(input()) for i in li: mask = 0 if "a" in i: mask = mask | 1 << 0 if "e" in i: mask = mask | 1 << 1 if "i" in i: mask = mask | 1 << 2 if "o" in i: mask = mask | 1 << 3 if "u" in i: mask = mask | 1 << 4 f[mask] += 1 res = 0 for i in range(len(f)): for j in range(i + 1, len(f)): if i | j == 31: res = res + f[i] * f[j] res = res + f[31] * (f[31] - 1) // 2 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF STRING VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once. Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes? ------ 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 a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$. ------ Output ------ For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes. ------ Constraints ------ $1 ≀ T ≀ 1,000$ $1 ≀ N ≀ 10^{5}$ $1 ≀ |D_{i}| ≀ 1,000$ for each valid $i$ the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ the sum of all |D_{i}| over all test cases does not exceed $20000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 3 aaooaoaooa uiieieiieieuuu aeioooeeiiaiei ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: There are three possible meals: - A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels. - A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'. - A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
t = int(input()) binrep = {"a": 16, "e": 8, "i": 4, "o": 2, "u": 1} def convert(ch): return binrep[ch] while t: n = int(input()) binrepfreq = [0] * 32 ans = 0 for i in range(n): rep = sum(map(convert, set(list(input())))) binrepfreq[rep] += 1 for i in range(1, 31): for j in range(i + 1, 32): if i | j == 31: pairscount = binrepfreq[i] * binrepfreq[j] ans += pairscount pair31 = (binrepfreq[31] * binrepfreq[31] - binrepfreq[31]) // 2 ans += pair31 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF RETURN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city ai. Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a1, a2, ..., an repeated m times). After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city. Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected. Input The first line contains three integers n, k and m (1 ≀ n ≀ 105, 2 ≀ k ≀ 109, 1 ≀ m ≀ 109). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105), where ai is the number of city, person from which must take seat i in the bus. Output Output the number of remaining participants in the line. Examples Input 4 2 5 1 2 3 1 Output 12 Input 1 9 10 1 Output 1 Input 3 2 10 1 2 1 Output 0 Note In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.
def main(): _, k, m = [int(x) for x in input().split()] a = [] last = "-1", 0 a.append(last) for ai in input().split(): if last[0] == ai: last = ai, last[1] + 1 a[-1] = last else: last = ai, 1 a.append(last) if last[1] == k: a.pop() last = a[-1] a.pop(0) s1 = 0 while len(a) > 0 and a[0][0] == a[-1][0]: if len(a) == 1: s = a[0][1] * m r1 = s % k if r1 == 0: print(s1 % k) else: print(r1 + s1) return join = a[0][1] + a[-1][1] if join < k: break elif join % k == 0: s1 += join a.pop() a.pop(0) else: s1 += join // k * k a[0] = a[0][0], join % k a.pop() break s = 0 for ai in a: s += ai[1] print(s * m + s1) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city ai. Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a1, a2, ..., an repeated m times). After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city. Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected. Input The first line contains three integers n, k and m (1 ≀ n ≀ 105, 2 ≀ k ≀ 109, 1 ≀ m ≀ 109). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105), where ai is the number of city, person from which must take seat i in the bus. Output Output the number of remaining participants in the line. Examples Input 4 2 5 1 2 3 1 Output 12 Input 1 9 10 1 Output 1 Input 3 2 10 1 2 1 Output 0 Note In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.
r = lambda: map(int, input().split()) n, k, m = r() a = list(r()) stck = [] for i in range(n): if len(stck) == 0 or stck[-1][0] != a[i]: stck.append([a[i], 1]) else: stck[-1][1] += 1 if stck[-1][1] == k: stck.pop() rem = 0 strt, end = 0, len(stck) - 1 if m > 1: while end - strt + 1 > 1 and stck[strt][0] == stck[end][0]: join = stck[strt][1] + stck[end][1] if join < k: break elif join % k == 0: rem += join strt += 1 end -= 1 else: stck[strt][1] = join % k stck[end][1] = 0 rem += join // k * k tr = 0 slen = end - strt + 1 for el in stck[:slen]: tr += el[1] if slen == 0: print(0) elif slen == 1: r = stck[strt][1] * m % k if r == 0: print(0) else: print(r + rem) else: print(tr * m + rem)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city ai. Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a1, a2, ..., an repeated m times). After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city. Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected. Input The first line contains three integers n, k and m (1 ≀ n ≀ 105, 2 ≀ k ≀ 109, 1 ≀ m ≀ 109). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105), where ai is the number of city, person from which must take seat i in the bus. Output Output the number of remaining participants in the line. Examples Input 4 2 5 1 2 3 1 Output 12 Input 1 9 10 1 Output 1 Input 3 2 10 1 2 1 Output 0 Note In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.
def process(A, m, k): n = len(A) start = n * m d = [] for i in range(n): if len(d) == 0 or d[-1][0] != A[i]: d.append([A[i], 1]) else: d[-1][1] += 1 if d[-1][1] == k: start -= k * m d.pop() if m == 1: return start p1 = 0 start_d = [] middle_d = [] end_d = [] for x in d: a, b = x start_d.append([a, b]) middle_d.append([a, b]) end_d.append([a, b]) if m == 2: middle_d = [] else: middle_d = [x for x in d] while True: if p1 >= len(middle_d): if len(start_d) == 0 or p1 == len(end_d): break v1, c1 = start_d[-1] v2, c2 = end_d[p1] if v1 == v2 and c1 + c2 >= k: start -= k start_d.pop() if c1 + c2 == k: p1 += 1 else: end_d[1] = (c1 + c2) % k else: break elif len(middle_d) - p1 == 1: v1, c1 = start_d[-1] v2, c2 = middle_d[p1] v3, c3 = end_d[p1] total_value = c2 * (m - 2) if v2 == v1: total_value += c1 if v2 == v3: total_value += c3 start -= total_value - total_value % k if total_value % k == 0: middle_d = [] if v2 == v1: start_d.pop() if v2 == v3: end_d = end_d p1 += 1 else: break else: v1, c1 = start_d[-1] v2, c2 = middle_d[p1] v3, c3 = middle_d[-1] v4, c4 = end_d[p1] if v2 == v3 and c2 + c3 >= k: this_value = (c2 + c3 - (c2 + c3) % k) * (m - 1) start -= this_value middle_d.pop() start_d.pop() middle_d[p1][1] = (c2 + c3) % k end_d[p1][1] = (c2 + c3) % k if middle_d[p1][1] == 0: p1 += 1 else: break return start n, k, m = [int(x) for x in input().split()] A = [int(x) for x in input().split()] print(process(A, m, k))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR WHILE NUMBER IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): cards = [N] for i in range(N - 1, 0, -1): cards = self.add_card(cards, i) return cards def add_card(self, current, m): target = [m] + current new = self.shuffle(target, m) return new @staticmethod def shuffle(arr, k): for i in range(k): arr = arr[-1:] + arr[:-1] return arr
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): x = [] x.append(N) N -= 1 while N != 0: x.insert(0, N) N -= 1 size = len(x) p = x[0] rot = p % size fp = x[: size - rot] lp = x[size - rot :] x = lp + fp return x
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, n): order = [(0) for i in range(n)] c = 1 cl = n j = 0 for i in range(1, n + 1): cnt = 0 while cnt < c: if order[j]: j += 1 j %= n else: cnt += 1 j += 1 j %= n while order[j]: j += 1 j %= n order[j] = i c += 1 return order pass
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, n): q = [] for i in range(1, n + 1): q.append(i) ans = [0] * (n + 1) for i in range(1, n + 1): rot = i % (n - i + 1) while rot is not 0: f = q.pop(0) q.append(f) rot -= 1 idx = q.pop(0) ans[idx] = i ans.pop(0) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
def rotatef(l, t): for i in range(t): x = l.pop() l.insert(0, x) return l class Solution: def rotation(self, N): lst = [] for i in range(N, 0, -1): lst.insert(0, i) lst = rotatef(lst, i) return lst
FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): ans = [] arr = [0] * N q = [] for i in range(0, N): q.append(i) i = 1 while len(q) != 0: j = 0 while j < i: x = q.pop(0) q.append(x) j += 1 loc = q[0] arr[loc] = i q.pop(0) i += 1 for i in range(0, N): ans.append(arr[i]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): t = [] for i in range(N, 0, -1): t = [i] + t size = len(t) rot = i % size m = size - rot t = t[m:] + t[:m] return t
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): ans = [0] * N index = [] for j in range(N): index.append(j) ans[1] = 1 ind = 1 index.remove(1) for j in range(2, N + 1): ind = (ind + j) % len(index) ans[index[ind]] = j index.remove(index[ind]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): ans = [] for i in range(N, 0, -1): ans = [i] + ans for j in range(i): popped = ans.pop(-1) ans = [popped] + ans return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
from itertools import permutations class Solution: def rotation(self, n): li = [] for i in range(n): li.append(0) p = 0 for i in range(1, n): k = i while k: if li[p] == 0: k -= 1 p += 1 if p > n - 1: p = li.index(0) if li[p] == 0: li[p] = i else: while li[p] != 0: p += 1 if p > n - 1: p = li.index(0) li[p] = i p = li.index(0) li[p] = n return li
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, n): ar = [str(i) for i in range(1, n + 1)] for i in range(n - 1, 0, -1): for j in range(i): temp = ar.pop() ar.insert(i - 1, temp) return ar
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): arr = [0] * N t = [0] * N i = 0 num = 1 while True: cnt = 0 while cnt < num: i = (i + 1) % N if t[i] == 0: cnt += 1 t[i] = -1 arr[i] = num num += 1 if num == N + 1: break while t[i] != 0: i = (i + 1) % N return arr
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, n): q = [i for i in range(1, n + 1)] k = 1 ans = [0] * (n + 1) while k <= n: l = k % len(q) j = 1 while j <= l: x = q[0] q = q[1:] q.append(x) j += 1 x = q[0] q = q[1:] ans[x] = k k += 1 return ans[1:]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR NUMBER
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): result = [(0) for _ in range(N)] current_index = -1 for i in range(1, N + 1): count = 0 while True: current_index = (current_index + 1) % N if count == i and result[current_index] == 0: count = 0 result[current_index] = i break elif count < i and result[current_index] == 0: count += 1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): q = [] while N > 0: q.append(N) i = 1 while i <= N: q.append(q.pop(0)) i += 1 N -= 1 return q[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): q = [N] for i in range(N - 1, 0, -1): q.insert(0, i) size = len(q) rot = i % size fp = q[: size - rot] lp = q[size - rot :] q = lp + fp return q
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, n): L = list(range(1, n + 1)) A = [(0) for i in range(n)] K = 1 count = 1 while len(L): for _ in range(count): x = L.pop(0) L.append(x) Index = L.pop(0) A[Index - 1] = K K += 1 count += 1 return A
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a sorted deck of cards numbered 1 to N. 1) We pick up 1 card and put it on the back of the deck. 2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck. 3) Now we pick up 2 cards and put it on the back of the deck. 4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ... We perform this step until the last card. If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1. Example 1: Input: N = 4 Output: 2 1 4 3 Explanation: We initially have [2 1 4 3] In Step1, we move the first card to the end. Deck now is: [1 4 3 2] In Step2, we get 1. Hence we remove it. Deck now is: [4 3 2] In Step3, we move the 2 front cards ony by one to the end ([4 3 2] -> [3 2 4] -> [2 4 3]). Deck now is: [2 4 3] In Step4, we get 2. Hence we remove it. Deck now is: [4 3] In Step5, the following sequence follows: [4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now is: [3 4] In Step6, we get 3. Hence we remove it. Deck now is: [4] Finally, we're left with a single card and thus, we stop. Example 2: Input: N = 5 Output: 3 1 4 5 2 Your Task: You don't need to read input or print anything. Your task is to complete the function rotation() which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1. Expected Time Complexity: O(N^2) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 1000
class Solution: def rotation(self, N): arr = [0] * 1001 q = [i for i in range(1, N + 1)] i = 1 while len(q): j = 0 while i > j: x = q.pop(0) q.append(x) j += 1 x = q.pop(0) if arr[x] != 0: return [-1] arr[x] = i i += 1 return arr[1 : N + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER RETURN LIST NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER
A "graph" consists of "nodes", also known as "vertices". Nodes may or may not be connected with one another. In our definition below the node "A0" is connected with the node "A3", but "A0" is not connected with "A1". The connecting line between two nodes is called an edge. If the edges between the nodes are undirected, the graph is called an undirected graph. A weighted graph is a graph in which a number (the weight) is assigned to each edge. A graph is acyclic if it has no loop. A graph can be represented as a dictionary: `graph = {'A0': [('A3', 1), ('A5', 4)], 'A1': [('A2', 2)], 'A2': [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)], 'A3': [('A0', 1), ('A2', 1)], 'A4': [('A2', 1), ('A4', 1)], 'A5': [('A3', 3)] }` Here the nodes are A0...A5; following each nodes is the edges list of linked nodes with their weight. A0 is linked to A3 with a weight of 1 and to A5 with weight 4. A dictionary is not ordered but the list of linked nodes is sorted. So: `'A0': [('A3', 1), ('A5', 4)]`is correct but `'A0': [('A5', 4), ('A3', 1)]`is not. The edges E of a graph G induce a binary relation that is called the adjacency relation of G. One can associate an adjacency matrix: `M = [[0, 0, 0, 1, 0, 4], [0, 0, 2, 0, 0, 0], [0, 1, 2, 1, 1, 0], [1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 3, 0, 0]]` Let us imagine that lines are numbered from A0 to A5 and the same for columns. The first line correspond to A0 and we can see that A0 is connected to A3 with weight 1, A0 is also connected to A5 with weight 4. Another way is to use an adjacency list: An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring edges: `L = [['A0', [('A3', 1), ('A5', 4)]], ['A1', [('A2', 2)]], ['A2', [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)]], ['A3', [('A0', 1), ('A2', 1)]], ['A4', [('A2', 1), ('A4', 1)]], ['A5', [('A3', 3)]]] ` L is sorted in order A0 to A5 and each sublist is sorted as in a graph dictionary. In the kata nodes will be numbered from A0 to A(n-1) for a graph with n nodes. The task is to go from each represention to each other one and at last to find *all* paths from a node to another node. Below is a skeleton for our class `Graph`. class Graph(): def __init__(self, vertices_num): # number of nodes (an integer) self.v = vertices_num # (maybe not useful here) : list of nodes from "A0", "A1" ... to "A index (vertices_num - 1)" self.nodes = None # from adjacency matrix to dictionary def adjmat_2_graph(self, adjm): # from dictionary to adjacency matrix def graph_2_mat(self, graph): # from dictionary to adjacency list def graph_2_list(self, graph): # from adjacency list to dictionary def list_2_graph(self, lst): # from adjacency matrix to adjacency list def mat_2_list(self, mat): # from adjacency list to adjacency matrix def list_2_mat(self, lst): # find all paths from node start_vertex to node end_vertex def find_all_paths(self, graph, start_vertex, end_vertex): # Examples: - see: graph, M, L above. - finding all paths: `dct = {'A3': [('A0', 1), ('A2', 1)], 'A0': [('A3', 1), ('A2', 1)], 'A4': [('A2', 1)], 'A1': [('A2', 1)], 'A2': [('A1', 1), ('A2', 1), ('A3', 1), ('A4', 1)]}` `g = Graph(5)` `g.find_all_paths(dct, "A0", "A4") return l = ['A0-A2-A4', 'A0-A3-A2-A4']` Note that ['A0-A2-A4', 'A0-A3-A2-A4'] is sorted (hint: sorted(sorted(l, key=str), key=len)) Translators and critics are welcome.
class Graph: def __init__(self, vertices_num): self.v = vertices_num def adjmat_2_graph(self, adjm): if len(adjm) != self.v: return "Dimension error" nodes = [("A" + str(i)) for i in range(0, self.v)] graph = {} for n, v in enumerate(nodes): voisin = [] for i, dist in enumerate(adjm[n]): if dist != 0: voisin.append((nodes[i], dist)) graph[v] = voisin self.graph = graph return graph def graph_2_mat(self, graph): result = [([0] * self.v) for _ in range(self.v)] for k, links in graph.items(): for n, d in links: result[int(k[1:])][int(n[1:])] = d return result def graph_2_list(self, graph): return list(map(list, sorted(graph.items()))) def list_2_graph(self, lst): return {k: a for k, a in lst} def mat_2_list(self, mat): return self.graph_2_list(self.adjmat_2_graph(mat)) def list_2_mat(self, lst): return self.graph_2_mat(self.list_2_graph(lst)) def find_all_paths(self, graph, start_vertex, end_vertex): pths = [] st = [[start_vertex]] while st: top = st.pop() if top[-1] == end_vertex: pths.append("-".join(top)) continue for n, _ in graph[top[-1]]: if n not in top: st.append(top + [n]) return sorted(sorted(pths), key=len)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN STRING ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR
A "graph" consists of "nodes", also known as "vertices". Nodes may or may not be connected with one another. In our definition below the node "A0" is connected with the node "A3", but "A0" is not connected with "A1". The connecting line between two nodes is called an edge. If the edges between the nodes are undirected, the graph is called an undirected graph. A weighted graph is a graph in which a number (the weight) is assigned to each edge. A graph is acyclic if it has no loop. A graph can be represented as a dictionary: `graph = {'A0': [('A3', 1), ('A5', 4)], 'A1': [('A2', 2)], 'A2': [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)], 'A3': [('A0', 1), ('A2', 1)], 'A4': [('A2', 1), ('A4', 1)], 'A5': [('A3', 3)] }` Here the nodes are A0...A5; following each nodes is the edges list of linked nodes with their weight. A0 is linked to A3 with a weight of 1 and to A5 with weight 4. A dictionary is not ordered but the list of linked nodes is sorted. So: `'A0': [('A3', 1), ('A5', 4)]`is correct but `'A0': [('A5', 4), ('A3', 1)]`is not. The edges E of a graph G induce a binary relation that is called the adjacency relation of G. One can associate an adjacency matrix: `M = [[0, 0, 0, 1, 0, 4], [0, 0, 2, 0, 0, 0], [0, 1, 2, 1, 1, 0], [1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 3, 0, 0]]` Let us imagine that lines are numbered from A0 to A5 and the same for columns. The first line correspond to A0 and we can see that A0 is connected to A3 with weight 1, A0 is also connected to A5 with weight 4. Another way is to use an adjacency list: An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring edges: `L = [['A0', [('A3', 1), ('A5', 4)]], ['A1', [('A2', 2)]], ['A2', [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)]], ['A3', [('A0', 1), ('A2', 1)]], ['A4', [('A2', 1), ('A4', 1)]], ['A5', [('A3', 3)]]] ` L is sorted in order A0 to A5 and each sublist is sorted as in a graph dictionary. In the kata nodes will be numbered from A0 to A(n-1) for a graph with n nodes. The task is to go from each represention to each other one and at last to find *all* paths from a node to another node. Below is a skeleton for our class `Graph`. class Graph(): def __init__(self, vertices_num): # number of nodes (an integer) self.v = vertices_num # (maybe not useful here) : list of nodes from "A0", "A1" ... to "A index (vertices_num - 1)" self.nodes = None # from adjacency matrix to dictionary def adjmat_2_graph(self, adjm): # from dictionary to adjacency matrix def graph_2_mat(self, graph): # from dictionary to adjacency list def graph_2_list(self, graph): # from adjacency list to dictionary def list_2_graph(self, lst): # from adjacency matrix to adjacency list def mat_2_list(self, mat): # from adjacency list to adjacency matrix def list_2_mat(self, lst): # find all paths from node start_vertex to node end_vertex def find_all_paths(self, graph, start_vertex, end_vertex): # Examples: - see: graph, M, L above. - finding all paths: `dct = {'A3': [('A0', 1), ('A2', 1)], 'A0': [('A3', 1), ('A2', 1)], 'A4': [('A2', 1)], 'A1': [('A2', 1)], 'A2': [('A1', 1), ('A2', 1), ('A3', 1), ('A4', 1)]}` `g = Graph(5)` `g.find_all_paths(dct, "A0", "A4") return l = ['A0-A2-A4', 'A0-A3-A2-A4']` Note that ['A0-A2-A4', 'A0-A3-A2-A4'] is sorted (hint: sorted(sorted(l, key=str), key=len)) Translators and critics are welcome.
class Graph: def __init__(self, vertices_num): pass @staticmethod def adjmat_2_graph(adjm): nodes_dct = {} for row, weights in enumerate(adjm): edges = [("A" + str(col), w) for col, w in enumerate(weights) if w > 0] nodes_dct["A" + str(row)] = edges return nodes_dct @staticmethod def graph_2_mat(graph): g_size = len(list(graph.keys())) matrix = [[(0) for _ in range(g_size)] for _ in range(g_size)] for vname, edges in list(graph.items()): v = int(vname[1:]) for e in edges: u = int(e[0][1:]) matrix[u][v] = e[1] return matrix @staticmethod def graph_2_list(graph): return [[k, v] for k, v in sorted(graph.items())] @staticmethod def list_2_graph(lst): return dict((vertex[0], vertex[1]) for vertex in lst) @staticmethod def mat_2_list(mat): return Graph.graph_2_list(Graph.adjmat_2_graph(mat)) @staticmethod def list_2_mat(lst): return Graph.graph_2_mat(Graph.list_2_graph(lst)) @staticmethod def find_all_paths(graph, start_vertex, end_vertex): if start_vertex == end_vertex: return [start_vertex] paths = [] def find(path): for e in graph[path[-1]]: v = e[0] if v not in path: new_path = path[:] + [v] if v == end_vertex: paths.append(new_path) else: find(new_path) find([start_vertex]) l = list(["-".join(p) for p in paths]) return sorted(sorted(l, key=str), key=len)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF RETURN LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN LIST VAR ASSIGN VAR LIST FUNC_DEF FOR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
A "graph" consists of "nodes", also known as "vertices". Nodes may or may not be connected with one another. In our definition below the node "A0" is connected with the node "A3", but "A0" is not connected with "A1". The connecting line between two nodes is called an edge. If the edges between the nodes are undirected, the graph is called an undirected graph. A weighted graph is a graph in which a number (the weight) is assigned to each edge. A graph is acyclic if it has no loop. A graph can be represented as a dictionary: `graph = {'A0': [('A3', 1), ('A5', 4)], 'A1': [('A2', 2)], 'A2': [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)], 'A3': [('A0', 1), ('A2', 1)], 'A4': [('A2', 1), ('A4', 1)], 'A5': [('A3', 3)] }` Here the nodes are A0...A5; following each nodes is the edges list of linked nodes with their weight. A0 is linked to A3 with a weight of 1 and to A5 with weight 4. A dictionary is not ordered but the list of linked nodes is sorted. So: `'A0': [('A3', 1), ('A5', 4)]`is correct but `'A0': [('A5', 4), ('A3', 1)]`is not. The edges E of a graph G induce a binary relation that is called the adjacency relation of G. One can associate an adjacency matrix: `M = [[0, 0, 0, 1, 0, 4], [0, 0, 2, 0, 0, 0], [0, 1, 2, 1, 1, 0], [1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 3, 0, 0]]` Let us imagine that lines are numbered from A0 to A5 and the same for columns. The first line correspond to A0 and we can see that A0 is connected to A3 with weight 1, A0 is also connected to A5 with weight 4. Another way is to use an adjacency list: An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring edges: `L = [['A0', [('A3', 1), ('A5', 4)]], ['A1', [('A2', 2)]], ['A2', [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)]], ['A3', [('A0', 1), ('A2', 1)]], ['A4', [('A2', 1), ('A4', 1)]], ['A5', [('A3', 3)]]] ` L is sorted in order A0 to A5 and each sublist is sorted as in a graph dictionary. In the kata nodes will be numbered from A0 to A(n-1) for a graph with n nodes. The task is to go from each represention to each other one and at last to find *all* paths from a node to another node. Below is a skeleton for our class `Graph`. class Graph(): def __init__(self, vertices_num): # number of nodes (an integer) self.v = vertices_num # (maybe not useful here) : list of nodes from "A0", "A1" ... to "A index (vertices_num - 1)" self.nodes = None # from adjacency matrix to dictionary def adjmat_2_graph(self, adjm): # from dictionary to adjacency matrix def graph_2_mat(self, graph): # from dictionary to adjacency list def graph_2_list(self, graph): # from adjacency list to dictionary def list_2_graph(self, lst): # from adjacency matrix to adjacency list def mat_2_list(self, mat): # from adjacency list to adjacency matrix def list_2_mat(self, lst): # find all paths from node start_vertex to node end_vertex def find_all_paths(self, graph, start_vertex, end_vertex): # Examples: - see: graph, M, L above. - finding all paths: `dct = {'A3': [('A0', 1), ('A2', 1)], 'A0': [('A3', 1), ('A2', 1)], 'A4': [('A2', 1)], 'A1': [('A2', 1)], 'A2': [('A1', 1), ('A2', 1), ('A3', 1), ('A4', 1)]}` `g = Graph(5)` `g.find_all_paths(dct, "A0", "A4") return l = ['A0-A2-A4', 'A0-A3-A2-A4']` Note that ['A0-A2-A4', 'A0-A3-A2-A4'] is sorted (hint: sorted(sorted(l, key=str), key=len)) Translators and critics are welcome.
class Graph: def __init__(self, vertices_num): self.v = vertices_num self.nodes = None def adjmat_2_graph(self, adjm): d = {} for indice, ligne in enumerate(adjm): print((indice, ligne)) cle = "A" + str(indice) laliste = [] for indiceVoisin, nombre in enumerate(ligne): if nombre: t = "A" + str(indiceVoisin), nombre laliste.append(t) d[cle] = laliste return d def graph_2_mat(self, graph): m = [[(0) for k in range(self.v)] for i in range(self.v)] for noeud in graph: indice = int(noeud[1:]) for voisin in graph[noeud]: indiceVoisin = int(voisin[0][1:]) distance = voisin[1] m[indice][indiceVoisin] = distance return m def graph_2_list(self, graph): m = [] for noeud in graph: laliste = [noeud, graph[noeud]] m.append(laliste) m.sort() return m def list_2_graph(self, lst): d = {} for elt in lst: cle = elt[0] laliste = elt[1] d[cle] = laliste return d def mat_2_list(self, mat): d = self.adjmat_2_graph(mat) adj = self.graph_2_list(d) return adj def list_2_mat(self, lst): d = self.list_2_graph(lst) a = self.graph_2_mat(d) return a def find_all_paths(self, graph, start_vertex, end_vertex): if start_vertex == end_vertex: return [end_vertex] start_vertex = int(start_vertex[1:]) end_vertex = int(end_vertex[1:]) lesChemins = [] m = self.graph_2_mat(graph) noeuds = list(range(self.v)) pile = [[start_vertex]] while pile: chemin = pile.pop() dernier = chemin[-1] lesVoisins = [k for k in noeuds if m[dernier][k] > 0 and k not in chemin] for voisin in lesVoisins: if voisin == end_vertex: bonChemin = chemin.copy() bonChemin.append(voisin) bonChemin = [("A" + str(k)) for k in bonChemin] lesChemins.append("-".join(bonChemin)) else: leChemin = chemin.copy() leChemin.append(voisin) pile.append(leChemin) lesChemins.sort() lesChemins.sort(key=len) return lesChemins
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN LIST VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
A "graph" consists of "nodes", also known as "vertices". Nodes may or may not be connected with one another. In our definition below the node "A0" is connected with the node "A3", but "A0" is not connected with "A1". The connecting line between two nodes is called an edge. If the edges between the nodes are undirected, the graph is called an undirected graph. A weighted graph is a graph in which a number (the weight) is assigned to each edge. A graph is acyclic if it has no loop. A graph can be represented as a dictionary: `graph = {'A0': [('A3', 1), ('A5', 4)], 'A1': [('A2', 2)], 'A2': [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)], 'A3': [('A0', 1), ('A2', 1)], 'A4': [('A2', 1), ('A4', 1)], 'A5': [('A3', 3)] }` Here the nodes are A0...A5; following each nodes is the edges list of linked nodes with their weight. A0 is linked to A3 with a weight of 1 and to A5 with weight 4. A dictionary is not ordered but the list of linked nodes is sorted. So: `'A0': [('A3', 1), ('A5', 4)]`is correct but `'A0': [('A5', 4), ('A3', 1)]`is not. The edges E of a graph G induce a binary relation that is called the adjacency relation of G. One can associate an adjacency matrix: `M = [[0, 0, 0, 1, 0, 4], [0, 0, 2, 0, 0, 0], [0, 1, 2, 1, 1, 0], [1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 3, 0, 0]]` Let us imagine that lines are numbered from A0 to A5 and the same for columns. The first line correspond to A0 and we can see that A0 is connected to A3 with weight 1, A0 is also connected to A5 with weight 4. Another way is to use an adjacency list: An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring edges: `L = [['A0', [('A3', 1), ('A5', 4)]], ['A1', [('A2', 2)]], ['A2', [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)]], ['A3', [('A0', 1), ('A2', 1)]], ['A4', [('A2', 1), ('A4', 1)]], ['A5', [('A3', 3)]]] ` L is sorted in order A0 to A5 and each sublist is sorted as in a graph dictionary. In the kata nodes will be numbered from A0 to A(n-1) for a graph with n nodes. The task is to go from each represention to each other one and at last to find *all* paths from a node to another node. Below is a skeleton for our class `Graph`. class Graph(): def __init__(self, vertices_num): # number of nodes (an integer) self.v = vertices_num # (maybe not useful here) : list of nodes from "A0", "A1" ... to "A index (vertices_num - 1)" self.nodes = None # from adjacency matrix to dictionary def adjmat_2_graph(self, adjm): # from dictionary to adjacency matrix def graph_2_mat(self, graph): # from dictionary to adjacency list def graph_2_list(self, graph): # from adjacency list to dictionary def list_2_graph(self, lst): # from adjacency matrix to adjacency list def mat_2_list(self, mat): # from adjacency list to adjacency matrix def list_2_mat(self, lst): # find all paths from node start_vertex to node end_vertex def find_all_paths(self, graph, start_vertex, end_vertex): # Examples: - see: graph, M, L above. - finding all paths: `dct = {'A3': [('A0', 1), ('A2', 1)], 'A0': [('A3', 1), ('A2', 1)], 'A4': [('A2', 1)], 'A1': [('A2', 1)], 'A2': [('A1', 1), ('A2', 1), ('A3', 1), ('A4', 1)]}` `g = Graph(5)` `g.find_all_paths(dct, "A0", "A4") return l = ['A0-A2-A4', 'A0-A3-A2-A4']` Note that ['A0-A2-A4', 'A0-A3-A2-A4'] is sorted (hint: sorted(sorted(l, key=str), key=len)) Translators and critics are welcome.
class Graph: def __init__(self, vertices_num): self.v = vertices_num self.nodes = None def adjmat_2_graph(self, matrix): dictionary = {} step = 0 for row in matrix: dictionary[f"A{step}"] = [] column = 0 for value in row: if value: dictionary[f"A{step}"].append((f"A{column}", value)) column += 1 step += 1 return dictionary def graph_2_mat(self, dictionary): matrix = [] step = 0 while step < len(dictionary): row = [0] * len(dictionary) for edge in dictionary[f"A{step}"]: row[int(edge[0][1])] = edge[1] matrix.append(row) step += 1 return matrix def graph_2_list(self, dictionary): list = [] step = 0 while step < len(dictionary): vertex = [f"A{step}", dictionary[f"A{step}"]] list.append(vertex) step += 1 return list def list_2_graph(self, list): dictionary = {} for vertex in list: dictionary[vertex[0]] = vertex[1] return dictionary def mat_2_list(self, matrix): list = [] step = 0 for row in matrix: vertex = [f"A{step}", []] column = 0 for value in row: if value: vertex[1].append((f"A{column}", value)) column += 1 list.append(vertex) step += 1 return list def list_2_mat(self, list): matrix = [] step = 0 while step < len(list): for vertex in list: if vertex[0] == f"A{step}": row = [0] * len(list) for edge in vertex[1]: row[int(edge[0][1])] = edge[1] matrix.append(row) step += 1 return matrix def find_all_paths(self, dictionary, start, end): def dfs(dictionary, start, end, path=[]): path = path + [start] if start == end: return [path] if start not in dictionary: return [] paths = [] for edge in dictionary[start]: if edge[0] not in path: newpaths = dfs(dictionary, edge[0], end, path) for newpath in newpaths: paths.append(newpath) return paths paths = dfs(dictionary, start, end) return sorted(sorted(["-".join(path) for path in paths], key=str), key=len)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR STRING VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST STRING VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER STRING VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FUNC_DEF LIST ASSIGN VAR BIN_OP VAR LIST VAR IF VAR VAR RETURN LIST VAR IF VAR VAR RETURN LIST ASSIGN VAR LIST FOR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR VAR
A "graph" consists of "nodes", also known as "vertices". Nodes may or may not be connected with one another. In our definition below the node "A0" is connected with the node "A3", but "A0" is not connected with "A1". The connecting line between two nodes is called an edge. If the edges between the nodes are undirected, the graph is called an undirected graph. A weighted graph is a graph in which a number (the weight) is assigned to each edge. A graph is acyclic if it has no loop. A graph can be represented as a dictionary: `graph = {'A0': [('A3', 1), ('A5', 4)], 'A1': [('A2', 2)], 'A2': [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)], 'A3': [('A0', 1), ('A2', 1)], 'A4': [('A2', 1), ('A4', 1)], 'A5': [('A3', 3)] }` Here the nodes are A0...A5; following each nodes is the edges list of linked nodes with their weight. A0 is linked to A3 with a weight of 1 and to A5 with weight 4. A dictionary is not ordered but the list of linked nodes is sorted. So: `'A0': [('A3', 1), ('A5', 4)]`is correct but `'A0': [('A5', 4), ('A3', 1)]`is not. The edges E of a graph G induce a binary relation that is called the adjacency relation of G. One can associate an adjacency matrix: `M = [[0, 0, 0, 1, 0, 4], [0, 0, 2, 0, 0, 0], [0, 1, 2, 1, 1, 0], [1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 3, 0, 0]]` Let us imagine that lines are numbered from A0 to A5 and the same for columns. The first line correspond to A0 and we can see that A0 is connected to A3 with weight 1, A0 is also connected to A5 with weight 4. Another way is to use an adjacency list: An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring edges: `L = [['A0', [('A3', 1), ('A5', 4)]], ['A1', [('A2', 2)]], ['A2', [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)]], ['A3', [('A0', 1), ('A2', 1)]], ['A4', [('A2', 1), ('A4', 1)]], ['A5', [('A3', 3)]]] ` L is sorted in order A0 to A5 and each sublist is sorted as in a graph dictionary. In the kata nodes will be numbered from A0 to A(n-1) for a graph with n nodes. The task is to go from each represention to each other one and at last to find *all* paths from a node to another node. Below is a skeleton for our class `Graph`. class Graph(): def __init__(self, vertices_num): # number of nodes (an integer) self.v = vertices_num # (maybe not useful here) : list of nodes from "A0", "A1" ... to "A index (vertices_num - 1)" self.nodes = None # from adjacency matrix to dictionary def adjmat_2_graph(self, adjm): # from dictionary to adjacency matrix def graph_2_mat(self, graph): # from dictionary to adjacency list def graph_2_list(self, graph): # from adjacency list to dictionary def list_2_graph(self, lst): # from adjacency matrix to adjacency list def mat_2_list(self, mat): # from adjacency list to adjacency matrix def list_2_mat(self, lst): # find all paths from node start_vertex to node end_vertex def find_all_paths(self, graph, start_vertex, end_vertex): # Examples: - see: graph, M, L above. - finding all paths: `dct = {'A3': [('A0', 1), ('A2', 1)], 'A0': [('A3', 1), ('A2', 1)], 'A4': [('A2', 1)], 'A1': [('A2', 1)], 'A2': [('A1', 1), ('A2', 1), ('A3', 1), ('A4', 1)]}` `g = Graph(5)` `g.find_all_paths(dct, "A0", "A4") return l = ['A0-A2-A4', 'A0-A3-A2-A4']` Note that ['A0-A2-A4', 'A0-A3-A2-A4'] is sorted (hint: sorted(sorted(l, key=str), key=len)) Translators and critics are welcome.
class Graph: def __init__(self, vertices_num): self.v = vertices_num self.nodes = [f"A{i}" for i in range(self.v)] self.ids = {x: i for i, x in enumerate(self.nodes)} def adjmat_2_graph(self, adjm): return { self.nodes[i]: [(self.nodes[j], x) for j, x in enumerate(row) if x] for i, row in enumerate(adjm) } def graph_2_mat(self, graph): result = [([0] * self.v) for _ in range(self.v)] for i, L in graph.items(): for j, x in L: result[self.ids[i]][self.ids[j]] = x return result def graph_2_list(self, graph): return list(map(list, sorted(graph.items()))) def list_2_graph(self, lst): return dict(lst) def mat_2_list(self, mat): return self.graph_2_list(self.adjmat_2_graph(mat)) def list_2_mat(self, lst): return self.graph_2_mat(self.list_2_graph(lst)) @staticmethod def gen(graph, start, end, seen): if start == end: yield [end] else: for k, v in graph[start]: if k not in seen: for L in Graph.gen(graph, k, end, seen | {k}): yield [start] + L def find_all_paths(self, graph, start_vertex, end_vertex): return sorted( map("-".join, Graph.gen(graph, start_vertex, end_vertex, {start_vertex})), key=lambda x: (len(x), x), )
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR EXPR LIST VAR FOR VAR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR BIN_OP LIST VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
A "graph" consists of "nodes", also known as "vertices". Nodes may or may not be connected with one another. In our definition below the node "A0" is connected with the node "A3", but "A0" is not connected with "A1". The connecting line between two nodes is called an edge. If the edges between the nodes are undirected, the graph is called an undirected graph. A weighted graph is a graph in which a number (the weight) is assigned to each edge. A graph is acyclic if it has no loop. A graph can be represented as a dictionary: `graph = {'A0': [('A3', 1), ('A5', 4)], 'A1': [('A2', 2)], 'A2': [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)], 'A3': [('A0', 1), ('A2', 1)], 'A4': [('A2', 1), ('A4', 1)], 'A5': [('A3', 3)] }` Here the nodes are A0...A5; following each nodes is the edges list of linked nodes with their weight. A0 is linked to A3 with a weight of 1 and to A5 with weight 4. A dictionary is not ordered but the list of linked nodes is sorted. So: `'A0': [('A3', 1), ('A5', 4)]`is correct but `'A0': [('A5', 4), ('A3', 1)]`is not. The edges E of a graph G induce a binary relation that is called the adjacency relation of G. One can associate an adjacency matrix: `M = [[0, 0, 0, 1, 0, 4], [0, 0, 2, 0, 0, 0], [0, 1, 2, 1, 1, 0], [1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 3, 0, 0]]` Let us imagine that lines are numbered from A0 to A5 and the same for columns. The first line correspond to A0 and we can see that A0 is connected to A3 with weight 1, A0 is also connected to A5 with weight 4. Another way is to use an adjacency list: An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring edges: `L = [['A0', [('A3', 1), ('A5', 4)]], ['A1', [('A2', 2)]], ['A2', [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)]], ['A3', [('A0', 1), ('A2', 1)]], ['A4', [('A2', 1), ('A4', 1)]], ['A5', [('A3', 3)]]] ` L is sorted in order A0 to A5 and each sublist is sorted as in a graph dictionary. In the kata nodes will be numbered from A0 to A(n-1) for a graph with n nodes. The task is to go from each represention to each other one and at last to find *all* paths from a node to another node. Below is a skeleton for our class `Graph`. class Graph(): def __init__(self, vertices_num): # number of nodes (an integer) self.v = vertices_num # (maybe not useful here) : list of nodes from "A0", "A1" ... to "A index (vertices_num - 1)" self.nodes = None # from adjacency matrix to dictionary def adjmat_2_graph(self, adjm): # from dictionary to adjacency matrix def graph_2_mat(self, graph): # from dictionary to adjacency list def graph_2_list(self, graph): # from adjacency list to dictionary def list_2_graph(self, lst): # from adjacency matrix to adjacency list def mat_2_list(self, mat): # from adjacency list to adjacency matrix def list_2_mat(self, lst): # find all paths from node start_vertex to node end_vertex def find_all_paths(self, graph, start_vertex, end_vertex): # Examples: - see: graph, M, L above. - finding all paths: `dct = {'A3': [('A0', 1), ('A2', 1)], 'A0': [('A3', 1), ('A2', 1)], 'A4': [('A2', 1)], 'A1': [('A2', 1)], 'A2': [('A1', 1), ('A2', 1), ('A3', 1), ('A4', 1)]}` `g = Graph(5)` `g.find_all_paths(dct, "A0", "A4") return l = ['A0-A2-A4', 'A0-A3-A2-A4']` Note that ['A0-A2-A4', 'A0-A3-A2-A4'] is sorted (hint: sorted(sorted(l, key=str), key=len)) Translators and critics are welcome.
class Graph: def __init__(self, vertices_num): self.v = vertices_num def sort_adj_list(self, lst): res = [] for l in lst: res.append([l[0], sorted(l[1])]) return sorted(res) def adjmat_2_graph(self, adjm): if len(adjm) != self.v: return "Dimension error" nodes = [("A" + str(i)) for i in range(0, self.v)] graph = {} for n, v in enumerate(nodes): voisin = [] for i, dist in enumerate(adjm[n]): if dist != 0: voisin.append((nodes[i], dist)) graph[v] = voisin self.graph = graph return graph def graph_2_mat(self, graph): nodes = sorted(graph.keys()) adjm = [] for node in nodes: L = [0] * len(nodes) parcours = graph[node] for voisin, dist in parcours: L[nodes.index(voisin)] = dist adjm.append(L) return adjm def graph_2_list(self, graph): r = [[k, graph[k]] for k in sorted(graph.keys())] return self.sort_adj_list(r) def list_2_graph(self, lst): if len(lst) != self.v: return "Dimension error" dct = {} for l in lst: dct[l[0]] = l[1] return dct def mat_2_list(self, mat): if len(mat) != self.v: return "Dimension error" return self.graph_2_list(self.adjmat_2_graph(mat)) def list_2_mat(self, lst): if len(lst) != self.v: return "Dimension error" return self.graph_2_mat(self.list_2_graph(lst)) def find_all_paths_aux(self, start, end, path=[]): graph = self.graph path = path + [start] if start == end: return [path] if start not in graph: return [] paths = [] for vertex in list([x[0] for x in graph[start]]): if vertex not in path: extended_paths = self.find_all_paths_aux(vertex, end, path) for p in extended_paths: paths.append(p) return paths def find_all_paths(self, graph, start, end): self.graph = graph paths = self.find_all_paths_aux(start, end) paths = list(["-".join(x) for x in paths]) return sorted(sorted(paths, key=str), key=len)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN STRING ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN STRING ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN STRING RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN STRING RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF LIST ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR IF VAR VAR RETURN LIST VAR IF VAR VAR RETURN LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
A "graph" consists of "nodes", also known as "vertices". Nodes may or may not be connected with one another. In our definition below the node "A0" is connected with the node "A3", but "A0" is not connected with "A1". The connecting line between two nodes is called an edge. If the edges between the nodes are undirected, the graph is called an undirected graph. A weighted graph is a graph in which a number (the weight) is assigned to each edge. A graph is acyclic if it has no loop. A graph can be represented as a dictionary: `graph = {'A0': [('A3', 1), ('A5', 4)], 'A1': [('A2', 2)], 'A2': [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)], 'A3': [('A0', 1), ('A2', 1)], 'A4': [('A2', 1), ('A4', 1)], 'A5': [('A3', 3)] }` Here the nodes are A0...A5; following each nodes is the edges list of linked nodes with their weight. A0 is linked to A3 with a weight of 1 and to A5 with weight 4. A dictionary is not ordered but the list of linked nodes is sorted. So: `'A0': [('A3', 1), ('A5', 4)]`is correct but `'A0': [('A5', 4), ('A3', 1)]`is not. The edges E of a graph G induce a binary relation that is called the adjacency relation of G. One can associate an adjacency matrix: `M = [[0, 0, 0, 1, 0, 4], [0, 0, 2, 0, 0, 0], [0, 1, 2, 1, 1, 0], [1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 3, 0, 0]]` Let us imagine that lines are numbered from A0 to A5 and the same for columns. The first line correspond to A0 and we can see that A0 is connected to A3 with weight 1, A0 is also connected to A5 with weight 4. Another way is to use an adjacency list: An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring edges: `L = [['A0', [('A3', 1), ('A5', 4)]], ['A1', [('A2', 2)]], ['A2', [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)]], ['A3', [('A0', 1), ('A2', 1)]], ['A4', [('A2', 1), ('A4', 1)]], ['A5', [('A3', 3)]]] ` L is sorted in order A0 to A5 and each sublist is sorted as in a graph dictionary. In the kata nodes will be numbered from A0 to A(n-1) for a graph with n nodes. The task is to go from each represention to each other one and at last to find *all* paths from a node to another node. Below is a skeleton for our class `Graph`. class Graph(): def __init__(self, vertices_num): # number of nodes (an integer) self.v = vertices_num # (maybe not useful here) : list of nodes from "A0", "A1" ... to "A index (vertices_num - 1)" self.nodes = None # from adjacency matrix to dictionary def adjmat_2_graph(self, adjm): # from dictionary to adjacency matrix def graph_2_mat(self, graph): # from dictionary to adjacency list def graph_2_list(self, graph): # from adjacency list to dictionary def list_2_graph(self, lst): # from adjacency matrix to adjacency list def mat_2_list(self, mat): # from adjacency list to adjacency matrix def list_2_mat(self, lst): # find all paths from node start_vertex to node end_vertex def find_all_paths(self, graph, start_vertex, end_vertex): # Examples: - see: graph, M, L above. - finding all paths: `dct = {'A3': [('A0', 1), ('A2', 1)], 'A0': [('A3', 1), ('A2', 1)], 'A4': [('A2', 1)], 'A1': [('A2', 1)], 'A2': [('A1', 1), ('A2', 1), ('A3', 1), ('A4', 1)]}` `g = Graph(5)` `g.find_all_paths(dct, "A0", "A4") return l = ['A0-A2-A4', 'A0-A3-A2-A4']` Note that ['A0-A2-A4', 'A0-A3-A2-A4'] is sorted (hint: sorted(sorted(l, key=str), key=len)) Translators and critics are welcome.
class Graph: def __init__(self, vertices_num): self.v = vertices_num self.nodes = None def adjmat_2_graph(self, adjm): gra = {} for vern, i in enumerate(adjm): tem = [] for ind, v in enumerate(i): if v != 0: tem.append(("A{}".format(ind), v)) gra["A{}".format(vern)] = tem return gra def graph_2_mat(self, graph): mat = [[(0) for _ in range(len(graph))] for _ in range(len(graph))] for k, v in graph.items(): for i in v: mat[int(k[1])][int(i[0][1])] = i[1] return mat def graph_2_list(self, graph): return sorted([[k, v] for k, v in graph.items()], key=lambda x: x[0]) def list_2_graph(self, lst): return {i[0]: i[1] for i in lst} def mat_2_list(self, mat): lst = [] for ind, v in enumerate(mat): tem = [] for ind1, i in enumerate(v): if i != 0: tem.append(("A{}".format(ind1), i)) lst.append(["A{}".format(ind), tem]) return sorted(lst, key=lambda x: x[0]) def list_2_mat(self, lst): mat = [[(0) for _ in range(len(lst))] for _ in range(len(lst))] for i in lst: for j in i[1]: mat[int(i[0][1])][int(j[0][1])] = j[1] return mat def find_all_paths(self, graph, start_vertex, end_vertex): all_paths = [] sta = [[start_vertex]] while sta: a = sta.pop() if a[-1] == end_vertex: all_paths.append(a) continue for i in graph[a[-1]]: if i[0] not in a: sta.append(a + [i[0]]) return ["-".join([i for i in p]) for p in sorted(sorted(all_paths), key=len)]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL STRING VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR NUMBER RETURN FUNC_CALL STRING VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
A "graph" consists of "nodes", also known as "vertices". Nodes may or may not be connected with one another. In our definition below the node "A0" is connected with the node "A3", but "A0" is not connected with "A1". The connecting line between two nodes is called an edge. If the edges between the nodes are undirected, the graph is called an undirected graph. A weighted graph is a graph in which a number (the weight) is assigned to each edge. A graph is acyclic if it has no loop. A graph can be represented as a dictionary: `graph = {'A0': [('A3', 1), ('A5', 4)], 'A1': [('A2', 2)], 'A2': [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)], 'A3': [('A0', 1), ('A2', 1)], 'A4': [('A2', 1), ('A4', 1)], 'A5': [('A3', 3)] }` Here the nodes are A0...A5; following each nodes is the edges list of linked nodes with their weight. A0 is linked to A3 with a weight of 1 and to A5 with weight 4. A dictionary is not ordered but the list of linked nodes is sorted. So: `'A0': [('A3', 1), ('A5', 4)]`is correct but `'A0': [('A5', 4), ('A3', 1)]`is not. The edges E of a graph G induce a binary relation that is called the adjacency relation of G. One can associate an adjacency matrix: `M = [[0, 0, 0, 1, 0, 4], [0, 0, 2, 0, 0, 0], [0, 1, 2, 1, 1, 0], [1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 3, 0, 0]]` Let us imagine that lines are numbered from A0 to A5 and the same for columns. The first line correspond to A0 and we can see that A0 is connected to A3 with weight 1, A0 is also connected to A5 with weight 4. Another way is to use an adjacency list: An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring edges: `L = [['A0', [('A3', 1), ('A5', 4)]], ['A1', [('A2', 2)]], ['A2', [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)]], ['A3', [('A0', 1), ('A2', 1)]], ['A4', [('A2', 1), ('A4', 1)]], ['A5', [('A3', 3)]]] ` L is sorted in order A0 to A5 and each sublist is sorted as in a graph dictionary. In the kata nodes will be numbered from A0 to A(n-1) for a graph with n nodes. The task is to go from each represention to each other one and at last to find *all* paths from a node to another node. Below is a skeleton for our class `Graph`. class Graph(): def __init__(self, vertices_num): # number of nodes (an integer) self.v = vertices_num # (maybe not useful here) : list of nodes from "A0", "A1" ... to "A index (vertices_num - 1)" self.nodes = None # from adjacency matrix to dictionary def adjmat_2_graph(self, adjm): # from dictionary to adjacency matrix def graph_2_mat(self, graph): # from dictionary to adjacency list def graph_2_list(self, graph): # from adjacency list to dictionary def list_2_graph(self, lst): # from adjacency matrix to adjacency list def mat_2_list(self, mat): # from adjacency list to adjacency matrix def list_2_mat(self, lst): # find all paths from node start_vertex to node end_vertex def find_all_paths(self, graph, start_vertex, end_vertex): # Examples: - see: graph, M, L above. - finding all paths: `dct = {'A3': [('A0', 1), ('A2', 1)], 'A0': [('A3', 1), ('A2', 1)], 'A4': [('A2', 1)], 'A1': [('A2', 1)], 'A2': [('A1', 1), ('A2', 1), ('A3', 1), ('A4', 1)]}` `g = Graph(5)` `g.find_all_paths(dct, "A0", "A4") return l = ['A0-A2-A4', 'A0-A3-A2-A4']` Note that ['A0-A2-A4', 'A0-A3-A2-A4'] is sorted (hint: sorted(sorted(l, key=str), key=len)) Translators and critics are welcome.
class Graph: def __init__(self, vertices_num): self.v = vertices_num self.nodes = [f"A{n}" for n in range(vertices_num)] def adjmat_2_graph(self, adjm): return { self.nodes[src_index]: [ (self.nodes[dst_index], weight) for dst_index, weight in enumerate(weights) if weight > 0 ] for src_index, weights in enumerate(adjm) if any(weight > 0 for weight in weights) } def graph_2_mat(self, graph): result = [[(0) for _ in range(self.v)] for _ in range(self.v)] for src, edges in graph.items(): src_index = self.nodes.index(src) for dst, weight in edges: dst_index = self.nodes.index(dst) result[src_index][dst_index] = weight return result def graph_2_list(self, graph): return sorted( [[src, edges] for src, edges in graph.items()], key=lambda x: x[0] ) def list_2_graph(self, lst): return {src: edges for src, edges in lst} def mat_2_list(self, mat): return self.graph_2_list(self.adjmat_2_graph(mat)) def list_2_mat(self, lst): return self.graph_2_mat(self.list_2_graph(lst)) def find_all_paths(self, graph, start_vertex, end_vertex, seen=None): if start_vertex == end_vertex and seen is None: return [start_vertex] if seen is None: seen = set() seen = seen | {start_vertex} next_options = [ dst for dst, weight in graph.get(start_vertex, []) if dst not in seen ] paths = [ (start_vertex + "-" + path) for option in next_options for path in self.find_all_paths(graph, option, end_vertex, seen) ] if end_vertex in next_options: paths.append(start_vertex + "-" + end_vertex) return sorted(sorted(paths, key=str), key=len)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR STRING VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF NONE IF VAR VAR VAR NONE RETURN LIST VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
A "graph" consists of "nodes", also known as "vertices". Nodes may or may not be connected with one another. In our definition below the node "A0" is connected with the node "A3", but "A0" is not connected with "A1". The connecting line between two nodes is called an edge. If the edges between the nodes are undirected, the graph is called an undirected graph. A weighted graph is a graph in which a number (the weight) is assigned to each edge. A graph is acyclic if it has no loop. A graph can be represented as a dictionary: `graph = {'A0': [('A3', 1), ('A5', 4)], 'A1': [('A2', 2)], 'A2': [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)], 'A3': [('A0', 1), ('A2', 1)], 'A4': [('A2', 1), ('A4', 1)], 'A5': [('A3', 3)] }` Here the nodes are A0...A5; following each nodes is the edges list of linked nodes with their weight. A0 is linked to A3 with a weight of 1 and to A5 with weight 4. A dictionary is not ordered but the list of linked nodes is sorted. So: `'A0': [('A3', 1), ('A5', 4)]`is correct but `'A0': [('A5', 4), ('A3', 1)]`is not. The edges E of a graph G induce a binary relation that is called the adjacency relation of G. One can associate an adjacency matrix: `M = [[0, 0, 0, 1, 0, 4], [0, 0, 2, 0, 0, 0], [0, 1, 2, 1, 1, 0], [1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 3, 0, 0]]` Let us imagine that lines are numbered from A0 to A5 and the same for columns. The first line correspond to A0 and we can see that A0 is connected to A3 with weight 1, A0 is also connected to A5 with weight 4. Another way is to use an adjacency list: An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring edges: `L = [['A0', [('A3', 1), ('A5', 4)]], ['A1', [('A2', 2)]], ['A2', [('A1', 1), ('A2', 2), ('A3', 1), ('A4', 1)]], ['A3', [('A0', 1), ('A2', 1)]], ['A4', [('A2', 1), ('A4', 1)]], ['A5', [('A3', 3)]]] ` L is sorted in order A0 to A5 and each sublist is sorted as in a graph dictionary. In the kata nodes will be numbered from A0 to A(n-1) for a graph with n nodes. The task is to go from each represention to each other one and at last to find *all* paths from a node to another node. Below is a skeleton for our class `Graph`. class Graph(): def __init__(self, vertices_num): # number of nodes (an integer) self.v = vertices_num # (maybe not useful here) : list of nodes from "A0", "A1" ... to "A index (vertices_num - 1)" self.nodes = None # from adjacency matrix to dictionary def adjmat_2_graph(self, adjm): # from dictionary to adjacency matrix def graph_2_mat(self, graph): # from dictionary to adjacency list def graph_2_list(self, graph): # from adjacency list to dictionary def list_2_graph(self, lst): # from adjacency matrix to adjacency list def mat_2_list(self, mat): # from adjacency list to adjacency matrix def list_2_mat(self, lst): # find all paths from node start_vertex to node end_vertex def find_all_paths(self, graph, start_vertex, end_vertex): # Examples: - see: graph, M, L above. - finding all paths: `dct = {'A3': [('A0', 1), ('A2', 1)], 'A0': [('A3', 1), ('A2', 1)], 'A4': [('A2', 1)], 'A1': [('A2', 1)], 'A2': [('A1', 1), ('A2', 1), ('A3', 1), ('A4', 1)]}` `g = Graph(5)` `g.find_all_paths(dct, "A0", "A4") return l = ['A0-A2-A4', 'A0-A3-A2-A4']` Note that ['A0-A2-A4', 'A0-A3-A2-A4'] is sorted (hint: sorted(sorted(l, key=str), key=len)) Translators and critics are welcome.
class Graph: def __init__(self, vertices_num): self.v = vertices_num self.nodes = None def adjmat_2_graph(self, adjm): g = {f"A{x}": [] for x in range(self.v)} for i in range(self.v): for j in range(self.v): if adjm[i][j] != 0: g[f"A{i}"].append((f"A{j}", adjm[i][j])) return g def graph_2_mat(self, graph): m = [[(0) for j in range(self.v)] for i in range(self.v)] for x in range(self.v): for g in graph[f"A{x}"]: m[x][int(g[0][1:])] = g[1] return m def graph_2_list(self, graph): return sorted([[key, val] for key, val in list(graph.items())]) def list_2_graph(self, lst): return {el[0]: el[1] for el in lst} def mat_2_list(self, mat): return self.graph_2_list(self.adjmat_2_graph(mat)) def list_2_mat(self, lst): return self.graph_2_mat(self.list_2_graph(lst)) def find_all_paths(self, graph, start_vertex, end_vertex): paths = [[start_vertex]] new_paths = [] res = [] if start_vertex == end_vertex: return [start_vertex] while paths: for p in paths: for t in graph[p[-1]]: if t[0] == end_vertex: res.append("-".join(p + [t[0]])) continue if t[0] in p: continue else: new_paths.append(p + [t[0]]) paths.clear() paths, new_paths = new_paths, paths return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF ASSIGN VAR STRING VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR STRING VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST LIST VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR VAR RETURN LIST VAR WHILE VAR FOR VAR VAR FOR VAR VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR LIST VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Coach Chef has selected all the players and now he has to separate them into 2 teams, $A$ and $B$. Each player must be included in exactly one of the 2 teams and each player $x$, has a skill level $S_{x}$. It is not necessary for both teams to have equal number of players, but they have to be non-empty. Since the number of players is way too high, Chef doesn't have an actual list of every player individually. Instead, he keeps a list of $N$ pairs $ {X_{i}, Y_{i} }$, which tells him that there are $Y_{i}$ players with skill level $X_{i}$. Chef thinks that a division into 2 teams is Good, if the skill value of every player in A divides the skill value of every player in B. More formally, $S_{b} \bmod S_{a} = 0$, βˆ€ a ∈ A, b ∈ B. Since he is busy, can you help him find total number of Good divisions? Since the answer can be very large, print it modulo $998244353$. NOTE : 2 divisions are considered different if there exists any player $x$ belonging to different teams in the 2 divisions. (See example for more clarity). ------ 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 for each testcase has a single integer $N$, denoting the number of pairs in Chef's list. $N$ lines follow. Each contains 2 space separated integers $X_{i}$ and $Y_{i}$, denoting Chef has $Y_{i}$ players with skill level $X_{i}$. ------ Output ------ Output one integer for every testcase. The number of total Good divisions, modulo $998244353$. ------ Constraints ------ $ $1 ≀ T, N ≀ 10^{5}$ $$ $1 ≀ X_{i} ≀ 10^{9}, X_{i} \neq X_{j}$ for $i \neq j$ $$ $1 ≀ Y_{i} ≀ 10^{5}$ $$ Sum of $N$ over all testcases does not exceed $10^{5}$ ------ Example Input ------ 5 1 10 1 3 1 1 3 1 6 1 3 1 1 3 2 6 1 10 1 30 2 40 3 30 6 50 12 100 18 500 216 400 24 999 432 9999 864 123 1 10 1000 ------ Example Output ------ 0 2 4 128248098 23226275 ------ Explanation ------ Example Case 1: Only one player P1. No way to split into teams. Example Case 2: 3 players total, P1, P2 & P3 with skill level 1, 3 & 6 respectively. Possible divisions: $ A=[P1], B=[P2, P3] $$ A=[P1, P2], B=[P3]Example Case 3: 4 players total, P1, P2, P3 & P4 with skill levels 1, 3, 3 & 6 respectively. Possible divisions : $ A=[P1], B=[P2, P3, P4] $$ A=[P1, P2], B=[P3, P4] $$ A=[P1, P3], B=[P2, P4] $$ A=[P1, P2, P3], B=[P4]
m = 998244353 def p(y): ans = 1 x = 2 while y > 0: if y & 1 == 1: ans = x * ans % m y = y >> 1 x = x * x % m return ans for _ in range(int(input())): n = int(input()) l = [] for _ in range(n): x, y = map(int, input().split()) l.append((x, y)) l.sort() ans = 0 for i in range(n): am = True j = i + 1 while j < n: if l[j][0] % l[i][0] != 0: am = False break j += 1 bm = True j = i - 1 while j >= 0: if l[i][0] % l[j][0] != 0: bm = False break j -= 1 if am and bm: ans = (ans + p(l[i][1]) - 1) % m if i == n - 1: ans = (ans - 1) % m if am and not bm: ans = (ans + 1) % m if i == n - 1: ans = (ans - 1) % m print(ans)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER 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 VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Coach Chef has selected all the players and now he has to separate them into 2 teams, $A$ and $B$. Each player must be included in exactly one of the 2 teams and each player $x$, has a skill level $S_{x}$. It is not necessary for both teams to have equal number of players, but they have to be non-empty. Since the number of players is way too high, Chef doesn't have an actual list of every player individually. Instead, he keeps a list of $N$ pairs $ {X_{i}, Y_{i} }$, which tells him that there are $Y_{i}$ players with skill level $X_{i}$. Chef thinks that a division into 2 teams is Good, if the skill value of every player in A divides the skill value of every player in B. More formally, $S_{b} \bmod S_{a} = 0$, βˆ€ a ∈ A, b ∈ B. Since he is busy, can you help him find total number of Good divisions? Since the answer can be very large, print it modulo $998244353$. NOTE : 2 divisions are considered different if there exists any player $x$ belonging to different teams in the 2 divisions. (See example for more clarity). ------ 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 for each testcase has a single integer $N$, denoting the number of pairs in Chef's list. $N$ lines follow. Each contains 2 space separated integers $X_{i}$ and $Y_{i}$, denoting Chef has $Y_{i}$ players with skill level $X_{i}$. ------ Output ------ Output one integer for every testcase. The number of total Good divisions, modulo $998244353$. ------ Constraints ------ $ $1 ≀ T, N ≀ 10^{5}$ $$ $1 ≀ X_{i} ≀ 10^{9}, X_{i} \neq X_{j}$ for $i \neq j$ $$ $1 ≀ Y_{i} ≀ 10^{5}$ $$ Sum of $N$ over all testcases does not exceed $10^{5}$ ------ Example Input ------ 5 1 10 1 3 1 1 3 1 6 1 3 1 1 3 2 6 1 10 1 30 2 40 3 30 6 50 12 100 18 500 216 400 24 999 432 9999 864 123 1 10 1000 ------ Example Output ------ 0 2 4 128248098 23226275 ------ Explanation ------ Example Case 1: Only one player P1. No way to split into teams. Example Case 2: 3 players total, P1, P2 & P3 with skill level 1, 3 & 6 respectively. Possible divisions: $ A=[P1], B=[P2, P3] $$ A=[P1, P2], B=[P3]Example Case 3: 4 players total, P1, P2, P3 & P4 with skill levels 1, 3, 3 & 6 respectively. Possible divisions : $ A=[P1], B=[P2, P3, P4] $$ A=[P1, P2], B=[P3, P4] $$ A=[P1, P3], B=[P2, P4] $$ A=[P1, P2, P3], B=[P4]
def gcd(a, b): if a == 0: return b return gcd(b % a, a) for _ in range(int(input())): n = int(input()) a = [] mod = 998244353 for i in range(n): x, y = map(int, input().split()) a.append((x, y)) a.sort() if n == 1: print((pow(2, a[0][1], mod) - 2) % mod) continue prefixlcm = [] lcm = 1 for i in range(n): lcm = lcm * a[i][0] // gcd(lcm, a[i][0]) prefixlcm.append(lcm) suffixgcd = [0] * n g = 0 for i in range(n - 1, -1, -1): g = gcd(g, a[i][0]) suffixgcd[i] = g res = 0 b = 0 for i in range(n - 1): if prefixlcm[i] > 1000000000.0: b = 1 break if suffixgcd[i] % prefixlcm[i] == 0: res = (res + pow(2, a[i][1], mod) - 2) % mod if i + 1 < n and suffixgcd[i + 1] % prefixlcm[i] == 0: res = (res + 1) % mod if not b and suffixgcd[n - 1] % prefixlcm[n - 1] == 0: res = (res + pow(2, a[n - 1][1], mod) - 2) % mod print(res)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
def countbits(num): cnt = 0 while num: num = num // 2 cnt += 1 return cnt po = [1] for i in range(1, 32): po.append(po[-1] * 2) for _ in range(int(input())): n = int(input()) a = list(map(int, input().strip().split())) mx = [0] * 32 mn = [float("inf")] * 32 for i in a: l = countbits(i) mx[l] = max(mx[l], i) mn[l] = min(mn[l], i) ans = -1 for i in range(32): for j in range(32): x = mx[i] y = mn[j] if x != 0 and y != float("inf"): xminusy = x * (po[j] - 1) - y * (po[i] - 1) ans = max(ans, xminusy) print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER 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 BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
from itertools import permutations for t in range(int(input())): n = int(input()) l = [int(x) for x in input().split()] if n < 1000: c = [x for x in permutations(l, 2)] a = [] for x in c: binx = bin(x[0])[2:] biny = bin(x[1])[2:] bxpy = binx + biny bypx = biny + binx xpy = int(bxpy, 2) ypx = int(bypx, 2) if xpy > ypx: a.append(xpy - ypx) print(max(a)) else: l.sort() m = max(l) binx = bin(m)[2:] a = 0 for i in range(n - 2, -1, -1): biny = bin(l[i])[2:] xpy = int(binx + biny, 2) ypx = int(biny + binx, 2) res = abs(xpy - ypx) if res > a: a = res print(a)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
t = int(input()) while t: t = t - 1 n = int(input()) l = list(map(int, input().split())) d = [] m = 0 ma = max(l) mb = bin(ma) mb = mb[2:] if n < 1000: for i in l: b = bin(i) d.append(b[2:]) for i in range(n): for j in range(i + 1, n): x = d[i] y = d[j] x1 = x + y y1 = y + x x1 = int(x1, 2) y1 = int(y1, 2) if m < abs(x1 - y1): m = abs(x1 - y1) print(m) else: for i in l: b = bin(i) d.append(b[2:]) for i in range(n): x = d[i] y = mb x1 = x + y y1 = y + x x1 = int(x1, 2) y1 = int(y1, 2) if m < abs(x1 - y1): m = abs(x1 - y1) print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
def binarycon(x, y): binx = bin(x)[2:] biny = bin(y)[2:] binxplsy = binx + biny binyplsx = biny + binx xplsy = int(binxplsy, 2) yplsx = int(binyplsx, 2) return xplsy - yplsx for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) rarr = [] if n < 500: for a in range(n): for b in range(n): rarr.append(binarycon(l[a], l[b])) print(max(rarr)) else: ans = 0 tmp = max(l) for x in range(n): rarr.append(binarycon(tmp, l[x])) print(max(rarr))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
def ans(x, y): x = bin(x)[2:] y = bin(y)[2:] return int(x + y, 2) - int(y + x, 2) for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) if n <= 800: x = max([ans(i, j) for j in a for i in a]) print(x) else: y = max(a) x = max([abs(ans(i, y)) for i in a]) print(x)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
def convert(a, b): ab = bin(a).replace("0b", "") ba = bin(b).replace("0b", "") aba = str(ab) + str(ba) bab = str(ba) + str(ab) x = int(aba, 2) y = int(bab, 2) return x - y t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] b = [] if n <= 500: for i in range(n): for j in range(n): b.append(convert(a[i], a[j])) print(max(b)) else: tmp = max(a) for i in range(n): b.append(convert(tmp, a[i])) print(max(b))
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
def binfun(x, y): binX = bin(x).replace("0b", "") binY = bin(y).replace("0b", "") binXplusY = binX + binY binYplusX = binY + binX XplusY = int(binXplusY, 2) YplusX = int(binYplusX, 2) return abs(XplusY - YplusX) t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) l = sorted(l) ans = 0 tmp = l[n - 1] for x in range(n): ans = max(ans, binfun(tmp, l[x])) tmp1 = l[n - 2] ans1 = 0 for x in range(n): ans1 = max(ans1, binfun(tmp1, l[x])) print(max(ans, ans1))
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
def dtb(n): return bin(n).replace("0b", "") def ans1(n, lst): res = 0 for i in range(n): for j in range(i, n): x = lst[i] y = lst[j] binx = dtb(x) biny = dtb(y) bfx = "0b" + binx + biny bfy = "0b" + biny + binx temp = abs(int(bfx, 2) - int(bfy, 2)) if temp >= res: res = temp return res def ans2(n, lst): res = 0 for i in range(n): x = lst[i] y = lst[n - 1] binx = dtb(x) biny = dtb(y) bfx = "0b" + binx + biny bfy = "0b" + biny + binx temp = abs(int(bfx, 2) - int(bfy, 2)) if temp >= res: res = temp return res for _ in range(int(input())): n = int(input()) lst = list(map(int, input().split())) lst.sort() if n < 1000: print(ans1(n, lst)) else: print(ans2(n, lst))
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
for i in range(int(input())): powers = [[None, None, None, None] for x in range(30)] n = int(input()) nums = [int(x) for x in input().split()] for i in nums: bi_num = bin(i)[2:] l = len(bi_num) - 1 if powers[l][0] == None or powers[l][0] > i: powers[l][0] = i powers[l][1] = l if powers[l][2] == None or powers[l][2] < i: powers[l][2] = i powers[l][3] = l res = 0 for j in range(30): for k in range(30): if powers[j][2] != None and powers[k][0] != None: tr = ( powers[j][2] * 2 ** (powers[k][1] + 1) + powers[k][0] - powers[k][0] * 2 ** (powers[j][3] + 1) - powers[j][2] ) if tr > res: res = tr print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NONE NONE NONE NONE VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NONE VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR IF VAR VAR NUMBER NONE VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER NONE VAR VAR NUMBER NONE ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
def strbin(x, y): binx = bin(x)[2:] biny = bin(y)[2:] xplusy = binx + biny yplusx = biny + binx return int(xplusy, 2) - int(yplusx, 2) test = int(input()) for _ in range(test): n = int(input()) a = list(map(int, input().split())) b = [] if n == 1: b.append(a[0]) else: a.sort() if n >= 10**3: for i in range(n - 1): b.append(strbin(a[n - 1], a[i])) else: for i in range(n): for j in range(n): if i == j: continue else: b.append(strbin(a[i], a[j])) print(max(b))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ 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 a single integer $N$. 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 maximum of BinaryConcatenation. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
def fun(x, y): lx = len(bin(x)[2:]) ly = len(bin(y)[2:]) var = x * pow(2, ly) + y - (y * pow(2, lx) + x) return var for _ in range(int(input())): n = int(input()) ar = list(map(int, input().split())) dic = {} for i in range(n): var = len(bin(ar[i])[2:]) - 1 if var in dic: dic[var].append(ar[i]) else: dic[var] = [ar[i]] for i in dic: dic[i].sort() m = None for i in dic: for j in dic: val = fun(dic[i][-1], dic[j][0]) if m == None or m < val: m = val print(m)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR