text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide a correct Python 3 solution for this coding contest problem. You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000. The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node. The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Example Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output 4 "Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**13 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) class UnionFind: def __init__(self, size): self.table = [-1 for _ in range(size)] def find(self, x): if self.table[x] < 0: return x else: self.table[x] = self.find(self.table[x]) return self.table[x] def union(self, x, y): s1 = self.find(x) s2 = self.find(y) if s1 != s2: self.table[s2] += self.table[s1] self.table[s1] = s2 return True return False def subsetall(self): a = [] for i in range(len(self.table)): if self.table[i] < 0: a.append((i, -self.table[i])) return a def main(): rr = [] def f(n,q): ps = [0, 1] + [I() for _ in range(n-1)] qs = [] ms = set() for i in range(q): s,t = LS() t = int(t) if s == 'M': if t in ms: continue ms.add(t) qs.append((s,t)) uf = UnionFind(n+1) for i in range(2,n+1): if i in ms: continue uf.union(i, ps[i]) r = 0 for s,t in qs[::-1]: if s == 'Q': r += uf.find(t) else: uf.union(t, ps[t]) return r while 1: n,q = LI() if n == 0 and q == 0: break rr.append(f(n,q)) return '\n'.join(map(str,rr)) print(main()) ```
89,200
Provide a correct Python 3 solution for this coding contest problem. You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000. The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node. The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Example Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output 4 "Correct Solution: ``` class UnionFind: def __init__(self, size): self.parent = list(range(size)) def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return self.parent[x] = y def same(self, x, y): return self.find(x) == self.find(y) while True: N, Q = map(int, input().split()) if N == Q == 0: break parent = [0, 1] + [int(input()) for i in range(2, N+1)] queries = [] marked = set([1]) for i in range(Q): o, v = input().split() v = int(v) if o == 'M': if v in marked: continue marked.add(v) queries.append((o, v)) uf = UnionFind(N+1) for i in range(1, N+1): if i not in marked: uf.unite(i, parent[i]) ans = 0 for o, v in reversed(queries): if o == 'M': uf.unite(v, parent[v]) else: ans += uf.find(v) print(ans) ```
89,201
Provide a correct Python 3 solution for this coding contest problem. You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000. The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node. The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Example Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output 4 "Correct Solution: ``` import sys sys.setrecursionlimit(10 ** 6) class UnionFindTree: """Disjoint-Set Data Structure Union-Find Tree complexity: init: O(n) find, unite, same: O(alpha(n)) used in SRM505 div.2 900, ATC001 A, DSL1A(AOJ) """ def __init__(self, n): self.par = list(range(n)) # parent self.rank = [0] * n # depth of tree def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def unite(self, x, y): x, y = self.find(x), self.find(y) if x == y: return if self.rank[x] < self.rank[y]: self.par[x] = y else: self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 def same(self, x, y): return self.find(x) == self.find(y) while True: N, Q = map(int, input().split()) if N == Q == 0: break parents = [0] + [int(input()) - 1 for _ in range(N - 1)] queries = [] marked = set() for _ in range(Q): k, v = input().split() v = int(v) - 1 if k == "Q": queries.append((k, v)) elif k == "M" and v not in marked: marked.add(v) queries.append((k, v)) uf = UnionFindTree(N) for i in range(1, N): if i not in marked: p_root = uf.find(parents[i]) uf.par[i] = p_root uf.rank[p_root] = max(uf.rank[p_root], uf.par[i] + 1) ans = 0 for k, v in reversed(queries): if k == "Q": ans += uf.find(v) + 1 elif not uf.same(v, parents[v]): p_root = uf.find(parents[v]) uf.par[v] = p_root uf.rank[p_root] = max(uf.rank[p_root], uf.par[v] + 1) print(ans) ```
89,202
Provide a correct Python 3 solution for this coding contest problem. You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000. The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node. The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Example Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output 4 "Correct Solution: ``` def root(x): while P[x] != x: x = P[x] return x def mark(x): P[x-1] = x-1 def query(x): v = root(x-1) +1 return v while True: N,Q = map(int,input().strip().split(" ")) if N == Q == 0: break P = [] P.append(0) for i in range(N-1): p_i = int(input().strip()) -1 P.append(p_i) s = 0 for j in range(Q): op = input().strip().split(" ") if op[0] == "M": mark(int(op[1])) else: s += query(int(op[1])) print(s) ```
89,203
Provide a correct Python 3 solution for this coding contest problem. You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000. The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node. The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Example Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output 4 "Correct Solution: ``` class UnionFind: def __init__(self, size): self.table = [None] * size def find(self, x): if self.table[x] == None: return x else: # compression of the path self.table[x] = self.find(self.table[x]) return self.table[x] def union(self, parent, child): p_root = self.find(parent) c_root = self.find(child) if p_root != c_root: self.table[c_root] = p_root def solve(): import sys input_lines = sys.stdin.readlines() while True: N, Q = map(int, input_lines[0].split()) if N == 0: break parent = [0, 1] + list(map(int, input_lines[1:N])) unmarked = [True] * (N + 1) unmarked[1] = False operation = [] for o in input_lines[N:N+Q]: mq, v = o.split() v = int(v) if mq == 'M': if unmarked[v]: unmarked[v] = False operation.append((mq, v)) else: operation.append((mq, v)) uf = UnionFind(N + 1) for v, p in enumerate(parent[2:], start=2): if unmarked[v]: uf.union(p, v) ans = 0 for mq, v in reversed(operation): if mq == 'M': uf.union(parent[v], v) else: ans += uf.find(v) print(ans) del input_lines[:N+Q] solve() ```
89,204
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000. The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node. The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Example Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output 4 Submitted Solution: ``` class UnionFind: def __init__(self, size): self.table = [None] * size def find(self, x): if self.table[x] == None: return x else: # compression of the path self.table[x] = self.find(self.table[x]) return self.table[x] def union(self, parent, child): p_root = self.find(parent) c_root = self.find(child) if p_root != c_root: self.table[c_root] = p_root def solve(): import sys input_lines = sys.stdin.readlines() while True: N, Q = map(int, input_lines[0].split()) if N == 0: break unmarked = [True] * (N + 1) unmarked[1] = False operation = [] for o in input_lines[N:N+Q]: mq, v = o.split() v = int(v) if mq == 'M': if unmarked[v]: unmarked[v] = False operation.append((mq, v)) else: operation.append((mq, v)) uf = UnionFind(N + 1) parent = [None, 1] + list(map(int, input_lines[1:N])) for v, p in enumerate(parent[2:], start=2): if unmarked[v]: uf.union(p, v) ans = 0 for o, v in reversed(operation): if o == 'M': uf.union(parent[v], v) else: ans += uf.find(v) print(ans) del input_lines[:N+Q] solve() ``` Yes
89,205
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000. The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node. The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Example Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output 4 Submitted Solution: ``` marked = [] par = [] rank = [] def find(x): if par[x] == x: return x else: if marked[x]: return x return find(par[x]) def main(): global marked, par, rank n,q = [int(i) for i in input().split()] marked = [False for i in range(n+1)] par = [ i for i in range(n+1)] rank = [ 0 for i in range(n+1)] ans = 0 for i in range(2,n+1): par[i] = int(input()) for i in range(q): s = input().split() if s[0] == 'Q': ans += find(int(s[1])) else: marked[int(s[1])]=True print(ans) input() if __name__ == '__main__': main() ``` No
89,206
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000. The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node. The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Example Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output 4 Submitted Solution: ``` marked = [] par = [] rank = [] def find(x): if par[x] == x: return x else: if marked[x]: return x return find(par[x]) def main(): global marked, par, rank n,q = [int(i) for i in input().split()] marked = [False for i in range(n+1)] par = [ i for i in range(n+1)] rank = [ 0 for i in range(n+1)] ans = 0 for i in range(2,n+1): par[i] = int(input()) for i in range(q): s = input().split() if s[0] == 'Q': ans += find(int(s[1])) else: marked[int(s[1])]=True input() print(ans) if __name__ == '__main__': main() ``` No
89,207
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000. The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node. The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Example Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output 4 Submitted Solution: ``` import sys sys.setrecursionlimit(1000000) marked = [] par = [] rank = [] def find(x): if par[x] == x: return x else: if marked[x]: return x return find(par[x]) def main(): global marked, par, rank n,q = [int(i) for i in input().split()] marked = [False for i in range(n+1)] par = [ i for i in range(n+1)] rank = [ 0 for i in range(n+1)] ans = 0 for i in range(2,n+1): par[i] = int(input()) for i in range(q): s = input().split() if s[0] == 'Q': print(find(int(s[1]))) else: marked[int(s[1])]=True input() if __name__ == '__main__': main() ``` No
89,208
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself. Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000. The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node. The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, print the sum of the outputs of all query operations in one line. Example Input 6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0 Output 4 Submitted Solution: ``` import sys sys.setrecursionlimit(10 ** 6) class UnionFindTree: """Disjoint-Set Data Structure Union-Find Tree complexity: init: O(n) find, unite, same: O(alpha(n)) used in SRM505 div.2 900, ATC001 A, DSL1A(AOJ) """ def __init__(self, n): self.par = list(range(n)) # parent self.rank = [0] * n # depth of tree def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def unite(self, x, y): x, y = self.find(x), self.find(y) if x == y: return if self.rank[x] < self.rank[y]: self.par[x] = y else: self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 def same(self, x, y): return self.find(x) == self.find(y) while True: N, Q = map(int, input().split()) if N == Q == 0: break parents = [0] + [int(input()) - 1 for _ in range(N - 1)] queries = [] marked = set() for _ in range(Q): k, v = input().split() v = int(v) - 1 queries.append((k, v)) if k == "M": marked.add(v) uf = UnionFindTree(N) for i in range(1, N): if i not in marked: p_root = uf.find(parents[i]) uf.par[i] = p_root uf.rank[p_root] = max(uf.rank[p_root], uf.par[i] + 1) ans = 0 for k, v in reversed(queries): if k == "Q": ans += uf.find(v) + 1 elif not uf.same(v, parents[v]): p_root = uf.find(parents[v]) uf.par[v] = p_root uf.rank[p_root] = max(uf.rank[p_root], uf.par[v] + 1) print(ans) ``` No
89,209
Provide a correct Python 3 solution for this coding contest problem. International Carpenters Professionals Company (ICPC) is a top construction company with a lot of expert carpenters. What makes ICPC a top company is their original language. The syntax of the language is simply given in CFG as follows: S -> SS | (S) | )S( | ε In other words, a right parenthesis can be closed by a left parenthesis and a left parenthesis can be closed by a right parenthesis in this language. Alex, a grad student mastering linguistics, decided to study ICPC's language. As a first step of the study, he needs to judge whether a text is well-formed in the language or not. Then, he asked you, a great programmer, to write a program for the judgement. Alex's request is as follows: You have an empty string S in the beginning, and construct longer string by inserting a sequence of '(' or ')' into the string. You will receive q queries, each of which consists of three elements (p, c, n), where p is the position to insert, n is the number of characters to insert and c is either '(' or ')', the character to insert. For each query, your program should insert c repeated by n times into the p-th position of S from the beginning. Also it should output, after performing each insert operation, "Yes" if S is in the language and "No" if S is not in the language. Please help Alex to support his study, otherwise he will fail to graduate the college. Input The first line contains one integer q (1 \leq q \leq 10^5) indicating the number of queries, follows q lines of three elements, p_i, c_i, n_i, separated by a single space (1 \leq i \leq q, c_i = '(' or ')', 0 \leq p_i \leq length of S before i-th query, 1 \leq n \leq 2^{20}). It is guaranteed that all the queries in the input are valid. Output For each query, output "Yes" if S is in the language and "No" if S is not in the language. Examples Input 3 0 ( 10 10 ) 5 10 ) 5 Output No No Yes Input 3 0 ) 10 10 ( 5 10 ( 5 Output No No Yes Input 3 0 ( 10 10 ) 20 0 ( 10 Output No No Yes "Correct Solution: ``` r=0 for i in range(int(input())): l=list(input().split()) r+=int(l[2])*(2*(l[1]=='(')-1) print(['No','Yes'][r==0]) ```
89,210
Provide a correct Python 3 solution for this coding contest problem. International Carpenters Professionals Company (ICPC) is a top construction company with a lot of expert carpenters. What makes ICPC a top company is their original language. The syntax of the language is simply given in CFG as follows: S -> SS | (S) | )S( | ε In other words, a right parenthesis can be closed by a left parenthesis and a left parenthesis can be closed by a right parenthesis in this language. Alex, a grad student mastering linguistics, decided to study ICPC's language. As a first step of the study, he needs to judge whether a text is well-formed in the language or not. Then, he asked you, a great programmer, to write a program for the judgement. Alex's request is as follows: You have an empty string S in the beginning, and construct longer string by inserting a sequence of '(' or ')' into the string. You will receive q queries, each of which consists of three elements (p, c, n), where p is the position to insert, n is the number of characters to insert and c is either '(' or ')', the character to insert. For each query, your program should insert c repeated by n times into the p-th position of S from the beginning. Also it should output, after performing each insert operation, "Yes" if S is in the language and "No" if S is not in the language. Please help Alex to support his study, otherwise he will fail to graduate the college. Input The first line contains one integer q (1 \leq q \leq 10^5) indicating the number of queries, follows q lines of three elements, p_i, c_i, n_i, separated by a single space (1 \leq i \leq q, c_i = '(' or ')', 0 \leq p_i \leq length of S before i-th query, 1 \leq n \leq 2^{20}). It is guaranteed that all the queries in the input are valid. Output For each query, output "Yes" if S is in the language and "No" if S is not in the language. Examples Input 3 0 ( 10 10 ) 5 10 ) 5 Output No No Yes Input 3 0 ) 10 10 ( 5 10 ( 5 Output No No Yes Input 3 0 ( 10 10 ) 20 0 ( 10 Output No No Yes "Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 998244353 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] n = I() ni = 0 t = 0 while ni < n: ni += 1 a = LS() if a[1] == '(': t -= int(a[2]) else: t += int(a[2]) if t == 0: rr.append('Yes') else: rr.append('No') return '\n'.join(map(str, rr)) print(main()) ```
89,211
Provide a correct Python 3 solution for this coding contest problem. International Carpenters Professionals Company (ICPC) is a top construction company with a lot of expert carpenters. What makes ICPC a top company is their original language. The syntax of the language is simply given in CFG as follows: S -> SS | (S) | )S( | ε In other words, a right parenthesis can be closed by a left parenthesis and a left parenthesis can be closed by a right parenthesis in this language. Alex, a grad student mastering linguistics, decided to study ICPC's language. As a first step of the study, he needs to judge whether a text is well-formed in the language or not. Then, he asked you, a great programmer, to write a program for the judgement. Alex's request is as follows: You have an empty string S in the beginning, and construct longer string by inserting a sequence of '(' or ')' into the string. You will receive q queries, each of which consists of three elements (p, c, n), where p is the position to insert, n is the number of characters to insert and c is either '(' or ')', the character to insert. For each query, your program should insert c repeated by n times into the p-th position of S from the beginning. Also it should output, after performing each insert operation, "Yes" if S is in the language and "No" if S is not in the language. Please help Alex to support his study, otherwise he will fail to graduate the college. Input The first line contains one integer q (1 \leq q \leq 10^5) indicating the number of queries, follows q lines of three elements, p_i, c_i, n_i, separated by a single space (1 \leq i \leq q, c_i = '(' or ')', 0 \leq p_i \leq length of S before i-th query, 1 \leq n \leq 2^{20}). It is guaranteed that all the queries in the input are valid. Output For each query, output "Yes" if S is in the language and "No" if S is not in the language. Examples Input 3 0 ( 10 10 ) 5 10 ) 5 Output No No Yes Input 3 0 ) 10 10 ( 5 10 ( 5 Output No No Yes Input 3 0 ( 10 10 ) 20 0 ( 10 Output No No Yes "Correct Solution: ``` N = int(input()) lp = rp = 0 for i in range(N): p,c,n = input().split() if c == '(': lp += int(n) else: rp += int(n) print('Yes' if lp == rp else 'No') ```
89,212
Provide a correct Python 3 solution for this coding contest problem. International Carpenters Professionals Company (ICPC) is a top construction company with a lot of expert carpenters. What makes ICPC a top company is their original language. The syntax of the language is simply given in CFG as follows: S -> SS | (S) | )S( | ε In other words, a right parenthesis can be closed by a left parenthesis and a left parenthesis can be closed by a right parenthesis in this language. Alex, a grad student mastering linguistics, decided to study ICPC's language. As a first step of the study, he needs to judge whether a text is well-formed in the language or not. Then, he asked you, a great programmer, to write a program for the judgement. Alex's request is as follows: You have an empty string S in the beginning, and construct longer string by inserting a sequence of '(' or ')' into the string. You will receive q queries, each of which consists of three elements (p, c, n), where p is the position to insert, n is the number of characters to insert and c is either '(' or ')', the character to insert. For each query, your program should insert c repeated by n times into the p-th position of S from the beginning. Also it should output, after performing each insert operation, "Yes" if S is in the language and "No" if S is not in the language. Please help Alex to support his study, otherwise he will fail to graduate the college. Input The first line contains one integer q (1 \leq q \leq 10^5) indicating the number of queries, follows q lines of three elements, p_i, c_i, n_i, separated by a single space (1 \leq i \leq q, c_i = '(' or ')', 0 \leq p_i \leq length of S before i-th query, 1 \leq n \leq 2^{20}). It is guaranteed that all the queries in the input are valid. Output For each query, output "Yes" if S is in the language and "No" if S is not in the language. Examples Input 3 0 ( 10 10 ) 5 10 ) 5 Output No No Yes Input 3 0 ) 10 10 ( 5 10 ( 5 Output No No Yes Input 3 0 ( 10 10 ) 20 0 ( 10 Output No No Yes "Correct Solution: ``` c=0 for _ in range(int(input())): _,s,a=input().split() a=int(a) c+=a if s=='(' else -a print(['No','Yes'][not c]) ```
89,213
Provide a correct Python 3 solution for this coding contest problem. International Carpenters Professionals Company (ICPC) is a top construction company with a lot of expert carpenters. What makes ICPC a top company is their original language. The syntax of the language is simply given in CFG as follows: S -> SS | (S) | )S( | ε In other words, a right parenthesis can be closed by a left parenthesis and a left parenthesis can be closed by a right parenthesis in this language. Alex, a grad student mastering linguistics, decided to study ICPC's language. As a first step of the study, he needs to judge whether a text is well-formed in the language or not. Then, he asked you, a great programmer, to write a program for the judgement. Alex's request is as follows: You have an empty string S in the beginning, and construct longer string by inserting a sequence of '(' or ')' into the string. You will receive q queries, each of which consists of three elements (p, c, n), where p is the position to insert, n is the number of characters to insert and c is either '(' or ')', the character to insert. For each query, your program should insert c repeated by n times into the p-th position of S from the beginning. Also it should output, after performing each insert operation, "Yes" if S is in the language and "No" if S is not in the language. Please help Alex to support his study, otherwise he will fail to graduate the college. Input The first line contains one integer q (1 \leq q \leq 10^5) indicating the number of queries, follows q lines of three elements, p_i, c_i, n_i, separated by a single space (1 \leq i \leq q, c_i = '(' or ')', 0 \leq p_i \leq length of S before i-th query, 1 \leq n \leq 2^{20}). It is guaranteed that all the queries in the input are valid. Output For each query, output "Yes" if S is in the language and "No" if S is not in the language. Examples Input 3 0 ( 10 10 ) 5 10 ) 5 Output No No Yes Input 3 0 ) 10 10 ( 5 10 ( 5 Output No No Yes Input 3 0 ( 10 10 ) 20 0 ( 10 Output No No Yes "Correct Solution: ``` r=l=0 for _ in range(int(input())): _,s,a=input().split() a=int(a) if s[0]=='(':l+=a else:r+=a print(['No','Yes'][l==r]) ```
89,214
Provide a correct Python 3 solution for this coding contest problem. Problem statement N-winged rabbit is on a balance beam of length L-1. The initial position of the i-th rabbit is the integer x_i, which satisfies 0 ≤ x_ {i} \ lt x_ {i + 1} ≤ L−1. The coordinates increase as you move to the right. Any i-th rabbit can jump to the right (ie, move from x_i to x_i + a_i) any number of times, just a distance a_i. However, you cannot jump over another rabbit or enter a position below -1 or above L. Also, at most one rabbit can jump at the same time, and at most one rabbit can exist at a certain coordinate. How many possible states of x_ {0},…, x_ {N−1} after starting from the initial state and repeating the jump any number of times? Find by the remainder divided by 1 \, 000 \, 000 \, 007. input The input is given in the following format. N L x_ {0}… x_ {N−1} a_ {0}… a_ {N−1} Constraint * All inputs are integers * 1 \ ≤ N \ ≤ 5 \,000 * N \ ≤ L \ ≤ 5 \,000 * 0 \ ≤ x_ {i} \ lt x_ {i + 1} \ ≤ L−1 * 0 \ ≤ a_ {i} \ ≤ L−1 output Print the answer in one line. sample Sample input 1 13 0 1 Sample output 1 3 If 1/0 is used to express the presence / absence of a rabbit, there are three ways: 100, 010, and 001. Sample input 2 twenty four 0 1 1 2 Sample output 2 Four There are four ways: 1100, 1001, 0101, 0011. Sample input 3 10 50 0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 Sample output 3 272278100 The binomial coefficient C (50,10) = 10 \, 272 \, 278 \, 170, and the remainder obtained by dividing it by 1 \, 000 \, 000 \, 007 is 272 \, 278 \, 100. Example Input 1 3 0 1 Output 3 "Correct Solution: ``` def main(): MOD = 1000000007 n, l = map(int, input().split()) xlst = map(int, input().split()) alst = map(int, input().split()) can_use = [] for x, a in zip(xlst, alst): if a == 0:s = {x} else:s = {k for k in range(x, l, a)} can_use.append(s) dp = [[0] * l for _ in range(n)] for j in range(l): dp[0][j] = dp[0][j - 1] + int(j in can_use[0]) for i in range(1, n): acc = 0 dpi = dp[i] dpi1 = dp[i - 1] st = can_use[i] for j in range(1, l): if j in st:acc = (acc + dpi1[j - 1]) % MOD dpi[j] = acc print(dp[n - 1][l - 1]) main() ```
89,215
Provide a correct Python 3 solution for this coding contest problem. problem Given the sequence $ A $ of length $ N $. Find the maximum value of $ \ sum B_i $, where $ B $ is one of the longest increasing subsequences of the sequence $ A $. The longest increasing subsequence of the sequence $ A $ is the longest subsequence that satisfies $ A_i <A_j $ with all $ i <j $. output Output the maximum value of $ \ sum B_i $, where $ B $ is one of the longest increasing subsequences of the sequence $ A $. Also, output a line break at the end. Example Input 4 6 4 7 8 Output 21 "Correct Solution: ``` import bisect N = int(input()) A = list(map(int,input().split())) lis = [] hist = [] for a in A: i = bisect.bisect_left(lis,a) if i == len(lis): lis.append(a) hist.append([a]) else: lis[i] = a hist[i].append(a) ans = last = hist[-1][0] for row in reversed(hist[:-1]): i = bisect.bisect_left(row[::-1], last) last = row[-i] ans += last print(ans) ```
89,216
Provide a correct Python 3 solution for this coding contest problem. C: Canisal cryptography problem Ebi-chan was given the string C obtained by encrypting a non-negative integer D with "canisal cipher". This cipher replaces each number in decimal notation with a fixed number (not necessarily different from the original). Different numbers will not be replaced with the same number, and the same number will not be rewritten to a different number depending on the position of appearance. For example, this encryption method can result in 2646 being 0545, but not 3456 being 1333 or 1333 being 3456. Now, Ebi-chan has been told that the remainder of dividing D by 10 ^ 9 + 7 is M. At this time, output one that can be considered as D. If you can think of more than one, you can output any of them. However, it is assumed that there is no extra `0` at the beginning of D. Input format M C Constraint * 0 \ leq M <10 ^ 9 + 7 * 1 \ leq | C | \ leq 10 ^ 5 Output format Print a non-negative integer that can be considered as D on one line. If it does not exist, output `-1`. Input example 1 2 1000000007 Output example 1 1000000009 The encryption method this time was to replace 0 with 0, 1 with 1, and 9 with 7. Input example 2 3 1000000007 Output example 2 -1 Input example 3 1 01 01 Output example 3 -1 There is no extra `0` at the beginning of the D. Input example 4 45 1000000023 Output example 4 6000000087 Since `1000000052` and` 2000000059` also satisfy the conditions, you can output them. Input example 5 0 940578326285963740 Output example 5 123456789864197523 Example Input 2 1000000007 Output 1000000009 "Correct Solution: ``` # 半分全列挙 from itertools import permutations, combinations M = int(input()) C = list(map(int, input())) if len(C)==1 and M==0: print(0) exit() mod = 10**9 + 7 L = [0] * 10 b = 1 for c in C[::-1]: L[c] += b b = b * 10 % mod for t_half1 in combinations(range(10), 5): L1, L2 = L[:5], L[5:] t_half2 = list(set(range(10)) - set(t_half1)) if int(C[0]) < 5: s1 = {sum(l * n for l, n in zip(L1, t1)) % mod: t1 for t1 in reversed(list(permutations(t_half1))) if t1[int(C[0])] != 0} else: s1 = {sum(l * n for l, n in zip(L1, t1)) % mod: t1 for t1 in reversed(list(permutations(t_half1)))} for t2 in permutations(t_half2): s = sum(l * n for l, n in zip(L2, t2)) if (M-s) % mod in s1: t = s1[(M-s) % mod] + t2 if t[int(C[0])] != 0: print("".join(map(lambda x: str(t[x]), C))) exit() print(-1) ```
89,217
Provide a correct Python 3 solution for this coding contest problem. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 "Correct Solution: ``` import sys # import re import math import collections # import decimal import bisect import itertools import fractions # import functools import copy # import heapq import decimal # import statistics import queue # import numpy as np sys.setrecursionlimit(10000001) INF = 10 ** 16 MOD = 10 ** 9 + 7 # MOD = 998244353 ni = lambda: int(sys.stdin.readline()) ns = lambda: map(int, sys.stdin.readline().split()) na = lambda: list(map(int, sys.stdin.readline().split())) # ===CODE=== eps = 10 ** (-9) def main(): h, w = ns() mat = [na() for _ in range(h)] dp = [[0 for _ in range(w)] for __ in range(h)] ans = 0 for i in range(h): for j in range(w): if mat[i][j]: continue if mat[i][j] == 0: dp[i][j] = 1 if i > 0 and j > 0: dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1 ans = max(ans, dp[i][j]) print(ans ** 2) if __name__ == '__main__': main() ```
89,218
Provide a correct Python 3 solution for this coding contest problem. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 "Correct Solution: ``` H, W = map(int, input().split()) S = [list(map(int, input().split())) for i in range(H)] dp = [[0]*(W) for i in range(H)] for i in range(H): for j in range(W): if S[i][j] == 0: dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1])+1 # print(max([max([max(a) for a in dp])**2])) print(max([max(a) for a in dp])**2) ```
89,219
Provide a correct Python 3 solution for this coding contest problem. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 "Correct Solution: ``` h, w = map(int, input().split( )) #c = [ [0] * w ] #下の奴がまずい。各行が同一のアドレス指してる。上も一応変えておく。 #DP = [ [0] * w ] * ( h + 1 ) c=[[0 for _ in range(w)]] DP = [[0 for _ in range(w)] for __ in range(h+1)] for _ in range(h): c_tmp = list(map(int, input().split( ))) c.append( c_tmp ) for i in range(w): for j in range(1,h+1): if c[j][i] == 1: DP[j][i] = 0 else: DP[j][i] = min(DP[j-1][i],DP[j][i-1],DP[j-1][i-1])+1 mx =0 for i in range(h+1): mx =max(mx,max(DP[i])) print(mx**2) ```
89,220
Provide a correct Python 3 solution for this coding contest problem. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 "Correct Solution: ``` # DPL_3_A: Largest Square import sys H, W = map(int, sys.stdin.readline().strip().split()) dp = [[0] * (W + 1) for _ in range(H + 1)] for h in range(1, H + 1): c = list(map(int, sys.stdin.readline().strip().split())) dirty = [i for i, x in enumerate(c) if x == 1] for w in dirty: dp[h][w + 1] = 'dirty' ans = 0 for h in range(1, H + 1): for w in range(1, W + 1): if dp[h][w] == 'dirty': dp[h][w] =0 continue dp[h][w] = min(dp[h][w - 1], dp[h - 1][w - 1], dp[h - 1][w]) + 1 ans = max(dp[h][w], ans) print(ans ** 2) ```
89,221
Provide a correct Python 3 solution for this coding contest problem. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 "Correct Solution: ``` import sys import itertools h, w = map(int, sys.stdin.readline().split()) dp = [[0] * w for _ in range(h)] G = [[int(j) for j in sys.stdin.readline().split()] for _ in range(h)] for x in range(h): dp[x][0] = 1 if G[x][0] == 0 else 0 for y in range(w): dp[0][y] = 1 if G[0][y] == 0 else 0 for i in range(1, h): for j in range(1, w): if(G[i][j] == 0): dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1 print(max(max(i) for i in dp) ** 2) ```
89,222
Provide a correct Python 3 solution for this coding contest problem. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 "Correct Solution: ``` # -*- coding: utf-8 -*- import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 H,W=MAP() # 四方に一回り大きいグリッドを作る grid = list2d(H+2, W+2, 0) for i in range(1, H+1): row = LIST() for j in range(1, W+1): grid[i][j] = row[j-1] # dp[i][j] := i,jを右端とする最大正方形の辺の長さ dp=list2d(H+2, W+2, 0) for i in range(1, H+1): for j in range(1, W+1): if grid[i][j]!=1: # i,jが汚れマスでなければ、左、左上、上から遷移させてくる dp[i][j]=min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])+1 mx=0 for i in range(1, H+1): mx=max(mx, max(dp[i])) print(mx**2) ```
89,223
Provide a correct Python 3 solution for this coding contest problem. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 "Correct Solution: ``` # DPL 3 A def solve(): H, W = map(int, input().split()) c = [list(map(int, input().split())) for _ in range(H)] dp = [[0] * (W+1) for _ in range(H+1)] ans = 0 for i in range(1, H+1): for j in range(1, W+1): if c[i-1][j-1] == 1: dp[i][j] == 0 else: dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1 ans = max(ans, dp[i][j]) print(ans**2) if __name__ == "__main__": solve() ```
89,224
Provide a correct Python 3 solution for this coding contest problem. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 "Correct Solution: ``` H, W = map(int, input().split()) c = [] for i in range(H): c.append([int(i) for i in input().split()]) dp = [[0 for i in range(W)] for i in range(H)] maxwidth = 0 for i in range(H): for j in range(W): dp[i][j] = (c[i][j] + 1) % 2 maxwidth |= dp[i][j] for i in range(1, H): for j in range(1, W): if c[i][j]: dp[i][j] = 0 else: dp[i][j] = min(dp[i-1][j], dp[i-1][j-1], dp[i][j-1]) + 1 maxwidth = max(maxwidth,dp[i][j]) print(maxwidth**2) ```
89,225
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 Submitted Solution: ``` # -*- coding: utf-8 -*- from itertools import chain if __name__ == '__main__': H, W = map(int, input().split()) C = [] dp = [] for i in range(H): l = input().split() C.append([int(x) for x in l]) dp.append([(int(x) + 1) % 2 for x in l]) for i in range(1, H): for j in range(1, W): if C[i][j] == 0: dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1 max_width = max(list(chain.from_iterable(dp))) print(max_width**2) ``` Yes
89,226
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 Submitted Solution: ``` import sys h, w = map(int, sys.stdin.readline().split()) dp = [[0] * w for _ in range(h)] G = [[int(j) for j in sys.stdin.readline().split()] for _ in range(h)] for x in range(h): dp[x][0] = 1 if G[x][0] == 0 else 0 for y in range(w): dp[0][y] = 1 if G[0][y] == 0 else 0 for i in range(1, h): for j in range(1, w): if(G[i][j] == 0): dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1 print(max([max(item) for item in dp])**2) ``` Yes
89,227
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 Submitted Solution: ``` H,W = map(int,input().split()) M = [[0]*(W+1) for _ in range(H+1)] for i in range(H): M[i+1][1:] = list(map(int,input().split())) dp = [[0]*(W+1) for _ in range(H+1)] for h in range(1,H+1): for w in range(1,W+1): if M[h][w] == 0: dp[h][w] = min(dp[h-1][w-1],dp[h-1][w],dp[h][w-1]) + 1 else: dp[h][w] = 0 print(max([max(x) for x in dp])**2) ``` Yes
89,228
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 Submitted Solution: ``` if __name__ == "__main__": H, W = map(lambda x: int(x), input().split()) dp = [list(map(int, input().split())) for _ in range(H)] for h in range(H): for w in range(W): dp[h][w] ^= 1 # 0 -> 1, 1 -> 0 for h in range(1, H): for w in range(1, W): if dp[h][w]: dp[h][w] = min(dp[h - 1][w - 1], dp[h - 1][w], dp[h][w - 1]) dp[h][w] += 1 print(max(w for h in dp for w in h) ** 2) # type: ignore ``` Yes
89,229
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 Submitted Solution: ``` H, W = map(int, input().split()) c = [] for i in range(H): c.append([int(i) for i in input().split()]) dp = [[0 for i in range(W)] for i in range(H)] dp[0][0] = 1 if c[0][0] == 0 else 0 for j in range(W): if c[0][0] == 1: dp[0][j] = 0 else: dp[0][j] = 1 for j in range(H): if c[0][0] == 1: dp[j][0] = 0 else: dp[j][0] = 1 for i in range(1, H): for j in range(1, W): dp[i][j] = min(dp[i-1][j], dp[i-1][j-1], dp[i][j-1]) + 1 print(dp[H-1][W-1]) ``` No
89,230
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 Submitted Solution: ``` def solve(): H, W = map(int, input().split()) c = [] for _ in range(H): c.append(list(map(int, input().split()))) dp = [0] * (W+1)*(H+1) for i in range(H): for j in range(W): if c[i][j] == 0: dp[i+1 + H*(j+1)] = min(dp[i + H*j], dp[i+1 + H*j], dp[i + H*(j+1)]) + 1 print(max(dp)**2) if __name__ == '__main__': solve() ``` No
89,231
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 Submitted Solution: ``` import sys h, w = map(int, sys.stdin.readline().split()) dp = [[0] * w for _ in range(h)] G = [list(map(int, i.split())) for i in sys.stdin.readlines()] for x in range(h): dp[x][0] = 1 if G[x][0] == 0 else 0 for y in range(w): dp[0][y] = 1 if G[0][y] == 0 else 0 for i in range(1, h): for j in range(1, w): if(G[i][j] == 0): dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1 print(max(sum(dp, [])) ** 2) ``` No
89,232
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4 Submitted Solution: ``` def counting(k, v, square): square += 1 for _ in range(square): for r in range(k, k + square): if r > len(matrix) - 1: return False for c in range(v, v + square): if c > len(matrix[k]) - 1: return False if matrix[r][c] == '1': return False return True line = input().split(' ') answer = [[0 for _ in range(int(line[1]))] for _ in range(int(line[0]))] matrix = [] maximum = 0 is_over = False ggg = 0 for _ in range(int(line[0])): row = input().split(' ') matrix.append(row) for i in range(len(matrix)): for j in range(len(matrix[i])): ggg += 1 for m in range(min(int(line[0]), int(line[1]))): res = counting(i, j, maximum) if res: maximum += 1 else: break print(1 if maximum == 0 else maximum * maximum) ``` No
89,233
Provide a correct Python 3 solution for this coding contest problem. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 "Correct Solution: ``` def gcd(a,b): if b>a: a,b = b,a r = a%b if r==0: return b else: return gcd(b, a%b) def solve(a,b): if a==1: return 1,0 if b==1: return 0,1 if a>b: q = a//b r = a%b x, y = solve(r,b) return x, y-q*x if a<b: q = b//a r = b%a x, y = solve(a,r) return x-q*y, y a,b = map(int, input().split()) g = gcd(a,b) a//=g b//=g x, y = solve(a,b) def p(n): return x+b*n def q(n): return y-a*n def f(n): return abs(p(n))+abs(q(n)) if a>b: n = y//a nn = n m = f(n) if f(n-1) < m: nn = n-1 m = f(n-1) if f(n+1) < m: nn = n+1 m = f(n+1) x = p(nn) y = q(nn) elif a<b: n = -x//b nn = n m = f(n) if f(n-1) < m: nn = n-1 m = f(n-1) if f(n+1) < m: nn = n+1 m = f(n+1) x = p(nn) y = q(nn) else: x = 0 y = 1 print("{} {}".format(x,y)) ```
89,234
Provide a correct Python 3 solution for this coding contest problem. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 "Correct Solution: ``` import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians from itertools import permutations, combinations, product from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from fractions import gcd from bisect import bisect_left from heapq import heappush, heappop def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 a, b = MAP() c = gcd(a, b) def extgcd(a, b, d=0): g = a if b == 0: x, y = 1, 0 else: x, y, g = extgcd(b, a % b) x, y = y, x - a // b * y return x, y, g print(*extgcd(a, b)[:2]) ```
89,235
Provide a correct Python 3 solution for this coding contest problem. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 "Correct Solution: ``` def gcd(a, b): global queue r = a % b if r: d = a // b sb = queue.pop() sa = queue.pop() queue.append(sb) queue.append(tuple(map(lambda x, y: x - d * y, sa, sb))) return gcd(b, r) else: return b a, b = map(int, input().split()) queue = [(1, 0), (0, 1)] g = gcd(a, b) print(*queue.pop()) ```
89,236
Provide a correct Python 3 solution for this coding contest problem. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 "Correct Solution: ``` #E a,b = map(int,input().split()) def extgcd(x,y): c1,c2 = x,y a1,a2 = 1,0 b1,b2 = 0,1 while c2: m = c1%c2 q = c1//c2 c1,c2 = c2,m a1,a2 = a2,(a1-q*a2) b1,b2 = b2,(b1-q*b2) return c1,a1,b1 ans = extgcd(a,b) print(ans[1],ans[2]) ```
89,237
Provide a correct Python 3 solution for this coding contest problem. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 "Correct Solution: ``` def extgcd(a, b): if b == 0: return a, 1, 0 else: d, y, x = extgcd(b, a%b) y -= a // b * x return d, x, y a, b = map(int, input().split()) _, x, y = extgcd(a, b) print(x, y) ```
89,238
Provide a correct Python 3 solution for this coding contest problem. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 "Correct Solution: ``` import math a, b = map(int, input().split()) c = math.gcd(a, b) def ex_euclid(x, y): c0, c1 = x, y a0, a1 = 1, 0 b0, b1 = 0, 1 while c1 != 0: m = c0 % c1 q = c0 // c1 c0, c1 = c1, m a0, a1 = a1, (a0 - q * a1) b0, b1 = b1, (b0 - q * b1) return c0, a0, b0 _, a, b = ex_euclid(a, b) print(a, b) ```
89,239
Provide a correct Python 3 solution for this coding contest problem. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 "Correct Solution: ``` def egcd(a, b): x,y, u,v = 0,1, 1,0 while a != 0: q, r = b//a, b%a m, n = x-u*q, y-v*q b,a, x,y, u,v = a,r, u,v, m,n gcd = b return gcd,x,y a,b = (int(_) for _ in input().split(" ")) gcd,x,y = egcd(a,b) if y * a + x * b == gcd: tmp = x x = y y = tmp print(x,y) ```
89,240
Provide a correct Python 3 solution for this coding contest problem. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 "Correct Solution: ``` def ex_euclid(x, y): a0, a1 = 1, 0 b0, b1 = 0, 1 while y!=0: a0, a1 = a1, (a0 - x//y * a1) b0, b1 = b1, (b0 - x//y * b1) x, y = y, x%y return a0, b0 a,b=map(int,input().split()) print(*ex_euclid(a,b)) ```
89,241
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 Submitted Solution: ``` def extended_gcd(a, b): """ax + by = gcd(a, b)の整数解(x, y)を求める""" if b == 0: return a, 1, 0 else: g, y, x = extended_gcd(b, a % b) y -= (a // b) * x return g, x, y a, b = map(int, input().split()) _, x, y = extended_gcd(a, b) print(x, y) ``` Yes
89,242
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 Submitted Solution: ``` r0, r1 = map(int, input().split()) a0, a1 = 1, 0 b0, b1 = 0, 1 while 0 < r1: q1 = r0 // r1 a0, a1 = a1, a0 - q1 * a1 b0, b1 = b1, b0 - q1 * b1 r0, r1 = r1, r0 % r1 print(a0, b0) ``` Yes
89,243
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 Submitted Solution: ``` import math a,b=map(int,input().split()) x=[0] y=[0] def extgcd(a,b,x,y): d=a if b!=0: d=extgcd(b,a%b,y,x) y[0]-=(a//b)*x[0] else: x[0]=1 y[0]=0 extgcd(a,b,x,y) print(x[0],y[0]) ``` Yes
89,244
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 Submitted Solution: ``` def extended_euclid(a,b): c = g = 1 e = f = 0 while b: div,mod = divmod(a,b) h = c - div * e i = f - div * g a,b = b,mod c,e = e,h f,g = g,i return (c,f) n,m = map(int,input().split(" ")) print(*extended_euclid(n,m)) ``` Yes
89,245
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 Submitted Solution: ``` # coding=utf-8 def gcd(n1, n2): if n1 < n2: n1, n2 = n2, n1 if n1 == n2 or n2 == 0: return n1 n1, n2 = n2, (n1 % n2) return gcd(n1, n2) def solve_int(number1, number2): answer1 = 1 while True: if (1+number1*answer1) % number2 == 0: return -answer1, (1+number1*answer1)//number2 answer1 += 1 def modify_solution(x_sp_s, y_sp_s, n1, n2): x_memo, y_memo = x_sp_s, y_sp_s if abs(x_sp_s + n2) + abs(y_sp_s - n1) < (abs(x_memo) + abs(y_memo)): m = y_sp_s//n1 if m == 0: return x_sp_s+n2, y_sp_s-n1 return modify_solution(x_sp_s + m*n2, y_sp_s - m*n1, n1, n2) if abs(x_sp_s + n2) + abs(y_sp_s - n1) == (abs(x_memo) + abs(y_memo)): pass return x_memo, y_memo if __name__ == '__main__': a, b = map(int, input().split()) right_side = gcd(a, b) if right_side > 1: a //= right_side b //= right_side x_sp, y_sp = solve_int(a, b) x_sp, y_sp = modify_solution(x_sp, y_sp, a, b) print(x_sp, y_sp) ``` No
89,246
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 Submitted Solution: ``` def gcd(a, b): x, y = [a, b] if a > b else [b, a] while y: x, y = y, x%y return x a, b = list(map(int, input().split(' '))) c = gcd(a, b) a, b = a//c, b//c x = 0 y = None while True: tmp = (1-a*x)/b if tmp == int(tmp): y = int(tmp) break x += 1 print(x, y) ``` No
89,247
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 Submitted Solution: ``` r,s=map(int,input().split()) a=d=1,b=c=0 while r: q=r//s r,s,a,c,b,d=s,r%s,c,a-q*c,d,b-q*d print(a,b) ``` No
89,248
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Extended Euclid Algorithm Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b. Constraints * 1 ≤ a, b ≤ 109 Input a b Two positive integers a and b are given separated by a space in a line. Output Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily). Examples Input 4 12 Output 1 0 Input 3 8 Output 3 -1 Submitted Solution: ``` # -*- coding: utf-8 -*- """ Created on Sat May 7 22:31:12 2016 @author: kt """ def gcd(a,b): #?????§??¬?´???°???????????°?????? r=0 while True: r=a%b if r==0: break a=b b=r return b a, b = map(int, input().split(" ")) g=gcd(a,b) a=a/g b=b/g c=0 x1=1 x2=0 z1=a y1=0 y2=1 z2=b q=0 while True: if z2==1: break q = (z1 - (z1 % z2)) / z2 x3=(x1-q*x2) y3=(y1-q*y2) z3=z1-q*z2 x1=x2 x2=x3 y1=y2 y2=y3 z1=z2 z2=z3 if x3>y3 and a==b: temp=y3 y3=x3 x3=temp print ("%d %d" % (int(x3), int(y3))) ``` No
89,249
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Tags: brute force, implementation, math, sortings Correct Solution: ``` def photo(n,m): if n<=1: return 0 m=m.split() m=[int(m[i]) for i in range(len(m))] m.sort() x1=m[len(m)-1]-m[0] c=m[1:len(m)-1] differ=[(c[i+n-1]-c[i]) for i in range(len(c)+2-n-1)] y1=min(differ) result=[x1*y1] x=(m[n-1]-m[0])*(m[len(m)-1]-m[n]) result.append(x) return min(result) print(photo(int(input().strip()),input().strip())) ```
89,250
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Tags: brute force, implementation, math, sortings Correct Solution: ``` from sys import stdin, stdout # string input n = int(stdin.readline()) arr = [int(x) for x in stdin.readline().split()] arr.sort() x1 = arr[0] x2 = arr[n-1] y1 = arr[n] y2 = arr[(2*n) - 1] mini = (x2-x1)*(y2-y1) for i in range(1, n): j = i + n - 1 x1 = arr[i] x2 = arr[j] y1 = arr[0] y2 = arr[2*n -1] prod = (x2-x1)*(y2-y1) if (prod < mini): mini = prod print(mini) # print to stdout #stdout.write(str(summation)) ```
89,251
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Tags: brute force, implementation, math, sortings Correct Solution: ``` # -*- coding: utf-8 -*- """ Created on Mon Jul 30 14:37:45 2018 @author: Amrita """ N = int(input()) Ns = list(map(int,input().strip().split())) Ns.sort() diff1 = Ns[N-1] - Ns[0] diff2 = Ns[-1] - Ns[N] best = diff1*diff2 diff_ends = Ns[-1] - Ns[0] for x in range(N): diff = (Ns[x+N-1] - Ns[x]) * diff_ends if diff < best: best = diff print(best) ```
89,252
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Tags: brute force, implementation, math, sortings Correct Solution: ``` n=int(input()) a=sorted(map(int,input().split())) print(min([(a[n-1]-a[0])*(a[-1]-a[n])]+[(a[-1]-a[0])*(a[i+n-1]-a[i]) for i in range(n)])) # Made By Mostafa_Khaled ```
89,253
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Tags: brute force, implementation, math, sortings Correct Solution: ``` n = int(input()) nums = [int(i) for i in input().split()] if n == 1: print(0) else: nums.sort() area_1 = None for i in range(1,n): temp = (nums[-1]-nums[0])*(nums[i+n-1]-nums[i]) if area_1 is None or temp < area_1: area_1 = temp temp = (nums[n-1]-nums[0])*(nums[-1]-nums[n]) if temp < area_1: area_1 = temp print(area_1) ```
89,254
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Tags: brute force, implementation, math, sortings Correct Solution: ``` if __name__ == "__main__": n = int(input()) s = sorted(map(int, input().split())) h = s[2 * n - 1] - s[0] best = (s[n-1] - s[0]) * (s[2*n - 1] - s[n]) for i in range(1, n): w = s[i + n - 1] - s[i] new = h * w if new < best: best = new print(best) ```
89,255
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Tags: brute force, implementation, math, sortings Correct Solution: ``` import sys import math import bisect def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def finput(): return float(input()) def tinput(): return input().split() def rinput(): return map(int, tinput()) def rlinput(): return list(rinput()) def modst(a, s): res = 1 while s: if s % 2: res *= a a *= a s //= 2 return res def size(x1, y1, x2, y2): return ((x1 + x2) ** 2 + (y1 + y2) ** 2) ** 0.5 def d(sum_size, size_x_y): rerurn (sum_size ** 2 - 8 * size_x_y) ** 0.5 def main(): n = iinput() a = rlinput() a.sort() if n == 1: return 0 q = 1e10 for i in range(1, n): q = min(q, a[i + n - 1] - a[i]) return min((a[2 * n - 1] - a[0]) * q, (a[n - 1] - a[0]) * (a[2 * n - 1] - a[n])) print(main()) ```
89,256
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Tags: brute force, implementation, math, sortings Correct Solution: ``` import heapq from math import sqrt from collections import defaultdict n = int(input()) a = list(map(int, input().split())) dic = defaultdict(int) if n == 1: print(0) exit() for v in a: dic[v] += 1 for v, j in dic.items(): if j >= n: print(0) exit() a.sort() m = n - 1 c = a[2*n - 1] - a[m + 1] d = a[m] - a[0] tmp = a[2 * n - 1] - a[0] aim = c * d / tmp d = [c * d] for i in range(n + 1): t = a[i + n - 1] - a[i] if t <= aim: d.append(t * tmp) print(min(d)) ```
89,257
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Submitted Solution: ``` from collections import Counter n = int(input()) a = list(map(int, input().split())) n_, n = n, 2 * n C = Counter(a) if max(C.values()) >= n_: print(0) exit() a_s = sorted(a) ans_1 = (a_s[n_ - 1] - a_s[0]) * (a_s[n - 1] - a_s[n_]) a0 = a_s[-1] - a_s[0] b0 = a0 for i in range(n_ - 1): b0 = min(b0, a_s[i + n_] - a_s[i + 1]) ans_2 = a0 * b0 ans = min(ans_1, ans_2) print(ans) ``` Yes
89,258
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Submitted Solution: ``` '''input 4 1 1 2 2 2 2 3 3 ''' n = int(input()) a = sorted(map(int, input().split())) x1, y2 = a[0], a[-1] x2, y1 = a[n-1], a[n] m = (x1 - x2) * (y1 - y2) x1, x2 = a[0], a[-1] for i in range(1, n): y1, y2 = a[i], a[i+n-1] m = min(m, (x1 - x2) * (y1 - y2)) print(m) ``` Yes
89,259
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Submitted Solution: ``` n = int(input()) l = input().split() l = [int(i) for i in l] l.sort() m = (l[2*n - 1] - l[n])*(l[n - 1] - l[0]) for i in range(1,n): if m > (l[2*n - 1] - l[0])*(l[i + n - 1] - l[i]): m = (l[2*n - 1] - l[0])*(l[i + n - 1] - l[i]); print(m) ``` Yes
89,260
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Submitted Solution: ``` n= int(input()) l = sorted(list(map(int,input().split()))) ret = (l[n-1]-l[0])*(l[-1]-l[n]) if ret>0: for x,y in zip(l[1:n],l[n:-1]): ret = min(ret, (y-x)*(l[-1]-l[0])) print(ret) ``` Yes
89,261
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Submitted Solution: ``` class State: def __init__(self, x, y): self.x = sorted(x) self.y = sorted(y) def area(self): return (max(self.x) - min(self.x)) * (max(self.y) - min(self.y)) def __repr__(self): return f"State with area {self.area()}, x={self.x}, y={self.y}" def try_switching_first(state): # Switching first elemnts of the array candidate_x = state.x[1:] + state.y[-1:] candidate_y = state.y[1:] + state.x[-1:] candiadte_state = State(candidate_x, candidate_y) if candiadte_state.area() < state.area(): return True, candiadte_state else: return False, state def try_switching_last(state): # Switching first elemnts of the array candidate_x = state.x[:-1] + state.y[:1] candidate_y = state.y[:-1] + state.x[-1:] candiadte_state = State(candidate_x, candidate_y) if candiadte_state.area() < state.area(): return True, candiadte_state else: return False, state def optimize_untill_done(s): result, new_state = try_switching_first(s) print("New:", new_state) if result: # print(f"Switching first worked. New state {new_state}") return optimize_untill_done(new_state) result, new_state = try_switching_last(s) if result: # print(f"Switching last worked. New state {new_state}") return optimize_untill_done(new_state) return s.area() if __name__ == "__main__": n = int(input()) data = list(map(int, input().split())) s = State(data[:n], data[n:]) print(optimize_untill_done(s)) ``` No
89,262
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Submitted Solution: ``` n=int(input())-1 s=sorted(map(int,input().split())) print((s[n]-s[0])*(s[2*n+1]-s[n+1])) ``` No
89,263
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Submitted Solution: ``` n= int(input()) l = list(map(int,input().split())) l.sort() print((l[n-1]-l[0])*(l[-1]-l[n])) ``` No
89,264
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer n (1 ≤ n ≤ 100 000), the number of points in Pavel's records. The second line contains 2 ⋅ n integers a_1, a_2, ..., a_{2 ⋅ n} (1 ≤ a_i ≤ 10^9), coordinates, written by Pavel in some order. Output Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records. Examples Input 4 4 1 3 2 3 2 1 3 Output 1 Input 3 5 8 5 5 7 5 Output 0 Note In the first sample stars in Pavel's records can be (1, 3), (1, 3), (2, 3), (2, 4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1, 3) and (2, 4)). Submitted Solution: ``` n = int(input()) m=list(map(int,input().split())) m.sort() n*=2 #print(m) print((m[n//2-1]-m[0])*(m[n-1]-m[n//2])) ``` No
89,265
Provide tags and a correct Python 3 solution for this coding contest problem. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) packets = [] i = 1 s = 0 while s < n: if s + i <= n: packets.append(i) s += i i *= 2 else: packets.append(n-s) s = n print(len(packets)) ```
89,266
Provide tags and a correct Python 3 solution for this coding contest problem. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) import math x = int(math.log(n,2)) print(x+1) ```
89,267
Provide tags and a correct Python 3 solution for this coding contest problem. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Tags: constructive algorithms, greedy, math Correct Solution: ``` def solve(n): k, t = 0, 1 while n > 0: n -= t t *= 2 k += 1 return k n = int(input()) print(solve(n)) ```
89,268
Provide tags and a correct Python 3 solution for this coding contest problem. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Tags: constructive algorithms, greedy, math Correct Solution: ``` import math print(math.floor(math.log2(int(input())))+1) ```
89,269
Provide tags and a correct Python 3 solution for this coding contest problem. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Tags: constructive algorithms, greedy, math Correct Solution: ``` n=int(input().strip()) print(len(bin(n))-2) ```
89,270
Provide tags and a correct Python 3 solution for this coding contest problem. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Tags: constructive algorithms, greedy, math Correct Solution: ``` from math import ceil, log2 print(ceil(log2(int(input()) + 1))) ```
89,271
Provide tags and a correct Python 3 solution for this coding contest problem. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Tags: constructive algorithms, greedy, math Correct Solution: ``` def mp(): return map(int, input().split()) n = int(input()) i = 1 while 2 ** i <= n: i += 1 print(i) ```
89,272
Provide tags and a correct Python 3 solution for this coding contest problem. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Tags: constructive algorithms, greedy, math Correct Solution: ``` #the idea which i thought first is correct import math x=int(input()) ans=math.floor(math.log(x,2))+1 print(ans) ```
89,273
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Submitted Solution: ``` import math , sys def lg(n): if n is 0: return 0; return lg(n//2) + 1 def main(): n = int(input()) print (lg(n)) main() ``` Yes
89,274
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Submitted Solution: ``` import math t=int(input()) if t==0: print("0") if t==1: print("1") if t<4 and t!=1 and t!=0: print("2") if t>3 and t <8: print("3") if t>7: log2 = int(math.log2(t)) print(log2+1) ``` Yes
89,275
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Submitted Solution: ``` n = int(input()) k = 0 while n // 2 != 0: n //= 2 k += 1 if n == 1: k += 1 print(k) ``` Yes
89,276
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Submitted Solution: ``` n = int(input()) i=0 while(n): i+=1 n = n//2 print(i) ``` Yes
89,277
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Submitted Solution: ``` n=int(input()) s=0;i=1; while(s<n): s+=i i+=1 print(i-1) ``` No
89,278
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Submitted Solution: ``` n=int(input()) s=0 c=0 for i in range(1,n+1): s+=i c+=1 if s>=n: break print(c) ``` No
89,279
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Submitted Solution: ``` import math n = int(input()) if int(math.sqrt(8 * n + 1)) * int(math.sqrt(8 * n + 1)) == 8 * n + 1: print((int)(math.sqrt(8 * n + 1) - 1) // 2) else: print(n) ``` No
89,280
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's. Find the minimum number of packets in such a distribution. Input The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have. Output Output a single integer — the minimum possible number of packets, satisfying the condition above. Examples Input 6 Output 3 Input 2 Output 2 Note In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6). * To get 1 use the packet with 1 coin. * To get 2 use the packet with 2 coins. * To get 3 use the packet with 3 coins. * To get 4 use packets with 1 and 3 coins. * To get 5 use packets with 2 and 3 coins * To get 6 use all packets. In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2). Submitted Solution: ``` n=int(input()) f=0 for i in range(1,n+1): if(i*(i+1)==2*n): print(i) break if(i*(i+1)>2*n): f=1 break if(f==1): print(n) ``` No
89,281
Provide tags and a correct Python 3 solution for this coding contest problem. Dark Assembly is a governing body in the Netherworld. Here sit the senators who take the most important decisions for the player. For example, to expand the range of the shop or to improve certain characteristics of the character the Dark Assembly's approval is needed. The Dark Assembly consists of n senators. Each of them is characterized by his level and loyalty to the player. The level is a positive integer which reflects a senator's strength. Loyalty is the probability of a positive decision in the voting, which is measured as a percentage with precision of up to 10%. Senators make decisions by voting. Each of them makes a positive or negative decision in accordance with their loyalty. If strictly more than half of the senators take a positive decision, the player's proposal is approved. If the player's proposal is not approved after the voting, then the player may appeal against the decision of the Dark Assembly. To do that, player needs to kill all the senators that voted against (there's nothing wrong in killing senators, they will resurrect later and will treat the player even worse). The probability that a player will be able to kill a certain group of senators is equal to A / (A + B), where A is the sum of levels of all player's characters and B is the sum of levels of all senators in this group. If the player kills all undesired senators, then his proposal is approved. Senators are very fond of sweets. They can be bribed by giving them candies. For each received candy a senator increases his loyalty to the player by 10%. It's worth to mention that loyalty cannot exceed 100%. The player can take no more than k sweets to the courtroom. Candies should be given to the senators before the start of voting. Determine the probability that the Dark Assembly approves the player's proposal if the candies are distributed among the senators in the optimal way. Input The first line contains three integers n, k and A (1 ≤ n, k ≤ 8, 1 ≤ A ≤ 9999). Then n lines follow. The i-th of them contains two numbers — bi and li — the i-th senator's level and his loyalty. The levels of all senators are integers in range from 1 to 9999 (inclusive). The loyalties of all senators are integers in range from 0 to 100 (inclusive) and all of them are divisible by 10. Output Print one real number with precision 10 - 6 — the maximal possible probability that the Dark Assembly approves the player's proposal for the best possible distribution of candies among the senators. Examples Input 5 6 100 11 80 14 90 23 70 80 30 153 70 Output 1.0000000000 Input 5 3 100 11 80 14 90 23 70 80 30 153 70 Output 0.9628442962 Input 1 3 20 20 20 Output 0.7500000000 Note In the first sample the best way of candies' distribution is giving them to first three of the senators. It ensures most of votes. It the second sample player should give all three candies to the fifth senator. Tags: brute force, probabilities Correct Solution: ``` import itertools as it def powerset(iterable): "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" s = list(iterable) return it.chain.from_iterable(it.combinations(s, r) \ for r in range(len(s)+1)) n, k, A = map(int, input().split()) best_result = 0 missing = 0 senators = [] for _ in range(n): level, loyality = map(int, input().split()) senators += [[level, loyality]] missing += 10 - loyality // 10 k = min([k, missing]) # no need for too many candies for comb in it.combinations_with_replacement(range(n), k): candies = [0] * n for senator, amount in [(x, len(list(y))) for x, y in it.groupby(comb)]: candies[senator] = amount overflow = False for senator in range(n): if candies[senator] * 10 + senators[senator][1] > 100: overflow = True if overflow: continue senators_tmp = [x[:] for x in senators] for senator in range(n): senators_tmp[senator][1] += candies[senator] * 10 cresult = 0 needed_to_win = n // 2 + 1 for sub_set in powerset(range(n)): prob = 1 bad_ones_str = 0 taken = [False] * n for i in sub_set: taken[i] = True for i in range(n): if taken[i]: prob *= senators_tmp[i][1] / 100 else: prob *= (100 - senators_tmp[i][1]) / 100 bad_ones_str += senators_tmp[i][0] if len(sub_set) >= needed_to_win: cresult += prob else: cresult += prob * A / (A + bad_ones_str) if cresult > best_result: best_result = cresult print(best_result) ```
89,282
Provide tags and a correct Python 3 solution for this coding contest problem. Dark Assembly is a governing body in the Netherworld. Here sit the senators who take the most important decisions for the player. For example, to expand the range of the shop or to improve certain characteristics of the character the Dark Assembly's approval is needed. The Dark Assembly consists of n senators. Each of them is characterized by his level and loyalty to the player. The level is a positive integer which reflects a senator's strength. Loyalty is the probability of a positive decision in the voting, which is measured as a percentage with precision of up to 10%. Senators make decisions by voting. Each of them makes a positive or negative decision in accordance with their loyalty. If strictly more than half of the senators take a positive decision, the player's proposal is approved. If the player's proposal is not approved after the voting, then the player may appeal against the decision of the Dark Assembly. To do that, player needs to kill all the senators that voted against (there's nothing wrong in killing senators, they will resurrect later and will treat the player even worse). The probability that a player will be able to kill a certain group of senators is equal to A / (A + B), where A is the sum of levels of all player's characters and B is the sum of levels of all senators in this group. If the player kills all undesired senators, then his proposal is approved. Senators are very fond of sweets. They can be bribed by giving them candies. For each received candy a senator increases his loyalty to the player by 10%. It's worth to mention that loyalty cannot exceed 100%. The player can take no more than k sweets to the courtroom. Candies should be given to the senators before the start of voting. Determine the probability that the Dark Assembly approves the player's proposal if the candies are distributed among the senators in the optimal way. Input The first line contains three integers n, k and A (1 ≤ n, k ≤ 8, 1 ≤ A ≤ 9999). Then n lines follow. The i-th of them contains two numbers — bi and li — the i-th senator's level and his loyalty. The levels of all senators are integers in range from 1 to 9999 (inclusive). The loyalties of all senators are integers in range from 0 to 100 (inclusive) and all of them are divisible by 10. Output Print one real number with precision 10 - 6 — the maximal possible probability that the Dark Assembly approves the player's proposal for the best possible distribution of candies among the senators. Examples Input 5 6 100 11 80 14 90 23 70 80 30 153 70 Output 1.0000000000 Input 5 3 100 11 80 14 90 23 70 80 30 153 70 Output 0.9628442962 Input 1 3 20 20 20 Output 0.7500000000 Note In the first sample the best way of candies' distribution is giving them to first three of the senators. It ensures most of votes. It the second sample player should give all three candies to the fifth senator. Tags: brute force, probabilities Correct Solution: ``` #!/usr/bin/env python3 n, k, A = map(int, input().rstrip().split()) senators = [] mx_bribe = 0 for i in range(n): lvl, loy = map(int, input().rstrip().split()) senators.append((lvl, loy)) mx_bribe += (100 - loy) // 10 bribe = [0] * n def calc(votes): bsum, cnt, p = 0, 0, 1.0 for i, s in enumerate(senators): if votes & (1 << i): p *= (s[1] + bribe[i]) / 100 cnt += 1 else: p *= (100 - s[1] - bribe[i]) / 100 bsum += s[0] if cnt > (n / 2): return p else: return p * A / (A + bsum) def dfs(cur, rk): if cur >= n: if rk > 0: return 0.0 sm = 0.0 for i in range(1 << n): sm += calc(i) return sm mx = 0.0 for i in range(rk + 1): if i * 10 + senators[cur][1] > 100: break bribe[cur] = i * 10 tmp = dfs(cur+1, rk-i) mx = max(tmp, mx) return mx print(dfs(0, min(k, mx_bribe))) ```
89,283
Provide tags and a correct Python 3 solution for this coding contest problem. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Tags: combinatorics, dp, math Correct Solution: ``` P = 998244353 def C(n, k): global P k = min(k, n-k) p = 1 for i in range(n, n-k, -1): p *= i for i in range(k, 0, -1): assert(p%i==0) p //= i return p%P n, m, k = map(int, input().split()) r = C(n-1, k) r = (r*m)%P for i in range(k): r = (r*(m-1))%P print(r) ```
89,284
Provide tags and a correct Python 3 solution for this coding contest problem. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Tags: combinatorics, dp, math Correct Solution: ``` def fact(i): if i == 0: return 1 if i < 0: return 0 res = 1 for t in range(1, i + 1): res *= t return res n, m, k = map(int, input().split(' ')) if k >= n: print(0) exit(0) print(fact(n - 1) // fact(k) // fact(n - k - 1) * m * (m - 1) ** k % 998244353) ```
89,285
Provide tags and a correct Python 3 solution for this coding contest problem. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Tags: combinatorics, dp, math Correct Solution: ``` from sys import stdin,stdout import math mod = 998244353 I = stdin.readline P = stdout.write n,m,k = map(int,I().split()) comb = ((math.factorial(n-1))//(math.factorial(n-1-k)))//(math.factorial(k)) comb%=mod power = pow(m-1,k,mod) comb*=power comb*=m comb%=mod P(str(comb)+"\n") ```
89,286
Provide tags and a correct Python 3 solution for this coding contest problem. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Tags: combinatorics, dp, math Correct Solution: ``` MOD = 998244353 def bpow(a, b): sol = 1 while b: if b&1: sol*=a if sol>=MOD: sol%=MOD a*=a if a>=MOD: a%=MOD b>>=1 return sol fac = [] fac += [1] for i in range(1,2001): fac.append(fac[i-1]*i%MOD) ifac = [] for i in range(0,2001): ifac.append(bpow(fac[i],MOD-2)) def nCr(a, b): return fac[a]*ifac[a-b]*ifac[b]%MOD n,m,k = map(int,input().split()) print (nCr(n-1,k)*m*bpow(m-1,k)%MOD) ```
89,287
Provide tags and a correct Python 3 solution for this coding contest problem. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Tags: combinatorics, dp, math Correct Solution: ``` """ this is a standard python template for codeforces task, repo: github.com/solbiatialessandro/pyComPro/codeforces """ stdin = lambda type_ = "int", sep = " ": list(map(eval(type_), input().split(sep))) joint = lambda sep = " ", *args: sep.join(str(i) if type(i) != list else sep.join(map(str, i)) for i in args) def binomial(n, k): """ A fast way to calculate binomial coefficients by Andrew Dalke. See http://stackoverflow.com/questions/3025162/statistics-combinations-in-python """ if 0 <= k <= n: ntok = 1 ktok = 1 for t in range(1, min(k, n - k) + 1): ntok *= n ktok *= t n -= 1 return ntok // ktok else: return 0 def solve(n, m, k): parts = m * pow(m - 1, k) return (binomial(n - 1, k) * parts) % 998244353 if __name__ == "__main__": """the solve(*args) structure is needed for testing purporses""" n, m, k = stdin() print(solve(n, m, k)) ```
89,288
Provide tags and a correct Python 3 solution for this coding contest problem. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Tags: combinatorics, dp, math Correct Solution: ``` s = input() s=s.split(' ') n = int(s[0]) m = int(s[1]) k = int(s[2]) ans = 1 if(k>0): for i in range(n-k, n): ans = ans * i for i in range(1, k + 1): ans = ans // i ans = ans * m for i in range(k): ans = ans * (m - 1) pass print(ans % 998244353) ```
89,289
Provide tags and a correct Python 3 solution for this coding contest problem. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Tags: combinatorics, dp, math Correct Solution: ``` MOD = 998244353 n, nColors, nDiff = map(int, input().split()) dp = [[0 for _ in range(nDiff + 1)] for _ in range(n)] dp[0][0] = nColors for size in range(1, n): for cnt in range(nDiff + 1): dp[size][cnt] += dp[size - 1][cnt] dp[size][cnt] %= MOD if cnt - 1 >= 0: dp[size][cnt] += (dp[size - 1][cnt - 1] * (nColors - 1)) % MOD dp[size][cnt] %= MOD print(dp[n - 1][nDiff]) ```
89,290
Provide tags and a correct Python 3 solution for this coding contest problem. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Tags: combinatorics, dp, math Correct Solution: ``` n, m, k = map(int, input().split()) dp = [[m if i == 0 else 0 for i in range(k + 2)] for j in range(n + 1)] dp[0][0] = 0 mod = 998244353 for i in range(1, n + 1): for j in range(1, k + 2): dp[i][j] = (dp[i - 1][j] + (m - 1) * dp[i - 1][j - 1]) % mod print(dp[n][k] % mod) ```
89,291
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Submitted Solution: ``` n,m,k = map(int, input().split()) import math print((math.factorial(n-1) // math.factorial(n-k-1) // math.factorial(k) * m * (m-1) ** k) % 998244353) ``` Yes
89,292
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Submitted Solution: ``` import math import bisect import itertools import sys I=lambda : sys.stdin.readline() mod=998244353 fact=[1]*20001 ifact=[1]*20001 for i in range(1,1001): fact[i]=((fact[i-1])*i)%mod ifact[i]=((ifact[i-1])*pow(i,mod-2,mod))%mod def ncr(n,r): return (((fact[n]*ifact[n-r])%mod)*ifact[r])%mod def npr(n,r): return (((fact[n]*ifact[n-r])%mod)) def mindiff(a): b=a[:] b.sort() m=10000000000 for i in range(len(b)-1): if b[i+1]-b[i]<m: m=b[i+1]-b[i] return m def lcm(a,b): return a*b//math.gcd(a,b) def merge(a,b): i=0;j=0 c=0 ans=[] while i<len(a) and j<len(b): if a[i]<b[j]: ans.append(a[i]) i+=1 else: ans.append(b[j]) c+=len(a)-i j+=1 ans+=a[i:] ans+=b[j:] return ans,c def mergesort(a): if len(a)==1: return a,0 mid=len(a)//2 left,left_inversion=mergesort(a[:mid]) right,right_inversion=mergesort(a[mid:]) m,c=merge(left,right) c+=(left_inversion+right_inversion) return m,c def is_prime(num): if num == 2: return True if num == 3: return True if num%2 == 0: return False if num%3 == 0: return False t = 5 a = 2 while t <= int(math.sqrt(num)): if num%t == 0: return False t += a a = 6 - a return True def ceil(a,b): if a%b==0: return a//b else: return (a//b + 1) def binsearch(arr,b,low,high): if low==high: return low if arr[math.ceil((low+high)/2)]<b: return binsearch(arr,b,low,math.ceil((low+high)/2) -1 ) else: return binsearch(arr,b,math.ceil((low+high)/2),high) def ncr1(n,r): s=1 for i in range(min(n-r,r)): s*=(n-i) s%=mod s*=pow(i+1,mod-2,mod) s%=mod return s def calc(n,m,r): s=0 for i in range(0,r+1,2): s+=ncr1(n,i)*ncr1(m,i) s%=mod return s #///////////////////////////////////////////////////////////////////////////////////////////////// mod1=998244353 t=int() n,m,k=map(int,input().split()) if (k>0 and m==1) or k>=n: print(0) elif k==0: print(m) else: k1=ncr1(n-1,k) k1*=pow(m-1,k,mod1) k1%=mod k1*=m print(k1%mod) ``` Yes
89,293
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Submitted Solution: ``` mod = 998244353 n, m, k = map(int, input().split()) a = pow(m-1, k, mod) * m % mod for i in range(n-k, n) : a = a * i for i in range(1, k+1) : a //= i print(a % mod) ``` Yes
89,294
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Submitted Solution: ``` n , m , k = map(int,input().split(' ')) # dp[i][j] <- no of ways of painting first i bricks such that j bricks are different from their left MOD = 998244353 dp = [[0]*(k+1) for _ in range(n)] dp[0][0] = m #base case for i in range(1,n) : for j in range(min(i+1,k+1)) : dp[i][j] = ( (0 if j==0 else (dp[i-1][j-1]*(m-1))%MOD) + dp[i-1][j])%MOD # print(dp) print(dp[-1][-1]) ``` Yes
89,295
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Submitted Solution: ``` # import collections, atexit, math, sys, bisect sys.setrecursionlimit(1000000) def getIntList(): return list(map(int, input().split())) try : #raise ModuleNotFoundError import numpy def dprint(*args, **kwargs): #print(*args, **kwargs, file=sys.stderr) # in python 3.4 **kwargs is invalid??? print(*args, file=sys.stderr) dprint('debug mode') except Exception: def dprint(*args, **kwargs): pass inId = 0 outId = 0 if inId>0: dprint('use input', inId) sys.stdin = open('input'+ str(inId) + '.txt', 'r') #标准输出重定向至文件 if outId>0: dprint('use output', outId) sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #标准输出重定向至文件 atexit.register(lambda :sys.stdout.close()) #idle 中不会执行 atexit N, M, K= getIntList() base = 998244353 J = N-1 - K # same Z = N - J dprint(Z) R = M for i in range(Z-1): R *= M-1 R%= base dprint(R) n0 = J m0 = Z dprint(n0,m0) #comb(n0 + m0 -1, m0-1) for i in range(m0 -1): g = n0 + m0 -1 -i R*= g R%= base def e_gcd(a, b ): if a==0 and b==0: return -1, 0, 0 if b==0: return a, 1,0 d, x,y = e_gcd(b, a%b ) y-= a //b *x return d , x,y def m_reverse(a,n): d,x,y = e_gcd(a,n) if d==1: if x%n<=0: return x%n+n else: return x%n else: return -1 for i in range(2, m0): t = m_reverse(i,base) R*=t R%=base print(R) ``` No
89,296
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Submitted Solution: ``` n,m,k = map(int, input().split()) dp = [[0 for _ in range(k+1)] for _ in range(n)] for i in range(n): dp[i][0] = m for i in range(1,n): for j in range(1,k+1): dp[i][j] = (m-1)*dp[i-1][j-1] + dp[i-1][j] dp[i][j] %= 998244354 #print(dp) print(dp[n-1][k]) ``` No
89,297
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Submitted Solution: ``` import math def colorful_bricks(): n, m, k = map(int, input().split()) comb = (math.factorial(int(n - 1)))/(math.factorial(int(k))*math.factorial(int(n - 1 - k))) print(comb * m * ((m - 1) ** k) % 998244353) colorful_bricks() ``` No
89,298
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353. Input The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integer — the number of ways to color bricks modulo 998 244 353. Examples Input 3 3 0 Output 3 Input 3 2 1 Output 4 Note In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings. <image> Submitted Solution: ``` # -*- coding: utf-8 -*- def fastpowmod(a,b,mod): if b==0: return 1 r=fastpowmod(a,int(b/2),mod) if b%2: return (r*r*a)% mod else: return (r*r)% mod def coefbinommod(n,k,mod): r=1.0 for i in range(k): r=r*(n-k+i+1)/(i+1) return int(r)%mod t=1 for i in range(t): X=input().split() n=int(X[0]) m=int(X[1]) k=int(X[2]) if k==0: print(m) else: if m==1: print(0) cd=(m*fastpowmod(m-1,k,998244353))%998244353 cd=(cd*coefbinommod(n-1,k,998244353))%998244353 print(cd) ``` No
89,299