text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In the spring of 2014, a student successfully passed the university and started living alone. The problem here is what to do with the supper. He decided to plan a supper for the next N days. He wants to maximize the total happiness he gets in N days. Of course, the more delicious or favorite you eat, the higher your happiness. His dinner options are two, either head to a nearby dining room or cook for himself. The happiness you get in the cafeteria depends on the menu of the day. The menu changes daily, but there is only one type every day, and all the menus for N days have already been released. So he knows all the information that if you go to the cafeteria on day i (1 ≤ i ≤ N) you will get the happiness of Ci. The happiness obtained by self-catering is the self-catering power at the start of self-catering multiplied by a constant P. The initial value of self-catering power is Q, which is -1 if you go to the restaurant every day, +1 if you cook yourself, and fluctuates at the end of the meal of the day. Find the maximum sum of happiness for him. Constraints * 1 ≤ N ≤ 500,000 * 0 ≤ P ≤ 500,000 * | Q | ≤ 500,000 * | Ci | ≤ 500,000 Input The input is given in the following format as N + 1 line. N P Q C1 C2 :: CN * The first line is given three integers N, P, and Q, which are the number of days, the constant for calculating the happiness of self-catering, and the initial value of self-catering power. * From the 2nd line to the N + 1st line, one integer is given, and the i + 1st line gives the happiness obtained when going to the cafeteria on the i day. Output Output the maximum value of happiness that can be taken in one line. Examples Input 1 1 1 2 Output 2 Input 3 2 1 3 3 3 Output 12 Input 3 1 -1 2 -3 2 Output 2 Input 3 1 -10 -10 -10 -10 Output -27 Submitted Solution: ``` #!usr/bin/env python3 from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n,p,q = LI() c = IR(n) cp = [c[i]+p*i for i in range(n)] cp.sort(reverse=True) ans = 0 s = 0 for i in range(n): s += cp[i] ns = s+p*(i+1)*(i+2-(q+2*n)) if ans < ns: ans = ns print(ans+p*(n*q+(n*(n-1))//2)) return #Solve if __name__ == "__main__": solve() ``` Yes
7,500
Provide a correct Python 3 solution for this coding contest problem. B: Parentheses Number problem Define the correct parenthesis string as follows: * The empty string is the correct parenthesis string * For the correct parenthesis string S, `(` S `)` is the correct parenthesis string * For correct parentheses S, T ST is the correct parentheses Here, the permutations are associated with the correct parentheses according to the following rules. * When the i-th closing brace corresponds to the j-th opening brace, the i-th value in the sequence is j. Given a permutation of length n, P = (p_1, p_2, $ \ ldots $, p_n), restore the corresponding parenthesis string. However, if the parenthesis string corresponding to the permutation does not exist, output `: (`. Input format n p_1 p_2 $ \ ldots $ p_n The number of permutations n is given in the first row. Permutations p_1, p_2, $ \ ldots $, p_i, $ \ ldots $, p_n are given in the second row, separated by blanks. Constraint * 1 \ leq n \ leq 10 ^ 5 * 1 \ leq p_i \ leq n * All inputs are integers. * P = (p_1, p_2, $ \ ldots $, p_n) is a permutation. Output format Output the parenthesized string corresponding to the permutation. Print `: (` if such a parenthesis string does not exist. Input example 1 2 twenty one Output example 1 (()) Input example 2 Ten 1 2 3 4 5 6 7 8 9 10 Output example 2 () () () () () () () () () () Input example 3 3 3 1 2 Output example 3 :( Example Input 2 2 1 Output (()) "Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) cnt = 0 ans = '' st = list() for x in a: while cnt < x: ans += '(' cnt += 1 st.append(cnt) if st[-1] == x: st.pop() ans += ')' else: ans = ':(' break print(ans) ```
7,501
Provide a correct Python 3 solution for this coding contest problem. B: Parentheses Number problem Define the correct parenthesis string as follows: * The empty string is the correct parenthesis string * For the correct parenthesis string S, `(` S `)` is the correct parenthesis string * For correct parentheses S, T ST is the correct parentheses Here, the permutations are associated with the correct parentheses according to the following rules. * When the i-th closing brace corresponds to the j-th opening brace, the i-th value in the sequence is j. Given a permutation of length n, P = (p_1, p_2, $ \ ldots $, p_n), restore the corresponding parenthesis string. However, if the parenthesis string corresponding to the permutation does not exist, output `: (`. Input format n p_1 p_2 $ \ ldots $ p_n The number of permutations n is given in the first row. Permutations p_1, p_2, $ \ ldots $, p_i, $ \ ldots $, p_n are given in the second row, separated by blanks. Constraint * 1 \ leq n \ leq 10 ^ 5 * 1 \ leq p_i \ leq n * All inputs are integers. * P = (p_1, p_2, $ \ ldots $, p_n) is a permutation. Output format Output the parenthesized string corresponding to the permutation. Print `: (` if such a parenthesis string does not exist. Input example 1 2 twenty one Output example 1 (()) Input example 2 Ten 1 2 3 4 5 6 7 8 9 10 Output example 2 () () () () () () () () () () Input example 3 3 3 1 2 Output example 3 :( Example Input 2 2 1 Output (()) "Correct Solution: ``` from collections import deque import sys sys.setrecursionlimit(10**7) N = int(input()) P = list(map(int,input().split())) Q = [-1]*N for i,p in enumerate(P): Q[p-1] = i S = '' a = 1 def rec(l,r): global S,a if l==r: return while l < r: ai = Q[a-1] if ai >= r: print(':(') exit() a += 1 S += '(' rec(l,ai) S += ')' l = ai + 1 rec(0,N) print(S) ```
7,502
Provide a correct Python 3 solution for this coding contest problem. Problem Define a function $ f $ that starts with $ 1 $ and takes a sequence of finite lengths as an argument as follows. $ \ displaystyle f (\\ {a_1, a_2, \ ldots, a_n \\}) = \ sum_ {i = 1} ^ n {a_i} ^ i $ Given a sequence of length $ N $, $ X = \\ {x_1, x_2, \ ldots, x_N \\} $, $ f (X) for all subsequences $ X'$ except empty columns. ') Find $ and output the sum of them divided by $ 998244353 $. However, the subsequence subsequences shall be renumbered starting from $ 1 $ while maintaining the relative order in the original sequence. Also, even if two subsequences are the same as columns, if they are taken out at different positions, they shall be counted separately. Constraints The input satisfies the following conditions. * $ 1 \ leq N \ leq 10 ^ 6 $ * $ 1 \ leq x_i \ leq 10 ^ 6 $ * All inputs are integers Input The input is given in the following format. $ N $ $ x_1 $ $ \ ldots $ $ x_N $ The first line is given the length $ N $. In the second row, the elements of the sequence $ X $ are given, separated by blanks. Output Find $ f (X') $ for all subsequences $ X'$ except empty columns, and divide the sum by $ 998244353 $ to output the remainder. Examples Input 3 1 2 3 Output 64 Input 5 100 200 300 400 500 Output 935740429 "Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 998244353 def solve(): n = I() x = LI() ans = 0 for i in range(n): xi = x[i] ans += pow(2,n-i-1,mod)*xi*pow(xi+1,i,mod) if ans >= mod: ans %= mod print(ans) return if __name__ == "__main__": solve() ```
7,503
Provide a correct Python 3 solution for this coding contest problem. Problem Define a function $ f $ that starts with $ 1 $ and takes a sequence of finite lengths as an argument as follows. $ \ displaystyle f (\\ {a_1, a_2, \ ldots, a_n \\}) = \ sum_ {i = 1} ^ n {a_i} ^ i $ Given a sequence of length $ N $, $ X = \\ {x_1, x_2, \ ldots, x_N \\} $, $ f (X) for all subsequences $ X'$ except empty columns. ') Find $ and output the sum of them divided by $ 998244353 $. However, the subsequence subsequences shall be renumbered starting from $ 1 $ while maintaining the relative order in the original sequence. Also, even if two subsequences are the same as columns, if they are taken out at different positions, they shall be counted separately. Constraints The input satisfies the following conditions. * $ 1 \ leq N \ leq 10 ^ 6 $ * $ 1 \ leq x_i \ leq 10 ^ 6 $ * All inputs are integers Input The input is given in the following format. $ N $ $ x_1 $ $ \ ldots $ $ x_N $ The first line is given the length $ N $. In the second row, the elements of the sequence $ X $ are given, separated by blanks. Output Find $ f (X') $ for all subsequences $ X'$ except empty columns, and divide the sum by $ 998244353 $ to output the remainder. Examples Input 3 1 2 3 Output 64 Input 5 100 200 300 400 500 Output 935740429 "Correct Solution: ``` N = int(input()) X = list(map(int, input().split())) ans = 0 mod = 998244353 for i, x in enumerate(X): ans = (ans * 2 + x * pow(x+1, i, mod)) % mod print(ans) ```
7,504
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 "Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from itertools import permutations import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n = I() a = IR(n) dp = [float("inf")]*n for i in a: j = bisect.bisect_left(dp,i) dp[j] = i print(n-dp.count(float("inf"))) return #Solve if __name__ == "__main__": solve() ```
7,505
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 "Correct Solution: ``` import sys from bisect import bisect_left def solve(): n = int(input()) A = [int(input()) for i in range(n)] inf = 10**9 + 1 dp = [inf] * n for a in A: j = bisect_left(dp, a) dp[j] = a for i, v in enumerate(dp): if v == inf: print(i) return print(n) def debug(x, table): for name, val in table.items(): if x is val: print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr) return None if __name__ == '__main__': solve() ```
7,506
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 "Correct Solution: ``` import sys from bisect import bisect_left read = sys.stdin.read readline = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 8) INF = float('inf') MOD = 10 ** 9 + 7 def main(): N = int(readline()) A = list(int(readline()) for _ in range(N)) LIS = [INF]*(N+1) for a in A: i = bisect_left(LIS,a) LIS[i] = a print(LIS.index(INF)) if __name__ == '__main__': main() ```
7,507
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 "Correct Solution: ``` #!/usr/bin/env python3 # n,m = map(int,sys.stdin.readline().split()) # a = list(map(int,sys.stdin.readline().split())) # a = [sys.stdin.readline() for _ in range(n)] # s = sys.stdin.readline().rstrip() # n = int(sys.stdin.readline()) INF = float("inf") import sys,bisect sys.setrecursionlimit(15000) n = int(sys.stdin.readline()) a = [int(sys.stdin.readline()) for _ in range(n)] # naive O(n**2) # dp = [0]*n # for i in range(n): # m = 0 # for j in range(0,i): # if a[j] < a[i] and m <= dp[j]: # m = dp[j] # dp[i] = m+1 # print(max(dp)) import bisect dp = [float("inf")]*n dp[0] = a[0] l = 0 for i in range(1,n): if dp[l] < a[i]: l += 1 dp[l] = a[i] else: j = bisect.bisect_left(dp,a[i]) dp[j] = a[i] print(bisect.bisect_left(dp,float("inf"))) ```
7,508
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 "Correct Solution: ``` import bisect n = int(input()) L = [] for i in range(n): L.append(int(input())) dp = [float('inf')]*n for i in range(n): k = bisect.bisect_left(dp,L[i]) dp[k] = L[i] ans = 0 for i in range(n): if dp[i] != float('inf'): ans += 1 else: break print(ans) ```
7,509
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 "Correct Solution: ``` # -*- coding: utf-8 -*- import bisect if __name__ == '__main__': n = int(input()) a = [int(input()) for _ in range(n)] L = [None] * n L[0] = a[0] length = 1 for i in range(1, n): if L[length - 1] < a[i]: L[length] = a[i] length += 1 else: indx = bisect.bisect_left(L[:length], a[i]) L[indx] = a[i] print(length) ```
7,510
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 "Correct Solution: ``` import bisect n = int(input()) A = [int(input()) for j in range(n)] dp = A[:1] for a_i in A[1:]: if dp[-1] < a_i: dp.append(a_i) else: dp[bisect.bisect_left(dp, a_i)] = a_i print(len(dp)) ```
7,511
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 "Correct Solution: ``` import sys, bisect def solve(): A = list(map(int, sys.stdin.readlines())) n = A[0] A = A[1:] L = A[:1] for a_i in A[1:]: if a_i > L[-1]: L.append(a_i) else: j = bisect.bisect_left(L, a_i) L[j] = a_i print(len(L)) solve() ```
7,512
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` from bisect import bisect_left N = int(input()) A = [int(input()) for _ in range(N)] LIS = [A[0]] # i番目の成分 = 長さ i+1 の増加部分列の最後の要素の値 for a in A[1:]: if a > LIS[-1]: # 最大の長さの増加部分列の最後の要素より大きい値なら、これを用いて1つ長い増加部分列を作ることができる。 # 更新前の最長増加部分列 = [..., L[-1]]、更新後の最長増加部分列 = [..., L[-1], a] LIS.append(a) else: LIS[bisect_left(LIS, a)] = a # 最長ではない増加部分列の最後の要素を小さい値に更新。これにより、a > LIS[-1] を満たす可能性が増える。 print(len(LIS)) ``` Yes
7,513
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` import bisect def LIS(): L.append(a[0]) length[0] = 1 for i in range(n-1): if L[-1]<a[i+1]: L.append(a[i+1]) length[i+1] = length[i]+1 else: L[bisect.bisect_left(L,a[i+1])] = a[i+1] length[i+1] = length[i] return length[-1] n = int(input()) a = [int(input()) for _ in range(n)] L = [] length = [None]*n print(LIS()) ``` Yes
7,514
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` #最長増加部分列問題 (LIS) import bisect N = int(input()) seq = [0] * N for i in range(N): seq[i] = int(input()) LIS = [seq[0]] #print(LIS) for i in range(len(seq)): if seq[i] > LIS[-1]: LIS.append(seq[i]) else: LIS[bisect.bisect_left(LIS, seq[i])] = seq[i] #print(LIS) #print(LIS) print(len(LIS)) ``` Yes
7,515
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` from bisect import bisect_left n=int(input()) a=[int(input()) for _ in range(n)] dp=[float('inf') for _ in range(n)] l=[] l.append(a[0]) dp[0]=a[0] for i in a[1:]: if l[-1]<i: l.append(i) dp[len(l)-1]=i else: x=bisect_left(l,i) dp[x]=i l[x]=i print(len(l)) ``` Yes
7,516
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` n = int(input()) num = [int(input()) for i in range(n)] gra = [1]*n for i in range(1,n): Max = 1 for j in range(i): if num[j]<num[i] and gra[j]>=Max: Max = gra[j] gra[i] = Max+1 print(max(gra)) ``` No
7,517
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` n = int(input()) in_list = [] for i in range(n): in_list.append(list(map(int, input().split()))) matrices = [["0" for _ in range(n)] for _ in range(n)] for i in range(n): inp = in_list[i] u = inp[0] k = inp[1] for j in range(k): m = inp[j+2] matrices[i][m-1] = "1" for i in range(n): print(" ".join(matrices[i])) ``` No
7,518
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` # fp = open("DPL_1_D-in11.txt") # inlist = list(map(int, fp.read().splitlines())) n = int(input()) A = [] dp = [0 for _ in range(n)] dp[0] = 1 for i in range(n): num = int(input()) A.append(num) dpmax = 0 for j in range(i): if A[j] < num and dp[j] > dpmax: dpmax = dp[j] dp[i] = max(dpmax + 1, 1) print(dp[-1]) ``` No
7,519
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` n = int(input()) num = [int(input()) for i in range(n)] gra = [1]*n Max = 1 for i in range(1,n): for j in range(i): if num[j]<num[i] and gra[j]>=Max: Max = gra[j] gra[i] = Max+1 Max += 1 print(Max) ``` No
7,520
Provide a correct Python 3 solution for this coding contest problem. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 "Correct Solution: ``` import bisect q = int(input()) M = {} sortedList = [] for value in range(q): query, *inp = input().split() key = inp[0] # insert if query == "0": x = int(inp[1]) if key not in M: bisect.insort_left(sortedList, key) M[key] = [] M[key].append(x) # get elif query == "1": if key in M: if M[key]: for value in M[key]: print(value) # delete elif query == "2": if key in M: M[key] = [] # dump else: L = inp[0] R = inp[1] index_left = bisect.bisect_left(sortedList, L) index_right = bisect.bisect_right(sortedList, R) for value in range(index_left, index_right): keyAns = sortedList[value] if M[keyAns]: for valueAns in M[keyAns]: print(keyAns, valueAns) ```
7,521
Provide a correct Python 3 solution for this coding contest problem. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 "Correct Solution: ``` from bisect import bisect_left, bisect_right, insort_left from typing import Dict, List if __name__ == "__main__": num_query = int(input()) d: Dict[str, List[int]] = {} keys: List[str] = [] for _ in range(num_query): op, *v = input().split() if ("0" == op): if v[0] not in d: insort_left(keys, v[0]) d[v[0]] = [] d[v[0]].append(int(v[1])) elif ("1" == op): if v[0] in d: for elem in d[v[0]]: print(elem) elif ("2" == op): if v[0] in d: d[v[0]] = [] else: left_key = bisect_left(keys, v[0]) right_key = bisect_right(keys, v[1], left_key) for k in range(left_key, right_key): for elem in d[keys[k]]: print(keys[k], elem) ```
7,522
Provide a correct Python 3 solution for this coding contest problem. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 "Correct Solution: ``` from bisect import bisect_left,bisect_right,insort_left dict = {} keytbl =[] q = int(input()) for i in range(q): a = list(input().split()) ki = a[1] if a[0] == "0": if ki not in dict: dict[ki] =[] insort_left(keytbl,ki) dict[ki].append(int(a[2])) elif a[0] == "1": if ki in dict and dict[ki] != []:print(*dict[ki],sep="\n") elif a[0] == "2": if ki in dict: dict[ki] = [] else: L =bisect_left(keytbl,a[1]) R = bisect_right(keytbl,a[2],L) for j in range(L,R): for k in dict[keytbl[j]]: print(keytbl[j],k) ```
7,523
Provide a correct Python 3 solution for this coding contest problem. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 "Correct Solution: ``` # AOJ ITP2_8_D: Multi-Map # Python3 2018.6.24 bal4u from bisect import bisect_left, bisect_right, insort_left dict = {} keytbl = [] q = int(input()) for i in range(q): a = list(input().split()) ki = a[1] if a[0] == '0': if ki not in dict: dict[ki] = [] insort_left(keytbl, ki) dict[ki].append(int(a[2])) elif a[0] == '1': if ki in dict and dict[ki] != []: print(*dict[ki], sep='\n') elif a[0] == '2': if ki in dict: dict[ki] = [] else: L = bisect_left (keytbl, a[1]) R = bisect_right(keytbl, a[2], L) for j in range(L, R): for k in dict[keytbl[j]]: print(keytbl[j], k) ```
7,524
Provide a correct Python 3 solution for this coding contest problem. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 "Correct Solution: ``` # -*- coding: utf-8 -*- """ Dictionary - Multi-Map http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_8_D&lang=jp """ from bisect import insort, bisect_right, bisect_left class Multi_map: def __init__(self): self.mm = dict() self.lr = [] def insert(self, x, y): if x in self.mm: self.mm[x].append(y) else: self.mm[x] = [y] insort(self.lr, x) def get(self, x): if x in self.mm and self.mm[x] != []: print(*self.mm[x], sep='\n') def delete(self, x): if x in self.mm: self.mm[x] = [] def dump(self, l, r): lb = bisect_left(self.lr, l) ub = bisect_right(self.lr, r) for i in range(lb, ub): k = self.lr[i] for v in self.mm[k]: print(f'{k} {v}') mm = Multi_map() for _ in range(int(input())): op, x, y = (input() + ' 1').split()[:3] if op == '0': mm.insert(x, int(y)) elif op == '1': mm.get(x) elif op == '2': mm.delete(x) else: mm.dump(x, y) ```
7,525
Provide a correct Python 3 solution for this coding contest problem. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 "Correct Solution: ``` import bisect from collections import defaultdict def main(): q = int(input()) d = defaultdict(list) inserted_flag = False sorted_keys = None for _ in range(q): para = input().split() if para[0] == "0": d[para[1]].append(int(para[2])) inserted_flag = True elif para[0] == "1": if para[1] in d.keys(): for i in range(len(d[para[1]])): print(d[para[1]][i]) elif para[0] == "2": if para[1] in d.keys(): del d[para[1]] inserted_flag = True elif para[0] == "3": if sorted_keys is None: sorted_keys = sorted(d.keys()) if inserted_flag: sorted_keys = sorted(d.keys()) inserted_flag = False l = bisect.bisect_left(sorted_keys, para[1]) r = bisect.bisect_right(sorted_keys, para[2]) for i in range(l, r): for j in range(len(d[sorted_keys[i]])): print("{} {}".format(sorted_keys[i], d[sorted_keys[i]][j])) main() ```
7,526
Provide a correct Python 3 solution for this coding contest problem. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 "Correct Solution: ``` # AOJ ITP2_8_D: Multi-Map # Python3 2018.6.24 bal4u import bisect from bisect import bisect_left, bisect_right, insort_left dict = {} keytbl = [] q = int(input()) for i in range(q): a = list(input().split()) ki = a[1] if a[0] == '0': if ki not in dict: dict[ki] = [] insort_left(keytbl, ki) dict[ki].append(int(a[2])) elif a[0] == '1' and ki in dict and dict[ki] != []: print(*dict[ki], sep='\n') elif a[0] == '2' and ki in dict: dict[ki] = [] elif a[0] == '3': L = bisect_left (keytbl, a[1]) R = bisect_right(keytbl, a[2]) for j in range(L, R): for k in dict[keytbl[j]]: print(keytbl[j], k) ```
7,527
Provide a correct Python 3 solution for this coding contest problem. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 "Correct Solution: ``` import sys import bisect from collections import defaultdict n = int(input()) arr = [] d = defaultdict(list) lines = sys.stdin.readlines() ans = [None] * n for i in range(n): q, *arg = lines[i].split() key = arg[0] l_idx = bisect.bisect_left(arr, arg[0]) r_idx = bisect.bisect_right(arr, arg[0]) if q == '0': # insert if l_idx == len(arr) or arr[l_idx] != key: arr.insert(l_idx, key) d[key].append(arg[1]) elif q == '1': # get if l_idx != r_idx: ans[i] = '\n'.join(d[key]) elif q == '2': # delete arr[l_idx:r_idx] = [] if l_idx != r_idx: del d[key] elif q == '3': # dump L R r_idx = bisect.bisect_right(arr, arg[1]) if l_idx != r_idx: keys = arr[l_idx:r_idx] ans[i] = '\n'.join(['\n'.join([f'{k} {x}' for x in d[k]]) for k in keys]) [print(x) for x in ans if x is not None] ```
7,528
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 Submitted Solution: ``` from bisect import * n = int(input()) dic = {} dic2 = {} box = [] for i in range(n): a, b, *c = input().split() if a == '0': insort_left(box, b) if b in dic: temp = dic[b] dic[b] = temp + [int(c[0])] else: dic[b] = [int(c[0])] elif a == '1': if b in dic: pri = [] for i in dic[b]: pri.append(str(i)) print("\n".join(pri)) elif a == '2': if b in dic: del dic[b] else: L = bisect_left(box,b) R = bisect_right(box, c[0]) while L<R: if box[L] in dic: for i in dic[box[L]]: print(box[L], i) L = bisect_right(box, box[L]) ``` Yes
7,529
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 Submitted Solution: ``` import bisect M={} D=[] def insert(M,D,key,x): if key not in M: M[key]=[] bisect.insort_left(D,key) M[key].append(x) return M,D def get(M,D,key): if key in M and M[key]!=[]: print('\n'.join(map(str,M[key]))) def erase(M,D,key): if key in M: M[key]=[] return M,D def dump(M,D,L,R): s=bisect.bisect_left(D,L) e=bisect.bisect_right(D,R) if e-s>0: #ループを使わずにできる方法を考える for i in range(s,e): for j in M[D[i]]: print(D[i],j) q=int(input()) for i in range(q): query=list(map(str,input().split())) query[0]=int(query[0]) if query[0]==0: M,D=insert(M,D,query[1],int(query[2])) elif query[0]==1: get(M,D,query[1]) elif query[0]==2: M,D=erase(M,D,query[1]) else: dump(M,D,query[1],query[2]) ``` Yes
7,530
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 Submitted Solution: ``` from bisect import bisect,bisect_left,insort dic = {} l = [] for i in range(int(input())): order = list(input().split()) if order[0] == "0": if not order[1] in dic: dic[order[1]] = [] insort(l,order[1]) dic[order[1]].append(int(order[2])) elif order[0] == "1": if order[1] in dic: for i in dic[order[1]]: print(i) elif order[0] == "2": if order[1] in dic: dic[order[1]] = [] elif order[0] == "3": L = bisect_left(l,order[1]) R = bisect(l,order[2]) for i in range(L,R): for j in dic[l[i]]: print(l[i],j) ``` Yes
7,531
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with the specified $key$. * delete($key$): Delete all elements with the specified $key$. * dump($L$, $R$): Print all elements formed by a pair of the key and the value such that the key is greater than or equal to $L$ and less than or equal to $R$ in lexicographic order. Constraints * $1 \leq q \leq 200,000$ * $1 \leq x \leq 1,000,000,000$ * $1 \leq $ length of $key$ $ \leq 20$ * $key$ consists of lower-case letters * $L \leq R$ in lexicographic order * The total number of elements printed by get operations does not exceed $500,000$ * The total number of elements printed by dump operations does not exceed $500,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $key$ $x$ or 1 $key$ or 2 $key$ or 3 $L$ $R$ where the first digits 0, 1, 2 and 3 represent insert, get, delete and dump operations. Output For each get operation, print the corresponding values in the order of insertions. For each dump operation, print the corresponding elements formed by a pair of the key and the value. For the dump operation, print the elements in ascending order of the keys, in case of a tie, in the order of insertions. Example Input 10 0 blue 6 0 red 1 0 blue 4 0 white 5 1 red 1 blue 2 red 1 black 1 red 3 w z Output 1 6 4 white 5 Submitted Solution: ``` # Solved by QBnewb # Discretization # off-line q = int(input()) s = [] rs = [] # for discretization # download the input for i in range(q): s.append(input().split()) if s[i][0] == '3': rs.append(s[i][1]) rs.append(s[i][2]) else: rs.append(s[i][1]) rs = sorted(list(set(rs))) index = {rs[i]:i for i in range(len(rs))} # discretization d = [[] for i in range(len(rs))] # discrete dictionary for i in range(q): op, key = int(s[i][0]), s[i][1] idx = index[key] if op == 0: d[idx].append(s[i][2]) elif op == 1: for item in d[idx]: print(item) elif op == 2: d[idx].clear() else: l = idx r = index[s[i][2]] for j in range(l, r+1): for item in d[j]: print(rs[j], item) ``` Yes
7,532
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Tags: dp, greedy, number theory Correct Solution: ``` s = input() n = len(s) fin = [-1]*3 fin[0] = 0 r = 0 z = [0]*(n + 1) for i in range(1, n + 1): r = (r + int(s[i - 1])) % 3 z[i] = z[i - 1] if fin[r] != -1: z[i] = max(z[i], z[fin[r]] + 1) fin[r] = i print(z[n]) ```
7,533
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Tags: dp, greedy, number theory Correct Solution: ``` n=input() ls='' t=0 for i in range(len(n)): if int(n[i])%3==0: ls='' t+=1 else: ls+=n[i] for j in range(0,len(ls)): if int(ls[j:])%3==0: t+=1 ls='' break print(t) ''' //////////////// ////// /////// // /////// // // // //// // /// /// /// /// // /// /// //// // //// //// /// /// /// /// // ///////// //// /////// //// ///// /// /// /// /// // /// /// //// // // ////////////// /////////// /////////// ////// /// /// // // // // ''' ```
7,534
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Tags: dp, greedy, number theory Correct Solution: ``` MOD = 1000000007 MOD2 = 998244353 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) let = '@abcdefghijklmnopqrstuvwxyz' s=si() n=len(s) l=[0]*n rem=[-1]*3 rem[0],x=0,0 for i in range(n): x=(x+int(s[i]))%3 if rem[x]!=-1: l[i]=max(l[i-1],l[rem[x]]+1) else: l[i]=l[i-1] rem[x]=i print(l[n-1]) ```
7,535
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Tags: dp, greedy, number theory Correct Solution: ``` s = input() n = len(s) ans = 0 c = 0 l = [] for i in range(n): a = int(s[i])%3 if a==0: ans+=1 c = 0 l = [] else: if c==0: l.append(int(s[i])) c+=1 elif c==1: if (a+l[0])%3==0: ans+=1 c = 0 l = [] else: c+=1 else: ans+=1 c=0 l = [] print(ans) ```
7,536
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Tags: dp, greedy, number theory Correct Solution: ``` li=[int(x)%3 for x in input()] start=0 end=0 ans=0 while end<len(li): if li[end]==0: ans+=1 end+=1 start=end else: count=1 add=li[end] while end-count>=start: add+=li[end-count] if add%3==0: ans+=1 # print(str(start)+' '+str(end)) start=end+1 break else: count+=1 end+=1 print(ans) ```
7,537
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Tags: dp, greedy, number theory Correct Solution: ``` m=1 r=s=0 for c in input(): s+=int(c);b=1<<s%3 if m&b:m=0;r+=1 m|=b print(r) ```
7,538
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Tags: dp, greedy, number theory Correct Solution: ``` s=input() lastCut=0 cpt=0 for i in range(1,len(s)+1): for k in range(i-lastCut): #print(lastCut+k,i,"chaine: ",s[lastCut+k:i],"cpt : ",cpt) #print(i!=lastCut,int(s[lastCut+k:i])%3==0,int(s[lastCut+k])==0,len(s[lastCut+k:i])==1) if(i!=lastCut and int(s[lastCut+k:i])%3==0 and not(int(s[lastCut+k])==0 and len(s[lastCut+k:i])!=1)): lastCut=i cpt+=1 print(cpt) ```
7,539
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Tags: dp, greedy, number theory Correct Solution: ``` from sys import stdin, stdout nmbr = lambda: int(input()) lst = lambda: list(map(int, input().split())) from functools import lru_cache @lru_cache(None) def fn(pos, rem): if pos==n:return 0 cur_rem=int(s[pos])%3 ans=fn(pos+1, 0) if (rem+cur_rem)%3==0 or cur_rem==0:ans=max(1+fn(pos+1, 0), ans) ans=max(ans, fn(pos+1, (rem+cur_rem)%3)) return ans for _ in range(1):#nmbr()): # n=nmbr() # n,k=lst() s=input() n=len(s) # print(fn(0,0)) NI=float('-inf') dp=[[NI for i in range(3)] for i in range(n+1)] dp[0][0]=0 for i in range(n): for rem in range(3): cur_rem=int(s[i])%3 dp[i+1][0]=max(dp[i+1][0], dp[i][rem]) if (rem + cur_rem) % 3 == 0 or cur_rem == 0: dp[i+1][0] = max(1 + dp[i][rem], dp[i+1][0]) dp[i+1][(rem+cur_rem)%3]=max(dp[i+1][(rem+cur_rem)%3], dp[i][rem]) # print(*dp, sep='\n') print(dp[n][0]) ```
7,540
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Submitted Solution: ``` S = input() n, c = 0, len(S) for i in reversed(range(len(S))): for k in range(i, c): if int(S[i:k+1])%3 == 0: n += 1 c = i break print(n) ``` Yes
7,541
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Submitted Solution: ``` s = input() f = [0]*len(s) cnt = 0 for i in range(len(s)): a = int(s[i]) if a % 3 == 0: cnt += 1 f[i] = 1 for i in range(len(s)-1): if f[i] == 0 and f[i+1] == 0: a = int(s[i])*10+int(s[i+1]) if a % 3 == 0: cnt += 1 f[i] = f[i+1] = 1 elif i < len(s)-2 and f[i] == 0 and f[i+1] == 0 and f[i+2] == 0: a = int(s[i]) * 100 + int(s[i + 1]) * 10 + int(s[i + 2]) if a % 3 == 0: cnt += 1 f[i] = f[i + 1] = f[i + 2] = 1 for i in range(len(s) - 2): if f[i] == 0 and f[i+1] == 0 and f[i+2] == 0: a = int(s[i])*100 + int(s[i+1])*10 + int(s[i+2]) if a % 3 == 0: cnt += 1 f[i] = f[i+1] = f[i+2] = 1 for i in range(len(s) - 3): if f[i] == 0 and f[i+1] == 0 and f[i+2] == 0 and f[i+3] == 0: a = int(s[i])*1000 + int(s[i+1])*100 + int(s[i+2])*10+ int(s[i+3]) if a % 3 == 0: cnt += 1 print(cnt) ``` Yes
7,542
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Submitted Solution: ``` a = input() count = 0 lenofsnake = 0 snaketotal = 0 for i in range(len(a)): if int(a[i])%3 == 0: count = count+1 lenofsnake = 0 snaketotal = 0 elif lenofsnake == 2 or (snaketotal+int(a[i]))%3 == 0: count = count + 1 lenofsnake = 0 snaketotal = 0 else: lenofsnake = lenofsnake + 1 snaketotal = snaketotal + int(a[i]) print(count) ``` Yes
7,543
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Submitted Solution: ``` s = input(); l = len(s); c = 0; k = 0; z = 0 for i in range(l): c += int(s[i]) z += 1 if c%3 == 0 or int(s[i])%3 == 0 or z%3 == 0: c = 0 z = 0 k += 1 print (k) ``` Yes
7,544
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Submitted Solution: ``` s = input() sum = 0 ans = 0 for i in s: sum += int(i) if sum % 3 == 0 or i in '0369': ans += 1 sum = 0 print(ans) ``` No
7,545
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Submitted Solution: ``` S = list(map(int, input())) SHORTEST = [0 for _ in S] for i in range(len(S)): SUM = 0 for j in range(i, len(S)): SUM += S[j] if S[i] == 0 or SUM % 3 == 0: SHORTEST[i] = j-i+1 break I = 0 ANSWER = 0 while I < len(S): N = SHORTEST[I] if N == 0: I += 1 else: ANSWER += 1 I += N print(ANSWER) ``` No
7,546
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Submitted Solution: ``` def main(): n = input() dp = [] s = '' if len(n) > 50: print(112135) else: if int(n[0]) % 3 == 0: dp.append(1) else: dp.append(0) for i in range(1, len(n)): dp.append(0) for j in range(1, i + 1): if n[i - j + 1:i - j + 2] == '0' and int(n[i - j + 1:i + 1]) != 0: continue else: if int(n[i - j + 1:i + 1]) % 3 == 0: k = 1 else: k = 0 dp[i] = max(dp[i], dp[i - j] + k) if int(n) % 3 == 0 and dp[-1] == 0: print(1) else: print(dp[-1]) main() # subscribe to Matskevich Play ``` No
7,547
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp likes numbers that are divisible by 3. He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3. For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 3 that Polycarp can obtain? Input The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅10^5, inclusive. The first (leftmost) digit is not equal to 0. Output Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s. Examples Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 Note In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. Submitted Solution: ``` print(''.join([str(int(i) % 3) for i in input()]).replace('12', '0').replace('21', '0').replace('111', '0').count('0')) ``` No
7,548
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Tags: dp, greedy, math Correct Solution: ``` import sys def input(): return sys.stdin.readline()[:-1] # def input(): return sys.stdin.buffer.readline()[:-1] n = int(input()) a = [int(x) for x in input().split()] ans = 1 cur = 1 for i in range(1, n): if a[i - 1] * 2 >= a[i]: cur += 1 else: cur = 1 ans = max(ans, cur) print(ans) ```
7,549
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Tags: dp, greedy, math Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) p=0 m=1 for i in a: if i>p*2: c=1 else: c+=1 m=max(c,m) p=i m=max(m,c) print(m) ```
7,550
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Tags: dp, greedy, math Correct Solution: ``` #iamanshup n = int(input()) l = list(map(int, input().split())) ans = 1 m = 1 for i in range(n-1): if 2*l[i]>=l[i+1]: m+=1 else: m = 1 ans = max(ans, m) print(ans) ```
7,551
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Tags: dp, greedy, math Correct Solution: ``` from collections import deque from sys import stdin lines = deque(line.strip() for line in stdin.readlines()) def nextline(): return lines.popleft() def types(cast, sep=None): return tuple(cast(x) for x in strs(sep=sep)) def ints(sep=None): return types(int, sep=sep) def strs(sep=None): return tuple(nextline()) if sep == '' else tuple(nextline().split(sep=sep)) def main(): # lines will now contain all of the input's lines in a list n = int(nextline()) nums = ints() largest_group = 0 last_num = nums[0] curr_group = 1 for i in range(1, n): if nums[i] > 2 * last_num: largest_group = max(largest_group, curr_group) curr_group = 1 else: curr_group += 1 last_num = nums[i] print(max(largest_group, curr_group)) if __name__ == '__main__': main() ```
7,552
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Tags: dp, greedy, math Correct Solution: ``` n= int(input()) l=list(map(int,input().split())) if(n==1): print(1) else: t=[] ans=1 for i in range(n-1): if(l[i]*2>=l[i+1]): ans+=1 else: t.append(ans) if(i==n-2): t.append(1) ans=1 if(i==n-2 and l[i]*2>=l[i+1]): t.append(ans) print(max(t)) ```
7,553
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Tags: dp, greedy, math Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) x = [0] for i in range(1, n): if a[i - 1] * 2 < a[i]: x.append(i) x.append(n) ans = 0 m = len(x) for i in range(1, m): ans = max(ans, x[i] - x[i - 1]) print(ans) ```
7,554
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Tags: dp, greedy, math Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) a.append(a[n - 1] * 3) max_ans = 1 ans = 1 for i in range(n): if a[i] * 2 < a[i + 1]: if ans > max_ans: max_ans = ans ans = 1 else: ans += 1 print(max_ans) ```
7,555
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Tags: dp, greedy, math Correct Solution: ``` amount = int(input()) diffs = [int(i) for i in input().split()] ret = 1 m = 1 for i in range(1, amount): if diffs[i] <= diffs[i - 1] * 2: m += 1 ret = max(ret, m) else: m = 1 print(ret) ```
7,556
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Submitted Solution: ``` a=int(input()) b=list(map(int, input().split())) b.append(b[a-1]+1) #print(b) m=1 t=1 for i in range(1, a): #print(b[i-1],end=" ") if (b[i-1]*2>=b[i]): t+=1 #print("!",end="") else: t=1 #print("\n") if t>m: m=t; print(m) ``` Yes
7,557
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Submitted Solution: ``` n = int(input()) l = list(map(int, input().split())) ans, cur = 0, 1 for i in range(n): if i : if l[i] <= (2 * l[i-1]): cur += 1 else: cur = 1 ans = max(ans, cur) print(ans) ``` Yes
7,558
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Submitted Solution: ``` length=int(input()) a=[] a=str(input()).split() maxi=1 count=1 a = [*map(int, a)] for i in range(length-1): if(a[i+1]<=a[i]*2): count+=1 else: maxi=max(count, maxi) count=1 maxi=max(count, maxi) print(maxi) ``` Yes
7,559
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Submitted Solution: ``` n = int(input()) l = list(map(int,input().split())) max=1 i=0 while(i<n-1): count=1 while(i<n-1 and 2*l[i]>=l[i+1]): count+=1 i+=1 if max<count: max=count i+=1 print(max) ``` Yes
7,560
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Submitted Solution: ``` n = int(input()) sn = input() an = sn.split() l = list() count = 0 if len(an)!=n: print("error") for i in range(1,len(an)): n1 = 2*int(an[i-1]) if (int(an[i]) <= n1) : l.append(i-1) continue else: continue k = list() for j in range(1,len(l)): print(l[j],l[j-1]) if l[j]==(l[j-1]+1): count = count+2 continue else: k.append(count+1) count = 0 continue print(k) print(max(k)) ``` No
7,561
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Submitted Solution: ``` # левый догоняет правый индекс поэтому 0(n) n = int(input()) a = list(map(int,input().split())) L,R,ans=0,0,1 while True: if R==n-1: ans=max(ans,R-L+1) break if a[R]*2>a[R+1]: R+=1 else: ans=max(ans,R-L+1) R+=1 L=R print(ans) ``` No
7,562
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Submitted Solution: ``` k=int(input()) s=input() l=s.split() l=list(map(int,l)) z=1 maxa=0 for i in range(len(l)-1): if((l[i+1])<=l[i]*2): z+=1 else: maxa=max(z,maxa) z=1 print(maxa) ``` No
7,563
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} ≤ a_{i_j} ⋅ 2 should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. Input The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of problems in the problemset. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. Output Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement. Examples Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 Note Description of the first example: there are 10 valid contests consisting of 1 problem, 10 valid contests consisting of 2 problems ([1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]), 5 valid contests consisting of 3 problems ([5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]) and a single valid contest consisting of 4 problems ([5, 6, 7, 10]). In the second example all the valid contests consist of 1 problem. In the third example are two contests consisting of 3 problems: [4, 7, 12] and [100, 150, 199]. Submitted Solution: ``` import bisect n=int(input()) arr=list(map(int,input().split())) d={};ans=0 for i in arr:d[i]=d.get(i,0)+1 #print(d) arr=sorted(arr) for i in range(n): k=2*arr[i] idx=bisect.bisect(arr,k) ans=max(ans,idx-i) print(ans) ``` No
7,564
Provide tags and a correct Python 3 solution for this coding contest problem. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Tags: implementation Correct Solution: ``` # Blackjack def cards(n): diff = n - 10 if diff == 10: return 15 if diff > 11 or diff == 0: return 0 if n < 10: return 0 else: return 4 n = int(input()) print(cards(n)) ```
7,565
Provide tags and a correct Python 3 solution for this coding contest problem. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Tags: implementation Correct Solution: ``` n = int(input()) if 1 <= n-10 <= 9 or n-10 == 11: print("4") elif n-10 == 10: print("15") else: print("0") ```
7,566
Provide tags and a correct Python 3 solution for this coding contest problem. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Tags: implementation Correct Solution: ``` n = int(input()) if n <= 10 or n > 21: print(0) else: if n == 11 or n == 21: print(4) elif n - 10 < 10: print(4) else: print(15) ```
7,567
Provide tags and a correct Python 3 solution for this coding contest problem. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Tags: implementation Correct Solution: ``` n=int(input()) n=n-10 if(n>=1 and n<=9): print(4) elif(n==10): print(15) elif(n==11): print(4) else: print(0) ```
7,568
Provide tags and a correct Python 3 solution for this coding contest problem. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Tags: implementation Correct Solution: ``` n=int(input()) if 11<=n<=19: print(4) elif n==20: print(15) elif n==21: print(4) else: print(0) ```
7,569
Provide tags and a correct Python 3 solution for this coding contest problem. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Tags: implementation Correct Solution: ``` a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 11, 10] * 4 a = a[:-1] def solve(n): print(a.count(n - 10)) n, = map(int, input().split()) solve(n) ```
7,570
Provide tags and a correct Python 3 solution for this coding contest problem. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Tags: implementation Correct Solution: ``` n=int(input()) x=n-10 if(x<=0): print(0) else: if(x>11): print(0) elif(x==10): print(15) else: print(4) ```
7,571
Provide tags and a correct Python 3 solution for this coding contest problem. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Tags: implementation Correct Solution: ``` # import sys # sys.stdin=open("input.in",'r') # sys.stdout=open("out.out",'w') n=int(input())-10 if n<=0: print(0) elif n==10: print(15) elif n<12: print(4) else: print(0) ```
7,572
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Submitted Solution: ``` x = int(input())-10 if 1<=x<=9 or x == 11: print(4) elif x==10 or x==20: print(15) else: print(0) ``` Yes
7,573
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Submitted Solution: ``` """ *** Author--Saket Saumya *** IIITM """ import math from sys import stdin def si(): return str(input()) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(mi()) n=ii() s=n-10 if s==0 or s>=12 or s<0: print('0') if(s==1) or s==2 or s==3 or s==4 or s==5 or s==6 or s==7 or s==8 or s==9: print('4') if(s==10): print('15') if s==11: print('4') ``` Yes
7,574
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Submitted Solution: ``` n = int(input()) if (n >= 0 and n <= 10) or n >= 22: print(0) elif (n >= 11 and n < 20) or n == 21: print(4) elif n == 20: print(15) ``` Yes
7,575
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Submitted Solution: ``` n=int(input()) if (n-10)<=0 or n>=22: print(0) elif 1<=(n-10)<=11 and n!=20: print(4) elif n==20: print(15) ``` Yes
7,576
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Submitted Solution: ``` n=int(input()) if n==10: print('0') elif n==20: print('15') else: print('4') ``` No
7,577
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Submitted Solution: ``` n=int(input()) if(n<=10): print('0') else: t=n-10 if(t==10): print('15') else: print('4') ``` No
7,578
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Submitted Solution: ``` def ans(n): if(n==0 or n>11): return 0 elif(1<=n<10) or (n==11): return 4 else: return 15 n=int(input()) n-=10 print(ans(n)) ``` No
7,579
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≤ n ≤ 25) — the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. Submitted Solution: ``` n = int(input()) if n == 20: print(15) elif 10 < n < 22: print(0) else: print(4) ``` No
7,580
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a matrix of size n × n filled with lowercase English letters. You can change no more than k letters in this matrix. Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the string that is formed by all the letters in the cells the path visits. Thus, the length of each string is 2n - 1. Find the lexicographically smallest string that can be associated with a path after changing letters in at most k cells of the matrix. A string a is lexicographically smaller than a string b, if the first different letter in a and b is smaller in a. Input The first line contains two integers n and k (1 ≤ n ≤ 2000, 0 ≤ k ≤ n^2) — the size of the matrix and the number of letters you can change. Each of the next n lines contains a string of n lowercase English letters denoting one row of the matrix. Output Output the lexicographically smallest string that can be associated with some valid path after changing no more than k letters in the matrix. Examples Input 4 2 abcd bcde bcad bcde Output aaabcde Input 5 3 bwwwz hrhdh sepsp sqfaf ajbvw Output aaaepfafw Input 7 6 ypnxnnp pnxonpm nxanpou xnnpmud nhtdudu npmuduh pmutsnz Output aaaaaaadudsnz Note In the first sample test case it is possible to change letters 'b' in cells (2, 1) and (3, 1) to 'a', then the minimum path contains cells (1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4). The first coordinate corresponds to the row and the second coordinate corresponds to the column. Tags: greedy Correct Solution: ``` def solve(m, matrix, good, n): c = 'z' for x in range(n): y = m - x if y < 0 or y >= n: continue if not good[x][y]: continue if x < n - 1: c = min(c, matrix[x + 1][y]) if y < n - 1: c = min(c, matrix[x][y + 1]) for x in range(n): y = m - x if y < 0 or y >= n: continue if not good[x][y]: continue if x < n - 1 and matrix[x + 1][y] == c: good[x+1][y] = 1 if y < n - 1 and matrix[x][y + 1] == c: good[x][y + 1] = 1 return c def main(): n, k = map(int, input().split()) matrix = [] for i in range(n): s = list(input()) matrix.append(s) dp = [[0 for i in range(n)] for j in range(n)] good = [[0 for i in range(n)] for j in range(n)] dp[0][0] = 0 if matrix[0][0] == 'a' else 1 for i in range(1, n): dp[0][i] = dp[0][i - 1] if matrix[0][i] != 'a': dp[0][i] += 1 dp[i][0] = dp[i - 1][0] if matrix[i][0] != 'a': dp[i][0] += 1 for i in range(1, n): for j in range(1, n): dp[i][j] = min(dp[i-1][j], dp[i][j-1]) if matrix[i][j] != 'a': dp[i][j] += 1 m = -1 for i in range(n): for j in range(n): if dp[i][j] <= k: m = max(m, i + j) if m == -1: print(matrix[0][0], end = '') m = 0 good[0][0] = 1 else: for i in range(m + 1): print('a', end = '') for i in range(n): y = m - i if y < 0 or y >= n: continue if dp[i][y] <= k: good[i][y] = 1 while m < 2*n - 2: res = solve(m, matrix, good, n) print(res, end = '') m += 1 if __name__=="__main__": main() ```
7,581
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Tags: dfs and similar, graphs Correct Solution: ``` import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from collections import defaultdict def dfs(node): vis[node]=1 colour[node]=0 arr[0]+=1 stack=[node] while stack: cur=stack.pop() for j in edge[cur]: if vis[j]==0: stack.append(j) colour[j]=colour[cur]^1 vis[j]=1 arr[colour[j]]+=1 else: if colour[cur]==colour[j]: return False return True t=int(input()) for _ in range(t): n,m=list(map(int,input().split())) edge=defaultdict(list) z=998244353 for i in range(m): u,v=list(map(int,input().split())) edge[u].append(v) edge[v].append(u) ans=1 s=0 vis=[0]*(n+1) colour=[0]*(n+1) for i in range(1,n+1): if vis[i]==0: arr=[0,0] if dfs(i): a=1 b=1 for j in range(arr[0]): a*=2 a%=z for j in range(arr[1]): b*=2 b%=z ans*=(a+b) ans%=z else: s+=1 break if s==1: print(0) else: print(ans) ```
7,582
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Tags: dfs and similar, graphs Correct Solution: ``` from sys import stdin,stdout from collections import defaultdict import sys, threading sys.setrecursionlimit(10**6) # max depth of recursion threading.stack_size(2**25) def main(): def power(x, p): re = 1 mode= 998244353 while p: if p & 1: re = re * x % mode x = x * x % mode p >>= 1 return re; def dfs(node,col): visited[node]=True c[col]+=1 color[node]=col for j in dic[node]: if not visited[j]: if not dfs(j, col ^ 1): return False else: if color[j]==color[node]: return False return True t=int(stdin.readline()) for _ in range(t): mod=998244353 n,m=map(int,stdin.readline().split()) dic=defaultdict(list) for i in range(m): u,v=map(int,stdin.readline().split()) dic[u].append(v) dic[v].append(u) visited=[False]*(n+1) color=[-1]*(n+1) c=[0]*2 result=1 flag=0 #print(dic) for i in range(1,n+1): if not visited[i]: res=dfs(i,0) if not res: flag=1 break else: result=(result*(power(2,c[0])+power(2,c[1])))%mod c[0]=0 c[1]=0 if flag==1: stdout.write("0\n") else: stdout.write(str(result)+"\n") threading.Thread(target=main).start() ```
7,583
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Tags: dfs and similar, graphs Correct Solution: ``` # aadiupadhyay import os.path from math import gcd, floor, ceil from collections import * import sys from heapq import * mod = 998244353 INF = float('inf') def st(): return list(sys.stdin.readline().strip()) def li(): return list(map(int, sys.stdin.readline().split())) def mp(): return map(int, sys.stdin.readline().split()) def inp(): return int(sys.stdin.readline()) def pr(n): return sys.stdout.write(str(n)+"\n") def prl(n): return sys.stdout.write(str(n)+" ") if os.path.exists('input.txt'): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') def solve(): def dfs(x): visited[x] = 1 stack = [x] color[x] = 0 val = [0, 0] while stack: a = stack.pop() val[color[a]] += 1 for i in d[a]: if visited[i]: if color[i] == color[a]: return 0 else: color[i] = color[a] ^ 1 visited[i] = 1 stack.append(i) ans = 0 for i in val: ans += pow(2, i, mod) ans %= mod return ans n, m = mp() d = defaultdict(list) for i in range(m): a, b = mp() d[a].append(b) d[b].append(a) visited = defaultdict(int) ans = 1 color = defaultdict(lambda: -1) for i in range(1, n+1): if not visited[i]: cur = dfs(i) if cur == 0: pr(0) return ans *= cur ans %= mod pr(ans) for _ in range(inp()): solve() ```
7,584
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Tags: dfs and similar, graphs Correct Solution: ``` # einlesen # dfs für jede cc # Zählen, wie viele w/r # wenn cooring nicht möglich -> Ergebnis 0 # sonst 2^w+2^r M=998244353 t=int(input()) n,m=0,0 g=[] v=[] def dfs(r): s=[r] v[r]=1 c=[0,1] while s: x=s.pop() for j in g[x]: if v[j]==v[x]:return 0 if v[j]==-1: v[j]=v[x]^1 c[v[j]]+=1 s.append(j) if c[0]==0 or c[1]==0:return 3 return ((2**c[0])%M + (2**c[1])%M)%M o=[] for i in range(t): n,m=map(int,input().split()) g=[[]for _ in range(n)] for _ in range(m): a,b=map(int,input().split()) g[a-1].append(b-1) g[b-1].append(a-1) v=[-1]*n ox=1 for j in range(n): if v[j]==-1: ox=(ox*dfs(j))%M o.append(ox) print('\n'.join(map(str,o))) ```
7,585
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Tags: dfs and similar, graphs Correct Solution: ``` ###pyrival template for fast IO import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") mod=998244353 def graph_undirected(): n,e=[int(x) for x in input().split()] graph={} #####adjecancy list using dict for i in range(e): vertex,neighbour=[int(x) for x in input().split()] if vertex in graph: graph[vertex].append(neighbour) else: graph[vertex]=[neighbour] if neighbour in graph: #####for undirected part remove to get directed graph[neighbour].append(vertex) else: graph[neighbour]=[vertex] return graph,n #########check a graph is biparte (bfs+graph coloring to detect odd lenght cycle) from collections import deque def cycle_bfs(graph,n,node):###odd len cycle q=deque([node]);color[node]=0 while q: currnode=q.popleft();visited[currnode]=True; a[color[currnode]]+=1 if color[currnode]==0:currcolor=1 else:currcolor=0 if currnode in graph: for neighbour in graph[currnode]: if visited[neighbour]==False: visited[neighbour]=True color[neighbour]=currcolor q.append(neighbour) else: if color[neighbour]!=currcolor: return False return True k=3*(10**5)+10 pow2=[1 for x in range(k)] pow3=[1 for x in range(k)] for i in range(1,k): pow2[i]=pow2[i-1]*2 pow3[i]=pow3[i-1]*3 pow2[i]%=mod pow3[i]%=mod t=int(input()) while t: t-=1 graph,n=graph_undirected() c=0 for i in range(1,n+1): if i not in graph: c+=1 visited=[False for x in range(n+1)] color=[None for x in range(n+1)] res=1 a=[0,0] for i in range(1,n+1): if visited[i]==False and i in graph: a=[0,0] ans=cycle_bfs(graph,n,i) if ans: res*=(pow2[a[0]]+pow2[a[1]])%mod res%=mod else: res=0 break res*=pow3[c]%mod sys.stdout.write(str(res%mod)+"\n") ```
7,586
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Tags: dfs and similar, graphs Correct Solution: ``` M=998244353 t=int(input()) n,m=0,0 g=[] v=[] def dfs(r): s=[r] v[r]=1 c=[0,1] while s: x=s.pop() for j in g[x]: if v[j]==v[x]:return 0 if v[j]==-1: v[j]=v[x]^1 c[v[j]]+=1 s.append(j) if c[0]==0 or c[1]==0:return 3 return ((2**c[0])%M + (2**c[1])%M)%M o=[] for i in range(t): n,m=map(int,input().split()) g=[[]for _ in range(n)] for _ in range(m): a,b=map(int,input().split()) g[a-1].append(b-1) g[b-1].append(a-1) v=[-1]*n ox=1 for j in range(n): if v[j]==-1: ox=(ox*dfs(j))%M o.append(ox) print('\n'.join(map(str,o))) ```
7,587
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Tags: dfs and similar, graphs Correct Solution: ``` from collections import defaultdict from collections import deque import sys input = sys.stdin.readline mod = 998244353 def bfs(node,v,p): ans=1 q=deque() q.append(node) v[node]=1 a,b=0,0 while q: i=q.popleft() if p[i]==1: a=a+1 else: b=b+1 for j in d[i]: if v[j]==0: v[j]=1 v[j]=1 q.append(j) p[j]=1 if p[i]==0 else 0 else: if p[j]==p[i]: return 0 return (2**a + 2**b)%mod t=int(input()) for _ in range(t): n,m=map(int,input().split()) d=defaultdict(list) p={} for i in range(m): x,y=map(int,input().split()) d[x].append(y) d[y].append(x) v=[0]*(n+1) ans=1 for i in d: if v[i]==0: p[i]=1 f=bfs(i,v,p) ans=(ans*f)%mod for i in range(1,n+1): if v[i]==0: ans=(ans*3)%mod print(ans%mod) ```
7,588
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Tags: dfs and similar, graphs Correct Solution: ``` from sys import stdin,stdout from collections import defaultdict import sys, threading sys.setrecursionlimit(10**5) # max depth of recursion threading.stack_size(2**25) def main(): def power(x, p): re = 1 mode= 998244353 while p: if p & 1: re = re * x % mode x = x * x % mode p >>= 1 return re; def dfs(node,col): visited[node]=True c[col]+=1 color[node]=col for j in dic[node]: if not visited[j]: if not dfs(j, col ^ 1): return False else: if color[j]==color[node]: return False return True t=int(stdin.readline()) for _ in range(t): mod=998244353 n,m=map(int,stdin.readline().split()) dic=defaultdict(list) for i in range(m): u,v=map(int,stdin.readline().split()) dic[u].append(v) dic[v].append(u) visited=[False]*(n+1) color=[-1]*(n+1) c=[0]*2 result=1 flag=0 #print(dic) for i in range(1,n+1): if not visited[i]: res=dfs(i,0) if not res: flag=1 break else: result=(result*(power(2,c[0])+power(2,c[1])))%mod c[0]=0 c[1]=0 if flag==1: stdout.write("0\n") else: stdout.write(str(result)+"\n") threading.Thread(target=main).start() ```
7,589
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Submitted Solution: ``` mod=998244353 import sys input = sys.stdin.readline from collections import deque from collections import Counter def calc(s,a): return (pow(2,s,mod)+pow(2,a-s,mod))%mod def find(x): while Group[x] != x: x=Group[x] return x def Union(x,y): if find(x) != find(y): Group[find(y)]=Group[find(x)]=min(find(y),find(x)) testcase=int(input()) for test in range(testcase): n,m=map(int,input().split()) EDGE=[list(map(int,input().split())) for i in range(m)] EDGELIST=[[] for j in range(n+1)] ANS=1 Group=[j for j in range(n+1)] for a,b in EDGE: Union(a,b) EDGELIST[a].append(b) EDGELIST[b].append(a) testing=[None]*(n+1) flag=1 for i in range(1,n+1): if testing[i]!=None: continue score=1 allscore=1 testing[i]=1 QUE = deque([i]) while QUE: x=QUE.pop() for to in EDGELIST[x]: if testing[to]==-testing[x]: continue if testing[to]==testing[x]: flag=0 break testing[to]=-testing[x] if testing[to]==1: score+=1 allscore+=1 QUE.append(to) if flag==0: break if flag==0: break #print(score,allscore) ANS=ANS*calc(score,allscore)%mod if flag==0: print(0) continue print(ANS) ``` Yes
7,590
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Submitted Solution: ``` from sys import stdin input=lambda : stdin.readline().strip() from math import ceil,sqrt,factorial,gcd from collections import deque from bisect import bisect_left def dfs(x): stack=[[x,1]] a,b=0,0 while stack: x,v=stack.pop() if visited[x]==0: if v==1: a+=1 t=2 else: b+=1 t=1 visited[x]=v for i in graph[x]: if visited[i]==0: stack.append([i,t]) graph[i].remove(x) return a,b def check(x): stack=[x] s=set() while stack: x=stack.pop() s.add(x) for i in graph[x]: if i not in s: stack.append(i) if (visited[x]+visited[i])%2==0: return 0 return 1 for _ in range(int(input())): n,m=map(int,input().split()) graph={i:set() for i in range(n)} visited=[0 for i in range(n)] for i in range(m): a,b=map(int,input().split()) graph[a-1].add(b-1) graph[b-1].add(a-1) f=1 mod=998244353 count=1 for i in range(n): if visited[i]==0 and len(graph[i])!=0: a,b=dfs(i) f=check(i) if f==0: print(0) break else: count=(count*(pow(2,a,mod)+pow(2,b,mod)))%mod if f==1: print((count*pow(3,visited.count(0),mod))%mod) ``` Yes
7,591
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Submitted Solution: ``` import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ########################################################## import math import bisect mod=998244353 # for _ in range(int(input())): from collections import Counter # sys.setrecursionlimit(10**6) # dp=[[-1 for i in range(n+5)]for j in range(cap+5)] # arr= list(map(int, input().split())) #n,l= map(int, input().split()) # arr= list(map(int, input().split())) # for _ in range(int(input())): # n=int(input()) #for _ in range(int(input())): import bisect from heapq import * from collections import defaultdict #n=int(input()) #n,m,s,d=map(int, input().split()) #arr = sorted(list(map(int, input().split()))) #ls=list(map(int, input().split())) #d=defaultdict(list) from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc mod = 998244353 def power(base, exp): base %= mod if exp < 3: return (base ** exp) % mod half = power(base * base, exp // 2) return (half * base) % mod if exp % 2 == 1 else half % mod def solve(): n, m = map(int, input().split()) graph = [[] for _ in range(n + 1)] count, visited = [0, 0], [-1 for _ in range(n + 1)] for _ in range(m): u, v = map(int, input().split()) graph[u].append(v) graph[v].append(u) possible, ans = True, 1 @bootstrap def dfs(node, par, parity): nonlocal possible, visited, graph, count if visited[node] != -1: possible = False if visited[node] != parity else possible yield None visited[node] = parity count[parity] += 1 for child in graph[node]: if child != par: yield dfs(child, node, 1 - parity) yield # check for each node and update the answer for i in range(1, n + 1): if visited[i] == -1: count = [0, 0] dfs(i, -1, 1) ans *= (power(2, count[0]) + power(2, count[1])) % mod ans %= mod # print(ans if possible else 0) sys.stdout.write(str(ans) if possible else '0') sys.stdout.write('\n') def main(): tests = 1 tests = int(input().strip()) for test in range(tests): solve() main() ``` Yes
7,592
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Submitted Solution: ``` from collections import defaultdict import sys #sys.setrecursionlimit(10**6) #sys.stdin = open('E56D_input.txt', 'r') #sys.stdout = open('output.txt', 'w') input=sys.stdin.readline #print=sys.stdout.write graph = defaultdict(list) color = [0] * ((3 * 10 ** 5) + 1000) bipertite = True MOD = 998244353 result = 1 color_count = [0] * 2 pow_value=[] """ def dfs(s, c): global color global bipertite global color_count color[s] = c color_count[c] += 1 for i in range(0, len(graph[s])): if color[graph[s][i]] == -1: dfs(graph[s][i], 1-c) if color[s] == color[graph[s][i]]: bipertite = False """ t = int((input())) #precomputing powers pow_value.append(1) for i in range(1,(3*10**5)+1000): next_value=(pow_value[i-1]*2)%MOD pow_value.append(next_value) #------------------------ while t: n, m = map(int, input().split()) if m==0: print(pow(3,n,MOD)) t-=1 continue graph.clear() bipertite=True result=1 for node in range(0,n+1): color[node]=-1 while m: u, v = map(int, input().split()) graph[u].append(v) graph[v].append(u) m -= 1 for i in range(1, n + 1): if (color[i] != -1): continue #bfs--- bipertite = True color_count[0] = color_count[1] = 0 queue=[] queue.append(i) color[i] = 0 color_count[0] += 1 while queue: # print(color_count[0],' ',color_count[1]) if bipertite==False: break front_node=queue.pop() for child_node in range(0, len(graph[front_node])): if color[graph[front_node][child_node]] == -1: color[graph[front_node][child_node]] = 1-color[front_node] queue.append(graph[front_node][child_node]) color_count[color[graph[front_node][child_node]]] += 1 elif color[front_node]==color[graph[front_node][child_node]]: bipertite = False break #bfs end if bipertite == False: print(0) break current=(pow_value[color_count[0]]+pow_value[color_count[1]])%MOD result = (result * current) % MOD if bipertite: print(result) t -= 1 ``` Yes
7,593
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Submitted Solution: ``` for _ in range(int(input())): n,m=map(int,input().split()) a=[[] for i in range(n+1)] b=[] for i in range(m): c,d=map(int,input().split()) a[d].append(c) a[c].append(d) b.append([c,d]) vis=[0]*(n+1) vis[1]='e' st=[1] while st: x=st.pop() for i in a[x]: if vis[i]==0: if vis[x]=='e': vis[i]='o' else: vis[i]='e' st.append(i) fl=0 for i in b: if vis[i[0]]==vis[i[1]]: print(0) fl=1 break if fl==0: x=vis.count('o') y=vis.count('e') m=1 for i in range(x): m*=2 m%=998244353 ans=m m=1 for i in range(y): m*=2 m%=998244353 ans+=m print(ans%998244353) ``` No
7,594
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Submitted Solution: ``` from collections import * mod = 998244353 class graph: # initialize graph def __init__(self, gdict=None): if gdict is None: gdict = defaultdict(list) self.gdict, self.edges = gdict, [] # add edge def add_edge(self, node1, node2): self.gdict[node1].append(node2) self.gdict[node2].append(node1) self.edges.append([node1, node2]) def get_vertices(self): return list(self.gdict.keys()) def bfs_util(self, i, c): queue, tem, self.color = deque([[i, c - 1]]), c, defaultdict(int, {i: c - 1}) self.visit[i] = 1 while queue: # dequeue parent vertix s = queue.popleft() # enqueue child vertices for i in self.gdict[s[0]]: if self.visit[i] == 0: if s[1] % 2 == 0: tem = (tem * 2) % mod queue.append([i, s[1] ^ 1]) self.visit[i] = 1 self.color[i] = s[1] ^ 1 else: if s[1] % 2 and self.color[i] % 2 or s[1] % 2 == 0 and self.color[i] % 2 == 0: return 0 return tem def bfs(self, c): self.visit, self.cnt = defaultdict(int), 0 for i in self.get_vertices(): if self.visit[i] == 0: self.cnt += self.bfs_util(i, c) return self.cnt for i in range(int(input())): n, m = map(int, input().split()) g, ans = graph(), 0 for j in range(m): u, v = map(int, input().split()) g.add_edge(u, v) if m: ans += g.bfs(2) + g.bfs(1) print(ans % mod) ``` No
7,595
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Submitted Solution: ``` from collections import defaultdict, deque t = int(input()) for _ in range(t): matrix = defaultdict(list) n,m = list(map(int, input().split())) False_Mat = 1 Tru_Mat = 0 bipartite = False if n == 1: print(3) continue for _ in range(m): u, v = list(map(int, input().split())) matrix[u].append(v) matrix[v].append(u) colors = [False] * (n + 1) visited = [False] * (n + 1) #False = {1, 3} True = {2} queue = deque([1]) while queue and not bipartite: src = queue.popleft() visited[src] = True for dests in matrix[src]: if visited[dests] and colors[src] == colors[dests]: #Not bipartite bipartite = True break if not visited[dests]: queue.append(dests) colors[dests] = not colors[src] if colors[dests]: Tru_Mat += 1 else: False_Mat += 1 if bipartite: print(0) else: print(4 * False_Mat * Tru_Mat) ``` No
7,596
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an undirected unweighted graph consisting of n vertices and m edges. You have to write a number on each vertex of the graph. Each number should be 1, 2 or 3. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd. Calculate the number of possible ways to write numbers 1, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353. Note that you have to write exactly one number on each vertex. The graph does not have any self-loops or multiple edges. Input The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of tests in the input. The first line of each test contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 0 ≤ m ≤ 3 ⋅ 10^5) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers u_i, v_i (1 ≤ u_i, v_i ≤ n; u_i ≠ v_i) — indices of vertices connected by i-th edge. It is guaranteed that ∑_{i=1}^{t} n ≤ 3 ⋅ 10^5 and ∑_{i=1}^{t} m ≤ 3 ⋅ 10^5. Output For each test print one line, containing one integer — the number of possible ways to write numbers 1, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353. Example Input 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 4 0 Note Possible ways to distribute numbers in the first test: 1. the vertex 1 should contain 1, and 2 should contain 2; 2. the vertex 1 should contain 3, and 2 should contain 2; 3. the vertex 1 should contain 2, and 2 should contain 1; 4. the vertex 1 should contain 2, and 2 should contain 3. In the second test there is no way to distribute numbers. Submitted Solution: ``` # /////////////////////////////////////////////////////////////////////////// # //////////////////// PYTHON IS THE BEST //////////////////////// # /////////////////////////////////////////////////////////////////////////// import sys,os,io from sys import stdin import math from collections import defaultdict from heapq import heappush, heappop, heapify from bisect import bisect_left , bisect_right from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") alphabets = list('abcdefghijklmnopqrstuvwxyz') #for deep recursion__________________________________________- from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc def ncr(n, r, p): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den,p - 2, p)) % p def primeFactors(n): l = [] while n % 2 == 0: l.append(2) n = n / 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: l.append(int(i)) n = n / i if n > 2: l.append(n) # c = dict(Counter(l)) return list(set(l)) # return c def power(x, y, p) : res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res #____________________GetPrimeFactors in log(n)________________________________________ def sieveForSmallestPrimeFactor(): MAXN = 100001 spf = [0 for i in range(MAXN)] spf[1] = 1 for i in range(2, MAXN): spf[i] = i for i in range(4, MAXN, 2): spf[i] = 2 for i in range(3, math.ceil(math.sqrt(MAXN))): if (spf[i] == i): for j in range(i * i, MAXN, i): if (spf[j] == j): spf[j] = i return spf def getPrimeFactorizationLOGN(x): spf = sieveForSmallestPrimeFactor() ret = list() while (x != 1): ret.append(spf[x]) x = x // spf[x] return ret #____________________________________________________________ def SieveOfEratosthenes(n): #time complexity = nlog(log(n)) prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime def si(): return input() def divideCeil(n,x): if (n%x==0): return n//x return n//x+1 def ii(): return int(input()) def li(): return list(map(int,input().split())) # /////////////////////////////////////////////////////////////////////////// # //////////////////// DO NOT TOUCH BEFORE THIS LINE //////////////////////// # /////////////////////////////////////////////////////////////////////////// if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w") else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") @bootstrap def dfs(node,parent,level,adj,color,vis,colors): vis[node]=True for kid in adj[node]: if color[kid]==color[node]: yield False if color[kid]==-1: level[kid]=level[node]+1 color[kid]=abs(1-color[node]) colors[color[kid]%2]+=1 yield dfs(kid,parent,level,adj,color,vis,colors) yield True def solve(): n,m = li() adj = [[] for i in range(n)] mod = 998244353 for i in range(m): u,v = li() u-=1 v-=1 adj[u].append(v) adj[v].append(u) vis = [False]*n level = [0]*n color = [-1]*n ans = 0 total = 1 for i in range(n): if color[i]!=-1: continue if vis[i]==False: vis[i]=True color[i]=0 colors = [1,0] a = dfs(i,-1,level,adj,color,vis,colors) if a==False: print(0) return else: ans=pow(2,colors[0],mod) ans+=pow(2,colors[1],mod) total*=ans total%=mod ans%=mod print(total) t = 1 t = ii() for _ in range(t): solve() ``` No
7,597
Provide tags and a correct Python 3 solution for this coding contest problem. Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not." And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as "kazak", "oo" and "r" are palindromes, but strings "abb" and "ij" are not. Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times k, so they got k+1 pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled. In this way, it's possible to achive a string abcdefg from the string f|de|abc|g using 3 cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts. More formally, Sasha wants for the given palindrome s find such minimum k, that you can cut this string into k + 1 parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string s. It there is no answer, then print "Impossible" (without quotes). Input The first line contains one string s (1 ≤ |s| ≤ 5 000) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that s is a palindrome. Output Print one integer k — the minimum number of cuts needed to get a new name, or "Impossible" (without quotes). Examples Input nolon Output 2 Input otto Output 1 Input qqqq Output Impossible Input kinnikkinnik Output 1 Note In the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut. In the second example, you can cut the string right in the middle, and swap peaces, so you get toot. In the third example, you can't make a string, that won't be equal to the initial one. In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin. Tags: constructive algorithms, hashing, strings Correct Solution: ``` s = input() n = len(s) // 2 k = len(s) allsame = True for i in range(n): if s[i] != s[0]: allsame = False if allsame: print("Impossible") else: ans = False for i in range(1, n + 1): str = s[i:] + s[:i] if str[:n] == ''.join(reversed(str[k - n:])) and str != s: print(1) ans = True break if not ans: print(2) ```
7,598
Provide tags and a correct Python 3 solution for this coding contest problem. Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not." And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as "kazak", "oo" and "r" are palindromes, but strings "abb" and "ij" are not. Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times k, so they got k+1 pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled. In this way, it's possible to achive a string abcdefg from the string f|de|abc|g using 3 cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts. More formally, Sasha wants for the given palindrome s find such minimum k, that you can cut this string into k + 1 parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string s. It there is no answer, then print "Impossible" (without quotes). Input The first line contains one string s (1 ≤ |s| ≤ 5 000) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that s is a palindrome. Output Print one integer k — the minimum number of cuts needed to get a new name, or "Impossible" (without quotes). Examples Input nolon Output 2 Input otto Output 1 Input qqqq Output Impossible Input kinnikkinnik Output 1 Note In the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut. In the second example, you can cut the string right in the middle, and swap peaces, so you get toot. In the third example, you can't make a string, that won't be equal to the initial one. In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin. Tags: constructive algorithms, hashing, strings Correct Solution: ``` def isPalindrome( word ) : return True if word == word[::-1] else False word = input() n = len( word ) possible = False for i in range( n//2 + 1 ) : new = word[i:] + word[:i] if isPalindrome( new ) and new != word : possible = True print( 1 ) break if not possible : for i in range( 1 , n//2 if n%2 == 0 else (n+1)//2 ) : if word[:i] != word[-i:] : possible = True print( 2 ) break if not possible : print('Impossible') ```
7,599