text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces — a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≤ l ≤ |s|, 0 ≤ len ≤ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≤ T ≤ 100) — the number of testcases. Each of the next T lines contains the initial password s~(3 ≤ |s| ≤ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" → "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords — output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` T = int(input()) hs = [ {chr(x) for x in range(ord('0'), ord('9')+1, 1)}, {chr(x) for x in range(ord('a'), ord('z')+1, 1)}, {chr(x) for x in range(ord('A'), ord('Z')+1, 1)} ] hch = ['1','a','A'] for t in range(T): s = input() c = [0] * 3 for i in range(len(s)): if s[i] in hs[0]: c[0] += 1 elif s[i] in hs[1]: c[1] += 1 else: c[2] += 1 if c.count(0) == 0: print(s) continue ls = list(s.strip()) if c.count(0) == 2: ip = 0 for hi in range(3): if c[hi] == 0: ls[ip] = hch[hi] ip += 1 print(*ls, sep='') continue # c.count(0) == 1: iipos = 0 while c[iipos] < 2: iipos += 1 ip = 0 while ls[ip] not in hs[iipos]: ip += 1 ls[ip] = hch[c.index(0)] print(*ls, sep='') continue ``` Yes
15,100
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces — a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≤ l ≤ |s|, 0 ≤ len ≤ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≤ T ≤ 100) — the number of testcases. Each of the next T lines contains the initial password s~(3 ≤ |s| ≤ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" → "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords — output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` # -*- coding: utf-8 -*- # @Date : 2018-09-22 16:53:43 # @Author : raj lath (oorja.halt@gmail.com) # @Link : http://codeforces.com/contest/1051/problem/A # @Version : 1.0.0 from sys import stdin max_val=int(10e12) min_val=int(-10e12) def read_int() : return int(stdin.readline()) def read_ints() : return [int(x) for x in stdin.readline().split()] def read_str() : return input() def read_strs() : return [x for x in stdin.readline().split()] def get_id(a): c_id = 2 if "a" <= a <= "z": c_id = 0 if "A" <= a <= "Z": c_id = 1 return c_id nb_test = read_int() for _ in range(nb_test): replacement = ["a","A","1"] password = [x for x in input()] c_counts = [0] * 3 for i in password: c_counts[get_id(i)] += 1 for i,v in enumerate(password): ids = get_id(v) if c_counts[ids] > 1: for j in range(3): if not c_counts[j]: password[i] = replacement[j] c_counts[ids] -= 1 c_counts[j] += 1 break print(''.join(password)) ``` Yes
15,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces — a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≤ l ≤ |s|, 0 ≤ len ≤ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≤ T ≤ 100) — the number of testcases. Each of the next T lines contains the initial password s~(3 ≤ |s| ≤ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" → "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords — output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` for _ in [0]*int(input()): s=list(input()) l=[-1]*3 for i,c in enumerate(s): l[(c>'9')+(c>'Z')]=i for i in range(len(s)): j = (s[i]>'0')+(s[i]>'Z') if l[j]<0and s[i]not in l:s[i]='0Aa'[j];l[j]=i print(''.join(s)) ``` No
15,102
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces — a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≤ l ≤ |s|, 0 ≤ len ≤ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≤ T ≤ 100) — the number of testcases. Each of the next T lines contains the initial password s~(3 ≤ |s| ≤ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" → "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords — output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` def solve(s): tp = ['']*len(s) pat = {'a' , 'A' , '0'} for i , ch in enumerate(s): if ch.isupper(): tp[i] = 'A' elif ch.islower(): tp[i] = 'a' else: tp[i] = '0' st = set(tp) if len(st) == 3 : return s elif len(st) == 1: return ''.join(pat - st) + s[2 : ] else: #if tp[0] == tp[1]: return ''.join(pat - st) + s[1 : ] #else: #return s[:-1] + ''.join(pat - st) t = int(input()) for tt in range(t): s = input() print(solve(s)) ``` No
15,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces — a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≤ l ≤ |s|, 0 ≤ len ≤ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≤ T ≤ 100) — the number of testcases. Each of the next T lines contains the initial password s~(3 ≤ |s| ≤ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" → "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords — output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` for _ in range(int(input())): s = list(input()) low, up, dig = 0, 0, 0 for c in s: if c.isupper(): up += 1 elif c.islower(): low += 1 else: dig += 1 if up > 0 and low > 0 and dig > 0: print("".join(s)) else: if len(s) == low: s[0] = "1" s[1] = "A" elif len(s) == up: s[0] = "1" s[1] = "a" elif len(s) == dig: s[0] = "a" s[1] = "A" elif low == 0: if s[0].isupper and up > 1: s[0] = "a" elif s[0].isdigit() and dig > 1: s[0] = "a" else: s[1] = "a" elif up == 0: if s[0].islower and low > 1: s[0] = "A" elif s[0].isdigit() and dig > 1: s[0] = "A" else: s[1] = "A" else: #dig == 0 if s[0].isupper and up > 1: s[0] = "1" elif s[0].islower() and lower > 1: s[0] = "1" else: s[1] = "1" print("".join(s)) ``` No
15,104
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces — a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≤ l ≤ |s|, 0 ≤ len ≤ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≤ T ≤ 100) — the number of testcases. Each of the next T lines contains the initial password s~(3 ≤ |s| ≤ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" → "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords — output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` from sys import stdin input = stdin.readline def is_good(arr): digit = False upper = False lower = False for c in arr: if c.isdigit(): digit = True elif c.isupper(): upper = True else: lower = True return (digit and upper and lower) def solve(): s = list(input().rstrip()) # length 0 if is_good(s): print(*s, sep="") return # length 1 for i in range(len(s)): x = s[i] s[i] = "1" if is_good(s): print(*s, sep="") return s[i] = "a" if is_good(s): print(*s, sep="") return s[i] = "A" if is_good(s): print(*s, sep="") return s[i] = x # length 2 for i in range(len(s)-1): x = s[i] y = s[i+1] s[i] = "1" s[i] = "a" if is_good(s): print(*s, sep="") return s[i] = "1" s[i] = "A" if is_good(s): print(*s, sep="") return s[i] = "a" s[i] = "A" if is_good(s): print(*s, sep="") return s[i] = x s[i] = y t = int(input()) for _ in range(t): solve() ``` No
15,105
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Tags: implementation, math Correct Solution: ``` def inint(): return int(input()) def inlist(): return list(map(int,input().split())) def main(): n=inint() a=inlist() b=inlist() i=0;j=0 ans={} for k in range(n): ans[a[k]]=k sol=[] #print(ans) for k in b: j=ans[k] #print(j,k,i) if j<i:sol.append(0) else:sol.append(j-i+1);i=j+1 print(*sol) if __name__ == "__main__": #import profile #profile.run("main()") main() ```
15,106
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Tags: implementation, math Correct Solution: ``` from collections import deque n = int(input()) stack = deque([int(p) for p in input().split()]) b = [int(p) for p in input().split()] st = set(stack) for i in range(len(b)): count = 0 if b[i] not in st: print(0) continue if stack: while True: bk = stack.popleft() st.remove(bk) count += 1 if bk == b[i]: print(count) break else: print(0) ```
15,107
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Tags: implementation, math Correct Solution: ``` if __name__ == "__main__": n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) L = [0]*(n+1) for i in range(n): L[a[i]] = i+1 ind = 0 for i in range(n): if L[b[i]] > ind: print(L[b[i]]-ind,end=' ') ind = L[b[i]] else: print(0,end=' ') ```
15,108
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Tags: implementation, math Correct Solution: ``` n=int(input()) arr=list(map(int,input().strip().split(' '))) arr1=list(map(int,input().strip().split(' '))) mp={} for i in range(n): mp[arr[i]]=i mx=-1 for i in range(n): if(mx>=mp[arr1[i]]): print(0,end=' ') else: print(mp[arr1[i]]-mx,end=' ') mx=max(mx,mp[arr1[i]]) print() ```
15,109
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Tags: implementation, math Correct Solution: ``` I=lambda:map(int,input().split()) I() a=*I(), b=I() s={0} i=0 r=[] for x in b: if x in s: r+=[0] continue c=i while a[i]!=x:s|={a[i]};i+=1 s|={a[i]} i+=1 r+=[i-c] print(*r) ```
15,110
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Tags: implementation, math Correct Solution: ``` def read(): return int(input()) def readlist(): return list(map(int, input().split())) def readmap(): return map(int, input().split()) n = read() A = readlist() B = readlist() S = set() j = 0 ans = [] for i in range(n): if B[i] in S: ans.append(0) else: ansi = 0 while not B[i] in S: S.add(A[j]) j += 1 ansi += 1 ans.append(ansi) print(" ".join(list(map(str, ans)))) ```
15,111
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Tags: implementation, math Correct Solution: ``` n = input() a = input().split() b = input().split() d = {el: i for i, el in enumerate(a)} diff_b = [] prev_number = 0 for i, el in enumerate(b): f = d[el] if prev_number < i: t = max(f - i + 1, 0) else: t = max(f - prev_number + 1, 0) prev_number += t diff_b.append(str(t)) print(' '.join(diff_b)) ```
15,112
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Tags: implementation, math Correct Solution: ``` n = int(input()) a_trans = [n + 1] * (n + 1) i = 1 for ai in input().split(): a_trans[int(ai)] = i i += 1 min_unt_ake_n = 1 res = [] for bi in input().split(): atb_i = a_trans[int(bi)] if atb_i < min_unt_ake_n: res.append('0') else: nex = atb_i + 1 res.append(nex - min_unt_ake_n) min_unt_ake_n = nex print(*res) ```
15,113
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Submitted Solution: ``` n = int(input()) ai = [int(a) for a in input().split()] bi = [int(a) for a in input().split()] result = [] current_idx = 0 backpack = [False] * (n + 1) for b in bi: if backpack[b]: result.append(0) continue nbooks = 0 while ai[current_idx] != b: backpack[ai[current_idx]] = True nbooks += 1 current_idx += 1 backpack[ai[current_idx]] = True nbooks += 1 current_idx += 1 result.append(nbooks) print(" ".join([str(x) for x in result])) ``` Yes
15,114
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) visited=[False for i in range(n)] d={a[i]:i for i in range(n)} #print(d) for i in b: x=d[i] if visited[x]==True: print(0,end=" ") else: ans=0 while x>=0 and visited[x]==False: ans=ans+1 visited[x]=True x=x-1 print(ans,end=" ") ``` Yes
15,115
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Submitted Solution: ``` def main(): n = int(input()) idx = [None] * (n+1) maxidx = 0 nums = list(map(int, input().split())) for index, n in enumerate(nums): idx[n] = index+1 queries = list(map(int, input().split())) for q in queries: if idx[q] > maxidx: # move idx[q] things # print("idx[", q, "] = ", idx[q]) print(idx[q] - maxidx, end=" ") maxidx = idx[q] # print("max = ", maxidx) else: print(0, end=" ") print() main() ``` Yes
15,116
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) d={} for k in range(len(a)): d[a[k]]=k+1 b=map(int,input().split()) s=0 for i in b: if d[i]>s: print(d[i]-s,end=' ') s=d[i] else: print(0,end=' ') ``` Yes
15,117
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Submitted Solution: ``` n = int(input()) pilha = input().split(" ") ordem = input().split(" ") limite = 0 ans="" for i in range(len(ordem)): for j in range(i,len(pilha)): if(pilha[j] == ordem[i]): ans+=str((j+1-limite)) + " " limite = j+1 teste= True break if(teste ==True and i+1<n): ans+="0 " teste = False ans = ans[:-1] print(ans) ``` No
15,118
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Submitted Solution: ``` N = int(input()) steps = [] if (0 < N < 2*(10**5)): a = input() if (0 < len(a) <= N + len(a) / 2): a = a.split(" ") b = input() if (0 < len(b) <= N + len(b) / 2): b = b.split(" ") for i in b: j = a.index(i) k = b.index(i) if ((j - k) < 0): steps.append(str(0)) else: steps.append(str(j-k+1)) print(" ".join(steps)) ``` No
15,119
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) c=[0]*n index=0 k=0 sum=0 for i in range(n): flag=True for j in range(k,n): flag=False if(c[b[i]-1]==1): print(0,end=" ") k+=1 break if b[i]==a[j]: print(j+1-sum,end=" ") k=j sum+=(j+1-sum) break else: c[a[j]-1]=1 if(flag): print(0,end=" ") ``` No
15,120
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has got n books, numbered from 1 to n, arranged in a stack. The topmost book has number a_1, the next one — a_2, and so on. The book at the bottom of the stack has number a_n. All numbers are distinct. Vasya wants to move all the books to his backpack in n steps. During i-th step he wants to move the book number b_i into his backpack. If the book with number b_i is in the stack, he takes this book and all the books above the book b_i, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1, 2, 3] (book 1 is the topmost), and Vasya moves the books in the order [2, 1, 3], then during the first step he will move two books (1 and 2), during the second step he will do nothing (since book 1 is already in the backpack), and during the third step — one book (the book number 3). Note that b_1, b_2, ..., b_n are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer n~(1 ≤ n ≤ 2 ⋅ 10^5) — the number of books in the stack. The second line contains n integers a_1, a_2, ..., a_n~(1 ≤ a_i ≤ n) denoting the stack of books. The third line contains n integers b_1, b_2, ..., b_n~(1 ≤ b_i ≤ n) denoting the steps Vasya is going to perform. All numbers a_1 ... a_n are distinct, the same goes for b_1 ... b_n. Output Print n integers. The i-th of them should be equal to the number of books Vasya moves to his backpack during the i-th step. Examples Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books [3, 1, 4]. After that only books 2 and 5 remain in the stack (2 is above 5). During the second step Vasya will take the books 2 and 5. After that the stack becomes empty, so during next steps Vasya won't move any books. Submitted Solution: ``` n=int(input()) a=[int(x) for x in input().split()] b=[int(x) for x in input().split()] ans=[] y=-1 for intg in b: if intg not in a[y+1:]: ans.append(0) else: x=a[y+1:].index(intg) ans.append(x+1) y=x print(*ans) ``` No
15,121
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Tags: implementation Correct Solution: ``` n = int(input()) a = sorted([*map(int, input().split())]) print (min(a[-1] - a[1], a[-2] - a[0])) ```
15,122
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Tags: implementation Correct Solution: ``` n=int(input()) arr=list(map(int,input().split())) arr.sort() left=arr[1]-arr[0] right=arr[-1]-arr[-2] if(right>left): print(arr[-2]-arr[0]) elif(left>right): print(arr[-1]-arr[1]) elif(left==right): print(arr[-1]-arr[1]) ```
15,123
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Tags: implementation Correct Solution: ``` x = int(input()) m = [] for i in input().split(): m.append(int(i)) min = m[0] max = m[0] for i in range(x): if m[i] < min: min = m[i] if m[i] > max: max = m[i] m.remove(min) x -= 1 min2 = m[0] for i in range(x): if min2 > m[i]: min2 = m[i] m.append(min) m.remove(max) max2 = m[0] for i in range(x): if max2 < m[i]: max2 = m[i] r = max - min if max2 - min < r: r = max2 - min if max - min2 < r: r = max - min2 print(r) ```
15,124
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Tags: implementation Correct Solution: ``` n = int(input()) m_list = list(map(int, input().split())) m_min = min(m_list) m_max = max(m_list) m_list.remove(m_min) m_list.remove(m_max) if len(m_list) == 0: print(0) else: new_min = min(m_list) new_max = max(m_list) print(min(m_max - new_min, new_max - m_min)) ```
15,125
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Tags: implementation Correct Solution: ``` n=int(input()) lis=list(map(int,input().split())) lis.sort() ans=min(lis[n-2]-lis[0],lis[n-1]-lis[1]) print(ans) ```
15,126
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Tags: implementation Correct Solution: ``` def stabilizirovanie(lst): if len(lst) == 2: return 0 a = sorted(lst) return min(a[len(lst) - 1] - a[1], a[len(lst) - 2] - a[0]) n = int(input()) b = [int(i) for i in input().split()] print(stabilizirovanie(b)) ```
15,127
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Tags: implementation Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) b = a.copy() c = a.copy() b.remove(max(b)) c.remove(min(c)) print(min(max(b)-min(b),max(c)-min(c))) ```
15,128
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Tags: implementation Correct Solution: ``` from __future__ import division, print_function import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def main(): pass # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() n=int(input()) arr=[int(x) for x in input().split()] arr.sort() print(min(arr[len(arr)-2]-arr[0],arr[len(arr)-1]-arr[1])) ```
15,129
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) a.sort() b=[] x=a[-2]-a[0] b.append(x) x=a[-1]-a[1] b.append(x) print(min(b)) ``` Yes
15,130
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Submitted Solution: ``` import sys t = int(input()) tab = list(map(int, sys.stdin.readline().split())) tab.sort(reverse=True) print(min(tab[1]-tab[t-1], tab[0]-tab[t-2])) ``` Yes
15,131
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Submitted Solution: ``` n,a=int(input()),list(map(int,input().split())) ma=max(a) mi=min(a) a.sort() x=min((a[n-2]-mi),(ma-a[1])) if x<0: print(0) else: print(x) ``` Yes
15,132
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) a.sort() if (a[1]-a[0] < a[n-1]-a[n-2]): #remove last ans = a[n-2]-a[0] else: #remove first ans = a[n-1]-a[1] print(ans) ``` Yes
15,133
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Submitted Solution: ``` n = int(input()) l = list(map(int, input().split())) sorted(l) print(min(l[n-1] - l[1], l[n-2] - l[0])) ``` No
15,134
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Submitted Solution: ``` #529_B l = int(input()) ln = [int(i) for i in input().split(" ")] mn = 10000000 mn2 = 10000000 omn = mn mx = 0 mx2 = 0 omx = mx for i in range(0, len(ln)): if ln[i] < mn: mn2 = mn mn = ln[i] elif ln[i] < mn2: mn2 = ln[i] if ln[i] > mx: mx2 = mx mx = ln[i] elif ln[i] > mx2: mx2 = ln[i] print(mx, mx2, mn2, mn) if len(ln) == 2: print(0) elif mx2 - mn < mx - mn2: print(mx2 - mn) else: print(mx - mn2) ``` No
15,135
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) l.sort() del l[-1] print(l[n-2]-l[0]) ``` No
15,136
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integer numbers. Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i. You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability. Input The first line of the input contains one integer n (2 ≤ n ≤ 10^5) — the number of elements in the array a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — elements of the array a. Output Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array a. Examples Input 4 1 3 3 7 Output 2 Input 2 1 100000 Output 0 Note In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2. In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) l.sort() l=l[0:len(l)-1] print(max(l)-min(l)) ``` No
15,137
Provide tags and a correct Python 3 solution for this coding contest problem. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Tags: brute force, implementation, math, number theory Correct Solution: ``` N, B = [int(x) for x in input().split()] v = [] for i in range(2, 10**6 + 1000): while B % i == 0: v.append(i) B //= i if B != 1: v.append(B) def zeros(p, v, N): res = 0 pk = p while pk <= N: res += N // pk pk *= p res //= v.count(p) return res print(min(zeros(x, v, N) for x in v)) ```
15,138
Provide tags and a correct Python 3 solution for this coding contest problem. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Tags: brute force, implementation, math, number theory Correct Solution: ``` # AC import sys class Main: def __init__(self): self.buff = None self.index = 0 def next(self): if self.buff is None or self.index == len(self.buff): self.buff = self.next_line() self.index = 0 val = self.buff[self.index] self.index += 1 return val def next_line(self, _map=str): return list(map(_map, sys.stdin.readline().split())) def next_int(self): return int(self.next()) def solve(self): n, b = self.next_line(int) rs = -1 for i in range(2, 1000001): if b % i == 0: ct = 0 while b % i == 0: b //= i ct += 1 t = self.cal(n, i) tt = t // ct if rs == -1 or tt < rs: rs = tt if b > 1: t = self.cal(n, b) if rs == -1 or t < rs: rs = t print(rs) def cal(self, n, k): return 0 if n == 0 else n // k + self.cal(n // k, k) if __name__ == '__main__': Main().solve() ```
15,139
Provide tags and a correct Python 3 solution for this coding contest problem. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Tags: brute force, implementation, math, number theory Correct Solution: ``` n,b = list(map(int,input().split())) primes = [] def seive(): nn = 1000100 vis = [False] * nn for i in range(4,nn,2): vis[i] = True i = 3 vis[0],vis[1] = True, True while(i*i<nn): if(not vis[i]): j = i*i while(j<nn): vis[j] = True j += 2*i i+=2 for i in range(nn): if(not vis[i]): primes.append(i) #print(len(primes)) seive() def find_factors(nn): ans = [] for i in primes: if(nn%i==0): count = 0 while(nn%i==0): count+=1 nn/=i ans.append((i,count)) if nn > 1: ans.append((nn,1)) return ans f = find_factors(b) def find_ans(t): ff,no = t ans = 0 temp = ff while(n//ff): ans += n//ff ff *= temp return ans//no final_ans = find_ans(f[0]) for i in range(1,len(f)): final_ans = min(final_ans, find_ans(f[i])) print(int(final_ans)) ```
15,140
Provide tags and a correct Python 3 solution for this coding contest problem. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Tags: brute force, implementation, math, number theory Correct Solution: ``` n,b=map(int,input().split()) i=2 nn=n fact_b=[] dict_b={} count=0 while(i*i<=b): count=0 if(b%i==0): while(b%i==0): count+=1 b//=i fact_b.append(i) dict_b[i]=count i+=1 if(b>1): fact_b.append(b) dict_b[b]=1 pow=1 add=0 dict_n={} for i in fact_b: pow=1 add=0 while(nn//i**pow>0): add+=nn//i**pow pow+=1 dict_n[i]=add mini=99999999999999999999999999 for i in fact_b: mini=min(mini,dict_n[i]//dict_b[i]) print(mini) ```
15,141
Provide tags and a correct Python 3 solution for this coding contest problem. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Tags: brute force, implementation, math, number theory Correct Solution: ``` # -*- coding: utf-8 -*- import math import collections import bisect import heapq import time import itertools import sys """ created by shhuan at 2019/2/12 19:21 """ def decomp(val): d = 2 ans = collections.defaultdict(int) while d <= math.sqrt(val): if val % d == 0: while val % d == 0: ans[d] += 1 val //= d d += 1 if val > 1: ans[val] += 1 return ans N, B = map(int, input().split()) comps = decomp(B) ans = float('inf') for y in comps.keys(): z = y x = 0 while z <= N: x += N // z z *= y ans = min(ans, x // comps[y]) print(ans) ```
15,142
Provide tags and a correct Python 3 solution for this coding contest problem. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Tags: brute force, implementation, math, number theory Correct Solution: ``` x=input().split(' ') n=int(x[0]) m=int(x[1]) factors=[] factorCount = {} ctr=0 while m%2==0: m=m//2 ctr=ctr+1 if ctr>0: factors.append(2) factorCount[2]=ctr i=3 while i*i<=m: ctr=0 while m%i==0: m=m//i ctr=ctr+1 if ctr>0: factors.append(i) factorCount[i]=ctr i=i+2 if m>1: factors.append(m) factorCount[m]=1 i=0 mini=0 while i<len(factors): f=factors[i] fc=factorCount[f] ctr=0 tf=f while n//tf>0: ctr=ctr+n//tf tf=tf*f if i==0: mini=ctr//fc else: mini=min(mini,ctr//fc) i=i+1 print(mini) ```
15,143
Provide tags and a correct Python 3 solution for this coding contest problem. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Tags: brute force, implementation, math, number theory Correct Solution: ``` n, b = map(int, input().split()) b2 = b factors = [] for i in range(2, b2): if i * i > b2: break c = 0 while b2 % i == 0: c += 1 b2 //= i if c > 0: factors.append((i, c)) if b2 > 1: factors.append((b2, 1)) zero_count = n**3 for f, div in factors: count = 0 f2 = f while f2 <= n: count += n // f2 f2 *= f zero_count = min(zero_count, count // div) print(zero_count) ```
15,144
Provide tags and a correct Python 3 solution for this coding contest problem. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Tags: brute force, implementation, math, number theory Correct Solution: ``` n,b = map(int,input().split()) import math x = int(math.sqrt(b)) fa =dict() for i in range(2,x+2): while b % i == 0: fa[i] = fa.get(i,0)+1 b = b // i if b != 1: fa[b]=fa.get(b,0)+1 ans = int(1e18) for f in fa: t = 0 tem = n while tem > 0: tem //= f t += tem ans = min(ans,t // fa[f]) print(ans) ```
15,145
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Submitted Solution: ``` import math from collections import Counter n, b = [int(i) for i in input().split()] def primeFactors(n): c = Counter() while n % 2 == 0: c[2] += 1 n //= 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i == 0: c[i] += 1 n //= i if n > 2: c[n] += 1 return c m = Counter() c = primeFactors(b) def largestPower(n, p): # Initialize result x = 0 # Calculate x = n/p + n/(p^2) + n/(p^3) + .... while n: n //= p x += n return x for key in c.keys(): m[key] = largestPower(n, key) factor_multiples = [mv // cv for mv, cv in zip(m.values(), c.values())] if not factor_multiples: print(0) else: print(min(factor_multiples)) ``` Yes
15,146
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Submitted Solution: ``` def prime_factors(n): i = 2 factors = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(i) if n > 1: factors.append(n) return factors n,b=map(int,input().split()) prime=prime_factors(b) minimum=10**18 for i in set(prime): total = 0 num = prime.count(i) temp = n while temp != 0: temp //= i total += temp total //= num if total<minimum: minimum=total print(minimum) ``` Yes
15,147
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Submitted Solution: ``` import math n, b = map(int, input().split()) m = b dic_prime = dict() while m % 2 == 0: m = m // 2 c = dic_prime.get(2, 0) dic_prime[2] = c + 1 for i in range(3, int(math.sqrt(m) + 1), 2): while m % i == 0: m = m // i c = dic_prime.get(i, 0) dic_prime[i] = c + 1 if m > 2: dic_prime[m] = 1 count_zeros = n for p in list(dic_prime.keys()): m = n count_p = m // p x = p * p while m > x: count_p += m // x x = x * p co = count_p // dic_prime[p] if count_zeros > co: count_zeros = co print(count_zeros) ``` Yes
15,148
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Submitted Solution: ``` # @oj: codeforces # @id: hitwanyang # @email: 296866643@qq.com # @date: 2020-11-10 17:10 # @url:https://codeforc.es/contest/1114/problem/C import sys,os from io import BytesIO, IOBase import collections,itertools,bisect,heapq,math,string from decimal import * # region fastio BUFSIZE = 8192 BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ ## 注意嵌套括号!!!!!! ## 先有思路,再写代码,别着急!!! ## 先有朴素解法,不要有思维定式,试着换思路解决 ## 精度 print("%.10f" % ans) ## sqrt:int(math.sqrt(n))+1 ## 字符串拼接不要用+操作,会超时 ## 二进制转换:bin(1)[2:].rjust(32,'0') ## array copy:cur=array[::] ## oeis:example 1, 3, _, 1260, _, _, _, _, _, 12164510040883200 def main(): n,b=map(int,input().split()) ans=0 d=collections.defaultdict(lambda:0) # 求b的质因数集合 i=2 while i*i<=b: while b%i==0: d[i]+=1 b=b//i i+=1 if b!=1: d[b]+=1 # print (d) ans=10**18+1 for k in d.keys(): cnt=0 # 求数n的质因数k的个数 tmp=n while tmp>=k: cnt+=tmp//k tmp=tmp//k ans=min(ans,cnt//d[k]) print (ans) if __name__ == "__main__": main() ``` Yes
15,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Submitted Solution: ``` input = __import__('sys').stdin.readline print = __import__('sys').stdout.write n, b = map(int, input().split()) a = [True] * (int(b ** 0.5) + 4) a[0] = False a[1] = False p = [] length = len(a) for i in range(2, length): if a[i]: p.append(i) for j in range(2 * i, length, i): a[j] = False l = {} idx = 0 try: while b != 1: if b % p[idx] == 0: b /= p[idx] if p[idx] not in l: l[p[idx]] = 1 else: l[p[idx]] += 1 else: idx += 1 except IndexError: l[b] = 1 max_key = max(l.keys()) current_key = max_key ans = 0 while current_key <= n: ans += n // current_key current_key *= max_key print(str(int(ans // l[max_key]))) ``` No
15,150
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Submitted Solution: ``` def fastio(): import sys from io import StringIO from atexit import register global input sys.stdin = StringIO(sys.stdin.read()) input = lambda : sys.stdin.readline().rstrip('\r\n') sys.stdout = StringIO() register(lambda : sys.__stdout__.write(sys.stdout.getvalue())) fastio() MOD = 10**9 + 7 I = lambda:list(map(int, input().split())) from math import * n, b = I() p = [1] * (1000001) for i in range(2, 1000001): if p[i]: for j in range(2*i, 1000001, i): p[j] = 0 d = b for i in range(2, 1000001): if b%i == 0 and p[i]: d = i s = d ans = 0 while d <= n: ans += n//d d = d*s t = s power = 1 while t < b: t *= s power += 1 if t == b: ans //= power print(ans) ``` No
15,151
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Submitted Solution: ``` # -*- coding: utf-8 -*- import sys, re from collections import deque, defaultdict, Counter from math import sqrt, hypot, factorial, pi, sin, cos, radians if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd from heapq import heappop, heappush, heapify, heappushpop from bisect import bisect_left, bisect_right from itertools import permutations, combinations, product from operator import itemgetter, mul from copy import deepcopy from functools import reduce, partial from fractions import Fraction from string import ascii_lowercase, ascii_uppercase, digits 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(a, b=1): return int(-(-a // b)) def round(x): return int((x*2+1) // 2) def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD def lcm(x, y): return (x * y) // gcd(x, y) def lcm_list(nums): return reduce(lcm, nums, 1) def gcd_list(nums): return reduce(gcd, nums, nums[0]) 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 def Base_10_to_n(X, n): if (int(X/n)): return Base_10_to_n(int(X/n), n)+str(X%n) return str(X%n) n, b = MAP() ans = Base_10_to_n(factorial(n), b) cnt = 0 for num in ans[::-1]: if num == '0': cnt += 1 else: break print(cnt) ``` No
15,152
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n ! ([factorial](https://en.wikipedia.org/wiki/Factorial) of n). Input The only line of the input contains two integers n and b (1 ≤ n ≤ 10^{18}, 2 ≤ b ≤ 10^{12}). Output Print an only integer — the number of trailing zero digits in the b-ary representation of n! Examples Input 6 9 Output 1 Input 38 11 Output 3 Input 5 2 Output 3 Input 5 10 Output 1 Note In the first example, 6!_{(10)} = 720_{(10)} = 880_{(9)}. In the third and fourth example, 5!_{(10)} = 120_{(10)} = 1111000_{(2)}. The representation of the number x in the b-ary base is d_1, d_2, …, d_k if x = d_1 b^{k - 1} + d_2 b^{k - 2} + … + d_k b^0, where d_i are integers and 0 ≤ d_i ≤ b - 1. For example, the number 720 from the first example is represented as 880_{(9)} since 720 = 8 ⋅ 9^2 + 8 ⋅ 9 + 0 ⋅ 1. You can read more about bases [here](https://en.wikipedia.org/wiki/Radix). Submitted Solution: ``` def fastio(): import sys from io import StringIO from atexit import register global input sys.stdin = StringIO(sys.stdin.read()) input = lambda : sys.stdin.readline().rstrip('\r\n') sys.stdout = StringIO() register(lambda : sys.__stdout__.write(sys.stdout.getvalue())) fastio() MOD = 10**9 + 7 I = lambda:list(map(int, input().split())) from math import * n, b = I() p = [1] * (100001) for i in range(2, 100001): if p[i]: for j in range(2*i, 100001, i): p[j] = 0 d = {} t = b hehe = 1 for i in range(2, 100001): count = 0 while t % i == 0 and p[i]: count += 1 t //= i hehe *= pow(i, count) if count: d[i] = count # d[b//hehe] = 1 ad = {} for i in d: t = i c = 0 while n//t: c += n//t t *= i ad[i] = c ans = MOD for i in d: ans = min(ans, ad[i]) # print(ad) print(ans) # s = d # ans = 0 # while d <= n: # ans += n//d # d = d*s # print(ans) ``` No
15,153
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Traveling around the world you noticed that many shop owners raise prices to inadequate values if the see you are a foreigner. You define inadequate numbers as follows: * all integers from 1 to 9 are inadequate; * for an integer x ≥ 10 to be inadequate, it is required that the integer ⌊ x / 10 ⌋ is inadequate, but that's not the only condition. Let's sort all the inadequate integers. Let ⌊ x / 10 ⌋ have number k in this order. Then, the integer x is inadequate only if the last digit of x is strictly less than the reminder of division of k by 11. Here ⌊ x / 10 ⌋ denotes x/10 rounded down. Thus, if x is the m-th in increasing order inadequate number, and m gives the remainder c when divided by 11, then integers 10 ⋅ x + 0, 10 ⋅ x + 1 …, 10 ⋅ x + (c - 1) are inadequate, while integers 10 ⋅ x + c, 10 ⋅ x + (c + 1), …, 10 ⋅ x + 9 are not inadequate. The first several inadequate integers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 30, 31, 32 …. After that, since 4 is the fourth inadequate integer, 40, 41, 42, 43 are inadequate, while 44, 45, 46, …, 49 are not inadequate; since 10 is the 10-th inadequate number, integers 100, 101, 102, …, 109 are all inadequate. And since 20 is the 11-th inadequate number, none of 200, 201, 202, …, 209 is inadequate. You wrote down all the prices you have seen in a trip. Unfortunately, all integers got concatenated in one large digit string s and you lost the bounds between the neighboring integers. You are now interested in the number of substrings of the resulting string that form an inadequate number. If a substring appears more than once at different positions, all its appearances are counted separately. Input The only line contains the string s (1 ≤ |s| ≤ 10^5), consisting only of digits. It is guaranteed that the first digit of s is not zero. Output In the only line print the number of substrings of s that form an inadequate number. Examples Input 4021 Output 6 Input 110 Output 3 Note In the first example the inadequate numbers in the string are 1, 2, 4, 21, 40, 402. In the second example the inadequate numbers in the string are 1 and 10, and 1 appears twice (on the first and on the second positions). Submitted Solution: ``` from math import floor l=[1,2,3,4,5,6,7,8,9] def a(x): if x>=1 and x<=9: return 1 if x>=10: if a(x//10) and (l.index(x//10)+1)%11>x%10: return 1 else: return 0 for i in range(10,100000): if a(i): l.append(i) count=0 an=int(input()) if an==3414031055: print (20) else: an=str(an) i=0 while(i<len(an)): for j in range(i+1,len(an)+1): if an[i:j][0]!='0' and int(an[i:j]) in l: count+=1 i+=1 print (count) ``` No
15,154
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Traveling around the world you noticed that many shop owners raise prices to inadequate values if the see you are a foreigner. You define inadequate numbers as follows: * all integers from 1 to 9 are inadequate; * for an integer x ≥ 10 to be inadequate, it is required that the integer ⌊ x / 10 ⌋ is inadequate, but that's not the only condition. Let's sort all the inadequate integers. Let ⌊ x / 10 ⌋ have number k in this order. Then, the integer x is inadequate only if the last digit of x is strictly less than the reminder of division of k by 11. Here ⌊ x / 10 ⌋ denotes x/10 rounded down. Thus, if x is the m-th in increasing order inadequate number, and m gives the remainder c when divided by 11, then integers 10 ⋅ x + 0, 10 ⋅ x + 1 …, 10 ⋅ x + (c - 1) are inadequate, while integers 10 ⋅ x + c, 10 ⋅ x + (c + 1), …, 10 ⋅ x + 9 are not inadequate. The first several inadequate integers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 30, 31, 32 …. After that, since 4 is the fourth inadequate integer, 40, 41, 42, 43 are inadequate, while 44, 45, 46, …, 49 are not inadequate; since 10 is the 10-th inadequate number, integers 100, 101, 102, …, 109 are all inadequate. And since 20 is the 11-th inadequate number, none of 200, 201, 202, …, 209 is inadequate. You wrote down all the prices you have seen in a trip. Unfortunately, all integers got concatenated in one large digit string s and you lost the bounds between the neighboring integers. You are now interested in the number of substrings of the resulting string that form an inadequate number. If a substring appears more than once at different positions, all its appearances are counted separately. Input The only line contains the string s (1 ≤ |s| ≤ 10^5), consisting only of digits. It is guaranteed that the first digit of s is not zero. Output In the only line print the number of substrings of s that form an inadequate number. Examples Input 4021 Output 6 Input 110 Output 3 Note In the first example the inadequate numbers in the string are 1, 2, 4, 21, 40, 402. In the second example the inadequate numbers in the string are 1 and 10, and 1 appears twice (on the first and on the second positions). Submitted Solution: ``` from math import floor l=[1,2,3,4,5,6,7,8,9] def a(x): if x>=1 and x<=9: return 1 if x>=10: if a(x//10) and (l.index(x//10)+1)%11>x%10: return 1 else: return 0 for i in range(10,100000): if a(i): l.append(i) count=0 a=int(input()) a=str(a) for i in range(0,len(a)): for j in range(i+1,len(a)+1): if a[i:j][0]!="0" and int(a[i:j]) in l: count+=1 print (count) ``` No
15,155
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Traveling around the world you noticed that many shop owners raise prices to inadequate values if the see you are a foreigner. You define inadequate numbers as follows: * all integers from 1 to 9 are inadequate; * for an integer x ≥ 10 to be inadequate, it is required that the integer ⌊ x / 10 ⌋ is inadequate, but that's not the only condition. Let's sort all the inadequate integers. Let ⌊ x / 10 ⌋ have number k in this order. Then, the integer x is inadequate only if the last digit of x is strictly less than the reminder of division of k by 11. Here ⌊ x / 10 ⌋ denotes x/10 rounded down. Thus, if x is the m-th in increasing order inadequate number, and m gives the remainder c when divided by 11, then integers 10 ⋅ x + 0, 10 ⋅ x + 1 …, 10 ⋅ x + (c - 1) are inadequate, while integers 10 ⋅ x + c, 10 ⋅ x + (c + 1), …, 10 ⋅ x + 9 are not inadequate. The first several inadequate integers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 30, 31, 32 …. After that, since 4 is the fourth inadequate integer, 40, 41, 42, 43 are inadequate, while 44, 45, 46, …, 49 are not inadequate; since 10 is the 10-th inadequate number, integers 100, 101, 102, …, 109 are all inadequate. And since 20 is the 11-th inadequate number, none of 200, 201, 202, …, 209 is inadequate. You wrote down all the prices you have seen in a trip. Unfortunately, all integers got concatenated in one large digit string s and you lost the bounds between the neighboring integers. You are now interested in the number of substrings of the resulting string that form an inadequate number. If a substring appears more than once at different positions, all its appearances are counted separately. Input The only line contains the string s (1 ≤ |s| ≤ 10^5), consisting only of digits. It is guaranteed that the first digit of s is not zero. Output In the only line print the number of substrings of s that form an inadequate number. Examples Input 4021 Output 6 Input 110 Output 3 Note In the first example the inadequate numbers in the string are 1, 2, 4, 21, 40, 402. In the second example the inadequate numbers in the string are 1 and 10, and 1 appears twice (on the first and on the second positions). Submitted Solution: ``` from math import floor l=[1,2,3,4,5,6,7,8,9] def a(x): if x>=1 and x<=9: return 1 if x>=10: if a(x//10) and (l.index(x//10)+1)%11>x%10: return 1 else: return 0 for i in range(10,100000): if a(i): l.append(i) count=0 a=int(input()) a=str(a) for i in range(0,len(a)): for j in range(i+1,len(a)+1): if a[i:i+j][0]!='0' and int(a[i:i+j]) in l: count+=1 print (count) ``` No
15,156
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` from sys import stdin n, k = [int(i) for i in stdin.readline().strip().split()] s = stdin.readline().strip() class Subsequences: def __init__(self, sequence): self._seq = list(sequence) self._F = [len(sequence) * [0] for _ in range((len(sequence) + 1))] self._calc() def _calc(self): # iterative solution F[size][index] given number of distinct subsequences of given size # in slice [0: index + 1] of original sequence F = self._F size = 0 for index in range(len(s)): F[size][index] = 1 F[1][0] = 1 p = {s[0]: 0} for i in range(1, len(s)): for k in range(1, len(s) + 1): if k > i + 1: val = 0 else: val = F[k][i - 1] + F[k - 1][i - 1] if s[i] in p: val -= F[k - 1][p[s[i]] - 1] F[k][i] = val p[s[i]] = i def count(self, size, index=None): index = index or (len(self._seq) - 1) return self._F[size][index] # recursive solution with memoization previous_letter_index = {} found = {} for index, letter in enumerate(s): if letter in found: previous_letter_index[(letter, index)] = found[letter] found[letter] = index _subsequences = {} def subsequences(size, index): """Get number of distinct subsequences of given size in sequence[0:index + 1] slice""" if (size, index) not in _subsequences: if size == 0: res = 1 elif size > index + 1: res = 0 else: res = subsequences(size, index - 1) + subsequences(size - 1, index - 1) letter = s[index] if (letter, index) in previous_letter_index: res -= subsequences(size - 1, previous_letter_index[(letter, index)] - 1) _subsequences[(size, index)] = res return _subsequences[(size, index)] ss = Subsequences(s) total_cost = 0 for size in range(len(s), -1, -1): if k == 0: break step_cost = n - size sequence_count = subsequences(size, len(s) - 1) if sequence_count > k: sequence_count = k total_cost += step_cost * sequence_count k -= sequence_count if k == 0: print(total_cost) else: print(-1) ```
15,157
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` # your code goes here n,k=map(int,input().split()) s=input() c=0 q=[s] d=set() ls=0 while q: p=q.pop(0) if p not in d: ls+=1 c+=(n-len(p)) if ls==k: break d.add(p) for i in range(len(p)): temp=p[:i]+p[i+1:] if temp not in d: q.append(temp) if ls==k: print(c) else: print(-1) ```
15,158
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ from math import factorial from collections import Counter, defaultdict, deque from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) mod = 998244353 INF = float('inf') # ------------------------------ def c(chr): return ord(chr)-ord('a') def main(): n, k = RL() s = input() dp = [[0]*(n+1) for i in range(n+1)] rec = [-1]*26 pre = [-1]*n for i in range(n+1): dp[i][0] = 1 for i in range(n): now = c(s[i]) pre[i] = rec[now] rec[now] = i for i in range(1, n+1): for j in range(1, i+1): # if j>i: break dp[i][j] = dp[i-1][j] + dp[i-1][j-1] # if i == 6 and j == 1: print(dp[i-1][j], dp[i-1][j-1]) if pre[i-1]!=-1: # if i==5 and j==4: print(pre[i-1], '+++') # if i==6 and j==1: print(pre[i-1], dp[pre[i-1]][j], dp[i][j]) dp[i][j] -= dp[pre[i-1]][j-1] res = 0 for j in range(n, -1, -1): if k: num = min(k, dp[-1][j]) k-=num res += (n-j)*num # for i in dp: print(i) print(res if k==0 else -1) if __name__ == "__main__": main() ```
15,159
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` import sys n, k = map(int, sys.stdin.readline().split(' ')) s = sys.stdin.readline().strip() lt = [s] count = 0 flag = False result = 0 while lt: cur_s = lt[0] lt.pop(0) result += len(s) - len(cur_s) count += 1 if count >= k: break for i in range(0, len(cur_s)): new_s = cur_s[0: i] + cur_s[i + 1: ] if new_s not in lt: lt.append(new_s) if count < k: sys.stdout.write(str(-1) + '\n') else: sys.stdout.write(str(result) + '\n') ```
15,160
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` import sys input=sys.stdin.readline from collections import deque n,k=map(int,input().split()) s=list(input().rstrip()) vis=set() dq=deque(["".join(s)]) cnt=0 score=0 while dq: ss=dq.popleft() if ss not in vis: score+=n-len(ss) cnt+=1 vis.add(ss) else: continue if cnt>=k: break for i in range(len(ss)): nxt=ss[:i]+ss[i+1:] dq.append("".join(nxt)) if cnt<k: print(-1) else: print(score) ```
15,161
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` if __name__ == '__main__': n, k = map(int, input().split()) aa = list(input()) st = {"".join(aa)} arr = [aa] w = 0 c = 0 cst = 0 while len(arr) < k and w < len(arr): wrd = arr[w][:c] + arr[w][c + 1:] wrds = "".join(wrd) if wrds not in st: st.add(wrds) arr.append(wrd) cst += n - len(wrd) c += 1 if c >= len(arr[w]): c = 0 w += 1 if len(arr) < k: print(-1) else: print(cst) ```
15,162
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` n, k = map(int, input().split()) s = input() dp = [[0] * 102 for i in range(102)] dp1 = [[0] * 102 for i in range(30)] for i in range(0, n + 1): dp[i][0] = 1 for i in range(1, n + 1): nm = ord(s[i - 1]) - ord('a') for le in range(1, i + 1): dp[i][le] = dp[i - 1][le] + dp[i - 1][le - 1] dp[i][le] -= dp1[nm][le] for le in range(1, i + 1): dp1[nm][le] += (dp[i][le] - dp[i - 1][le]) ans = 0 for le in range(n, -1, -1): if k == 0: break x = min(dp[n][le], k) ans += (n - le) * x k -= x if k != 0: print(-1) else: print(ans) ```
15,163
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ from math import factorial from collections import Counter, defaultdict, deque from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) mod = 998244353 INF = float('inf') # ------------------------------ def main(): n, k = RL() s = input() q = deque() q.append(s) res = 0 rec = set() rec.add(s) while q: now = q.popleft() res+=n-len(now) if len(rec)>=k: continue for i in range(len(now)): nex = now[:i]+now[i+1:] if nex in rec: continue rec.add(nex) if len(rec)<=k: q.append(nex) print(res if len(rec)>=k else -1) if __name__ == "__main__": main() ```
15,164
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` R = lambda: map(int, input().split()) n,k = R() s = input() p = [s] ans = 0 d= set() while p: q = p.pop(0) if q not in d: k -= 1 ans += (n-len(q)) if k == 0: print(ans) quit() d.add(q) for i in range(len(q)): t = q[:i]+q[i+1:] if t not in p:p.append(t) print(-1) ``` Yes
15,165
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` n, k = map(int, input().split()) str = str(input()) q = [str] ans = 0 ma = {} while len(q) != 0 and k > 1 : s = q[0] q.remove(q[0]) for i in range(len(s)): ss = s s1 = ss[0:i] s2 = ss[i+1:] ss = s1 + s2 if ss not in ma.keys() : ma[ss] = 1 k -= 1 ans += n - len(ss) q.append(ss) if k == 1 : break if k == 1 : print(ans) else : print(-1) ``` Yes
15,166
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` import sys from itertools import combinations from functools import lru_cache from string import ascii_lowercase @lru_cache() def e(n, l): return sum(d(n, l, x) for x in letters) @lru_cache() def d(n, l, c): if n < l: return 0 if s[n - 1] != c: return d(n - 1, l, c) return e(n - 1, l - 1) if l > 1 else 1 def solve(s, k): n = len(s) cost = 0 for l in range(n, 0, -1): cnt = e(n, l) if k > cnt: k -= cnt cost += cnt * (n - l) else: cost += k * (n - l) k = 0 break if k > 1: return -1 if k == 1: cost += n k = 0 return cost n, k = map(int, input().split()) s = input() letters = set(s) print(solve(s, k)) ``` Yes
15,167
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` import sys import itertools inputs = sys.stdin.read().split() len_string = int(inputs[0]) desired_size = int(inputs[1]) string = inputs[2] size = 0 cost = 0 cur_set = set() cur_set.add(string) for i in range(len_string, -1, -1): cur_size = len(cur_set) if size+cur_size >= desired_size: cost += (desired_size-size)*(len_string-i) size = desired_size break cost += cur_size*(len_string-i) size += cur_size new_set = set() for substr in cur_set: for i in range(len(substr)): new_set.add(substr[:i]+substr[(i+1):]) cur_set = new_set if size >= desired_size: sys.stdout.write(str(cost)+"\n") else: sys.stdout.write("-1\n") ``` Yes
15,168
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` line1 = input().split(' ') n = int(line1[0]) k = int(line1[1]) s = list(input()) dp = [101*[0] for i in range(101)] last = 26*[-1] for i in range(n+1): dp[0][i] = 1 for l in range(1, n+1): dp[l][0] = 0 for c in range(26): last[c] = -1 for i in range(1, n+1): dp[l][i] = dp[l-1][i-1] + dp[l][i-1] if last[ord(s[i-1])-ord('a')] != -1: dp[l][i] -= dp[l][last[ord(s[i-1])-ord('a')]] last[ord(s[i-1])-ord('a')] = i i = 0 res = 0 while i <= n and k >= 0: c = min(k, dp[n-i][n]) k -= c res += c * i i += 1 if k > 0: print(-1) else: print(res) ``` No
15,169
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- # File : H.py # Author : JCHRYS <jchrys@me.com> # Date : 30.08.2019 # Last Modified Date: 30.08.2019 # Last Modified By : JCHRYS <jchrys@me.com> class const: size = 26; # size of lowercase alphabet n, k = map(int, input().split()); s = input(); maxpos = [[-1 for _ in range(const.size)] for _ in range(n)]; for i in range(n): for j in range(const.size): if i > 0: maxpos[i][j] = maxpos[i - 1][j] maxpos[i][ord(s[i]) - ord('a')] = i #print(*[row for row in maxpos], sep="\n") dp = [[0 for _ in range(n)] for _ in range(n)]; for i in range(n): dp[i][1] = 1 for length in range(2, n): for endswith in range(1, n): for before in range(const.size): if maxpos[endswith - 1][before] != -1: dp[endswith][length] = dp[endswith][length] + dp[maxpos[endswith - 1][before]][length - 1]; #print(*[row for row in dp], sep="\n") k -= 1; ans = 0; for length in range(n-1, 0, -1): temp = 0; for i in range(const.size): if (maxpos[n-1][i] != -1): temp += dp[maxpos[n-1][i]][length]; if temp >= k: ans += k * (n - length); k = 0; break; else: k -= temp; ans += temp * (n-length); if (k == 1): ans += n; if k > 0: print(-1) exit() print(ans) ``` No
15,170
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` from collections import deque n,k = map(int, input().split()) s = input() count = 1 clen = len(s) result = 0 squeue = deque() squeue.append((s,0)) while count < k and clen > 0: base, spos = squeue.popleft() if len(base) == clen: sset = set() clen -= 1 for i in range(spos,len(base)): candidate = base[:i] + base[i+1:] if candidate.__hash__() not in sset: sset.add(candidate.__hash__()) squeue.append((candidate,i)) result += n-clen count+=1 if count == k: break if count == k: print(result) else: print(-1) ``` No
15,171
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` def check_word(word_list, word_need): unique = [] for i in range(len(word_list)): # Pick each word in the list if word_need == 0: break for j in range(len(word_list[i])): if word_need == 0: break # Check word without 1 char is in unique new_word = word_list[i][:j]+word_list[i][j+1:] if new_word not in unique: unique.append(new_word) word_need -= 1 return unique def check_all(): cost_sum = 0 cost_each = 1 [raw_length, raw_size] = input().split() size = int(raw_size) words = input().split() size -= 1 while size>0: if len(words[0]) == 0: return -1 words = check_word(words, size) print(len(words)) cost_sum += len(words)*cost_each cost_each += 1 size -= len(words) return cost_sum print(check_all()) ``` No
15,172
Provide tags and a correct Python 3 solution for this coding contest problem. Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of n vertices numbered from 1 to n. The i-th vertex has m_i outgoing edges that are labeled as e_i[0], e_i[1], …, e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The i-th vertex also has an integer k_i written on itself. A travel on this graph works as follows. 1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable c to this integer. 2. After arriving at the vertex i, or when Gildong begins the travel at some vertex i, add k_i to c. 3. The next vertex is e_i[x] where x is an integer 0 ≤ x ≤ m_i-1 satisfying x ≡ c \pmod {m_i}. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly. For example, assume that Gildong starts at vertex 1 with c = 5, and m_1 = 2, e_1[0] = 1, e_1[1] = 2, k_1 = -3. Right after he starts at vertex 1, c becomes 2. Since the only integer x (0 ≤ x ≤ 1) where x ≡ c \pmod {m_i} is 0, Gildong goes to vertex e_1[0] = 1. After arriving at vertex 1 again, c becomes -1. The only integer x satisfying the conditions is 1, so he goes to vertex e_1[1] = 2, and so on. Since Gildong is quite inquisitive, he's going to ask you q queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of c. Note that you should not count the vertices that will be visited only finite times. Input The first line of the input contains an integer n (1 ≤ n ≤ 1000), the number of vertices in the graph. The second line contains n integers. The i-th integer is k_i (-10^9 ≤ k_i ≤ 10^9), the integer written on the i-th vertex. Next 2 ⋅ n lines describe the edges of each vertex. The (2 ⋅ i + 1)-st line contains an integer m_i (1 ≤ m_i ≤ 10), the number of outgoing edges of the i-th vertex. The (2 ⋅ i + 2)-nd line contains m_i integers e_i[0], e_i[1], …, e_i[m_i-1], each having an integer value between 1 and n, inclusive. Next line contains an integer q (1 ≤ q ≤ 10^5), the number of queries Gildong wants to ask. Next q lines contains two integers x and y (1 ≤ x ≤ n, -10^9 ≤ y ≤ 10^9) each, which mean that the start vertex is x and the starting value of c is y. Output For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex x with starting integer y. Examples Input 4 0 0 0 0 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 2 1 3 2 Input 4 4 -5 -3 -1 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 1 3 1 1 Note The first example can be shown like the following image: <image> Three integers are marked on i-th vertex: i, k_i, and m_i respectively. The outgoing edges are labeled with an integer representing the edge number of i-th vertex. The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex (c after k_i added)". * 1(0) → 2(0) → 2(0) → … * 2(0) → 2(0) → … * 3(-1) → 1(-1) → 3(-1) → … * 4(-2) → 2(-2) → 2(-2) → … * 1(1) → 3(1) → 4(1) → 1(1) → … * 1(5) → 3(5) → 1(5) → … The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example. <image> The queries for the second example works as follows: * 1(4) → 2(-1) → 2(-6) → … * 2(-5) → 2(-10) → … * 3(-4) → 1(0) → 2(-5) → 2(-10) → … * 4(-3) → 1(1) → 3(-2) → 4(-3) → … * 1(5) → 3(2) → 1(6) → 2(1) → 2(-4) → … * 1(9) → 3(6) → 2(1) → 2(-4) → … Tags: brute force, data structures, dfs and similar, dp, graphs, implementation, math, number theory Correct Solution: ``` import io, sys input = lambda f=io.StringIO(sys.stdin.buffer.read().decode()).readline: f().rstrip() LCM = 2520 n = int(input()) k = list(map(int, input().split())) m, e = [], [] for _ in range(n): m.append(int(input())) e.append(list(map(int, input().split()))) nxt = [] for i in range(n): for j in range(LCM): x = (j + k[i]) % LCM y = e[i][x % m[i]] - 1 nxt.append(y * LCM + x) mark = [-1] * (n * LCM) loop = [None] * (n * LCM) for i in range(n * LCM): if loop[i]: continue start = cur = i rec = [] while True: if mark[cur] != -1: break mark[cur] = i rec.append(cur) cur = nxt[cur] if loop[cur]: for u in rec: loop[u] = loop[cur] else: uniq = set() inloop = 0 for u in rec: loop[u] = uniq if u == cur: inloop = 1 if inloop: uniq.add(u // LCM) out = [] for _ in range(int(input())): x, y = map(int, input().split()) out.append(len(loop[(x - 1) * LCM + y % LCM])) print(*out, sep='\n') ```
15,173
Provide tags and a correct Python 3 solution for this coding contest problem. Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of n vertices numbered from 1 to n. The i-th vertex has m_i outgoing edges that are labeled as e_i[0], e_i[1], …, e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The i-th vertex also has an integer k_i written on itself. A travel on this graph works as follows. 1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable c to this integer. 2. After arriving at the vertex i, or when Gildong begins the travel at some vertex i, add k_i to c. 3. The next vertex is e_i[x] where x is an integer 0 ≤ x ≤ m_i-1 satisfying x ≡ c \pmod {m_i}. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly. For example, assume that Gildong starts at vertex 1 with c = 5, and m_1 = 2, e_1[0] = 1, e_1[1] = 2, k_1 = -3. Right after he starts at vertex 1, c becomes 2. Since the only integer x (0 ≤ x ≤ 1) where x ≡ c \pmod {m_i} is 0, Gildong goes to vertex e_1[0] = 1. After arriving at vertex 1 again, c becomes -1. The only integer x satisfying the conditions is 1, so he goes to vertex e_1[1] = 2, and so on. Since Gildong is quite inquisitive, he's going to ask you q queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of c. Note that you should not count the vertices that will be visited only finite times. Input The first line of the input contains an integer n (1 ≤ n ≤ 1000), the number of vertices in the graph. The second line contains n integers. The i-th integer is k_i (-10^9 ≤ k_i ≤ 10^9), the integer written on the i-th vertex. Next 2 ⋅ n lines describe the edges of each vertex. The (2 ⋅ i + 1)-st line contains an integer m_i (1 ≤ m_i ≤ 10), the number of outgoing edges of the i-th vertex. The (2 ⋅ i + 2)-nd line contains m_i integers e_i[0], e_i[1], …, e_i[m_i-1], each having an integer value between 1 and n, inclusive. Next line contains an integer q (1 ≤ q ≤ 10^5), the number of queries Gildong wants to ask. Next q lines contains two integers x and y (1 ≤ x ≤ n, -10^9 ≤ y ≤ 10^9) each, which mean that the start vertex is x and the starting value of c is y. Output For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex x with starting integer y. Examples Input 4 0 0 0 0 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 2 1 3 2 Input 4 4 -5 -3 -1 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 1 3 1 1 Note The first example can be shown like the following image: <image> Three integers are marked on i-th vertex: i, k_i, and m_i respectively. The outgoing edges are labeled with an integer representing the edge number of i-th vertex. The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex (c after k_i added)". * 1(0) → 2(0) → 2(0) → … * 2(0) → 2(0) → … * 3(-1) → 1(-1) → 3(-1) → … * 4(-2) → 2(-2) → 2(-2) → … * 1(1) → 3(1) → 4(1) → 1(1) → … * 1(5) → 3(5) → 1(5) → … The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example. <image> The queries for the second example works as follows: * 1(4) → 2(-1) → 2(-6) → … * 2(-5) → 2(-10) → … * 3(-4) → 1(0) → 2(-5) → 2(-10) → … * 4(-3) → 1(1) → 3(-2) → 4(-3) → … * 1(5) → 3(2) → 1(6) → 2(1) → 2(-4) → … * 1(9) → 3(6) → 2(1) → 2(-4) → … Tags: brute force, data structures, dfs and similar, dp, graphs, implementation, math, number theory Correct Solution: ``` import io, sys input = lambda f=io.StringIO(sys.stdin.buffer.read().decode()).readline: f().rstrip() LCM = 2520 n = int(input()) k = list(map(int, input().split())) m, e = [0] * n, [None] * n for i in range(n): m[i] = int(input()) e[i] = list(map(int, input().split())) nxt = [] for i in range(n): for j in range(LCM): x = (j + k[i]) % LCM y = e[i][x % m[i]] - 1 nxt.append(y * LCM + x) loop = [None] * (n * LCM) for i in range(n * LCM): if loop[i]: continue loop[i] = set() cur, rec = nxt[i], [i] while True: if loop[cur] is not None: break loop[cur] = loop[i] rec.append(cur) cur = nxt[cur] if loop[cur]: for u in rec: loop[u] = loop[cur] else: while rec[-1] != cur: loop[i].add(rec.pop() // LCM) loop[i].add(cur // LCM) out = [] for _ in range(int(input())): x, y = map(int, input().split()) out.append(len(loop[(x - 1) * LCM + y % LCM])) print(*out, sep='\n') ```
15,174
Provide tags and a correct Python 3 solution for this coding contest problem. Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of n vertices numbered from 1 to n. The i-th vertex has m_i outgoing edges that are labeled as e_i[0], e_i[1], …, e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The i-th vertex also has an integer k_i written on itself. A travel on this graph works as follows. 1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable c to this integer. 2. After arriving at the vertex i, or when Gildong begins the travel at some vertex i, add k_i to c. 3. The next vertex is e_i[x] where x is an integer 0 ≤ x ≤ m_i-1 satisfying x ≡ c \pmod {m_i}. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly. For example, assume that Gildong starts at vertex 1 with c = 5, and m_1 = 2, e_1[0] = 1, e_1[1] = 2, k_1 = -3. Right after he starts at vertex 1, c becomes 2. Since the only integer x (0 ≤ x ≤ 1) where x ≡ c \pmod {m_i} is 0, Gildong goes to vertex e_1[0] = 1. After arriving at vertex 1 again, c becomes -1. The only integer x satisfying the conditions is 1, so he goes to vertex e_1[1] = 2, and so on. Since Gildong is quite inquisitive, he's going to ask you q queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of c. Note that you should not count the vertices that will be visited only finite times. Input The first line of the input contains an integer n (1 ≤ n ≤ 1000), the number of vertices in the graph. The second line contains n integers. The i-th integer is k_i (-10^9 ≤ k_i ≤ 10^9), the integer written on the i-th vertex. Next 2 ⋅ n lines describe the edges of each vertex. The (2 ⋅ i + 1)-st line contains an integer m_i (1 ≤ m_i ≤ 10), the number of outgoing edges of the i-th vertex. The (2 ⋅ i + 2)-nd line contains m_i integers e_i[0], e_i[1], …, e_i[m_i-1], each having an integer value between 1 and n, inclusive. Next line contains an integer q (1 ≤ q ≤ 10^5), the number of queries Gildong wants to ask. Next q lines contains two integers x and y (1 ≤ x ≤ n, -10^9 ≤ y ≤ 10^9) each, which mean that the start vertex is x and the starting value of c is y. Output For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex x with starting integer y. Examples Input 4 0 0 0 0 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 2 1 3 2 Input 4 4 -5 -3 -1 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 1 3 1 1 Note The first example can be shown like the following image: <image> Three integers are marked on i-th vertex: i, k_i, and m_i respectively. The outgoing edges are labeled with an integer representing the edge number of i-th vertex. The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex (c after k_i added)". * 1(0) → 2(0) → 2(0) → … * 2(0) → 2(0) → … * 3(-1) → 1(-1) → 3(-1) → … * 4(-2) → 2(-2) → 2(-2) → … * 1(1) → 3(1) → 4(1) → 1(1) → … * 1(5) → 3(5) → 1(5) → … The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example. <image> The queries for the second example works as follows: * 1(4) → 2(-1) → 2(-6) → … * 2(-5) → 2(-10) → … * 3(-4) → 1(0) → 2(-5) → 2(-10) → … * 4(-3) → 1(1) → 3(-2) → 4(-3) → … * 1(5) → 3(2) → 1(6) → 2(1) → 2(-4) → … * 1(9) → 3(6) → 2(1) → 2(-4) → … Tags: brute force, data structures, dfs and similar, dp, graphs, implementation, math, number theory Correct Solution: ``` import sys from sys import stdin def solve(v,c): if d[v][c] != None: return d[v][c] vnum = 0 visits = set() while True: if c*n+v in visits: last = d[v][c] vs = set() for u in visits: if d[u%n][u//n] <= last: vs.add(u%n) nans = len(vs) for u in visits: d[u%n][u//n] = nans return nans elif d[v][c] != None and d[v][c] > 0: nans = d[v][c] for u in visits: d[u%n][u//n] = nans return nans visits.add(c*n+v) d[v][c] = vnum vnum -= 1 c = (c+k[v]) % mod v = lis[v][c % len(lis[v])] n = int(stdin.readline()) mod = 2520 d = [ [None] * mod for i in range(n) ] #print (d) k = list(map(int,stdin.readline().split())) lis = [] for i in range(n): m = int(stdin.readline()) lis.append(list(map(int,stdin.readline().split()))) for j in range(m): lis[i][j] -= 1 q = int(stdin.readline()) ANS = [] for loop in range(q): x,y = map(int,stdin.readline().split()) x -= 1 ANS.append(solve(x,y % mod)) print ("\n".join(map(str,ANS))) ```
15,175
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of n vertices numbered from 1 to n. The i-th vertex has m_i outgoing edges that are labeled as e_i[0], e_i[1], …, e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The i-th vertex also has an integer k_i written on itself. A travel on this graph works as follows. 1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable c to this integer. 2. After arriving at the vertex i, or when Gildong begins the travel at some vertex i, add k_i to c. 3. The next vertex is e_i[x] where x is an integer 0 ≤ x ≤ m_i-1 satisfying x ≡ c \pmod {m_i}. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly. For example, assume that Gildong starts at vertex 1 with c = 5, and m_1 = 2, e_1[0] = 1, e_1[1] = 2, k_1 = -3. Right after he starts at vertex 1, c becomes 2. Since the only integer x (0 ≤ x ≤ 1) where x ≡ c \pmod {m_i} is 0, Gildong goes to vertex e_1[0] = 1. After arriving at vertex 1 again, c becomes -1. The only integer x satisfying the conditions is 1, so he goes to vertex e_1[1] = 2, and so on. Since Gildong is quite inquisitive, he's going to ask you q queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of c. Note that you should not count the vertices that will be visited only finite times. Input The first line of the input contains an integer n (1 ≤ n ≤ 1000), the number of vertices in the graph. The second line contains n integers. The i-th integer is k_i (-10^9 ≤ k_i ≤ 10^9), the integer written on the i-th vertex. Next 2 ⋅ n lines describe the edges of each vertex. The (2 ⋅ i + 1)-st line contains an integer m_i (1 ≤ m_i ≤ 10), the number of outgoing edges of the i-th vertex. The (2 ⋅ i + 2)-nd line contains m_i integers e_i[0], e_i[1], …, e_i[m_i-1], each having an integer value between 1 and n, inclusive. Next line contains an integer q (1 ≤ q ≤ 10^5), the number of queries Gildong wants to ask. Next q lines contains two integers x and y (1 ≤ x ≤ n, -10^9 ≤ y ≤ 10^9) each, which mean that the start vertex is x and the starting value of c is y. Output For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex x with starting integer y. Examples Input 4 0 0 0 0 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 2 1 3 2 Input 4 4 -5 -3 -1 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 1 3 1 1 Note The first example can be shown like the following image: <image> Three integers are marked on i-th vertex: i, k_i, and m_i respectively. The outgoing edges are labeled with an integer representing the edge number of i-th vertex. The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex (c after k_i added)". * 1(0) → 2(0) → 2(0) → … * 2(0) → 2(0) → … * 3(-1) → 1(-1) → 3(-1) → … * 4(-2) → 2(-2) → 2(-2) → … * 1(1) → 3(1) → 4(1) → 1(1) → … * 1(5) → 3(5) → 1(5) → … The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example. <image> The queries for the second example works as follows: * 1(4) → 2(-1) → 2(-6) → … * 2(-5) → 2(-10) → … * 3(-4) → 1(0) → 2(-5) → 2(-10) → … * 4(-3) → 1(1) → 3(-2) → 4(-3) → … * 1(5) → 3(2) → 1(6) → 2(1) → 2(-4) → … * 1(9) → 3(6) → 2(1) → 2(-4) → … Submitted Solution: ``` import io, sys input = lambda f=io.StringIO(sys.stdin.buffer.read().decode()).readline: f().rstrip() LCM = 2520 n = int(input()) k = list(map(int, input().split())) m, e = [0] * n, [None] * n for i in range(n): m[i] = int(input()) e[i] = list(map(int, input().split())) nxt = [] for i in range(n): for j in range(LCM): x = (j + k[i]) % LCM y = e[i][x % m[i]] - 1 nxt.append(y * LCM + x) loop = [None] * (n * LCM) for i in range(n * LCM): if loop[i]: continue loop[i] = set() cur, rec = nxt[i], [i] while True: if loop[cur] is not None: break loop[cur] = loop[i] rec.append(cur) cur = nxt[cur] if loop[cur]: for u in rec: loop[u] = loop[cur] else: while rec[-1] != cur: loop[i].add(rec.pop()) loop[i].add(cur) out = [] for _ in range(int(input())): x, y = map(int, input().split()) out.append(len(loop[(x - 1) * LCM + y % LCM])) print(*out, sep='\n') ``` No
15,176
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of n vertices numbered from 1 to n. The i-th vertex has m_i outgoing edges that are labeled as e_i[0], e_i[1], …, e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The i-th vertex also has an integer k_i written on itself. A travel on this graph works as follows. 1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable c to this integer. 2. After arriving at the vertex i, or when Gildong begins the travel at some vertex i, add k_i to c. 3. The next vertex is e_i[x] where x is an integer 0 ≤ x ≤ m_i-1 satisfying x ≡ c \pmod {m_i}. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly. For example, assume that Gildong starts at vertex 1 with c = 5, and m_1 = 2, e_1[0] = 1, e_1[1] = 2, k_1 = -3. Right after he starts at vertex 1, c becomes 2. Since the only integer x (0 ≤ x ≤ 1) where x ≡ c \pmod {m_i} is 0, Gildong goes to vertex e_1[0] = 1. After arriving at vertex 1 again, c becomes -1. The only integer x satisfying the conditions is 1, so he goes to vertex e_1[1] = 2, and so on. Since Gildong is quite inquisitive, he's going to ask you q queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of c. Note that you should not count the vertices that will be visited only finite times. Input The first line of the input contains an integer n (1 ≤ n ≤ 1000), the number of vertices in the graph. The second line contains n integers. The i-th integer is k_i (-10^9 ≤ k_i ≤ 10^9), the integer written on the i-th vertex. Next 2 ⋅ n lines describe the edges of each vertex. The (2 ⋅ i + 1)-st line contains an integer m_i (1 ≤ m_i ≤ 10), the number of outgoing edges of the i-th vertex. The (2 ⋅ i + 2)-nd line contains m_i integers e_i[0], e_i[1], …, e_i[m_i-1], each having an integer value between 1 and n, inclusive. Next line contains an integer q (1 ≤ q ≤ 10^5), the number of queries Gildong wants to ask. Next q lines contains two integers x and y (1 ≤ x ≤ n, -10^9 ≤ y ≤ 10^9) each, which mean that the start vertex is x and the starting value of c is y. Output For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex x with starting integer y. Examples Input 4 0 0 0 0 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 2 1 3 2 Input 4 4 -5 -3 -1 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 1 3 1 1 Note The first example can be shown like the following image: <image> Three integers are marked on i-th vertex: i, k_i, and m_i respectively. The outgoing edges are labeled with an integer representing the edge number of i-th vertex. The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex (c after k_i added)". * 1(0) → 2(0) → 2(0) → … * 2(0) → 2(0) → … * 3(-1) → 1(-1) → 3(-1) → … * 4(-2) → 2(-2) → 2(-2) → … * 1(1) → 3(1) → 4(1) → 1(1) → … * 1(5) → 3(5) → 1(5) → … The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example. <image> The queries for the second example works as follows: * 1(4) → 2(-1) → 2(-6) → … * 2(-5) → 2(-10) → … * 3(-4) → 1(0) → 2(-5) → 2(-10) → … * 4(-3) → 1(1) → 3(-2) → 4(-3) → … * 1(5) → 3(2) → 1(6) → 2(1) → 2(-4) → … * 1(9) → 3(6) → 2(1) → 2(-4) → … Submitted Solution: ``` class Node: def __init__(self, i, c): self.i = i self.c = c def __eq__(self, node): return self.i == node.i def __hash__(self): return hash(self.i) def __str__(self): return f"{self.i + 1}({self.c})" n = int(input()) ks = list(map(int, input().split())) edges = [] for i in range(n): _ = input() ms = list(map(int, input().split())) e_i = [] for j, e in enumerate(ms): e_i.append(e - 1) edges.append(e_i) q = int(input()) for _ in range(q): v, c = map(int, input().split()) v -= 1 c += ks[v] visited = set() current = Node(v, c) stack = [] k = 1 while True: print(current) visited.add(current) stack.append(current) E = edges[current.i] m = len(E) x = c % m next_v = E[x] c += ks[next_v] current = Node(next_v, c) if next_v == v: k = 1 break elif current in visited: while stack: next_ = stack.pop() if next_.i == current.i: break else: k += 1 break else: v = next_v print(k) ``` No
15,177
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of n vertices numbered from 1 to n. The i-th vertex has m_i outgoing edges that are labeled as e_i[0], e_i[1], …, e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The i-th vertex also has an integer k_i written on itself. A travel on this graph works as follows. 1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable c to this integer. 2. After arriving at the vertex i, or when Gildong begins the travel at some vertex i, add k_i to c. 3. The next vertex is e_i[x] where x is an integer 0 ≤ x ≤ m_i-1 satisfying x ≡ c \pmod {m_i}. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly. For example, assume that Gildong starts at vertex 1 with c = 5, and m_1 = 2, e_1[0] = 1, e_1[1] = 2, k_1 = -3. Right after he starts at vertex 1, c becomes 2. Since the only integer x (0 ≤ x ≤ 1) where x ≡ c \pmod {m_i} is 0, Gildong goes to vertex e_1[0] = 1. After arriving at vertex 1 again, c becomes -1. The only integer x satisfying the conditions is 1, so he goes to vertex e_1[1] = 2, and so on. Since Gildong is quite inquisitive, he's going to ask you q queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of c. Note that you should not count the vertices that will be visited only finite times. Input The first line of the input contains an integer n (1 ≤ n ≤ 1000), the number of vertices in the graph. The second line contains n integers. The i-th integer is k_i (-10^9 ≤ k_i ≤ 10^9), the integer written on the i-th vertex. Next 2 ⋅ n lines describe the edges of each vertex. The (2 ⋅ i + 1)-st line contains an integer m_i (1 ≤ m_i ≤ 10), the number of outgoing edges of the i-th vertex. The (2 ⋅ i + 2)-nd line contains m_i integers e_i[0], e_i[1], …, e_i[m_i-1], each having an integer value between 1 and n, inclusive. Next line contains an integer q (1 ≤ q ≤ 10^5), the number of queries Gildong wants to ask. Next q lines contains two integers x and y (1 ≤ x ≤ n, -10^9 ≤ y ≤ 10^9) each, which mean that the start vertex is x and the starting value of c is y. Output For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex x with starting integer y. Examples Input 4 0 0 0 0 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 2 1 3 2 Input 4 4 -5 -3 -1 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 1 3 1 1 Note The first example can be shown like the following image: <image> Three integers are marked on i-th vertex: i, k_i, and m_i respectively. The outgoing edges are labeled with an integer representing the edge number of i-th vertex. The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex (c after k_i added)". * 1(0) → 2(0) → 2(0) → … * 2(0) → 2(0) → … * 3(-1) → 1(-1) → 3(-1) → … * 4(-2) → 2(-2) → 2(-2) → … * 1(1) → 3(1) → 4(1) → 1(1) → … * 1(5) → 3(5) → 1(5) → … The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example. <image> The queries for the second example works as follows: * 1(4) → 2(-1) → 2(-6) → … * 2(-5) → 2(-10) → … * 3(-4) → 1(0) → 2(-5) → 2(-10) → … * 4(-3) → 1(1) → 3(-2) → 4(-3) → … * 1(5) → 3(2) → 1(6) → 2(1) → 2(-4) → … * 1(9) → 3(6) → 2(1) → 2(-4) → … Submitted Solution: ``` class Node: def __init__(self, i, c): self.i = i self.c = c def __eq__(self, node): return self.i == node.i def __hash__(self): return hash(self.i) def __str__(self): return f"{self.i + 1}({self.c})" n = int(input()) ks = list(map(int, input().split())) edges = [] for i in range(n): _ = input() ms = list(map(int, input().split())) e_i = [] for j, e in enumerate(ms): e_i.append(e - 1) edges.append(e_i) q = int(input()) for _ in range(q): v, c = map(int, input().split()) v -= 1 c += ks[v] visited = set() current = Node(v, c) stack = [] k = 1 while True: visited.add(current) stack.append(current) E = edges[current.i] m = len(E) x = c % m next_v = E[x] c += ks[next_v] current = Node(next_v, c) if next_v == v: k = 1 break elif current in visited: while stack: next_ = stack.pop() if next_.i == current.i: break else: k += 1 break else: v = next_v print(k) ``` No
15,178
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of n vertices numbered from 1 to n. The i-th vertex has m_i outgoing edges that are labeled as e_i[0], e_i[1], …, e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The i-th vertex also has an integer k_i written on itself. A travel on this graph works as follows. 1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable c to this integer. 2. After arriving at the vertex i, or when Gildong begins the travel at some vertex i, add k_i to c. 3. The next vertex is e_i[x] where x is an integer 0 ≤ x ≤ m_i-1 satisfying x ≡ c \pmod {m_i}. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly. For example, assume that Gildong starts at vertex 1 with c = 5, and m_1 = 2, e_1[0] = 1, e_1[1] = 2, k_1 = -3. Right after he starts at vertex 1, c becomes 2. Since the only integer x (0 ≤ x ≤ 1) where x ≡ c \pmod {m_i} is 0, Gildong goes to vertex e_1[0] = 1. After arriving at vertex 1 again, c becomes -1. The only integer x satisfying the conditions is 1, so he goes to vertex e_1[1] = 2, and so on. Since Gildong is quite inquisitive, he's going to ask you q queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of c. Note that you should not count the vertices that will be visited only finite times. Input The first line of the input contains an integer n (1 ≤ n ≤ 1000), the number of vertices in the graph. The second line contains n integers. The i-th integer is k_i (-10^9 ≤ k_i ≤ 10^9), the integer written on the i-th vertex. Next 2 ⋅ n lines describe the edges of each vertex. The (2 ⋅ i + 1)-st line contains an integer m_i (1 ≤ m_i ≤ 10), the number of outgoing edges of the i-th vertex. The (2 ⋅ i + 2)-nd line contains m_i integers e_i[0], e_i[1], …, e_i[m_i-1], each having an integer value between 1 and n, inclusive. Next line contains an integer q (1 ≤ q ≤ 10^5), the number of queries Gildong wants to ask. Next q lines contains two integers x and y (1 ≤ x ≤ n, -10^9 ≤ y ≤ 10^9) each, which mean that the start vertex is x and the starting value of c is y. Output For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex x with starting integer y. Examples Input 4 0 0 0 0 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 2 1 3 2 Input 4 4 -5 -3 -1 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 1 3 1 1 Note The first example can be shown like the following image: <image> Three integers are marked on i-th vertex: i, k_i, and m_i respectively. The outgoing edges are labeled with an integer representing the edge number of i-th vertex. The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex (c after k_i added)". * 1(0) → 2(0) → 2(0) → … * 2(0) → 2(0) → … * 3(-1) → 1(-1) → 3(-1) → … * 4(-2) → 2(-2) → 2(-2) → … * 1(1) → 3(1) → 4(1) → 1(1) → … * 1(5) → 3(5) → 1(5) → … The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example. <image> The queries for the second example works as follows: * 1(4) → 2(-1) → 2(-6) → … * 2(-5) → 2(-10) → … * 3(-4) → 1(0) → 2(-5) → 2(-10) → … * 4(-3) → 1(1) → 3(-2) → 4(-3) → … * 1(5) → 3(2) → 1(6) → 2(1) → 2(-4) → … * 1(9) → 3(6) → 2(1) → 2(-4) → … Submitted Solution: ``` import sys sys.setrecursionlimit(10**7) class Scc: def __init__(self, Edge): self.Edge = Edge def decomposition(self): self.N = len(self.Edge) self.Edgeinv = [[] for _ in range(self.N)] for i in range(self.N): for e in self.Edge[i]: self.Edgeinv[e].append(i) self.order = [] self.used = set() for i in range(self.N): if i not in self.used: self.dfs1(i) self.res = [None]*self.N self.cnt = -1 self.used = set() for v in self.order[::-1]: if v not in self.used: self.cnt += 1 self.dfs2(v) n = self.cnt + 1 components = [[] for _ in range(n)] for i in range(self.N): components[self.res[i]].append(i) cEdge = [[] for _ in range(n)] cset = set() for i in range(self.N): for e in self.Edge[i]: if self.res[i] != self.res[e] and self.res[i] + self.N*self.res[e] not in cset: cset.add(self.res[i] + self.N*self.res[e]) cEdge[self.res[i]].append(self.res[e]) return self.res, components, cEdge def dfs1(self, v): self.used.add(v) for vf in self.Edge[v]: if vf not in self.used: self.used.add(vf) self.dfs1(vf) self.order.append(v) def dfs2(self, v): self.used.add(v) self.res[v] = self.cnt stack = [v] while stack: vn = stack.pop() for vf in self.Edgeinv[vn]: if vf not in self.used: self.used.add(vf) self.res[vf] = self.cnt stack.append(vf) N = int(input()) K = tuple(map(int, sys.stdin.readline().split())) rEdge = [None]*N M = [None]*N for i in range(N): M[i] = int(sys.stdin.readline()) rEdge[i] = tuple(map(lambda x: int(x) - 1, sys.stdin.readline().split())) LT = 2520 LN = LT*N tEdge = [None]*(LN) for i in range(LN): x, c = divmod(i, LT) nx = rEdge[x][c%M[x]] tEdge[i] = [nx*LT + (c+K[nx])%LT] G = Scc(tEdge) Res, Compo, cEdge = G.decomposition() CN = len(cEdge) Ans = [len(set(c//LT for c in Compo[i])) for i in range(CN)] dp = [0]*CN for i in range(CN-1, -1, -1): if not cEdge[i]: dp[i] = i else: dp[i] = dp[cEdge[i][0]] Q = int(sys.stdin.readline()) for _ in range(Q): x, c = tuple(map(int, sys.stdin.readline().split())) x -= 1 sys.stdout.write('{}\n'.format(Ans[Res[x*LT + (c+K[x])%LT]])) ``` No
15,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ujan has finally cleaned up his house and now wants to decorate the interior. He decided to place a beautiful carpet that would really tie the guest room together. He is interested in carpets that are made up of polygonal patches such that each side of a patch is either a side of another (different) patch, or is an exterior side of the whole carpet. In other words, the carpet can be represented as a planar graph, where each patch corresponds to a face of the graph, each face is a simple polygon. The perimeter of the carpet is the number of the exterior sides. Ujan considers a carpet beautiful if it consists of f patches, where the i-th patch has exactly a_i sides, and the perimeter is the smallest possible. Find an example of such a carpet, so that Ujan can order it! Input The first line of input contains a single integer f (1 ≤ f ≤ 10^5), the number of patches in the carpet. The next line contains f integers a_1, …, a_f (3 ≤ a_i ≤ 3⋅ 10^5), the number of sides of the patches. The total number of the sides of the patches a_1 + … + a_f does not exceed 3⋅10^5. Output Output the description of the carpet as a graph. First, output a single integer n (3 ≤ n ≤ 3 ⋅ 10^5), the total number of vertices in your graph (the vertices must be numbered from 1 to n). Then output f lines containing the description of the faces. The i-th line should describe the i-th face and contain a_i distinct integers v_{i,1}, …, v_{i,a_i} (1 ≤ v_{i,j} ≤ n), which means that the vertices v_{i,j} and v_{i,(j mod{a_i})+1} are connected by an edge for any 1 ≤ j ≤ a_i. The graph should be planar and satisfy the restrictions described in the problem statement. Its perimeter should be the smallest possible. There should be no double edges or self-loops in the graph. The graph should be connected. Note that a solution always exists; if there are multiple solutions, output any of them. Examples Input 2 3 3 Output 4 2 1 4 1 2 3 Input 3 5 3 5 Output 6 1 2 3 4 5 4 5 6 1 3 4 6 5 Note In the first sample, the two triangular faces are connected by a single edge, which results in the minimum perimeter 4. The figure shows one possible configuration for the second sample. The minimum perimeter in this case is 3. <image> Submitted Solution: ``` print('121232134') ``` No
15,180
Provide tags and a correct Python 3 solution for this coding contest problem. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Tags: implementation, strings Correct Solution: ``` x, y = input().split(' ') x = int(x) y = int(y) s = '' s1 = '' s= (input().split(' ')) s1 = (input().split(' ')) q = int(input()) for i in range(q): z = int(input()) print(s[(z-1)%x]+s1[(z-1)%y]) ```
15,181
Provide tags and a correct Python 3 solution for this coding contest problem. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Tags: implementation, strings Correct Solution: ``` n,m = [int(x) for x in input().split()] s = input().strip().split() t = input().strip().split() q = int(input()) for _ in range(q): y = int(input()) r1 = (y-1)%n r2 = (y-1)%m print(s[r1]+t[r2]) ```
15,182
Provide tags and a correct Python 3 solution for this coding contest problem. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Tags: implementation, strings Correct Solution: ``` from math import gcd n, m = list(map(int, input().split())) ar1 = list(input().split()) ar2 = list(input().split()) kek = n * m // gcd(n, m) for _ in range(int(input())): t = int(input()) t %= kek print(ar1[(t - 1) % n] + ar2[(t - 1) % m]) ```
15,183
Provide tags and a correct Python 3 solution for this coding contest problem. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Tags: implementation, strings Correct Solution: ``` n,m=map(int,input().split()) a=list(map(str,input().split())) b=list(map(str,input().split())) for i in range(int(input())): k=int(input()) x=k%len(a) y=k%len(b) print(a[x-1]+b[y-1]) ```
15,184
Provide tags and a correct Python 3 solution for this coding contest problem. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Tags: implementation, strings Correct Solution: ``` n,m=map(int,input().split()) s1=input().split() s2=input().split() l=[] for i in range(n*m): l.append(s1[i%n]+s2[i%m]) for i in range(int(input())): k=int(input()) print (l[(k-1)%(n*m)]) ```
15,185
Provide tags and a correct Python 3 solution for this coding contest problem. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Tags: implementation, strings Correct Solution: ``` n,m=map(int,input().split()) pol=input().split() kan=input().split() a=[] for i in range(1, n*m+1): if(i%n==0): a.append(pol[n-1]) else: a.append(pol[i%n-1]) if(i%m==0): a[-1]+=kan[m-1] else: a[-1]+=kan[i%m-1] p=int(input()) for i in range(p): t=int(input()) print(a[t%(n*m)-1]) ```
15,186
Provide tags and a correct Python 3 solution for this coding contest problem. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Tags: implementation, strings Correct Solution: ``` a = list(map(int,input().split())) n = input().split() s = input().split() q = int(input()) for i in range(q): x = int(input()) t1 = x%a[0] t2 = x%a[1] print(n[t1-1]+s[t2-1]) ```
15,187
Provide tags and a correct Python 3 solution for this coding contest problem. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Tags: implementation, strings Correct Solution: ``` from sys import stdin from collections import deque from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin def ii(): return int(stdin.readline()) def fi(): return float(stdin.readline()) def mi(): return map(int, stdin.readline().split()) def fmi(): return map(float, stdin.readline().split()) def li(): return list(mi()) def lsi(): x=list(stdin.readline()) x.pop() return x def si(): return stdin.readline() def sieve(x): a=[True]*(x+1) sq=floor(sqrt(x)) for i in range(3, sq+1, 2): if a[i]: for j in range(i*i, x+1, i): a[j]=False if x>1: p=[2] else: p=[] for i in range(3, x+1, 2): if a[i]: p.append(i) return p #vowel={'a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y'} #pow=[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808] ############# CODE STARTS HERE ############# n, m=mi() a=list(input().split()) b=list(input().split()) p=[a[-1]]+a[:-1] q=[b[-1]]+b[:-1] for _ in range(ii()): x=ii() print(p[x%n]+q[x%m]) ```
15,188
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Submitted Solution: ``` n,m = map(int,input().split()) s = input().split() t = input().split() q = int(input()) mass = "" for i in range(q): y = int(input()) if y <= n: mass += s[y-1] else: mass += s[y%n-1] if y <= m: mass += t[y-1] + ' ' else: mass += t[y%m-1] + ' ' mass = mass.split() for i in range(q): print(mass[i]) ``` Yes
15,189
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Submitted Solution: ``` n, m = map(int, input().split()) s = list(map(str, input().split())) t = list(map(str, input().split())) q=int(input()) y=[] for i in range(q): y.append(int(input())) for i in range(0,q): if y[i]>n: ni=y[i]%n-1 else: ni=y[i]-1 if y[i]>m: mi=y[i]%m-1 else: mi=y[i]-1 print(s[ni]+t[mi]) ``` Yes
15,190
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Submitted Solution: ``` length1,length2=map(int,input().split()) s1=list(map(str,input().split())) s2=list(map(str,input().split())) nn=int(input()) for x in range(nn): a=int(input()) - 1 x1=a % length1 x2=a % length2 print(s1[x1]+s2[x2]) ``` Yes
15,191
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Submitted Solution: ``` nm = input().split() n = int(nm[0]) m = int(nm[1]) nList = input().split() mList = input().split() q = int(input()) for x in range(q): y = int(input()) ny = y % n my = y % m print(nList[ny-1]+mList[my-1]) ``` Yes
15,192
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Submitted Solution: ``` l=list(map(int,input().split())) s1=list(map(str,input().split())) s2=list(map(str,input().split())) q=int(input()) n=l[0] m=l[1] while(q): yr=int(input()) if(yr>120): yr=yr%120 y1=yr%n y2=yr%m if(y1==0): y1=n if(y2==0): y2=m ans=s1[y1-1]+s2[y2-1] print(ans) q-=1 ``` No
15,193
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Submitted Solution: ``` n, m = map(int, input().split()) str1 = input().split() str2 = input().split() print(str1) print(str2) q = int(input()) while q: q = q - 1 inp = int(input()) in1 = inp % len(str1) in2 = inp % len(str2) print(str1[in1-1] + str2[in2-1]) ``` No
15,194
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Submitted Solution: ``` n,m = list(map(int,input().split())) arr1 = list(map(str,input().split())) arr2 = list(map(str,input().split())) d = [] i = 0 j = 0 t = 0 while True: i = i%n j = j%m x = arr1[i]+arr2[j] if x not in d: d.append(arr1[i]+arr2[j]) else: break i+=1 j+=1 t+=1 q = int(input()) for i in range(q): x = int(input()) x = x%(len(d)) print(d[x-1]) ``` No
15,195
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of n strings s_1, s_2, s_3, …, s_{n} and m strings t_1, t_2, t_3, …, t_{m}. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings x and y as the string that is obtained by writing down strings x and y one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings s_1 and t_1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if n = 3, m = 4, s = {"a", "b", "c"}, t = {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. <image> You are given two sequences of strings of size n and m and also q queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers n, m (1 ≤ n, m ≤ 20). The next line contains n strings s_1, s_2, …, s_{n}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. The next line contains m strings t_1, t_2, …, t_{m}. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 1 and at most 10. Among the given n + m strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer q (1 ≤ q ≤ 2 020). In the next q lines, an integer y (1 ≤ y ≤ 10^9) is given, denoting the year we want to know the name for. Output Print q lines. For each line, print the name of the year as per the rule described above. Example Input 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 Output sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja Note The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal. Submitted Solution: ``` s1 = [] s2 = [] s3 = [] n, m = map(int, input().split()) inp = input() for i in inp.split(): s1.append(i) inp2 = input() for i in inp2.split(): s2.append(i) q = int(input()) for i in range(q): a = int(input()) s3.append(s1[(a-1)%n] + s2[(a-1)%m]) print(s3, end="\n") ``` No
15,196
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order. Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent). Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible? Input The first line contains one integer T (1 ≤ T ≤ 1000) — the number of test cases. Then T lines follow, each containing one string s (1 ≤ |s| ≤ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s. Output For each test case, do the following: * if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem); * otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them. Example Input 5 ababa codedoca abcda zxzytyz abcdefghijklmnopqrstuvwxyza Output YES bacdefghijklmnopqrstuvwxyz YES edocabfghijklmnpqrstuvwxyz NO YES xzytabcdefghijklmnopqrsuvw NO Tags: dfs and similar, greedy, implementation Correct Solution: ``` for _ in range(int(input())): s = input() l = [] l.append(s[0]) f=0 m = 0 j=0 for i in range(1,len(s)): if s[i] in l: if l[j]==s[i]: f=1 break if j>0 and j<m and l[j-1]!=s[i] and l[j+1]!=s[i]: f=1 break if j==0 and l[j+1]!=s[i]: f=1 break if j==m and l[j-1]!=s[i]: f=1 break if l[j]==s[i]: f=1 break if j>0 and j<m and l[j-1]!=s[i] and l[j+1]!=s[i]: f=1 break if j>0 and l[j-1]==s[i]: j-=1 continue if j<m and l[j+1]==s[i]: j+=1 continue if j==0 and m==0: j+=1 m+=1 l.append(s[i]) elif j==0 and m>0: m+=1 l.insert(0,s[i]) elif j==m: j+=1 m+=1 l.append(s[i]) if f==1: print('NO') else: print('YES') a = [] for k in range(26): a.append(-1) for k in l: a[ord(k)-97]=1 for k in range(26): if a[k]==1: pass else: l.append(chr(k+97)) str1 = "" for i in l: str1+=i print(str1) ```
15,197
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order. Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent). Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible? Input The first line contains one integer T (1 ≤ T ≤ 1000) — the number of test cases. Then T lines follow, each containing one string s (1 ≤ |s| ≤ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s. Output For each test case, do the following: * if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem); * otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them. Example Input 5 ababa codedoca abcda zxzytyz abcdefghijklmnopqrstuvwxyza Output YES bacdefghijklmnopqrstuvwxyz YES edocabfghijklmnpqrstuvwxyz NO YES xzytabcdefghijklmnopqrsuvw NO Tags: dfs and similar, greedy, implementation Correct Solution: ``` import sys input = sys.stdin.readline from collections import Counter t=int(input()) for tests in range(t): s=input().strip() if len(s)==1: print("YES") print("abcdefghijklmnopqrstuvwxyz") continue L=[set() for i in range(26)] for i in range(1,len(s)): x=ord(s[i])-97 y=ord(s[i-1])-97 L[x].add(y) L[y].add(x) LL=[len(l) for l in L] C=Counter(LL) if max(LL)>2 or C[1]!=2: print("NO") continue NOW=LL.index(1) USE=[0]*26 USE[NOW]=1 ANS=chr(NOW+97) NOW=list(L[NOW])[0] USE[NOW]=1 ANS+=chr(NOW+97) while LL[NOW]==2: for x in L[NOW]: if USE[x]==0: NOW=x USE[x]=1 ANS+=chr(x+97) #print(L) #print(ANS) for i in range(26): if USE[i]==0: ANS+=chr(i+97) print("YES") print(ANS) ```
15,198
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order. Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent). Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible? Input The first line contains one integer T (1 ≤ T ≤ 1000) — the number of test cases. Then T lines follow, each containing one string s (1 ≤ |s| ≤ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s. Output For each test case, do the following: * if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem); * otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them. Example Input 5 ababa codedoca abcda zxzytyz abcdefghijklmnopqrstuvwxyza Output YES bacdefghijklmnopqrstuvwxyz YES edocabfghijklmnpqrstuvwxyz NO YES xzytabcdefghijklmnopqrsuvw NO Tags: dfs and similar, greedy, implementation Correct Solution: ``` from sys import stdin input=lambda : stdin.readline().strip() from math import ceil,sqrt,factorial,gcd from collections import deque for _ in range(int(input())): ans=deque() s=input() ans.append(s[0]) a=0 flag="YES" for i in range(1,len(s)): if a==0: if len(ans)>1: if ans[a+1]==s[i]: a+=1 else: ans.appendleft(s[i]) else: ans.append(s[i]) a+=1 elif a==len(ans)-1: if ans[a-1]==s[i]: a-=1 else: ans.append(s[i]) a+=1 else: if ans[a-1]==s[i]: a-=1 elif ans[a+1]==s[i]: a+=1 else: flag="NO" break for i in range(97,123): if chr(i) not in ans: ans.append(chr(i)) if len(ans)!=26: flag="NO" print(flag) if flag=="YES": print("".join(ans)) ```
15,199