description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def hamdist(str1, str2): diffs = 0 for ch1, ch2 in zip(str1, str2): if ch1 != ch2: diffs += 1 return diffs t = int(input()) for i in range(t): n = int(input()) s = [int(x) for x in input().split()] j = 1 b = s.copy() s.append(s[0]) if n > 2: while j < n: if s[j] != s[j + 1]: s[j], s[j + 1] = s[j + 1], s[j] j += 2 elif s[j] != s[j - 1]: s[j], s[j - 1] = s[j - 1], s[j] j += 1 if s[0] != s[n]: for i in range(n): if s[n] != s[i] and s[n] != b[i]: s[n], s[i] = s[i], s[n] break elif s[0] == b[0]: for i in range(n): if s[0] != s[i] and s[0] != b[i]: s[0], s[i] = s[i], s[0] break ss = s[:n] print(hamdist(ss, b)) for kk in ss: print(kk, end=" ") print() elif n == 1: print(0) print(s[0]) elif n == 2: if s[0] == s[1]: print(0) print(s[0], s[1]) else: print(2) print(s[1], s[0])
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
T = int(input()) for ai in range(T): N = int(input()) inp = input() S = [int(m) for m in inp.split()] T = [int(m) for m in inp.split()] visit = [] for i in range(N): if S[i] == T[i] and i < N - 1: try: if S[i] != S[i + 1] and S[i + 1] != T[i] and S[i] != T[i + 1]: a = S[i] S[i] = S[i + 1] S[i + 1] = a elif S[i] != S[-1] and S[-1] != T[i] and S[i] != T[-1]: a = S[i] S[i] = S[-1] S[-1] = a elif S[i] != S[i - 1] and S[i - 1] != T[i] and S[i] != T[i - 1]: a = S[i] S[i] = S[i - 1] S[i - 1] = a elif S[i] != S[i - 2] and S[i - 2] != T[i] and S[i] != T[i - 2]: a = S[i] S[i] = S[i - 2] S[i - 2] = a except IndexError: pass elif S[i] == T[i]: try: if S[i] != S[-2] and S[-2] != T[i] and S[i] != T[-2]: a = S[i] S[i] = S[-2] S[-2] = a elif S[i] != S[-3] and S[-3] != T[i] and S[i] != T[-3]: a = S[i] S[i] = S[-3] S[-3] = a elif S[i] != S[-4] and S[-4] != T[i] and S[i] != T[-4]: a = S[i] S[i] = S[-4] S[-4] = a except IndexError: pass hd = 0 for i in range(N): if S[i] != T[i]: hd += 1 print(hd) print(" ".join(map(str, 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
import itertools T = int(input()) def hamming(A, B): N = 0 for a, b in zip(A, B): if a != b: N += 1 return N for t in range(T): N = int(input()) vals = list(int(x) for x in input().split()) i = 0 j = len(vals) - 1 left = [] right = [] permut = False while i <= j: left.append(vals[j]) if i != j: right.append(vals[i]) i = i + 1 j = j - 1 res = left[::-1] + right h = hamming(res, vals) print(h) print(" ".join(str(x) for x in res))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) while t > 0: n = int(input()) a = list(map(int, input().split())) z = [0] * n if n == 1: print(0) print(a[0]) elif n == 2: if a[0] == a[1]: print(0) print(a[0], a[1]) else: print(2) print(a[1], a[0]) elif n == 3: if a[0] == a[1]: print(2) print(a[2], a[0], a[1]) elif a[0] == a[2]: print(2) print(a[1], a[0], a[2]) elif a[1] == a[2]: print(2) print(a[1], a[0], a[2]) else: print(3) print(a[2], a[0], a[1]) else: r = n % 4 tt = n // 4 if r == 3: if a[n - 3] == a[n - 2]: z[n - 1] = a[n - 3] z[n - 3] = a[n - 1] elif a[n - 3] == a[n - 1]: z[n - 3] = a[n - 2] z[n - 2] = a[n - 3] elif a[n - 1] == a[n - 2]: z[n - 2] = a[n - 3] z[n - 3] = a[n - 2] else: z[n - 3] = a[n - 1] z[n - 2] = a[n - 3] z[n - 1] = a[n - 2] for i in range(0, tt * 4 - 4 + 1, 4): if a[i + 2] == a[i + 3] or a[i] == a[i + 1]: z[i] = a[i + 3] z[i + 3] = a[i] z[i + 1] = a[i + 2] z[i + 2] = a[i + 1] elif ( a[i] == a[i + 2] or a[i] == a[i + 3] or a[i + 1] == a[i + 3] or a[i + 1] == a[i + 2] ): z[i] = a[i + 1] z[i + 1] = a[i] z[i + 2] = a[i + 3] z[i + 3] = a[i + 2] else: z[i] = a[i + 1] z[i + 1] = a[i] z[i + 2] = a[i + 3] z[i + 3] = a[i + 2] if z[n - 1] == 0 and z[n - 2] == 0: rmt = a[n - 2] smt = a[n - 1] z[n - 2] = z[0] z[n - 1] = z[1] z[0] = rmt z[1] = smt elif z[n - 1] == 0: for i in range(n): if a[n - 1] != z[i] and a[i] != a[n - 1]: z[n - 1] = z[i] z[i] = a[n - 1] break elif z[n - 2] == 0: for i in range(n): if a[n - 2] != z[i] and a[i] != a[n - 2]: z[n - 2] = z[i] z[i] = a[n - 2] break print(n) for i in range(n): print(z[i], end=" ") print() t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 BIN_OP LIST NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().strip().split(" "))) b = a[:] distance = 0 for j in range(n): if a[j] != b[j]: continue for k in range(n): if a[j] != b[k] and a[k] != b[j]: b[j], b[k] = b[k], b[j] break for j in range(n): if a[j] != b[j]: distance += 1 print(distance) print(" ".join(map(str, b)))
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 FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for _ in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] if n == 1 or n == 2 and a[0] == a[1]: print(0) print(" ".join([str(i) for i in a])) continue elif n == 3 and (a[0] == a[1] or a[0] == a[2] or a[1] == a[2]): print(2) if a[0] == a[1]: a[1], a[2] = a[2], a[1] elif a[1] == a[2]: a[0], a[2] = a[2], a[0] elif a[0] == a[2]: a[0], a[1] = a[1], a[0] print(" ".join([str(i) for i in a])) continue else: print(n) if n == 2: a[0], a[1] = a[1], a[0] print(" ".join([str(i) for i in a])) continue latencies = set() index = {} for i in range(len(a)): if a[i] in index.keys(): latencies.add(i - index[a[i]]) latencies.add(n - (i - index[a[i]])) else: index[a[i]] = i rot = 1 while rot <= len(a): if rot not in latencies: break rot += 1 a = a[rot:] + a[:rot] print(" ".join([str(i) for i in 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 VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) while t > 0: n = int(input()) inp = input() inp = inp.split(" ") l = list(inp) m = n // 2 for i in range(n): j = i + 1 while j < n and (inp[i] == l[j] or inp[j] == l[i]): j += 1 if j != n: l[i], l[j] = l[j], l[i] if l[n - 1] == inp[n - 1]: for o in range(n - 1): if inp[o] != l[n - 1] and l[o] != inp[n - 1]: l[o], l[n - 1] = l[n - 1], l[o] break p = 0 for i in range(n): if inp[i] != l[i]: p += 1 print(p) for i in range(n - 1): print(int(l[i]), end=" ") print(l[n - 1]) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
test = int(input()) for _ in range(test): n = int(input()) l = list(map(int, input().split())) nl = l[:] hammed = [(0) for i in range(n)] cnt = 0 for i in range(n - 1): if l[i] == nl[i]: nl[i], nl[i + 1] = nl[i + 1], nl[i] if l[i] != nl[i]: cnt += 1 hammed[i] = 1 elif i + 2 < n: nl[i], nl[i + 2] = nl[i + 2], nl[i] cnt += 1 hammed[i] = 1 else: cnt += 1 hammed[i] = 1 if l[-1] != nl[-1]: cnt += 1 hammed[-1] = 1 else: try: temp = hammed.index(1) if l[temp] != nl[-1] and nl[temp] != l[-1]: nl[temp], nl[-1] = nl[-1], nl[temp] cnt += 1 else: for i in range(temp + 1, n): if hammed[i] == 1 and nl[-1] != l[i] and nl[i] != l[-1]: nl[i], nl[-1] = nl[-1], nl[i] cnt += 1 break except: pass for i in range(n - 1): if hammed[i] == 0: try: temp = hammed.index(1) if l[temp] != nl[i] and nl[temp] != l[i]: nl[temp], nl[i] = nl[i], nl[temp] cnt += 1 else: for j in range(temp + 1, n): if hammed[j] == 1 and nl[i] != l[j] and nl[j] != l[i]: nl[j], nl[i] = nl[i], nl[j] cnt += 1 break except: pass print(cnt) print(*nl)
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 VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) new_l = l[:] for i in range(n - 1): l[i], l[i + 1] = l[i + 1], l[i] hd = n for i in range(n): if l[i] == new_l[i]: for j in range(n): if l[i] != l[j] and l[i] != new_l[j]: l[i], l[j] = l[j], l[i] break else: hd -= 1 print(hd) print(*l)
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 VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) for x in range(t): n = int(input()) arrA = [int(i) for i in input().split()] nc = 0 mydb = {} for i in range(n): if arrA[i] in mydb: mydb[arrA[i]][1] += 1 mydb[arrA[i]][2] = i nc += 1 else: mydb[arrA[i]] = [i, 1, -1] ci = 0 si = nc ns = n - 2 * nc barray = [0] * (n + 2) index = [-1] * n adj = 1 if nc == 1: adj = 2 else: adj = 1 for num in arrA: tmplist = mydb[num] if tmplist[1] == 1: barray[si + adj] = num index[si] = tmplist[0] si += 1 elif tmplist[1] == 2: barray[ci + adj] = num index[ci] = tmplist[0] barray[ci + nc + ns + adj] = num index[ci + nc + ns] = tmplist[2] ci += 1 tmplist[1] = 0 if adj == 1: barray[0] = barray[n] elif adj == 2: barray[0] = barray[n] barray[1] = barray[n + 1] newB = [0] * n for i in range(n): newB[index[i]] = barray[i] if n == 3 and nc == 1: print(2) elif n == 1 or n == 2 and nc == 1: print(0) else: print(n) for i in range(n): print(newB[i], end=" ") print()
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 NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ 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 an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
MAX = 100001 t = int(input()) for z in range(t): n = int(input()) a = list(map(int, input().split(" "))) c = [0] * MAX for i in a: c[i] += 1 if n == 1: print(0) print(a[0]) continue if n == 2: if a[0] == a[1]: print(0) print(a[0], a[0]) else: print(2) print(a[1], a[0]) continue if n == 3: if a[0] == a[1]: print(2) print(a[2], a[1], a[0]) elif a[0] == a[2]: print(2) print(a[0], a[2], a[1]) elif a[1] == a[2]: print(2) print(a[1], a[0], a[2]) else: print(3) print(a[1], a[2], a[0]) continue print(n) x = [] y = [] for i in range(MAX): if c[i] == 1: x.append(i) elif c[i] == 2: y.append(i) d = {} if len(x) == 0: d[y[0]] = y[len(y) - 1] for i in range(1, len(y)): d[y[i]] = y[i - 1] for i in range(n): print(d[a[i]], end=" ") print() elif len(y) == 0: d[x[0]] = x[len(x) - 1] for i in range(1, len(x)): d[x[i]] = x[i - 1] for i in range(n): print(d[a[i]], end=" ") print() elif len(y) == 1: for i in range(2, len(x)): d[x[i]] = x[i - 2] d[x[0]] = y[0] d[x[1]] = y[0] flag = 0 for i in range(n): if c[a[i]] == 1: print(d[a[i]], end=" ") elif flag == 0: print(x[len(x) - 1], end=" ") flag = 1 else: print(x[len(x) - 2], end=" ") print() elif len(x) == 1: for i in range(1, len(y)): d[y[i]] = y[i - 1] flag = 0 for i in range(n): if c[a[i]] == 1: print(y[len(y) - 1], end=" ") elif a[i] == y[0]: if flag == 0: print(y[len(y) - 1], end=" ") flag = 1 else: print(x[0], end=" ") else: print(d[a[i]], end=" ") print() else: d[x[0]] = x[len(x) - 1] for i in range(1, len(x)): d[x[i]] = x[i - 1] d[y[0]] = y[len(y) - 1] for i in range(1, len(y)): d[y[i]] = y[i - 1] for i in range(n): print(d[a[i]], end=" ") print()
ASSIGN 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 STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING IF VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): N = len(S) mp = [(0) for i in range(26)] for i in range(26): mp[i] = 0 for i in range(N): mp[ord(S[i]) - ord("a")] += 1 ans = 0 for i in range(N): ans += N - mp[ord(S[i]) - ord("a")] ans //= 2 for i in range(26): if mp[i] >= 2: ans += 1 break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, s): n = len(s) d = {} for i in s: if i not in d: d[i] = 1 else: d[i] += 1 an = 0 for i in d.values(): if i > 1: an += i * (i - 1) // 2 ans = n * (n - 1) // 2 - an if an > 0: ans += 1 return ans if __name__ == "__main__": t = int(input()) for _ in range(t): S = input() ob = Solution() ans = ob.countStrings(S) print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, s): arr = [(0) for i in range(26)] for i in range(len(s)): arr[ord(s[i]) - ord("a")] += 1 ans = 0 for i in range(len(s)): ans += len(s) - arr[ord(s[i]) - ord("a")] ans = ans // 2 for i in range(26): if arr[i] >= 2: ans += 1 break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, s): b = list(s) l = set(b) j = len(s) - 1 t = j * (j + 1) // 2 if len(l) == len(s): return t else: m = [] for i in l: m.append(b.count(i)) for i in m: t = t - i * (i - 1) // 2 return t + 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): if len(S) == len(set(list(S))): n = len(S) return n * (n - 1) // 2 else: freq = [] lst = list(S) for i in set(lst): freq.append(lst.count(i)) n = len(S) sum = n * (n - 1) // 2 for i in freq: if i > 1: sum = sum - i * (i - 1) // 2 return sum + 1
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): l = [0] * 26 n = len(S) for i in S: l[ord(i) - 97] += 1 ans = 0 for i in S: ans += n - l[ord(i) - 97] ans //= 2 for i in l: if i >= 2: ans += 1 break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): v = [0] * 26 a = 0 n = len(S) for i in range(len(S)): v[ord(S[i]) - ord("a")] += 1 a = n * (n - 1) // 2 f = False for i in range(26): if v[i] > 1: f = True a -= v[i] * (v[i] - 1) // 2 if f: return a + 1 else: return a
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR RETURN BIN_OP VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): freq = [(0) for i in range(26)] for i in range(len(S)): freq[ord(S[i]) - ord("a")] += 1 repeats = 0 for i in range(26): repeats += freq[i] * (freq[i] - 1) // 2 total = len(S) * (len(S) - 1) // 2 if repeats > 0: return 1 + total - repeats return total
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP NUMBER VAR VAR RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): count = [0] * 26 n = len(S) for elem in S: pos = ord(elem) - 97 count[pos] += 1 ans = 0 for elem in S: pos = ord(elem) - 97 ans += n - count[pos] ans //= 2 for i in range(26): if count[i] >= 2: ans += 1 break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, s): d = {} for i in s: if i in d: d[i] += 1 else: d[i] = 1 c = 0 for i in s: c = c + (len(s) - d[i]) c = c // 2 for i in d: if d[i] >= 2: c = c + 1 break return c
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, s): n = len(s) d = {} for i in s: if i not in d: d[i] = 1 else: d[i] += 1 an = 0 for i in d.values(): if i > 1: an += i * (i - 1) // 2 ans = n * (n - 1) // 2 - an if an > 0: ans += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): n = len(S) - 1 curr = ans = n * (n + 1) // 2 arr = [(0) for _ in range(26)] for ch in S: arr[ord(ch) - 97] += 1 for i in range(26): cnt = arr[i] - 1 ans -= int(cnt * (cnt + 1) / 2) return ans if ans == curr else ans + 1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR BIN_OP VAR NUMBER
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, s): d = {} a = 0 count = 0 n = len(s) for i in s: if d.get(i) == None: d[i] = 1 else: a = 1 d[i] += 1 if a == 1: count = 1 for i in range(n): if d.get(s[i]) == 1: count += n - 1 - i else: d[s[i]] = d[s[i]] - 1 count += n - 1 - i - d[s[i]] return count
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): count = [0] * 26 for i in S: count[ord(i) - ord("a")] += 1 dup = 0 n = len(S) for i in range(26): dup += count[i] * (count[i] - 1) // 2 if dup > 0: return 1 + n * (n - 1) // 2 - dup else: return n * (n - 1) // 2
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, s): f = len(s) * (len(s) - 1) // 2 arr = [0] * 26 for i in s: arr[ord(i) - ord("a")] += 1 j = 0 for i in range(26): if arr[i] >= 2: f -= arr[i] * (arr[i] - 1) // 2 j += 1 return f if j == 0 else f + 1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER VAR BIN_OP VAR NUMBER
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): f = [(0) for i in range(26)] for i in S: f[ord(i) - 97] += 1 n = len(S) c = n * (n - 1) // 2 z = c for i in f: if i > 1: c -= i * (i - 1) // 2 if z == c: return c return c + 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): d = {} n = len(S) f = 0 for i in S: if i not in d.keys(): d[i] = 1 else: f = 1 d[i] += 1 ans = 0 for i in S: ans += n - d[i] if f == 0: return ans // 2 else: return ans // 2 + 1
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): ans = 0 n = len(S) charset = [0] * 26 for i in S: charset[ord(i) - ord("a")] += 1 for i in range(26): ans += charset[i] * (charset[i] - 1) // 2 if ans > 0: return n * (n - 1) // 2 - ans + 1 else: return n * (n - 1) // 2
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): l = len(S) s = list(set(S)) l_u = len(s) total_word = (l - 1) * l // 2 if l == l_u: return total_word freq_count = {} for each in S: if each in freq_count: freq_count[each] += 1 else: freq_count[each] = 1 rep_words = 0 for each in freq_count: if freq_count[each] > 1: rep_words += freq_count[each] * (freq_count[each] - 1) // 2 unique_words = total_word - rep_words + 1 return unique_words if __name__ == "__main__": t = int(input()) for _ in range(t): S = input() ob = Solution() ans = ob.countStrings(S) print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, s): c = 0 d = {} l = len(s) for i in s: d[i] = d.get(i, 0) + 1 for i in d: if d[i] > 1: c += d[i] * (d[i] - 1) // 2 ans = l * (l - 1) // 2 if c > 0: ans = ans - c + 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): n = len(S) d = {} for i in S: if i in d: d[i] += 1 else: d[i] = 1 sum1 = n * (n - 1) // 2 for i in d: sum1 -= d[i] * (d[i] - 1) // 2 l1 = list(d.values()) if l1.count(1) == len(l1): return sum1 return sum1 + 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, s): count = len(S) * (len(S) - 1) // 2 arr = {} for ele in s: if ele in arr: arr[ele] += 1 else: arr[ele] = 1 for value in arr.values(): if value == 1: continue else: count -= value * (value - 1) // 2 for value in arr.values(): if value > 1: count += 1 break return count
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): n = len(S) char = {} count = 0 for i in S: if i in char: count = count + char[i] + 1 char[i] += 1 else: char[i] = 0 ans = n * (n - 1) // 2 if count > 0: ans = ans - count + 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): s = {} for i in S: if i in s: s[i] += 1 else: s[i] = 1 d = len(S) p = d * (d - 1) // 2 e = p for i in s: p = p - s[i] * (s[i] - 1) // 2 if p == e: return p return p + 1
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): freq = dict() for i in S: if i in freq: freq[i] += 1 else: freq[i] = 1 resp = 0 for i in freq.keys(): resp += freq[i] * (freq[i] - 1) / 2 n = len(S) return int(1 + n * (n - 1) / 2 - resp if resp > 0 else n * (n - 1) / 2 - resp)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): a = S l = [0] * 26 for i in a: d = ord(i) - ord("a") l[d] = l[d] + 1 n = len(a) - 1 res = 0 flag = 0 for i in a: d = ord(i) - ord("a") l[d] = l[d] - 1 if l[d] > 0: flag = 1 if l[d] >= 0 and n - l[d] > 0: res = res + n - l[d] n = n - 1 return res + flag
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): uniq = 0 alpha = [0] * 26 for s in S: x = ord(s) - ord("a") alpha[x] += 1 ans = 0 extra = 0 for i in range(len(S)): x = ord(S[i]) - ord("a") if alpha[x] > 1: extra = 1 alpha[x] -= 1 ans += len(S) - i - 1 - alpha[x] return ans + extra
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): m = {} n = len(S) count, r = 0, 0 temp = sorted(list(S)) for i in range(n): m[S[i]] = 0 for i in range(n): m[S[i]] += 1 for i in range(n): count += int(n - m[S[i]]) if m[S[i]] > 1: r += 1 count = int(count // 2) if r > 0: return count + 1 else: return count
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, s): d = {} res = 0 n = len(s) for i in s: if i in d: d[i] += 1 else: d[i] = 1 m = max(d.values()) for i in range(n): d[s[i]] -= 1 res += n - 1 - i - d[s[i]] if m > 1: return res + 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): ans = 0 dict = {} n = len(S) any = 0 for i in range(n): if not S[i] in dict: dict[S[i]] = 1 ans += i else: any = 1 ans += i - dict[S[i]] dict[S[i]] += 1 return ans + any
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR
Given a string S containing lowercase english alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j. Example 1: Input: S = "geek" Output: 6 Explanation: After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg") Example 2: Input: S = "ab" Output: 1 Explanation: Only one string is possible after one swap(i.e "ba") Your Task: You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer. Expected Time Complexity: O(|S|) . Expected Auxiliary Space: O(1) . Constraints: 2 ≤ |S| ≤ 10^{5} S contains lowercase characters
class Solution: def countStrings(self, S): n = len(S) ans = n * (n - 1) // 2 combo = [(0) for i in range(26)] for i in range(n): combo[ord(S[i]) - 97] += 1 f = 0 for i in range(26): if combo[i] >= 2: f += 1 ans = ans - combo[i] * (combo[i] - 1) // 2 if f == 0: return ans return ans + 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
mod = 998244353 n = int(input()) p = list(map(int, input().split())) i100 = pow(100, mod - 2, mod) p = [(i * i100 % mod) for i in p] top = 1 for i in range(n - 2, -1, -1): top *= p[i] top += 1 top %= mod bot = 1 for i in range(n): bot *= p[i] bot %= mod ans = top * pow(bot, mod - 2, mod) % mod print(ans)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
import sys reader = (map(int, line.split()) for line in sys.stdin) input = reader.__next__ def mod_inverse(x, modulo): inv = 0 nextInv = 1 gcd = modulo nextGcd = x while nextGcd != 0: quotient = gcd // nextGcd inv, nextInv = nextInv, inv - quotient * nextInv gcd, nextGcd = nextGcd, gcd - quotient * nextGcd if gcd > 1: raise ValueError("gcd(x, modulo) > 1: argument is not invertible") if inv < 0: inv = inv + modulo return inv def mod_division(ratio, modulo): a, b = ratio b_inv = mod_inverse(b, modulo) return a * b_inv % modulo modulo = 998244353 (n,) = input() p = list(input()) a = 0 add = 1 b = 1 for pi in p: b *= pi b %= modulo a += add a *= 100 a %= modulo add *= pi add %= modulo ans = mod_division((a, b), modulo) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
MOD = 998244353 def powr(n, N): temp = 1 while N > 0: if N % 2 != 0: temp = temp * n % MOD n = n * n % MOD N = N // 2 return temp % MOD def MODI(a, b): ans = powr(a, b) % MOD return ans n = int(input()) p = [int(x) for x in input().split()] mul = 1 kk = 1 for i in range(0, len(p)): mul = mul * p[i] % MOD kk = kk * 100 % MOD d = MODI(mul, MOD - 2) % MOD pre = MODI(100, MOD - 2) % MOD dp = kk * d % MOD tot = 0 for i in range(0, len(p)): tot = (tot + dp) % MOD dp = dp * p[i] % MOD dp = dp * pre % MOD print(tot)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
input() p, f = 998244353, 0 for i in map(int, input().split()): f = 100 * (f + 1) * pow(i, p - 2, p) % p print(f)
EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
M = 998244353 def gcd(a, b): if a == 0: return 0, 1 x, y = gcd(b % a, a) return y - b // a * x, x k = input() probs = list(map(int, input().split(" "))) num, denum = 0, 1 for p in probs: num = (num + denum) * 100 % M denum = denum * p % M inv, _ = gcd(denum, M) print(num * inv % M)
ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
def extended_gcd(aa, bb): lastremainder, remainder = abs(aa), abs(bb) x, lastx, y, lasty = 0, 1, 1, 0 while remainder: lastremainder, (quotient, remainder) = remainder, divmod( lastremainder, remainder ) x, lastx = lastx - quotient * x, x y, lasty = lasty - quotient * y, y return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1) def modinv(a, m): g, x, y = extended_gcd(a, m) if g != 1: raise ValueError return x % m m = 998244353 n = int(input()) p = list(map(int, input().split(" "))) up = 0 low = 1 for i in range(n): up = up + low up = up * 100 % m low = low * p[i] % m print(up * modinv(low, m) % m)
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
N = int(input()) P = list(map(int, input().split())) mod = 998244353 p = 0 q = 1 for i in range(N): p, q = 100 * (p + q) % mod, P[i] * q % mod print(p * pow(q, mod - 2, mod) % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
def main(): m = 998244353 n = int(input()) pp = map(int, input().split()) probb = 100 num = 0 for i, p in enumerate(pp, 1): probu = (100 - p) * probb % m probb = p * probb % m num = (num * 100 + i * probu) % m num = (num + n * probb) % m print(num * pow(probb, m - 2, m) % m) main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
mod = 998244353 n = int(input()) ar = list(map(int, input().split())) m, mul = 1, [1] div = pow(100, mod - 2, mod) for i in ar: mul.append(mul[-1] * i * div % mod) num = 0 den = pow(mul[n], mod - 2, mod) for i in mul: num += i num %= mod print((num * den - 1) % mod)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER LIST NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
def solve(): s = input() if "aa" in s or "bb" in s or "cc" in s: print(-1) return syms = s + "@" ans = ["@"] for i, sym in enumerate(s): if sym != "?": ans.append(sym) continue for x in "abc": if x != ans[-1] and x != syms[i + 1]: ans.append(x) break print("".join(ans[1:])) def solveb(): n = int(input()) perm = [int(x) for x in input().split()] num___idx = [(-1) for _ in range(n + 1)] for i, num in enumerate(perm): num___idx[num] = i curr_max = -1 curr_min = 2 * n num___pretty = [(0) for _ in range(n + 1)] for num in range(1, n + 1): curr_max = max(num___idx[num], curr_max) curr_min = min(num___idx[num], curr_min) if curr_max - curr_min + 1 == num: num___pretty[num] = 1 print(*num___pretty[1:], sep="") def solvec(): n = int(input()) rank___problems_nr = [int(x) for x in input().split()] weird_prefsums = [0] last_num = rank___problems_nr[0] for num in rank___problems_nr: if num != last_num: last_num = num weird_prefsums.append(weird_prefsums[-1]) weird_prefsums[-1] += 1 gold = weird_prefsums[0] silvers = 0 i = 1 for i in range(1, len(weird_prefsums)): x = weird_prefsums[i] if x - gold > gold: silvers = x - gold break bronzes = 0 for j in range(i, len(weird_prefsums)): x = weird_prefsums[j] if x > n / 2: break if x - gold - silvers > gold: bronzes = x - gold - silvers if bronzes == 0 or silvers == 0: print(0, 0, 0) return print(gold, silvers, bronzes) def solved(): a, b, c, d = (int(x) for x in input().split()) ab_len = min(a, b) a -= ab_len b -= ab_len cd_len = min(c, d) c -= cd_len d -= cd_len if a == 1 and cd_len == 0 and d == 0 and c == 0: print("YES") print("0 1 " * ab_len + "0") return if d == 1 and ab_len == 0 and a == 0 and b == 0: print("YES") print("3 " + "2 3 " * cd_len) return if a > 0 or d > 0: print("NO") return cb_len = min(b, c) b -= cb_len c -= cb_len if b > 1 or c > 1: print("NO") return print("YES") print("1 " * b + "0 1 " * ab_len + "2 1 " * cb_len + "2 3 " * cd_len + "2" * c) def get_me(prob, mod): return 100 * pow(prob, mod - 2, mod) % mod def solvee(): n = int(input()) mod = 998244353 idx___prob = [int(x) for x in input().split()] curr_me = get_me(idx___prob[0], mod) for prob in idx___prob[1:]: me = get_me(prob, mod) curr_me *= me curr_me %= mod curr_me += me curr_me %= mod print(curr_me) solvee()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF STRING VAR STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR LIST STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR STRING IF VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR STRING RETURN IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING VAR RETURN IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
from sys import stdin input = stdin.readline mod = 998244353 inv = [0] * 103 inv[1] = 1 for i in range(2, 101): inv[i] = (mod - mod // i) * inv[mod % i] % mod n = int(input()) h = [0] * (n + 3) h[0] = 1 for i in range(1, n + 2): h[i] = h[i - 1] * 100 % mod p = list(map(int, input().split())) tot = 1 * h[n] % mod now = 1 iv = 1 for i in range(n): if i == n - 1: iv = iv * inv[p[i]] % mod continue now = now * p[i] % mod tot += now * h[n - i - 1] % mod tot %= mod iv = iv * inv[p[i]] % mod print(tot * iv % mod)
ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
import sys import time f = sys.stdin t1 = time.time() n = int(f.readline()) MOD = 998244353 prob = list(map(int, f.readline().split())) num = pow(100, n, MOD) new_term = num inv_100 = pow(100, MOD - 2, MOD) for x in prob[:-1]: new_term = new_term * x * inv_100 % MOD num = (num + new_term) % MOD t4 = time.time() den = new_term * prob[-1] * inv_100 inverse = pow(den, MOD - 2, MOD) print(num * inverse % MOD)
IMPORT IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
n = int(input()) p = list(map(int, input().split())) ans = 0 size = 998244353 for p in p: ans = (ans + 1) % size ans = ans * 100 % size * pow(p, size - 2, size) % size print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
mod = 998244353 def inv_mod(n): return pow(n, mod - 2, mod) n = int(input()) p = [int(x) for x in input().split()] res = 0 for i in p: res = (res + 1) * 100 * inv_mod(i) % mod print(res)
ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
import sys input = sys.stdin.readline mod = 998244353 def extgcd(a, b): r = [1, 0, a] w = [0, 1, b] while w[2] != 1: q = r[2] // w[2] r2 = w w2 = [r[0] - q * w[0], r[1] - q * w[1], r[2] - q * w[2]] r = r2 w = w2 return [w[0], w[1]] def mod_inv(a, m=mod): x = extgcd(a, m)[0] return (m + x % m) % m N = int(input()) A = list(map(int, input().split())) K = 0 P = 0 Q = 1 for i, a in enumerate(A): p0 = a * mod_inv(100) % mod q0 = (100 - a) * mod_inv(100) % mod P = (P + (i + 1) * Q * q0) % mod K = (K + Q * q0) % mod Q = Q * p0 % mod inv = (mod + 1 - K) % mod w = (N * Q + P) % mod ans = w * mod_inv(inv) % mod print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER VAR WHILE VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN LIST VAR NUMBER VAR NUMBER FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
mod = 998244353 def pow_(x, p, mod): if p == 1: return x % mod tmp = pow_(x, p // 2, mod) if p % 2 == 0: return tmp * tmp % mod else: return tmp * tmp * x % mod def reverse(x, mod): return pow_(x, mod - 2, mod) n = int(input()) + 1 p = [0] + list(map(int, input().split())) dp = [0] * n rev = [0] * 101 for i in range(1, 101): rev[i] = reverse(i, mod) for i in range(1, n): dp[i] = (dp[i - 1] + 1) * 100 * rev[p[i]] % mod print(dp[-1])
ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
from sys import stdin, stdout read = lambda: map(int, stdin.readline().split()) I = lambda: stdin.readline() m = 998244353 n = int(I()) arr = tuple(read()) p = 1 fac = 1 s1 = s2 = 0 pows = [1] for i in range(n): pows.append(pows[-1] * 100 % m) for ind, i in enumerate(arr): fac = p * (100 - i) * pows[n - ind - 1] s1 = (s1 + fac * (ind + 1)) % m s2 = (s2 + fac) % m p = p * i % m s1 = (s1 + p * n) % m s0 = pow(100, n, m) p, q = s1, s0 - s2 div, mod = divmod(p, q) if mod == 0: print(div) else: q1 = pow(q, m - 2, m) print(p * q1 % m)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
n = int(input()) exp = 0 M = 998244353 for pi in map(int, input().split()): exp = (exp + 1) * 100 * pow(pi, M - 2, M) % M print(int(exp % M))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
import sys input = sys.stdin.readline n = int(input()) L = map(int, input().split()) k = 0 c = 1 m = 998244353 counter = 0 for i in L: k += c * pow(100, n - counter, m) k %= m c *= i c %= m counter += 1 def modInverse(a, m): m0 = m y = 0 x = 1 if m == 1: return 0 while a > 1: q = a // m t = m m = a % m a = t t = y y = x - q * y x = t if x < 0: x = x + m0 return x p = k % m q = c % m print(modInverse(q, m) * p % m)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
mod = 998244353 N = 1000 inverse = [0, 1] for i in range(2, N + 1): inverse.append(-inverse[mod % i] * (mod // i) % mod) n = int(input()) p = list(map(int, input().split())) a = [0] * (n + 1) b = [0] * (n + 1) a[0] = 1 for i in range(1, n + 1): a[i] = 100 * inverse[p[i - 1]] * (a[i - 1] + p[i - 1] * inverse[100] - 1) % mod b[i] = 100 * inverse[p[i - 1]] * (b[i - 1] - 1) % mod ans = -b[n] * pow(a[n], mod - 2, mod) % mod print(ans)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
MOD = 998244353 def power(x, y): res = 1 while y > 0: if y & 1: res = x * res % MOD y = y >> 1 x = x * x % MOD return res n = int(input()) prob = input().split() p = power(100, n) q = 1 for i in range(0, n - 1): q = q * int(prob[i]) % MOD p = (p + q * power(100, n - i - 1) % MOD) % MOD q = q * int(prob[n - 1]) % MOD print(p * power(q, MOD - 2) % MOD)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
import sys MOD = 998244353 def powmod(a, b): if b == 0: return 1 res = powmod(a, b // 2) res *= res res %= MOD if b & 1: res *= a res %= MOD return res def inv(n): return powmod(n, MOD - 2) _100 = inv(100) n = int(input()) p = list(map(lambda x: int(x) * _100 % MOD, input().split())) if n == 1: print(inv(p[0])) sys.exit(0) pp = [0] * n pp[0] = p[0] for i in range(1, n): pp[i] = pp[i - 1] * p[i] % MOD k = 0 for i in range(0, n - 1): c = inv(pp[n - 2]) if i: c *= pp[i - 1] c %= MOD k += c k %= MOD res = (k + 1) * inv(p[-1]) res %= MOD print(res)
IMPORT ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
n = int(input()) a = list(map(int, input().split())) mod = 998244353 rng = 1100 inv = [0, 1] for i in range(2, rng): inv.append(pow(i, mod - 2, mod)) acc = [0] * n acc[-1] = 100 * inv[a[-1]] for i in range(n - 1)[::-1]: acc[i] = acc[i + 1] * 100 * inv[a[i]] % mod print(sum(acc) % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
n = input() n = int(n) md = 998244353 L = list(map(int, input().split())) def inv(x): global md return pow(x, md - 2, md) ml = 1 ans = 0 for el in reversed(L): ml *= 100 ml *= inv(el) ml %= md ans += ml ans %= md print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
import sys input = sys.stdin.readline MOD = 998244353 n = int(input()) a = [int(item) for item in input().split()] A = 0 B = 0 child = 1 bases = [] base = 1 for i in range(n + 10): bases.append(base) base *= 100 base %= MOD for i, item in enumerate(a): A += child * (100 - item) * bases[n - i - 1] A %= MOD B += child * (100 - item) * bases[n - i - 1] * (i + 1) B %= MOD child *= item child %= MOD B += child * n ans = B * pow(bases[n] - A, MOD - 2, MOD) ans %= MOD print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
mod = 998244353 def inv_mod(n): return pow(n, mod - 2, mod) def frac_mod(a, b): return a * inv_mod(b) % mod n = int(input()) a = input().split() res = 0 for v in a: res = (res + 1) * 100 * inv_mod(int(v)) % mod print(res)
ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
n = int(input()) x = list(map(int, input().split())) s, dp, mod = 0, 1, 998244353 for i in range(n): if i > 0: s = s * 100 % mod s = (s + (100 - x[i]) * dp * (i + 1)) % mod if i == n - 1: s = (s + x[i] * dp * (i + 1)) % mod dp = dp * x[i] % mod ans = s * pow(dp, mod - 2, mod) % mod print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Some mirrors are called checkpoints. Initially, only the $1$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $i$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $q$ queries, each query is represented by an integer $u$: If the $u$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $u$-th mirror is no longer a checkpoint. After each query, you need to calculate the expected number of days until Creatnx becomes happy. Each of this numbers should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains two integers $n$, $q$ ($2 \leq n, q \le 2 \cdot 10^5$)  — the number of mirrors and queries. The second line contains $n$ integers: $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). Each of $q$ following lines contains a single integer $u$ ($2 \leq u \leq n$) — next query. -----Output----- Print $q$ numbers – the answers after each query by modulo $998244353$. -----Examples----- Input 2 2 50 50 2 2 Output 4 6 Input 5 5 10 20 30 40 50 2 3 4 5 3 Output 117 665496274 332748143 831870317 499122211 -----Note----- In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $\frac{1}{2}$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $2$ and the answer will be equal to $4 = 2 + 2$.
import sys input = sys.stdin.readline mod = 998244353 n = int(input()) p = list(map(int, input().split())) a = pow(100, n, mod) b = pow(100, mod - 2, mod) x = a y = 1 for i in range(n - 1): a = a * b % mod y = y * p[i] % mod x = (x + a * y) % mod y = y * p[n - 1] % mod y_inv = pow(y, mod - 2, mod) print(x * y_inv % mod)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): freq = {} temp = [] ans = [] for v in a: if v not in freq: freq[v] = 1 temp.append(v) i = len(temp) - 1 else: freq[v] += 1 i = temp.index(v) while i > 0: if freq[temp[i]] > freq[temp[i - 1]]: temp[i], temp[i - 1] = temp[i - 1], temp[i] i -= 1 elif freq[temp[i]] == freq[temp[i - 1]]: if temp[i] < temp[i - 1]: temp[i], temp[i - 1] = temp[i - 1], temp[i] i -= 1 else: break if len(temp) <= k: ans.extend(temp) else: ans.extend(temp[:k]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, kk): d = {} ar = [] ans = [] for x in a: if x in d: d[x][0] += 1 k = d[x][1] while k >= 1: v = ar[k - 1] if d[v][0] < d[x][0]: ar[k] = ar[k - 1] d[v][1] = k elif d[v][0] == d[x][0] and v > x: ar[k] = ar[k - 1] d[v][1] = k else: break k -= 1 ar[k] = x d[x][1] = k else: ar.append(x) d[x] = [1, None] k = len(ar) - 1 while k >= 1: v = ar[k - 1] if v > x and d[v][0] <= d[x][0]: ar[k] = ar[k - 1] d[v][1] = k else: break k -= 1 ar[k] = x d[x][1] = k ans.extend(ar[:kk]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, arr, n, k): ans = [] top = [0] * (k + 1) freq = {i: (0) for i in range(k + 1)} for i in range(n): try: freq[arr[i]] += 1 except: freq[arr[i]] = 1 top[k] = arr[i] index = top.index(arr[i]) - 1 while index >= 0: if freq[top[index]] < freq[top[index + 1]]: top[index], top[index + 1] = top[index + 1], top[index] elif ( freq[top[index]] == freq[top[index + 1]] and top[index] > top[index + 1] ): top[index], top[index + 1] = top[index + 1], top[index] else: break index -= 1 for i in range(len(top)): if top[i] != 0 and i < k: ans.append(top[i]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): top = [(0) for i in range(k + 1)] freq = {i: (0) for i in range(k + 1)} ans = [] for m in range(n): if a[m] in freq.keys(): freq[a[m]] += 1 else: freq[a[m]] = 1 top[k] = a[m] i = top.index(a[m]) i -= 1 while i >= 0: if freq[top[i]] < freq[top[i + 1]]: t = top[i] top[i] = top[i + 1] top[i + 1] = t elif freq[top[i]] == freq[top[i + 1]] and top[i] > top[i + 1]: t = top[i] top[i] = top[i + 1] top[i + 1] = t else: break i -= 1 i = 0 while i < k and top[i] != 0: ans.append(top[i]) i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, arr, n, k): mapx = {} ans = [] for i in range(n): if arr[i] not in mapx: mapx[arr[i]] = 1 else: mapx[arr[i]] += 1 q = list(mapx.items()) q.sort() q.sort(reverse=True, key=lambda x: x[1]) i = 0 while q and i < k: tup = q.pop(0) ans.append(tup[0]) i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): class LLNode: def __init__(self, key, val): self.key = key self.val = val self.prev = None self.next = None class LL: def __init__(self): self.head = None self.tail = None def append(self, node): if self.head is None: self.head = node self.tail = node else: self.tail.next = node node.prev = self.tail self.tail = node def shift(self, node): while True: if node.prev is None or node.prev.val > node.val: break if node.prev.val == node.val and node.prev.key < node.key: break prev_node = node.prev prev_node.next = node.next if not node.next is None: node.next.prev = prev_node else: self.tail = prev_node node.prev = prev_node.prev if not prev_node.prev is None: prev_node.prev.next = node else: self.head = node node.next = prev_node prev_node.prev = node def pprint(self, k=None): result = [] n = 0 node = self.head while not node is None: if not k is None and n >= k: break result.append(node.key) node = node.next n += 1 return result result = [] ll = LL() node_map = {} for x in a: if not x in node_map: node = LLNode(x, 1) ll.append(node) ll.shift(node) node_map[x] = node else: node = node_map[x] node.val += 1 ll.shift(node) result += ll.pprint(k) return result
CLASS_DEF FUNC_DEF CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF WHILE NUMBER IF VAR NONE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF NONE ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): ans = [] d = {} for i in a: if d.get(i) == None: d[i] = 0 d[i] += 1 g = sorted(list(d.keys()), key=lambda x: (-d[x], x)) ans.extend(g[:k]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): d = {} for i in a: if i not in d: d[i] = 0 st = "" def aa(a, d, st, K): s = [] for i in a: d[i] = d[i] + 1 for k, v in d.items(): s.append([k] * v) s.sort() aa = sorted(s, key=len, reverse=True) ruk = 0 for i in aa: ruk += 1 for j in i: st = st + str(j) st = st + " " break if ruk >= K: break return st for i in range(1, n + 1): st = aa(a[:i], d, st, k) d = {} for i in a: if i not in d: d[i] = 0 return st.split() t = int(input()) for _ in range(0, t): n, k = list(map(int, input().split())) a = list(map(int, input().split())) ob = Solution() ans = ob.kTop(a, n, k) print(*ans)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
def find(i, q): for j in range(len(q)): if q[j] == i: return j def updateq(i, q, freqDict): pos = find(i, q) while pos > 0 and freqDict[q[pos - 1]] < freqDict[q[pos]]: q[pos - 1], q[pos] = q[pos], q[pos - 1] pos -= 1 while pos > 0 and freqDict[q[pos - 1]] == freqDict[q[pos]] and q[pos - 1] > q[pos]: q[pos - 1], q[pos] = q[pos], q[pos - 1] pos -= 1 def receive(i, q, freqDict, windDict, k): if i in windDict: freqDict[i] += 1 updateq(i, q, freqDict) else: if i in freqDict: freqDict[i] += 1 else: freqDict[i] = 1 windDict[i] = 1 q.append(i) updateq(i, q, freqDict) if len(q) > k: el = q.pop() windDict.pop(el) def printQueue(q): for i in q: print(str(i), end=" ") class Solution: def kTop(self, a, n, k): freqDict = {} windDict = {} q = [] ret = "" for i in a: receive(i, q, freqDict, windDict, k) printQueue(q) return ret
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): k = min(n, k) ans = [] temp = [] mp = {} j = 0 for i in range(n): mp[a[i]] = mp.get(a[i], 0) - 1 if mp[a[i]] == -1: temp.append(a[i]) j += 1 temp = sorted(temp, key=lambda x: (mp[x], x)) for p in range(min(k, len(temp))): ans.append(temp[p]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): d = {} ans = [] max_fre = 1 for i in a: if i in d: d[i] += 1 if d[i] > max_fre: max_fre = d[i] else: d[i] = 1 anstemp = [] for j in range(max_fre, 0, -1): temp = [] for g in d: if d[g] == j: temp.append(g) temp.sort() anstemp.extend(temp) ans.extend(anstemp[:k]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): d = {} ans = [] for i in range(n): if a[i] in d: d[a[i]] += 1 else: d[a[i]] = 1 ans += sorted(d.keys(), key=lambda x: (-d[x], x))[:k] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): q = [] for i in range(n): l = a[: i + 1] d = {} for i in l: m = l.count(i) if m in d: d[m].append(i) else: d[m] = [i] arr = [] for i in d: arr.append(i) c = 0 arr.sort(reverse=True) for i in arr: if c >= k: break p = d[i] p = set(p) p = list(p) p.sort() for j in p: if c >= k: break q.append(j) c += 1 return q
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): vis = {} ans = [] for i in range(n): arr = [] temp = a[i] if temp in vis.keys(): vis[temp] += 1 else: vis[temp] = 1 for key in vis.keys(): arr.append([key, vis[key]]) arr.sort(key=lambda x: (-x[1], x[0])) for t in range(len(arr)): if t == k: break ans.append(arr[t][0]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, arr, n, k): k_arr = [] freq = {} out = [] for num in arr: if num in freq: freq[num] += 1 else: freq[num] = 1 k_arr.append(num) curr_index = len(k_arr) - 1 for i in reversed(range(1, len(k_arr))): if freq[k_arr[i]] > freq[k_arr[i - 1]]: k_arr[i], k_arr[i - 1] = k_arr[i - 1], k_arr[i] for i in reversed(range(1, len(k_arr))): if freq[k_arr[i]] == freq[k_arr[i - 1]] and k_arr[i] < k_arr[i - 1]: k_arr[i], k_arr[i - 1] = k_arr[i - 1], k_arr[i] for i in range(len(k_arr)): if i < k: out.append(k_arr[i]) return out
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. Example 1: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 2} Output: 5 2 5 1 2 5 1 2 3 5 2 1 3 5 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, which is smallet among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5 Then again 2, which has the highest frequency among all number so 2 1 3 5. Example 2: Input: N=5, K=4 arr[] = {5, 2, 1, 3, 4} Output: 5 2 5 1 2 5 1 2 3 5 1 2 3 4 Explanation: Firstly their was 5 whose frequency is max till now. so print 5. Then 2 , which is smaller than 5 but their frequency is same so print 2 5. Then 1, Which is smallest among all the number arrived, so print 1 2 5. Then 3 , so print 1 2 3 5. Then 4, so 1 2 3 4 as K is 4 so print at-most k elements. Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array a, integer n and integer k as parameters and returns the array that contains our desired output. Expected Time Complexity: O(N*K). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N,K ≤ 10^{3}
class Solution: def kTop(self, a, n, k): ans = [] freq = {} pos = {} curr = [] for item in a: if item not in pos: curr.append(item) freq[item] = 1 pos[item] = len(curr) - 1 else: freq[item] += 1 for j in range(pos[item], 0, -1): if ( freq[curr[j - 1]] < freq[curr[j]] or freq[curr[j - 1]] == freq[curr[j]] and curr[j - 1] > curr[j] ): curr[j - 1], curr[j] = curr[j], curr[j - 1] pos[curr[j - 1]] = j - 1 pos[curr[j]] = j else: break ans.extend(curr[:k]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two numbers as strings s1 and s2. Calculate their Product. Note: The numbers can be negative and You are not allowed to use any built-in function or convert the strings to integers. Example 1: Input: s1 = "33" s2 = "2" Output: 66 Example 2: Input: s1 = "11" s2 = "23" Output: 253 Your Task: You don't need to read input or print anything. Your task is to complete the function multiplyStrings() which takes two strings s1 and s2 as input and returns their product as a string. Expected Time Complexity: O(n_{1}* n_{2}) Expected Auxiliary Space: O(n_{1 }+ n_{2}); where n_{1} and n_{2} are sizes of strings s1 and s2 respectively. Constraints: 1 ≤ length of s1 and s2 ≤ 10^{3}
class Solution: def multiplyStrings(self, a, b): if a == "0" or b == "0": return "0" negative = False if a[0] == "-": negative = not negative a = a[1:] if b[0] == "-": negative = not negative b = b[1:] product = [(0) for _ in range(len(a) + len(b))] for i in range(len(b) - 1, -1, -1): digit1 = int(b[i]) carry = 0 for j in range(len(a) - 1, -1, -1): digit2 = int(a[j]) product[i + j + 1] += digit1 * digit2 + carry carry = product[i + j + 1] // 10 product[i + j + 1] = product[i + j + 1] % 10 nextIndex = i while carry: product[nextIndex] += carry carry = product[nextIndex] // 10 product[nextIndex] = product[nextIndex] % 10 nextIndex -= 1 res = "".join(str(x) for x in product) zeroes = 0 while zeroes < len(res) - 1 and res[zeroes] == "0": zeroes += 1 res = res[zeroes:] if negative: res = "-" + res return res
CLASS_DEF FUNC_DEF IF VAR STRING VAR STRING RETURN STRING ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR BIN_OP STRING VAR RETURN VAR
Given two numbers as strings s1 and s2. Calculate their Product. Note: The numbers can be negative and You are not allowed to use any built-in function or convert the strings to integers. Example 1: Input: s1 = "33" s2 = "2" Output: 66 Example 2: Input: s1 = "11" s2 = "23" Output: 253 Your Task: You don't need to read input or print anything. Your task is to complete the function multiplyStrings() which takes two strings s1 and s2 as input and returns their product as a string. Expected Time Complexity: O(n_{1}* n_{2}) Expected Auxiliary Space: O(n_{1 }+ n_{2}); where n_{1} and n_{2} are sizes of strings s1 and s2 respectively. Constraints: 1 ≤ length of s1 and s2 ≤ 10^{3}
class Solution: def StringToInt(self, s): k = 0 l = len(s) if s[0] == "-": for i in range(l - 1): k = k + 10 ** (l - i - 2) * (ord(s[i + 1]) - 48) return -1 * k else: for i in range(l): k = k + 10 ** (l - i - 1) * (ord(s[i]) - 48) return k def multiplyStrings(self, s1, s2): var = Solution() n1 = Solution.StringToInt(var, s1) n2 = Solution.StringToInt(var, s2) return n1 * n2
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR
Given two numbers as strings s1 and s2. Calculate their Product. Note: The numbers can be negative and You are not allowed to use any built-in function or convert the strings to integers. Example 1: Input: s1 = "33" s2 = "2" Output: 66 Example 2: Input: s1 = "11" s2 = "23" Output: 253 Your Task: You don't need to read input or print anything. Your task is to complete the function multiplyStrings() which takes two strings s1 and s2 as input and returns their product as a string. Expected Time Complexity: O(n_{1}* n_{2}) Expected Auxiliary Space: O(n_{1 }+ n_{2}); where n_{1} and n_{2} are sizes of strings s1 and s2 respectively. Constraints: 1 ≤ length of s1 and s2 ≤ 10^{3}
class Solution: def multiplyStrings(self, s1, s2): if s1 != "0": s1_sanitized = s1.lstrip("0") else: s1_sanitized = s1 if s2 != "0": s2_sanitized = s2.lstrip("0") else: s2_sanitized = s2 return eval(s1_sanitized) * eval(s2_sanitized)
CLASS_DEF FUNC_DEF IF VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Given two numbers as strings s1 and s2. Calculate their Product. Note: The numbers can be negative and You are not allowed to use any built-in function or convert the strings to integers. Example 1: Input: s1 = "33" s2 = "2" Output: 66 Example 2: Input: s1 = "11" s2 = "23" Output: 253 Your Task: You don't need to read input or print anything. Your task is to complete the function multiplyStrings() which takes two strings s1 and s2 as input and returns their product as a string. Expected Time Complexity: O(n_{1}* n_{2}) Expected Auxiliary Space: O(n_{1 }+ n_{2}); where n_{1} and n_{2} are sizes of strings s1 and s2 respectively. Constraints: 1 ≤ length of s1 and s2 ≤ 10^{3}
class Solution: def multiplyStrings(self, s1, s2): n1 = n2 = 0 for i in range(len(s1)): if s1[i] == "-": continue n1 = n1 * 10 + int(s1[i]) if s1[0] == "-": n1 = -n1 for i in range(len(s2)): if s2[i] == "-": continue n2 = n2 * 10 + int(s2[i]) if s2[0] == "-": n2 = -n2 return n1 * n2
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER STRING ASSIGN VAR VAR RETURN BIN_OP VAR VAR
Given two numbers as strings s1 and s2. Calculate their Product. Note: The numbers can be negative and You are not allowed to use any built-in function or convert the strings to integers. Example 1: Input: s1 = "33" s2 = "2" Output: 66 Example 2: Input: s1 = "11" s2 = "23" Output: 253 Your Task: You don't need to read input or print anything. Your task is to complete the function multiplyStrings() which takes two strings s1 and s2 as input and returns their product as a string. Expected Time Complexity: O(n_{1}* n_{2}) Expected Auxiliary Space: O(n_{1 }+ n_{2}); where n_{1} and n_{2} are sizes of strings s1 and s2 respectively. Constraints: 1 ≤ length of s1 and s2 ≤ 10^{3}
class Solution: def multiplyStrings(self, s1, s2): num1 = num2 = 0 size1 = len(s1) size2 = len(s2) string = [] if size1 > 0 and s1[0] == "-": sign1 = -1 else: sign1 = 1 num1 = ord(s1[0]) - ord("0") if size2 > 0 and s2[0] == "-": sign2 = -1 else: sign2 = 1 num2 = ord(s2[0]) - ord("0") for i in range(1, size1): num1 = num1 * 10 + (ord(s1[i]) - ord("0")) for i in range(1, size2): num2 = num2 * 10 + (ord(s2[i]) - ord("0")) mul = num1 * num2 if mul == 0: string.append("0") while mul > 0: char = mul % 10 + ord("0") string.append(chr(char)) mul //= 10 if sign1 * sign2 == -1 and num1 * num2 != 0: string.append("-") separator = "" res = separator.join(string) res = res[::-1] return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR
Given two numbers as strings s1 and s2. Calculate their Product. Note: The numbers can be negative and You are not allowed to use any built-in function or convert the strings to integers. Example 1: Input: s1 = "33" s2 = "2" Output: 66 Example 2: Input: s1 = "11" s2 = "23" Output: 253 Your Task: You don't need to read input or print anything. Your task is to complete the function multiplyStrings() which takes two strings s1 and s2 as input and returns their product as a string. Expected Time Complexity: O(n_{1}* n_{2}) Expected Auxiliary Space: O(n_{1 }+ n_{2}); where n_{1} and n_{2} are sizes of strings s1 and s2 respectively. Constraints: 1 ≤ length of s1 and s2 ≤ 10^{3}
class Solution: def multiplyStrings(self, s1, s2): d1 = 0 d2 = 0 b = 1 flag = 0 for i in range(len(s1) - 1, -1, -1): if s1[i] != "-" and s1[i] != "+": d1 = d1 + b * int(s1[i]) b = b * 10 else: s = s1[i] flag = 1 if flag == 1 and s == "-": d1 = d1 * -1 flag = 0 b = 1 for i in range(len(s2) - 1, -1, -1): if s2[i] != "-" and s2[i] != "+": d2 = d2 + b * int(s2[i]) b = b * 10 else: s = s2[i] flag = 1 if flag == 1 and s == "-": d2 = d2 * -1 return d1 * d2
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller. The elements should follow the following order ! # $ % & * @ ^ ~ . Example 1: Input: N = 5 nuts[] = {@, %, $, #, ^} bolts[] = {%, @, #, $ ^} Output: # $ % @ ^ # $ % @ ^ Example 2: Input: N = 9 nuts[] = {^, &, %, @, #, *, $, ~, !} bolts[] = {~, #, @, %, &, *, $ ,^, !} Output: ! # $ % & * @ ^ ~ ! # $ % & * @ ^ ~ Your Task: You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 9 Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}.
class Solution: def matchPairs(self, nuts, bolts, n): elts = "! # $ % & * @ ^ ~".split(" ") res = [e for e in elts if e in nuts and e in bolts] nuts.clear() bolts.clear() nuts.extend(res) bolts.extend(res)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL STRING STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller. The elements should follow the following order ! # $ % & * @ ^ ~ . Example 1: Input: N = 5 nuts[] = {@, %, $, #, ^} bolts[] = {%, @, #, $ ^} Output: # $ % @ ^ # $ % @ ^ Example 2: Input: N = 9 nuts[] = {^, &, %, @, #, *, $, ~, !} bolts[] = {~, #, @, %, &, *, $ ,^, !} Output: ! # $ % & * @ ^ ~ ! # $ % & * @ ^ ~ Your Task: You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 9 Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}.
class Solution: def matchPairs(self, nuts, bolts, n): nuts.sort() for i in range(len(nuts)): if nuts[i] in bolts: t = bolts.index(nuts[i]) if i == t: continue else: temp = bolts[i] bolts[i] = bolts[t] bolts[t] = temp
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller. The elements should follow the following order ! # $ % & * @ ^ ~ . Example 1: Input: N = 5 nuts[] = {@, %, $, #, ^} bolts[] = {%, @, #, $ ^} Output: # $ % @ ^ # $ % @ ^ Example 2: Input: N = 9 nuts[] = {^, &, %, @, #, *, $, ~, !} bolts[] = {~, #, @, %, &, *, $ ,^, !} Output: ! # $ % & * @ ^ ~ ! # $ % & * @ ^ ~ Your Task: You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 9 Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}.
class Solution: def matchPairs(self, nuts, bolts, n): for i in range(n): for j in range(i, n): if nuts[i] == bolts[j]: temp = bolts[j] bolts[j] = bolts[i] bolts[i] = temp break nuts.sort() bolts.sort()
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller. The elements should follow the following order ! # $ % & * @ ^ ~ . Example 1: Input: N = 5 nuts[] = {@, %, $, #, ^} bolts[] = {%, @, #, $ ^} Output: # $ % @ ^ # $ % @ ^ Example 2: Input: N = 9 nuts[] = {^, &, %, @, #, *, $, ~, !} bolts[] = {~, #, @, %, &, *, $ ,^, !} Output: ! # $ % & * @ ^ ~ ! # $ % & * @ ^ ~ Your Task: You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 9 Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}.
class Solution: def matchPairs(self, nuts, bolts, n): L = ["!", "#", "$", "%", "&", "*", "@", "^", "~"] N, B = [], [] for i in L: if i in nuts: N.append(i) if i in bolts: B.append(i) nuts.clear() bolts.clear() nuts.extend(N) bolts.extend(B) return ()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR LIST LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller. The elements should follow the following order ! # $ % & * @ ^ ~ . Example 1: Input: N = 5 nuts[] = {@, %, $, #, ^} bolts[] = {%, @, #, $ ^} Output: # $ % @ ^ # $ % @ ^ Example 2: Input: N = 9 nuts[] = {^, &, %, @, #, *, $, ~, !} bolts[] = {~, #, @, %, &, *, $ ,^, !} Output: ! # $ % & * @ ^ ~ ! # $ % & * @ ^ ~ Your Task: You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 9 Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}.
class Solution: def matchPairs(self, nuts, bolts, n): for i in range(n): for j in range(i, n): if nuts[i] > nuts[j]: nuts[i], nuts[j] = nuts[j], nuts[i] for i in range(n): for j in range(i, n): if bolts[i] > bolts[j]: bolts[i], bolts[j] = bolts[j], bolts[i]
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller. The elements should follow the following order ! # $ % & * @ ^ ~ . Example 1: Input: N = 5 nuts[] = {@, %, $, #, ^} bolts[] = {%, @, #, $ ^} Output: # $ % @ ^ # $ % @ ^ Example 2: Input: N = 9 nuts[] = {^, &, %, @, #, *, $, ~, !} bolts[] = {~, #, @, %, &, *, $ ,^, !} Output: ! # $ % & * @ ^ ~ ! # $ % & * @ ^ ~ Your Task: You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 9 Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}.
class Solution: def matchPairs(self, nuts, bolts, n): l = ["!", "#", "$", "%", "&", "*", "@", "^", "~"] nut = nuts[:] bolt = bolts[:] i = 0 for c in l: if nut.__contains__(c) and bolt.__contains__(c): nuts[i] = c bolts[i] = c i += 1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller. The elements should follow the following order ! # $ % & * @ ^ ~ . Example 1: Input: N = 5 nuts[] = {@, %, $, #, ^} bolts[] = {%, @, #, $ ^} Output: # $ % @ ^ # $ % @ ^ Example 2: Input: N = 9 nuts[] = {^, &, %, @, #, *, $, ~, !} bolts[] = {~, #, @, %, &, *, $ ,^, !} Output: ! # $ % & * @ ^ ~ ! # $ % & * @ ^ ~ Your Task: You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 9 Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}.
class Solution: def __init__(self): pass def priority(self, x): if x == "!": return 1 if x == "#": return 2 if x == "$": return 3 if x == "%": return 4 if x == "&": return 5 if x == "*": return 6 if x == "@": return 7 if x == "^": return 8 if x == "~": return 9 @staticmethod def cmp(a, b): sol = Solution() return sol.priority(a) < sol.priority(b) def matchPairs(self, nuts, bolts, n): nuts.sort(key=lambda x: self.priority(x)) bolts.sort(key=lambda x: self.priority(x))
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller. The elements should follow the following order ! # $ % & * @ ^ ~ . Example 1: Input: N = 5 nuts[] = {@, %, $, #, ^} bolts[] = {%, @, #, $ ^} Output: # $ % @ ^ # $ % @ ^ Example 2: Input: N = 9 nuts[] = {^, &, %, @, #, *, $, ~, !} bolts[] = {~, #, @, %, &, *, $ ,^, !} Output: ! # $ % & * @ ^ ~ ! # $ % & * @ ^ ~ Your Task: You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 9 Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}.
class Solution: def matchPairs(self, p_nuts, p_bolts, n): global nuts global bolts lst = [] x = ["!", "#", "$", "%", "&", "*", "@", "^", "~"] for i in x: if i in p_nuts or len(lst) > 10: lst += [i] nuts = lst bolts = lst
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR VAR