text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide tags and a correct Python 3 solution for this coding contest problem. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Tags: implementation Correct Solution: ``` n = int(input()) u = [n] for i in range(n // 2, 1, -1): if n % i == 0: u.append(i) s = list(input()) u.reverse() for i in u: d = s[:i] d.reverse() s = d + s[i:] for i in s: print(i, end = '') ```
108,400
Provide tags and a correct Python 3 solution for this coding contest problem. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Tags: implementation Correct Solution: ``` n = int(input()) k = list(input()) l = [i for i in range(2,(n**1//2)+1) if n%i==0 ] for i in l: k[:i] = k[i-1::-1] print(''.join(k[::-1])) ```
108,401
Provide tags and a correct Python 3 solution for this coding contest problem. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Tags: implementation Correct Solution: ``` n=int(input()) x=input() for i in range(2,n+1): # print(n,i) if n%i==0: s=x[i-1::-1] s+=x[i:] x=s # print(s) print(x) ```
108,402
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Submitted Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- n = int(input()) s = list(input()) for i in range(1, n+1): if n % i == 0: s[0:i] = s[i-1::-1] print(''.join(s)) ``` Yes
108,403
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Submitted Solution: ``` n =int(input()) s = input() def findAllDivisors(k): lst = [] for i in range(1,k+1): if k%i == 0: lst.append(i) return lst lst = findAllDivisors(n) for i in range(0, len(lst)): newStr = s[0:lst[i]] newStr = newStr[::-1] newStr1 = s[lst[i]:len(s)] s = newStr + newStr1 print(s) ``` Yes
108,404
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Submitted Solution: ``` n=int(input()) a=input() for i in range(2,n): if n%i==0: #print(i,a) #print(a[]) a=a[i-1::-1]+a[i:] #print(i,a) a=a[::-1] print(a) ``` Yes
108,405
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Submitted Solution: ``` # http://codeforces.com/contest/999/problem/B def read_nums(): return [int(x) for x in input().split()] def main(): n, = read_nums() s = input() for i in range(1, n + 1): if n % i == 0: one = s[:i][::-1] two = s[i:] s = one + two print(s) if __name__ == '__main__': main() ``` Yes
108,406
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Submitted Solution: ``` def reverse(y): p="" for k in range(len(y)-1,-1,-1): p=p+y[k] return p n=int(input()) s=input() L=[] for i in range(1,n+1): if n%i==0: L.append(i) for j in range(len(L)): a=s[:L[j]] s=reverse(a)+s[L[j]:] print(s) ``` No
108,407
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Submitted Solution: ``` n = int(input()) string = list(input()) divisors = [] for i in range(1, n+1): if n % i == 0: divisors.append(i-1) for divisor in divisors: print(divisor) rev = string[:divisor+1] rev = rev[::-1] string = rev + string[divisor+1:] # print(string) print("".join(string)) ``` No
108,408
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Submitted Solution: ``` a = int(input()) s = list(input()) b = [] for i in range(a, 1, -1): if a % i == 0: b.append(i) for i in b: s[0:i] = reversed(s[0:i]) s = "".join(s) print(s) ``` No
108,409
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A string s of length n can be encrypted by the following algorithm: * iterate over all divisors of n in decreasing order (i.e. from n to 1), * for each divisor d, reverse the substring s[1 ... d] (i.e. the substring which starts at position 1 and ends at position d). For example, the above algorithm applied to the string s="codeforces" leads to the following changes: "codeforces" → "secrofedoc" → "orcesfedoc" → "rocesfedoc" → "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1). You are given the encrypted string t. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string s always exists and is unique. Input The first line of input consists of a single integer n (1 ≤ n ≤ 100) — the length of the string t. The second line of input consists of the string t. The length of t is n, and it consists only of lowercase Latin letters. Output Print a string s such that the above algorithm results in t. Examples Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z Note The first example is described in the problem statement. Submitted Solution: ``` q=lambda:map(int,input().split()) qi=lambda:int(input()) qs=lambda:input().split() def lol(n): for i in range(1,n+1): if n%i==0: yield i n=qi() a=input() for i in lol(n): a=''.join(reversed(a[0:i]))+a[i:10] print(a) ``` No
108,410
Provide a correct Python 3 solution for this coding contest problem. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD "Correct Solution: ``` #-*- coding:utf-8 -*- t = input() print(t.replace("?","D")) ```
108,411
Provide a correct Python 3 solution for this coding contest problem. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD "Correct Solution: ``` T = input() y = T.replace('?', 'D') print(y) ```
108,412
Provide a correct Python 3 solution for this coding contest problem. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD "Correct Solution: ``` T = input().replace('?', 'D') print(T) ```
108,413
Provide a correct Python 3 solution for this coding contest problem. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD "Correct Solution: ``` T = input() _T = T.replace('?','D') print(_T) ```
108,414
Provide a correct Python 3 solution for this coding contest problem. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD "Correct Solution: ``` t=input() for i in t: print('P' if i=='P' else 'D',end='') ```
108,415
Provide a correct Python 3 solution for this coding contest problem. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD "Correct Solution: ``` t = str(input()) ans = t.replace('?', 'D') print(ans) ```
108,416
Provide a correct Python 3 solution for this coding contest problem. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD "Correct Solution: ``` T = input() D = T.replace("?","D") print(D) ```
108,417
Provide a correct Python 3 solution for this coding contest problem. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD "Correct Solution: ``` s = input() while "?" in s: s = s.replace("?","D") print(s) ```
108,418
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD Submitted Solution: ``` t = input() tmp = t.replace('?', 'D') print(tmp) ``` Yes
108,419
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD Submitted Solution: ``` t=str(input()) t=t.replace('?','D') print(t) ``` Yes
108,420
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD Submitted Solution: ``` T = input() s = T.replace("?","D") print(s) ``` Yes
108,421
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD Submitted Solution: ``` T=input() tmp=T.replace('?','D') print(tmp) ``` Yes
108,422
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD Submitted Solution: ``` word = input() start = 0 end = len(word) while(True): i = word.find('?', start, end) if (i == -1): break elif ((i == (end-1)) or (word[i-1] == 'P')): word.replace('?', "D", 1) elif ((word[i+1] == "?") or (word[i+1] == "D")): word.replace('?', "P", 1) start = i + 1 print(word) ``` No
108,423
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD Submitted Solution: ``` if __name__ == "__main__": t = input() result = "" pre_c = "" for curr_c in t: curr_c = "D" if pre_c == "P" else "P" result += curr_c pre_c = curr_c print(result) ``` No
108,424
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD Submitted Solution: ``` # coding: utf-8 # Your code here! s = list(input()) #print(s) for i,j in enumerate(s): if j == "?": if (s[i - 1] == "P") or (s[i + 1] == "P"): s[i] = "D" for k in s: print(k,end="") ``` No
108,425
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a string S consisting of the uppercase English letters `P` and `D`, let the doctoral and postdoctoral quotient of S be the total number of occurrences of `D` and `PD` in S as contiguous substrings. For example, if S = `PPDDP`, it contains two occurrences of `D` and one occurrence of `PD` as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3. We have a string T consisting of `P`, `D`, and `?`. Among the strings that can be obtained by replacing each `?` in T with `P` or `D`, find one with the maximum possible doctoral and postdoctoral quotient. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `P`, `D`, and `?`. Input Input is given from Standard Input in the following format: T Output Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each `?` in T with `P` or `D`. If there are multiple such strings, you may print any of them. Examples Input PD?D??P Output PDPDPDP Input P?P? Output PDPD Submitted Solution: ``` T = input() ans = T[-1] if T[-1] == '?': ans = 'D' for i in range(1, len(T)): s, t = T[-1-i], T[-i] if s == '?': if t == 'D' : ans += 'P' elif t == 'P': ans += 'D' else: if ans[i-1] == 'D': ans += 'P' else: ans += 'D' else: ans += s print(ans[::-1]) ``` No
108,426
Provide a correct Python 3 solution for this coding contest problem. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 "Correct Solution: ``` from fractions import gcd N = int(input()) A = list(map(int, input().split())) MOD = 1000000007 lcm = 1 for a in A: lcm = a // gcd(lcm, a) * lcm ans = 0 for a in A: ans += lcm // a ans %= MOD print(ans) ```
108,427
Provide a correct Python 3 solution for this coding contest problem. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 "Correct Solution: ``` # from math import gcd from fractions import gcd n = int(input()) a = list(map(int,input().split())) ans = 1 for i in a: ans = ans * i // gcd(ans, i) mod = 10**9+7 print(sum(map(lambda x: ans // x, a)) % mod) ```
108,428
Provide a correct Python 3 solution for this coding contest problem. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 "Correct Solution: ``` from fractions import * n,*a=map(int,open(0).read().split()) mod=10**9+7 b=a[0] for i in a: b=b*i//gcd(b,i) print(sum(b//i for i in a)%mod) ```
108,429
Provide a correct Python 3 solution for this coding contest problem. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 "Correct Solution: ``` from fractions import gcd from functools import reduce N = int(input()) A = list(map(int, input().split())) m = 1000000007 lcm = reduce(lambda a, b: a * b // gcd(a, b), A) print(sum(lcm // a for a in A) % m) ```
108,430
Provide a correct Python 3 solution for this coding contest problem. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 "Correct Solution: ``` import fractions n=int(input()) l=list(map(int,input().split())) b=0 c=0 for i in range(n): if (i==0): b=l[i] else: b=b*l[i]//fractions.gcd(l[i],b) for i in range(n): c+=b//l[i] print(c%1000000007) ```
108,431
Provide a correct Python 3 solution for this coding contest problem. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 "Correct Solution: ``` from fractions import gcd MOD = 10**9+7 N = int(input()) A = list(map(int, input().split())) lcm = 1 for a in A: lcm *= a//gcd(lcm, a) print(sum(lcm//a for a in A)%MOD) ```
108,432
Provide a correct Python 3 solution for this coding contest problem. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 "Correct Solution: ``` import fractions def lcm(a, b): return a*b // fractions.gcd(a,b) N = int(input()) nums = list(map(int, input().split())) num = 1 for i in nums: num = lcm(num, i) print(sum([num // i for i in nums]) % (10 ** 9 + 7)) ```
108,433
Provide a correct Python 3 solution for this coding contest problem. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 "Correct Solution: ``` from functools import reduce from fractions import gcd n=int(input()) A=list(map(int,input().split())) lc=reduce(lambda a,b: a*b//gcd(a,b),A) b=[lc//i for i in A] print(sum(b)%(1000*1000000+7)) ```
108,434
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 Submitted Solution: ``` N = int(input()) A = list(map(int, input().split())) mod = 10 ** 9 + 7 from fractions import gcd L = A[0] for i in range(1,N): L=(L*A[i])//gcd(L,A[i]) ans = 0 for i in range(N): ans += (L // A[i]) print(ans % mod) ``` Yes
108,435
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 Submitted Solution: ``` #逆元しない場合 from fractions import gcd n=int(input()) a=list(map(int,input().split())) mod=10**9+7 L=a[0] def lcm(x,y): return x//gcd(x,y)*y for i in a: L=lcm(L,i) print(sum([L//i for i in a])%mod) ``` Yes
108,436
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 Submitted Solution: ``` n=int(input()) a=[int(j) for j in input().split()] t=1 mod=10**9+7 from fractions import* for i in a: t*=i//gcd(t,i) print(sum(t//i for i in a)%mod) ``` Yes
108,437
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 Submitted Solution: ``` from fractions import gcd;from functools import reduce;n,*a=map(int,open(0).read().split());l=reduce(lambda a,b:a*b//gcd(a,b),a);print(sum(l//i for i in a)%1000000007) ``` Yes
108,438
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 Submitted Solution: ``` from fractions import gcd def main(): N = int(input()) A = list(map(int, input().split())) MOD = 10**9 + 7 lcm = 1 for a in A: lcm = lcm * a // gcd(lcm, a) ans = 0 for a in A: ans += lcm // a print(ans % MOD) if __name__ == "__main__": main() ``` No
108,439
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 Submitted Solution: ``` from functools import reduce from math import gcd def lcm(x, y): return x*y//gcd(x, y) MOD = 10**9 + 7 N = int(input()) A = list(map(int, input().split())) x = reduce(lcm, A) ans = 0 for a in A: ans += x // a ans %= MOD print(ans) ``` No
108,440
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 Submitted Solution: ``` from fractions import gcd def lcm(a,b): return a*(b//gcd(a,b)) mod=10**9+7 n=int(input()) A=list(map(int,input().split())) L=A[0] for i in range(1,n): L=lcm(L,A[i]) ans=0 for i in range(n): ans+=(L//A[i])%mod print(ans%mod) ``` No
108,441
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508 Submitted Solution: ``` N = int(input()) A = list(map(int, input().split())) if N == 1: print(1) exit() def saisho_ko_baisu(A,B): # A < B x = A*B r = B%A while r!=0: B = A A = r r = B%A return x//A A.sort(reverse = True) tmp = saisho_ko_baisu(A[0], A[1]) for i in range(2,N): if tmp % A[i] != 0: tmp = saisho_ko_baisu(A[i], tmp) res = 0 for a in A: res += tmp//a print(res % (10**9 + 7)) ``` No
108,442
Provide a correct Python 3 solution for this coding contest problem. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 "Correct Solution: ``` # -*- coding: utf-8 -*- MOD = 10**9+7 n = int(input()) s = input() if s[0]=="W" or s[-1]=="W": print(0) exit() lr = ["L"] for i in range(1,2*n): if s[i-1]==s[i]: if lr[i-1]=="L": lr.append("R") else: lr.append("L") else: lr.append(lr[i-1]) la = [0 for _ in range(2*n+1)] ra = [0 for _ in range(2*n+1)] for i in range(2*n): if lr[i]=="L": la[i+1] = la[i]+1 ra[i+1] = ra[i] else: la[i+1] = la[i] ra[i+1] = ra[i]+1 if la[-1]!=ra[-1]: print(0) exit() res = 1 for i in range(2*n): if lr[i]=="R": res *= la[i]-ra[i] res %= MOD for i in range(2,n+1): res = (res*i)%MOD print(res) ```
108,443
Provide a correct Python 3 solution for this coding contest problem. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 "Correct Solution: ``` #各マスがちょうど 1 回ずつ選ばれる n=int(input()) s=input() d=[False] for i in range(1,n*2): d.append((s[i]==s[i-1])^d[-1]) from collections import Counter cd =Counter(d) if not cd[True] or cd[0]!=cd[1] or s[0]=="W": print(0) exit() st=d.index(True) l=st ans=1;mod=10**9+7 for i in range(st,2*n): if d[i]: ans*=l l-=1 ans%=mod else: l+=1 for i in range(1,n+1): ans*=i ans%=mod print(ans) ```
108,444
Provide a correct Python 3 solution for this coding contest problem. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 "Correct Solution: ``` from itertools import groupby import heapq N=int(input()) S=input() mod=10**9+7 fact=[i for i in range(2*N+1)] fact[0]=1 for i in range(1,2*N+1): fact[i]*=fact[i-1] fact[i]%=mod inverse=[pow(fact[i],mod-2,mod) for i in range(2*N+1)] data=["" for i in range(2*N)] for i in range(2*N): if S[i]=="B" and i%2==1: data[i]="L" elif S[i]=="B" and i%2==0: data[i]="R" elif S[i]=="W" and i%2==1: data[i]="R" elif S[i]=="W" and i%2==0: data[i]="L" if data[0]=="L" or data[-1]=="R": print(0) exit() ans=fact[N] data=groupby(data) que=[] heapq.heapify(que) i=0 for key,group in data: g=len(list(group)) heapq.heappush(que,(i,g)) i+=1 rest=0 while que: i,R=heapq.heappop(que) j,L=heapq.heappop(que) if L>R+rest: print(0) break else: ans*=fact[R+rest]*inverse[R+rest-L] ans%=mod rest=R+rest-L else: if rest==0: print(ans) else: print(0) ```
108,445
Provide a correct Python 3 solution for this coding contest problem. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 "Correct Solution: ``` n = int(input()) S = [v for v in input()] # determine L or R lst = [0 for i in range(2 * n)] for i in range(2 * n): if S[i] == 'W' and (2 * n - i - 1) % 2 == 0: lst[i] = 'L' elif S[i] == 'W' and (2 * n - i - 1) % 2 == 1: lst[i] = 'R' elif S[i] == 'B' and (2 * n - i - 1) % 2 == 0: lst[i] = 'R' elif S[i] == 'B' and (2 * n - i - 1) % 2 == 1: lst[i] = 'L' deq = 0 val = 1 #tracing lst for i in range(2 * n): if lst[i] == 'L': deq += 1 elif lst[i] == 'R': val *= deq val %= 10 ** 9 + 7 deq -= 1 if deq != 0: print(0) else: for i in range(1, n + 1): val = val * i % (10 ** 9 +7) print(val) ```
108,446
Provide a correct Python 3 solution for this coding contest problem. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 "Correct Solution: ``` # c M = 10**9+7 n = int(input()) s = input() if (s[0] != 'B') | (s[-1] != 'B'): print(0); exit() a = [1]+[0]*(2*n-1) b = [0]*2*n for i in range(1, 2*n): if s[i] == s[i-1]: a[i] = 1^a[i-1] b[i] = 1^b[i-1] else: a[i] = a[i-1] b[i] = b[i-1] if (sum(a) != sum(b)) | (b[-1] == 0): print(0); exit() for i in range(1, 2*n): a[i] += a[i-1]-b[i] c = 1 for i, j in zip(a, b[1:]): if j: c *= i*j; c %= M for i in range(1, n+1): c = c*i%M print(c) ```
108,447
Provide a correct Python 3 solution for this coding contest problem. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 "Correct Solution: ``` import sys import math from pprint import pprint def solve(n, s): ans = 0 if s[0] == "W" or s[2*n-1] == "W": return ans l_c = 0 r_c = 0 o_c = 1 mod = 10**9 + 7 for s_i in s: if s_i == "B" and (l_c - r_c) % 2 == 0: l_c += 1 elif s_i == "W" and (l_c - r_c) % 2 == 0: o_c *= l_c - r_c o_c %= mod r_c += 1 elif s_i == "B" and (l_c - r_c) % 2 == 1: o_c *= l_c - r_c o_c %= mod r_c += 1 elif s_i == "W" and (l_c - r_c) % 2 == 1: l_c += 1 # print(s_i, l_c, r_c, o_c) if l_c - r_c < 0: return 0 # print(l_c, r_c, o_c) if l_c - r_c > 0: return 0 return (o_c * math.factorial(n)) % mod if __name__ == '__main__': n = int(sys.stdin.readline().strip()) s = list(sys.stdin.readline().strip()) print(solve(n, s)) ```
108,448
Provide a correct Python 3 solution for this coding contest problem. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 "Correct Solution: ``` import bisect N = int(input()) S = input() mod = 10 ** 9 + 7 small = [] large = [] for i in range(N * 2): if S[i] == 'B': if i % 2 == 0: small.append(i) else: large.append(i) else: if i % 2 == 0: large.append(i) else: small.append(i) if len(small) != len(large): print(0) else: ans = 1 for i in range(N): large_i = large[i] smaller = bisect.bisect_left(small, large_i) ans = (ans * (smaller - i)) % mod for i in range(1, N + 1): ans = (ans * i) % mod print(ans) ```
108,449
Provide a correct Python 3 solution for this coding contest problem. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 "Correct Solution: ``` import math N = int(input()) S = input() mod = 10**9 + 7 left = 0 ret = 1 for i in range(len(S)): if S[i] == 'W': if left % 2 == 1: left += 1 else: if left > 0: ret *= left ret %= mod left -= 1 else: print(0) exit() else: if left % 2 == 1: ret *= left ret %= mod left -= 1 else: left += 1 if left > 0: print(0) exit() for i in range(1, N+1): ret *= i ret %= mod print(ret) ```
108,450
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 Submitted Solution: ``` import sys, math n = int(input()) s = list(input()) m = 10**9+7 store = 0 ans = 1 for i in range(2*n): if i % 2 == 0: if s[i] == "B": s[i] = "W" else: s[i] = "B" for i in range(2*n): if s[i] == "W": store += 1 else: if store <= 0: print(0) sys.exit() ans = ans % m * store store -= 1 if not store == 0: print(0) sys.exit() print(ans*math.factorial(n) % m) ``` Yes
108,451
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 Submitted Solution: ``` def f_c(): n = int(input()) s = input() q = 10**9+7 stack, c = 1, 1 for si in s[1:]: if si=="B": if stack%2==0: stack += 1 else: c *= stack stack -= 1 else: if stack%2==0: c *= stack stack -= 1 else: stack += 1 c %= q if stack!=0 or c==0: print(0) else: for i in range(1, n+1): c *= i c %= q print(c) if __name__ == "__main__": f_c() ``` Yes
108,452
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 Submitted Solution: ``` n=int(input()) n*=2 A=list(input()) mod=10**9+7 if A[0]=="W" or A[-1]=="W": print(0);exit() A=[i=="B" for i in A] LR=[""]*n LR[0]="L" for i in range(n-1): LR[i+1]=LR[i] if A[i]!=A[i+1] else ["L","R"][LR[i]=="L"] from collections import Counter #print(LR) C=Counter(LR) if C["L"]!=C["R"]:print(0);exit() if LR[-1]=="L": print(0);exit() r=0 ans=1 n//=2 for s in range(2*n-1,-1,-1): if LR[s]=="R": r+=1 else: ans*=r ans%=mod r-=1 for i in range(1,n+1): ans*=i ans%=mod print(ans) ``` Yes
108,453
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 Submitted Solution: ``` MOD = 10 ** 9 + 7 n = int(input()) s = list(str(input())) if s[0] == "W" or s[-1] == "W": print(0) else: dir = [0] * (2*n) for i in range(1,2*n): if s[i] != s[i-1]: dir[i] = dir[i-1] else: dir[i] = (dir[i-1] + 1) % 2 c = 0 for x in dir: if x == 0: c += 1 #print(c) #print(dir) if c != n: print(0) else: ans = 1 count = 0 for i in range(2*n): if dir[i] == 0: count += 1 else: ans = (ans * count) % MOD count -= 1 for k in range(1,n+1): ans = (ans * k) % MOD print(ans) ``` Yes
108,454
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 Submitted Solution: ``` def main(): import sys def input(): return sys.stdin.readline()[:-1] N = int(input()) S = list(input()) if S[0]!='B' or S[-1]!='B': print(0) exit() mod = 10**9 + 7 cnt_l = 1 cnt = 1 pre = 'B' d = 1 #1がleft ans = 1 for i in range(1,N*2): if S[i-1]==S[i]: if d: d = 0 ans *= cnt%mod cnt -= 1 else: d = 1 cnt_l += 1 cnt += 1 else: if d: cnt_l += 1 cnt += 1 else: ans *= cnt%mod cnt -= 1 for i in range(1,N+1): ans *= i ans %= mod if cnt_l!=N: print(0) exit() print(ans) if __name__=='__main__': main() ``` No
108,455
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 Submitted Solution: ``` import math n = int(input()) s = input() count = 0 ans = 1 for i, x in enumerate(s): if (i%2 == 0 and x == "B") or (i%2 == 1 and x == "W"): # B:1(R), W:0(L) count += 1 else: ans *= count count -= 1 ans *= math.factorial(n) if count != 0: print(0) else: print(ans%(10**9+7)) ``` No
108,456
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 Submitted Solution: ``` import sys input = sys.stdin.readline sys.setrecursionlimit(10**7) n = int(input()) s = input().rstrip() MOD = 10**9+7 cnt = 0 point = 1 for i, x in enumerate(s): if (x == 'B' and i % 2 == 0) or (x == 'W' and i % 2 != 0): cnt += 1 else: point *= cnt cnt -= 1 # factorial for x in range(1, n+1): point *= x point %= MOD print(point) ``` No
108,457
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares. The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`. You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa. Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once. Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7. Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different. Constraints * 1 \leq N \leq 10^5 * |S| = 2N * Each character of S is `B` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0. Examples Input 2 BWWB Output 4 Input 4 BWBBWWWB Output 288 Input 5 WWWWWWWWWW Output 0 Submitted Solution: ``` import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) MOD = 10**9 + 7 N = int(input()) S = input().rstrip() is_left = [(i%2 == 0) ^ (x == 'W') for i,x in enumerate(S)] answer = 1 left = 0 for bl in is_left: if bl: left += 1 else: answer *= left left -= 1 answer %= MOD for i in range(1,N+1): answer *= i answer %= MOD print(answer) ``` No
108,458
Provide a correct Python 3 solution for this coding contest problem. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 "Correct Solution: ``` def gcd(a, b): while b: a, b = b, a % b return a def prime_decomposition(n): i = 2 d = {} while i * i <= n: while n % i == 0: n //= i if i not in d: d[i] = 0 d[i] += 1 i += 1 if n > 1: if n not in d: d[n] = 1 return d def eratosthenes(n): if n < 2: return [] prime = [] limit = n**0.5 numbers = [i for i in range(2,n+1)] while True: p = numbers[0] if limit <= p: return prime + numbers prime.append(p) numbers = [i for i in numbers if i%p != 0] return prime def ok(p): if A[0]%p != 0: return False B = [A[i]%p for i in range(1,N+1)] mod = [0]*(p-1) for i in range(N): mod[i%(p-1)] += B[i] mod[i%(p-1)] %= p return sum(mod)==0 N = int(input()) A = [int(input()) for i in range(N+1)][::-1] g = abs(A[0]) for a in A: g = gcd(g,abs(a)) d = prime_decomposition(g) ans = [p for p in d] prime = eratosthenes(N+1) for p in prime: if ok(p): ans.append(p) ans = list(set(ans)) ans.sort() for p in ans: print(p) ```
108,459
Provide a correct Python 3 solution for this coding contest problem. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 "Correct Solution: ``` def gcd(a, b): a, b = max(a, b), min(a, b) while a % b > 0: a, b = b, a%b return b def divisor(a): i = 1 divset = set() while i * i <= a: if a % i == 0: divset |= {i, a//i} i += 1 divset.remove(1) return divset def is_prime(a): if a <= 3: prime = [False, False, True, True] return prime[a] i = 2 while i * i <= a: if a % i == 0: return False i += 1 else: return True N = int(input()) A = [None] * (N+1) for i in reversed(range(N+1)): A[i] = int(input()) primes_bool = [True] * (N + 1) primes = [] for p in range(2, N+1): if primes_bool[p]: primes.append(p) p_mult = p * 2 while p_mult <= N: primes_bool[p_mult] = False p_mult += p ans = [] used = set() gcd_of_A = abs(A[N]) for a in A[:N]: if a != 0: gcd_of_A = gcd(abs(a), gcd_of_A) commondiv = divisor(gcd_of_A) for d in commondiv: if d > 1 and is_prime(d): ans.append(d) used |= {d} for p in primes: if A[0] % p == 0: for i in range(1, p): coefficient = 0 index_a = i while index_a <= N: coefficient += A[index_a] coefficient %= p index_a += p-1 if coefficient > 0: break else: if p not in used: ans.append(p) ans.sort() for a in ans: print(a) ```
108,460
Provide a correct Python 3 solution for this coding contest problem. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 "Correct Solution: ``` def factorization(n): arr = [] temp = n for i in range(2, int(-(-n**0.5//1))+1): if temp%i==0: cnt=0 while temp%i==0: cnt+=1 temp //= i arr.append([i, cnt]) if temp!=1: arr.append([temp, 1]) if arr==[]: arr.append([n, 1]) return arr sosuu=[2];n=int(input()) for L in range(3,n+100): chk=True for L2 in sosuu: if L%L2 == 0:chk=False if chk==True:sosuu.append(L) S=set(sosuu) A=[0]*(n+1) g=0 for i in range(n+1): A[i]=int(input()) P=[] PP=factorization(abs(A[0])) for i in range(len(PP)): P.append(PP[i][0]) if P==[1]: P=[] P=set(P) P=S|P P=list(P) P.sort() for i in range(len(P)): p=P[i] B=A[0:n+2] b=0 for j in range(n+1): if p+j<n+1: B[j+p-1]=(B[j+p-1]+B[j])%p else: if B[j]%p!=0: b=1 break if b==0: print(p) ```
108,461
Provide a correct Python 3 solution for this coding contest problem. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 "Correct Solution: ``` def prime_fact(n: int)->list: '''n の素因数を返す ''' if n < 2: return [] d = 2 res = [] while n > 1 and d * d <= n: if n % d == 0: res.append(d) while n % d == 0: n //= d d += 1 if n > 1: # n が素数 res.append(n) return res def gen_primes(n: int)->int: '''n まで(n 含む)の素数を返します。 ''' if n < 2: return [] primes = [2] if n < 3: return primes primes.append(3) def is_prime(n: int)->bool: for p in primes: if n % p == 0: return False return True # 2,3 以降の素数は 6k+1 あるいは 6k-1 # の形をしていることを利用して列挙する。 for k in range((N+1)//6): k += 1 if is_prime(6*k-1): primes.append(6*k-1) if is_prime(6*k+1): primes.append(6*k+1) return primes def gcd(a: int, b: int)->int: if a < b: a, b = b, a return a if b == 0 else gcd(b, a % b) def gcd_list(A: list)->int: if len(A) == 0: return 0 if len(A) == 1: return A[0] g = abs(A[0]) for a in A[1:]: g = gcd(g, abs(a)) return g def polynominal_divisors(N: int, A: list)->list: # primes = prime_fact(abs(A[-1])) A.reverse() # 候補となる素数は、N 以下の素数あるいは、aN の素因数 def check(p: int)->bool: '''素数 p が任意の整数 x について f(x) を割り切れるかを check する。具体的には、 - a0 - a1 + ap + a{2p-1} + ... - a2 + a{p+1} + a{2p} + ... ... - a{p-1} + a{2p-2} + ... という項が p で割り切れるかを検査する。 ''' if A[0] % p != 0: return False for i in range(1, min(N, p)): # ai + a{i+p-1} + ... を求めて # p で割り切れるか検査する。 # print('p={}'.format(p)) b = 0 while i <= N: # print('i={}'.format(i)) b = (b + A[i]) % p i += p-1 if b != 0: return False return True primes = set(gen_primes(N) + prime_fact(gcd_list(A))) res = [p for p in primes if check(p)] res.sort() return res if __name__ == "__main__": N = int(input()) A = [int(input()) for _ in range(N+1)] ans = polynominal_divisors(N, A) for a in ans: print(a) ```
108,462
Provide a correct Python 3 solution for this coding contest problem. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 "Correct Solution: ``` import sys from fractions import gcd n = int(input()) a = [int(input()) for _ in range(n+1)] x = 0 while True: m = abs(sum([a[i]*pow(x, n-i) for i in range(n+1)])) if m != 0: break x += 1 ps = [] i = 2 while i**2 <= m and i <= n+1: #for i in range(2, int(sqrt(m))+1): if m%i == 0: ps.append(i) while m%i == 0: m //= i i += 1 if m != 1 and n != 0: ps.append(m) #print(ps) g = a[0] for i in range(1, n+1): g = gcd(g, abs(a[i])) #print(g) ans = [] i = 2 while i**2 <= g: #for i in range(2, int(sqrt(m))+1): if g%i == 0: if i >= n+1: ans.append(i) while g%i == 0: g //= i i += 1 if g != 1 and g >= n+1: ans.append(g) a = a[::-1] for p in ps: if p-1 > n: ng = False for i in range(1, n+1): if a[i]%p != 0: ng = True break if not ng: ans.append(p) else: mods = [0 for _ in range(p-1)] for i in range(1, n+1): mods[i%(p-1)] += a[i] mods[i%(p-1)] %= p if mods[0] == 0: ng = False for i in range(1, p-1): if mods[i] != 0: ng = True break if not ng: ans.append(p) ans = sorted(list(set(ans))) if ans: print(*ans, sep="\n") ```
108,463
Provide a correct Python 3 solution for this coding contest problem. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 "Correct Solution: ``` from functools import reduce import random MOD = 59490679579998635868564022242503445659680322440679327938309703916140405638383434744833220555452130558140772894046169700747186409941317550665821619479140098743288335106272856533978719769677794737287419461899922456614256826709727133515885581878614846298251428096512495590545989536239739285290509870860732435109794045491640561011775744617218426011793283330497878491137999673760740717904979730106994180691956740996319139767941335979146924614249205862051182094081815447356600355657858936442589201739350474320966093143098768914226528498860747050474426418211313776073365498029087492308102876323562641651940555529817223923967479711293692697592800750806084802623966596666156361791014935954720420383555300213829134029560826306201326905674141090667002959274513478562033467485802590354539887133866434256562999666615074220857502563011098759348850149774083443643246907501430076141377431759984926248272553982527014011245310690986644461251576837779050124866641531488351016070300247918750613207952783437248236577750579092103591540592540838395240861443615842939673889972504776455114767522430347610500271801944701491874649306605557851914677316523034726044356368337787113190245601235705959816327016988315321850324065292964680148884818699916224411245926491625245571963741062587277736372090007125908660590314049976206281064703285728879581199596401313352724403492043120507597057004633720111008838673185885941472392623805512709896864520875740497617898566981218781313900004406341154884180472153171064617953661517880143988867189622824081729539392205701820144922307223627345876707465251206005262622236311161449785941834002795854996108322186230425179217618301526151712928790928933798369678844576216735378555233905935973195721247604933753363412045618703247367192610615234835041956551569232557323060407648325565048712478527583315981204846341857095134567470182330491262285172727037299211391244340592936174221176781260586188162350081192408213470101717320475998863222409777310443027421196981193126541663212124245716187453863438039402316877152286468198891603632606578778749292403571792687832081974134637014026451921536576338243322267400651083805535119025415817887075652758045539565968044552126338330231434466204888993650859153585380124240540573308417330478048240203241631072371322849430883727355239704116556046700749530006852187064160849175332758172150251213637470549781080491037088372092203085237973008861896576796238915011886636658033019385943299986285181723378096425117379056207797455963451889971988904466449319007760192467211209692128894691704353648198130409333996534250878389064152054828983825841234644875996912485916827004219887033833599723481903489316488764021700996686817244736947119285629049355809027206179193628292018441744552168286541735687924729198455883895791600170372255284216915442808139396541702893732917062958054499525549626455191658842064247047426187897146172971001949767308335268505414284088528125611263685734457560292833389995980698745893243832547007166243476192958601735336260255598581701267151224204461879782815468518040925292817115377696676461775120750971210951527384637825092221708015393564320979357698186262039029460777050248162599194429941464248920952161182024344007059684970270762248243899640259750891406957836740989312390963091260380990901672119736141666856448171380781556117025832312710039041398538035351795267732240730608951176127282191528457681241590895740457571038936173983449289126574141189374690274057472401359482497502067814596008557725079835212621242944853319496441084441343866446380876967613370281793088540430288658573281302876742323336651987699532240686371751448290351615451932054274752105688318766958111191822471878078268490672607804265064578569581247796205593441336042254502454646980009177290888726099355974892680373341371214123450598691915802122913613669446370194221846225597401405625536693874723700374068542217340621938985167525416725638580266200550048001242788847319217369432802469735271132107428204697172144851088692772696511622350096452418244968500432645305761138012204888798724453956720733374364721511323104353496583927094760495687785031050687852300161433757934364774351673531859394855389527851678630166084819717354779333605871648837631164630550613112327670353108353791304451834904017538583880634608113426156225757314283269948216017294095002859515842577481167417167637534527111130468710 def gcd(a, b): b = abs(b) while b != 0: r = a%b a,b = b,r return a def gcd_mult(numbers): return reduce(gcd, numbers) N = int(input()) A = [int(input()) for _ in range(N+1)][::-1] g = gcd_mult(A) A = [a//g for a in A] def f(n): ret = A[N] for i in range(N-1, -1, -1): ret = (ret * n + A[i]) % MOD return ret ans = MOD for _ in range(20): k = random.randrange(10**10) ans = gcd(ans, f(k)) def primeFactor(N): i = 2 ret = {} n = N if n < 0: ret[-1] = 1 n = -n if n == 0: ret[0] = 1 d = 2 sq = int(n ** (1/2)) while i <= sq: k = 0 while n % i == 0: n //= i k += 1 ret[i] = k if k > 0: sq = int(n**(1/2)) if i == 2: i = 3 elif i == 3: i = 5 elif d == 2: i += 2 d = 4 else: i += 4 d = 2 if n > 1: ret[n] = 1 return ret ANS = [] for i in primeFactor(ans*g): ANS.append(i) ANS = sorted(ANS) for ans in ANS: print(ans) ```
108,464
Provide a correct Python 3 solution for this coding contest problem. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 "Correct Solution: ``` import sys from fractions import gcd n = int(input()) a = [int(input()) for _ in range(n+1)] x = 0 while True: m = abs(sum([a[i]*pow(x, n-i) for i in range(n+1)])) if m != 0: break x += 1 ps = [] i = 2 while i**2 <= m and i <= n+1: if m%i == 0: ps.append(i) while m%i == 0: m //= i i += 1 if m != 1 and n != 0: ps.append(m) g = a[0] for i in range(1, n+1): g = gcd(g, abs(a[i])) ans = [] i = 2 while i**2 <= g: if g%i == 0: if i >= n+1: ans.append(i) while g%i == 0: g //= i i += 1 if g != 1 and g >= n+1: ans.append(g) a = a[::-1] for p in ps: if p-1 > n: ng = False for i in range(1, n+1): if a[i]%p != 0: ng = True break if not ng: ans.append(p) else: mods = [0 for _ in range(p-1)] for i in range(1, n+1): mods[i%(p-1)] += a[i] mods[i%(p-1)] %= p if mods[0] == 0: ng = False for i in range(1, p-1): if mods[i] != 0: ng = True break if not ng: ans.append(p) ans = sorted(list(set(ans))) if ans: print(*ans, sep="\n") ```
108,465
Provide a correct Python 3 solution for this coding contest problem. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 "Correct Solution: ``` def factors(z): ret = [] for i in range(2, int(z**(1/2))+1): if z%i == 0: ret.append(i) while z%i == 0: z //= i if z != 1: ret.append(z) return ret def eratosthenes(N): if N == 0: return [] from collections import deque work = [True] * (N+1) work[0] = False work[1] = False ret = [] for i in range(N+1): if work[i]: ret.append(i) for j in range(2* i, N+1, i): work[j] = False return ret N = int( input()) A = [ int( input()) for _ in range(N+1)] Primes = eratosthenes(N) ANS = [] F = factors( abs(A[0])) for f in F: if f >= N+1: Primes.append(f) for p in Primes: if p >= N+1: check = 1 for i in range(N+1): # f が恒等的に 0 であるかどうかのチェック if A[i]%p != 0: check = 0 break if check == 1: ANS.append(p) else: poly = [0]*(p-1) for i in range(N+1): # フェルマーの小定理 poly[(N-i)%(p-1)] = (poly[(N-i)%(p-1)] + A[i])%p check = 0 if sum(poly) == 0 and A[N]%p == 0: # a_0 が 0 かつ、g が恒等的に 0 であることをチェックしている check = 1 if check == 1: ANS.append(p) for ans in ANS: print(ans) ```
108,466
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 Submitted Solution: ``` # E # 入力 # input N = int(input()) a_list = [0]*(N+1) for i in range(N+1): a_list[N-i] = int(input()) # 10^5までの素数を計算 # list primes <= 10^5 primes = list(range(10**5)) primes[0] = 0 primes[1] = 0 for p in range(2, 10**5): if primes[p]: for i in range(2*p, 10**5, p): primes[i] = 0 primes = [p for p in primes if p>0] # primes <= N p_list_small = [p for p in primes if p <= N] # A_Nを素因数分解、Nより大きい素因数をp_list_largeに追加 # A_N, ..., A_0の最大公約数の素因数でやっても同じ # list prime factors of A_N, if greater then N, add to p_list_large # you can use prime factors of gcd(A_N, ..., A_0) instead p_list_large = [] A = abs(a_list[-1]) for p in primes: if A % p == 0: if p > N: p_list_large.append(p) while A % p == 0: A = A // p if A != 1: p_list_large.append(A) # 個別に条件確認 # check all p in p_list_small # x^p ~ x (mod p) res_list = [] for p in p_list_small: r = 0 check_list = [0]*(N+1) for i in range(p): check_list[i] = a_list[i] for i in range(p, N+1): check_list[(i-1) % (p-1) + 1] += a_list[i] for i in range(p): if check_list[i] % p != 0: r = 1 if r == 0: res_list.append(p) # 個別に条件確認 # check all p in p_list_large # a_i % p == 0 for p in p_list_large: r = 0 for i in range(N+1): if a_list[i] % p != 0: r = 1 if r == 0: res_list.append(p) for p in res_list: print(p) ``` Yes
108,467
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 Submitted Solution: ``` def gcd(x,y): if x<y: x,y=y,x if y==0: return x if x%y==0: return y else: return gcd(x%y,y) N=int(input()) a=[int(input()) for i in range(N+1)][::-1] #N=10000 #a=[i+1 for i in range(N+1)] g=abs(a[0]) for i in range(1,N+1): g=gcd(g,abs(a[i])) def check(P): b=[a[i]%P for i in range(1,N+1)] if a[0]%P!=0: return False if N<P: for i in b: if i!=0: return False return True else: c=[0 for i in range(P-1)] for i in range(N): c[i%(P-1)]+=b[i] c[i%(P-1)]%=P for i in c: if i!=0: return False return True Plist=set() X=[1 for i in range(max(N+1,3))] X[0]=0;X[1]=0 i=2 while(i*i<=N): for j in range(i,N+1,i): if i==j: if X[i]==0: break else: X[j]=0 i+=1 i=2 tmp=g while(i*i<=g): if tmp%i==0: Plist.add(i) while(1): if tmp%i==0: tmp=tmp//i else: break i+=1 if tmp>1: Plist.add(tmp) for j in range(N+1): if X[j]==1: Plist.add(j) Plist=sorted(list(Plist)) for p in Plist: if check(p): print(p) ``` Yes
108,468
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 Submitted Solution: ``` def p_factors(n): if n < 2: return [] d = 2 result = [] while n > 1 and d*d <= n: if n % d == 0: result.append(d) while n % d == 0: n //= d d += 1 if n > 1: # n is a prime result.append(n) return result def sieve_of_eratosthenes(ub): # O(n loglog n) implementation. # returns prime numbers <= ub. if ub < 2: return [] prime = [True]*(ub+1) prime[0] = False prime[1] = False p = 2 while p*p <= ub: if prime[p]: # don't need to check p+1 ~ p^2-1 (they've already been checked). for i in range(p*p, ub+1, p): prime[i] = False p += 1 return [i for i in range(ub+1) if prime[i]] def gcd(a, b): if a < b: a, b = b, a while b: a, b = b, a%b return a def gcd_list(lst): if len(lst) == 0: return 0 if len(lst) == 1: return lst[0] g = abs(lst[0]) for l in lst[1:]: g = gcd(g, abs(l)) return g N = int(input()) A = [int(input()) for _ in range(N+1)] cands = set(sieve_of_eratosthenes(N) + p_factors(gcd_list(A))) answer = [] A = list(reversed(A)) for p in cands: flag = True # f(x) = Q(x) (x^p - x) + \sum_{i = 0}^{p-1} h_i x^i (mod p), # where # h_0 = a_0 (i = 0) # h_i = \sum_{j < N, j % (p-1) = i} a_j (i = 1, ..., p-2) # h_{p-1} = \sum_{j < N, j % (p-1) = 0} a_j - a_0 (i = p-1) if A[0] % p != 0: flag = False else: for i in range(1, min(N, p-1) + 1): h = 0 j = i while j <= N: h = (h + A[j]) % p j += p - 1 if h != 0: flag = False break if flag: answer.append(p) if answer: answer = sorted(answer) print(*answer, sep='\n') ``` Yes
108,469
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 Submitted Solution: ``` import sys from fractions import gcd def eratosthenes_generator(): yield 2 n = 3 h = {} while True: m = n if n in h: b = h[n] del h[n] else: b = n yield n m += b << 1 while m in h: m += b << 1 h[m] = b n += 2 def prime(n): ret = [] if n % 2 == 0: ret.append(2) while n % 2 == 0: n >>= 1 for i in range(3, int(n ** 0.5) + 1, 2): if n % i == 0: ret.append(i) while n % i == 0: n = n // i if n == 1: break if n > 1: ret.append(n) return ret def solve(n, aaa): g = abs(aaa[-1]) for a in aaa[:-1]: if a != 0: g = gcd(g, abs(a)) ans = set(prime(g)) for p in eratosthenes_generator(): if p > n + 2: break if p in ans or aaa[0] % p != 0: continue q = p - 1 tmp = [0] * q for i, a in enumerate(aaa): tmp[i % q] += a if all(t % p == 0 for t in tmp): ans.add(p) ans = sorted(ans) return ans n = int(input()) aaa = list(map(int, sys.stdin)) aaa.reverse() print('\n'.join(map(str, solve(n, aaa)))) ``` Yes
108,470
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 Submitted Solution: ``` def prime_fact(n: int)->list: '''n の素因数を返す ''' if n < 2: return [] d = 2 res = [] while n > 1 and d * d <= n: if n % d == 0: res.append(d) while n % d == 0: n //= d d += 1 if n > 1: # n が素数 res.append(n) return res def gen_primes(n: int)->int: '''n まで(n 含む)の素数を返します。 ''' if n < 2: return [] primes = [2] if n < 3: return primes primes.append(3) def is_prime(n: int)->bool: for p in primes: if n % p == 0: return False return True # 2,3 以降の素数は 6k+1 あるいは 6k-1 # の形をしていることを利用して列挙する。 for k in range((N+1)//6): k += 1 if is_prime(6*k-1): primes.append(6*k-1) if is_prime(6*k+1): primes.append(6*k+1) return primes def polynominal_divisors(N: int, A: list)->list: # primes = prime_fact(abs(A[-1])) A.reverse() # 候補となる素数は、N 以下の素数あるいは、aN の素因数 def check(p: int)->bool: '''素数 p が任意の整数 x について f(x) を割り切れるかを check する。具体的には、 - a0 - a1 + ap + a{2p-1} + ... - a2 + a{p+1} + a{2p} + ... ... - a{p-1} + a{2p-2} + ... という項が p で割り切れるかを検査する。 ''' if A[0] % p != 0: return False for i in range(1, p): # ai + a{i+p-1} + ... を求めて # p で割り切れるか検査する。 b = 0 while i <= N: b = (b + A[i]) % p i += p-1 if b != 0: return False return True primes = set([p for p in gen_primes(N) if check(p)] + prime_fact(A[-1])) return sorted(primes) if __name__ == "__main__": N = int(input()) A = [int(input()) for _ in range(N+1)] ans = polynominal_divisors(N, A) for a in ans: print(a) ``` No
108,471
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 Submitted Solution: ``` def eratosthenes(n): l=[1]*n r=[] for i in range(2,n+1): #i=2 #while i<=n: if l[i-1]!=0: r+=[i] j=2 while i*j<=n: l[i*j-1]=0 j+=1 # i+=1 return r Ps = eratosthenes(40000) def divisor(n): for p in Ps: if p*p>n:return -1 if n%p==0:return p def prime_division(n): d={} while n>1: p=divisor(n) if p==-1:d[n]=d.get(n,0)+1;break else:d[p]=d.get(p,0)+1;n//=p return d import math from functools import reduce def gcd(*numbers): return reduce(math.gcd, numbers) N=int(input()) A=[int(input())for _ in[0]*-~N] G=sorted(set(prime_division(gcd(*A)).keys())|set(eratosthenes(N))) def div(p): if A[-1]%p!=0:return A[-1]%p l=[a%p for a in A] r=[a%p for a in A[:p]] ind=0 for i in range(N-p+1): a=r[ind];ind+=1 r[-1]=(r[-1]+a)%p r+=[l[p+i]] return sum(r[-p:]) for p in G: if div(p)==0:print(p) ``` No
108,472
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 Submitted Solution: ``` inpl = lambda: list(map(int,input().split())) N = int(input()) a = [] for i in range(N): a.append(int(input())) from functools import reduce import fraction #import math as fraction def gcd_list(numbers): return reduce(fraction.gcd, numbers) g = gcd_list(a) plist = [] while i*i <= g: if g % i == 0: while g % i == 0: g %= i i += 1 if g > 1: plist.append(g) for p in plist: print(p) ``` No
108,473
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x. Constraints * 0 \leq N \leq 10^4 * |a_i| \leq 10^9(0\leq i\leq N) * a_N \neq 0 * All values in input are integers. Input Input is given from Standard Input in the following format: N a_N : a_0 Output Print all prime numbers p that divide f(x) for every integer x, in ascending order. Examples Input 2 7 -7 14 Output 2 7 Input 3 1 4 1 5 Output Input 0 998244353 Output 998244353 Submitted Solution: ``` import math from fractions import gcd printn = lambda x: sys.stdout.write(x) inn = lambda : int(input()) inl = lambda: list(map(int, input().split())) inm = lambda: map(int, input().split()) DBG = True and False def ddprint(x): if DBG: print(x) def isprime(x): q = int(math.sqrt(x)) # int() rounds to zero for i in range(2,q+1): # upto q if x%i == 0: return False return True def prime_division(x): q = int(math.sqrt(x)) a = [] for i in range(2,q+1): ex = 0 while x%i == 0: ex += 1 x //= i if ex>0: a.append([i,ex]) if x>q: a.append([x,1]) return a n = inn() a = [0] * (n+1) for i in range(n+1): a[n-i] = inn() ddprint(a) g = a[n] for i in range(n): g = gcd(g, a[i]) p = prime_division(abs(g)) b = [] for x in p: if x[0] != 1: b.append(x[0]) ddprint("b:") ddprint(b) if a[0] != 0: p0 = prime_division(abs(a[0])) ps = [x[0] for x in p0 if x[0]<30] else: ps = [2,3,5,7,11,13,17,19,23,29] ddprint("ps:") ddprint(ps) for p in ps: if p in b: continue ok = True for r in range(1,p): v = a[0] r2i = 1 for i in range(1,n+1): r2i = (r2i * r) % p v = (v + a[i] * r2i) % p if v != 0: ok = False break if ok: b.append(p) b.sort() for x in b: print(x) ``` No
108,474
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 "Correct Solution: ``` n,k,*h=map(int,open(0).read().split());h.sort();print(min(h[i]-h[i-k+1]for i in range(k-1,n))) ```
108,475
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 "Correct Solution: ``` n,k=map(int,input().split()) h=sorted([int(input()) for i in range(n)]) a=[] for i in range(n-k+1): a.append(h[i+k-1]-h[i]) print(min(a)) ```
108,476
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 "Correct Solution: ``` n,k=map(int,input().split()) l=sorted([int(input()) for i in range(n)]) print(min([abs(l[i]-l[i+k-1]) for i in range(n-k+1)])) ```
108,477
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 "Correct Solution: ``` n,k = map(int, input().split()) h = sorted(list(map(int, [input() for _ in range(n)]))) print(min([h[i+k-1]-h[i] for i in range(n-k+1)])) ```
108,478
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 "Correct Solution: ``` N, K, *H = map(int, open(0).read().split()) H.sort() print(min(y - x for x, y in zip(H, H[K - 1:]))) ```
108,479
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 "Correct Solution: ``` N,K = map(int, input().split()) h = [int(input()) for i in range(N)] h.sort() ans = min(h[i+K-1] - h[i] for i in range(N-K+1)) print(ans) ```
108,480
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 "Correct Solution: ``` N, K = map(int, input().split()) h = sorted([int(input()) for _ in range(N)]) print(min([h[i+K-1]-h[i] for i in range(N - K + 1)])) ```
108,481
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 "Correct Solution: ``` n, k = map(int, input().split()) h = [int(input()) for i in range(n)] h.sort() print(min([h[i+k-1]-h[i] for i in range(n-k+1)])) ```
108,482
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 Submitted Solution: ``` N, K = map(int, input().split()) H = [int(input()) for _ in range(N)] H.sort() print(min(H[i+K-1] - H[i] for i in range(N-K+1))) ``` Yes
108,483
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 Submitted Solution: ``` (n, k) = map(int, input().split()) h = [int(input()) for _ in range(n)] h.sort() print(min(h[i+k-1] - h[i] for i in range(n-k+1))) ``` Yes
108,484
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 Submitted Solution: ``` n,k,*a=map(int,open(0).read().split());a.sort();print(min(a[s+k-1]-a[s]for s in range(n-k+1))) ``` Yes
108,485
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 Submitted Solution: ``` n, k = [int(x) for x in input().split()] h = sorted([int(input()) for i in range(n)]) print(min(h[i + k - 1] - h[i] for i in range(n - k + 1))) ``` Yes
108,486
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 Submitted Solution: ``` ## C - Christmas Eve N, K = map(int, input().split()) H = sorted([int(input()) for i in range(N)]) rt = 10**9 for i in range(0,N-K): rt = H[K - 1 + i] - H[i] if rt > H[K - 1 + i] - H[i] else rt print(rt) ``` No
108,487
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 Submitted Solution: ``` N, K = map(int, input().split()) hs = sorted([int(input()) for i in range(N)]) anslist = [] for h in range(N-K+1): anslist.append(abs(hs[h+K-1]-hs[N-(K-1)])) print(min(anslist)) ``` No
108,488
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 Submitted Solution: ``` n,k =map(int,input().split()) h = [int(input()) for _ in range(n)] l = [] h.sort() for i in range(n-1-k): x = h[i+k-1] - h[i] l.append(x) print(min(l)) ``` No
108,489
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? Constraints * 2 \leq K < N \leq 10^5 * 1 \leq h_i \leq 10^9 * h_i is an integer. Input Input is given from Standard Input in the following format: N K h_1 h_2 : h_N Output Print the minimum possible value of h_{max} - h_{min}. Examples Input 5 3 10 15 11 14 12 Output 2 Input 5 3 5 7 5 7 7 Output 0 Submitted Solution: ``` N, K = map(int, input().split()) H = [0] * N for i in range(N): H[i] = int(input()) H.sort() d = max(H) for i in range(N-K+1): d = min(max(H[i:i+K]) - min(H[i:i+K]), d) print(d) ``` No
108,490
Provide a correct Python 3 solution for this coding contest problem. There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black. Takahashi the human wants to achieve the following objective: * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it. * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it. In order to achieve this, he can perform the following operation: * Swap two adjacent balls. Find the minimum number of operations required to achieve the objective. Constraints * 1 ≤ N ≤ 2000 * 1 ≤ a_i ≤ N * c_i = `W` or c_i = `B`. * If i ≠ j, (a_i,c_i) ≠ (a_j,c_j). Input Input is given from Standard Input in the following format: N c_1 a_1 c_2 a_2 : c_{2N} a_{2N} Output Print the minimum number of operations required to achieve the objective. Examples Input 3 B 1 W 2 B 3 W 1 W 3 B 2 Output 4 Input 4 B 4 W 4 B 3 W 3 B 2 W 2 B 1 W 1 Output 18 Input 9 W 3 B 1 B 4 W 1 B 5 W 9 W 2 B 6 W 5 B 3 W 8 B 9 W 7 B 2 B 8 W 4 W 6 B 7 Output 41 "Correct Solution: ``` import sys input=sys.stdin.readline sys.setrecursionlimit(10 ** 6) def main(): inf = 5000 n = int(input()[:-1]) # iより前に色cのj以上が何個あるかがcnts[c][i][j] cnts = [[[0] * (n + 1) for _ in range(2 * n + 1)] for __ in range(2)] pos = [[0] * n for _ in range(2)] for i in range(n * 2): c, a = input().split() c = (c == "W") * 1 a = int(a) - 1 pos[c][a] = i for cc in range(2): for j in range(n): if c == cc and a >= j: cnts[cc][i + 1][j] = cnts[cc][i][j] + 1 else: cnts[cc][i + 1][j] = cnts[cc][i][j] # for x in cnts: # print(x) # print(pos) dp = [[inf] * (n + 1) for _ in range(n + 1)] dp[0][0] = 0 for w in range(1, n + 1): i = pos[1][w - 1] dp[0][w] = dp[0][w - 1] + cnts[0][i][0] + cnts[1][i][w - 1] for b in range(1, n + 1): i = pos[0][b - 1] dp[b][0] = dp[b - 1][0] + cnts[0][i][b - 1] + cnts[1][i][0] for b in range(1, n + 1): for w in range(1, n + 1): bi = pos[0][b - 1] wi = pos[1][w - 1] dp[b][w] = min(dp[b - 1][w] + cnts[0][bi][b - 1] + cnts[1][bi][w], dp[b][w - 1] + cnts[0][wi][b] + cnts[1][wi][w - 1]) print(dp[n][n]) main() ```
108,491
Provide a correct Python 3 solution for this coding contest problem. There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black. Takahashi the human wants to achieve the following objective: * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it. * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it. In order to achieve this, he can perform the following operation: * Swap two adjacent balls. Find the minimum number of operations required to achieve the objective. Constraints * 1 ≤ N ≤ 2000 * 1 ≤ a_i ≤ N * c_i = `W` or c_i = `B`. * If i ≠ j, (a_i,c_i) ≠ (a_j,c_j). Input Input is given from Standard Input in the following format: N c_1 a_1 c_2 a_2 : c_{2N} a_{2N} Output Print the minimum number of operations required to achieve the objective. Examples Input 3 B 1 W 2 B 3 W 1 W 3 B 2 Output 4 Input 4 B 4 W 4 B 3 W 3 B 2 W 2 B 1 W 1 Output 18 Input 9 W 3 B 1 B 4 W 1 B 5 W 9 W 2 B 6 W 5 B 3 W 8 B 9 W 7 B 2 B 8 W 4 W 6 B 7 Output 41 "Correct Solution: ``` class BIT():#1-indexed def __init__(self, size): self.table = [0 for _ in range(size+2)] self.size = size def Sum(self, i):#1からiまでの和 s = 0 while i > 0: s += self.table[i] i -= (i & -i) return s def Add(self, i, x):# while i <= self.size: self.table[i] += x i += (i & -i) return n = int(input()) s = [input().split() for _ in range(2*n)] costb = [[0 for _ in range(n+1)] for _ in range(n+1)] costw = [[0 for _ in range(n+1)] for _ in range(n+1)] bitb, bitw = BIT(n), BIT(n) for i in range(2*n-1, -1, -1): c, x = s[i][0], int(s[i][1]) if c == "B": base = bitb.Sum(x-1) for j in range(n+1): costb[x-1][j] = base + bitw.Sum(j) bitb.Add(x, 1) else: base = bitw.Sum(x-1) for j in range(n+1): costw[j][x-1] = base + bitb.Sum(j) bitw.Add(x, 1) dp = [[0 for _ in range(n+1)] for _ in range(n+1)] for i in range(n+1): for j in range(n+1): if i+j == 0: continue if i == 0: dp[i][j] = dp[i][j-1] + costw[i][j-1] elif j == 0: dp[i][j] = dp[i-1][j] + costb[i-1][j] else: dp[i][j] = min(dp[i][j-1] + costw[i][j-1], dp[i-1][j] + costb[i-1][j]) print(dp[n][n]) ```
108,492
Provide a correct Python 3 solution for this coding contest problem. There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black. Takahashi the human wants to achieve the following objective: * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it. * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it. In order to achieve this, he can perform the following operation: * Swap two adjacent balls. Find the minimum number of operations required to achieve the objective. Constraints * 1 ≤ N ≤ 2000 * 1 ≤ a_i ≤ N * c_i = `W` or c_i = `B`. * If i ≠ j, (a_i,c_i) ≠ (a_j,c_j). Input Input is given from Standard Input in the following format: N c_1 a_1 c_2 a_2 : c_{2N} a_{2N} Output Print the minimum number of operations required to achieve the objective. Examples Input 3 B 1 W 2 B 3 W 1 W 3 B 2 Output 4 Input 4 B 4 W 4 B 3 W 3 B 2 W 2 B 1 W 1 Output 18 Input 9 W 3 B 1 B 4 W 1 B 5 W 9 W 2 B 6 W 5 B 3 W 8 B 9 W 7 B 2 B 8 W 4 W 6 B 7 Output 41 "Correct Solution: ``` N = int(input()) X = [i for i in range(N+1)] Y = [[] for _ in range(N)] B, W = [], [] ans = 0 for i in range(2 * N): c, a = input().split() a = int(a) - 1 if c == "B": X = [X[i] + 1 if i <= a else X[i] - 1 for i in range(N+1)] B.append(a) ans += len([b for b in B if b > a]) else: Y[a] = X[:] W.append(a) ans += len([b for b in W if b > a]) Z = [0] * (N+1) for y in Y: for i in range(N+1): Z[i] += y[i] for i in range(1, N+1): Z[i] = min(Z[i], Z[i-1]) ans += Z[-1] print(ans) ```
108,493
Provide a correct Python 3 solution for this coding contest problem. There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black. Takahashi the human wants to achieve the following objective: * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it. * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it. In order to achieve this, he can perform the following operation: * Swap two adjacent balls. Find the minimum number of operations required to achieve the objective. Constraints * 1 ≤ N ≤ 2000 * 1 ≤ a_i ≤ N * c_i = `W` or c_i = `B`. * If i ≠ j, (a_i,c_i) ≠ (a_j,c_j). Input Input is given from Standard Input in the following format: N c_1 a_1 c_2 a_2 : c_{2N} a_{2N} Output Print the minimum number of operations required to achieve the objective. Examples Input 3 B 1 W 2 B 3 W 1 W 3 B 2 Output 4 Input 4 B 4 W 4 B 3 W 3 B 2 W 2 B 1 W 1 Output 18 Input 9 W 3 B 1 B 4 W 1 B 5 W 9 W 2 B 6 W 5 B 3 W 8 B 9 W 7 B 2 B 8 W 4 W 6 B 7 Output 41 "Correct Solution: ``` from itertools import accumulate def solve(n, rev): def existence_right(rev_c): n2 = n * 2 acc = [[0] * n2] row = [0] * n2 for x in rev_c: row[n2 - x - 1] += 1 acc.append(list(reversed(list(accumulate(row))))) return acc # How many white/black ball lower than 'k' righter than index x? (0<=x<=2N-1) # cost[color][k][x] cost = list(map(existence_right, rev)) dp = [0] + list(accumulate(c[y] for y, c in zip(rev[1], cost[1]))) for x, cw0, cw1 in zip(rev[0], cost[0], cost[0][1:]): ndp = [0] * (n + 1) cw0x = cw0[x] ndp[0] = prev = dp[0] + cw0x for b, (y, cb0, cb1) in enumerate(zip(rev[1], cost[1], cost[1][1:])): ndp[b + 1] = prev = min(dp[b + 1] + cw0x + cb1[x], prev + cw1[y] + cb0[y]) dp = ndp return dp[n] n = int(input()) # White/Black 'k' ball is what-th in whole row? rev = [[0] * n, [0] * n] for i in range(n * 2): c, a = input().split() a = int(a) - 1 rev[int(c == 'B')][a] = i print(solve(n, rev)) ```
108,494
Provide a correct Python 3 solution for this coding contest problem. There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black. Takahashi the human wants to achieve the following objective: * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it. * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it. In order to achieve this, he can perform the following operation: * Swap two adjacent balls. Find the minimum number of operations required to achieve the objective. Constraints * 1 ≤ N ≤ 2000 * 1 ≤ a_i ≤ N * c_i = `W` or c_i = `B`. * If i ≠ j, (a_i,c_i) ≠ (a_j,c_j). Input Input is given from Standard Input in the following format: N c_1 a_1 c_2 a_2 : c_{2N} a_{2N} Output Print the minimum number of operations required to achieve the objective. Examples Input 3 B 1 W 2 B 3 W 1 W 3 B 2 Output 4 Input 4 B 4 W 4 B 3 W 3 B 2 W 2 B 1 W 1 Output 18 Input 9 W 3 B 1 B 4 W 1 B 5 W 9 W 2 B 6 W 5 B 3 W 8 B 9 W 7 B 2 B 8 W 4 W 6 B 7 Output 41 "Correct Solution: ``` # copy # https://beta.atcoder.jp/contests/arc097/submissions/2509168 N = int(input()) balls = [] for _ in range(2 * N): c, a = input().split() balls += [(c, int(a))] # numB[i][b]: 初期状態において、i番目の玉より右にある、1~bが書かれた黒玉の数 numB = [[0] * (N + 1) for _ in range(2 * N)] numW = [[0] * (N + 1) for _ in range(2 * N)] for i in reversed(range(1, 2 * N)): c, a = balls[i] numB[i - 1] = list(numB[i]) numW[i - 1] = list(numW[i]) if c == 'B': for b in range(a, N + 1): numB[i - 1][b] += 1 else: for w in range(a, N + 1): numW[i - 1][w] += 1 # costB[b][w]: 黒玉1~b、白玉1~wのうち、初期状態において、黒玉[b+1]より右にある玉の数 costB = [[0] * (N + 1) for _ in range(N + 1)] costW = [[0] * (N + 1) for _ in range(N + 1)] for i in reversed(range(2 * N)): c, a = balls[i] if c == 'B': cost = numB[i][a - 1] for w in range(N + 1): costB[a - 1][w] = cost + numW[i][w] else: cost = numW[i][a - 1] for b in range(N + 1): costW[b][a - 1] = cost + numB[i][b] INF = 10 ** 9 + 7 # dp[b][w]: 黒玉1-b, 白玉1-wまで置いたときの転倒数の最小値 dp = [[INF] * (N + 1) for _ in range(N + 1)] dp[0][0] = 0 for b in range(N + 1): for w in range(N + 1): if b > 0: dp[b][w] = min(dp[b][w], dp[b - 1][w] + costB[b - 1][w]) if w > 0: dp[b][w] = min(dp[b][w], dp[b][w - 1] + costW[b][w - 1]) print(dp[N][N]) ```
108,495
Provide a correct Python 3 solution for this coding contest problem. There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black. Takahashi the human wants to achieve the following objective: * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it. * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it. In order to achieve this, he can perform the following operation: * Swap two adjacent balls. Find the minimum number of operations required to achieve the objective. Constraints * 1 ≤ N ≤ 2000 * 1 ≤ a_i ≤ N * c_i = `W` or c_i = `B`. * If i ≠ j, (a_i,c_i) ≠ (a_j,c_j). Input Input is given from Standard Input in the following format: N c_1 a_1 c_2 a_2 : c_{2N} a_{2N} Output Print the minimum number of operations required to achieve the objective. Examples Input 3 B 1 W 2 B 3 W 1 W 3 B 2 Output 4 Input 4 B 4 W 4 B 3 W 3 B 2 W 2 B 1 W 1 Output 18 Input 9 W 3 B 1 B 4 W 1 B 5 W 9 W 2 B 6 W 5 B 3 W 8 B 9 W 7 B 2 B 8 W 4 W 6 B 7 Output 41 "Correct Solution: ``` def examC(): ans = 0 print(ans) return def examD(): ans = 0 print(ans) return # 解説AC def examE(): N = I() Bnum = [-1]*N Wnum = [-1]*N B = {} W = {} for i in range(2*N): c, a = LSI() a = int(a)-1 if c=="W": W[i] = a+1 Wnum[a] = i else: B[i] = a+1 Bnum[a] = i SB = [[0]*(N*2) for _ in range(N+1)]; SW = [[0]*(N*2) for _ in range(N+1)] for i in range(2*N): if not i in B: continue cur = B[i] cnt = 0 SB[cur][i] = 0 for j in range(2*N): if not j in B: SB[cur][j] = cnt continue if B[j]<=cur: cnt += 1 SB[cur][j] = cnt for i in range(2*N): if not i in W: continue cur = W[i] cnt = 0 SW[cur][i] = 0 for j in range(2*N): if not j in W: SW[cur][j] = cnt continue if W[j]<=cur: cnt += 1 SW[cur][j] = cnt #for b in SB: #print(b) #print(SW) #print(SW,len(SW)) dp = [[inf]*(N+1) for _ in range(N+1)] dp[0][0] = 0 for i in range(N): dp[0][i+1] = dp[0][i]+Bnum[i]-SB[i][Bnum[i]] dp[i+1][0] = dp[i][0]+Wnum[i]-SW[i][Wnum[i]] #print(dp) for i in range(N): w = Wnum[i] for j in range(N): b = Bnum[j] costb = b - (SB[j][b]+SW[i+1][b]) costw = w - (SB[j+1][w]+SW[i][w]) #if i==2 and j==4: # print(w,SB[i][w],SW[j+1][w]) # print(b,SB[i][b],SW[j+1][b]) #print(costb,costw,i,j) #input() if costb<0: costb = 0 #print(i,j) if costw < 0: costw = 0 #print(i,j) dp[i+1][j+1] = min(dp[i+1][j+1],dp[i+1][j]+costb,dp[i][j+1]+costw) ans = dp[-1][-1] #for v in dp: # print(v) print(ans) return def examF(): ans = 0 print(ans) return from decimal import getcontext,Decimal as dec import sys,bisect,itertools,heapq,math,random from copy import deepcopy from heapq import heappop,heappush,heapify from collections import Counter,defaultdict,deque read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines def I(): return int(input()) def LI(): return list(map(int,sys.stdin.readline().split())) def DI(): return dec(input()) def LDI(): return list(map(dec,sys.stdin.readline().split())) def LSI(): return list(map(str,sys.stdin.readline().split())) def LS(): return sys.stdin.readline().split() def SI(): return sys.stdin.readline().strip() global mod,mod2,inf,alphabet,_ep mod = 10**9 + 7 mod2 = 998244353 inf = 10**18 _ep = dec("0.000000000001") alphabet = [chr(ord('a') + i) for i in range(26)] alphabet_convert = {chr(ord('a') + i): i for i in range(26)} getcontext().prec = 28 sys.setrecursionlimit(10**7) if __name__ == '__main__': examE() """ 142 12 9 1445 0 1 asd dfg hj o o aidn """ ```
108,496
Provide a correct Python 3 solution for this coding contest problem. There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black. Takahashi the human wants to achieve the following objective: * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it. * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it. In order to achieve this, he can perform the following operation: * Swap two adjacent balls. Find the minimum number of operations required to achieve the objective. Constraints * 1 ≤ N ≤ 2000 * 1 ≤ a_i ≤ N * c_i = `W` or c_i = `B`. * If i ≠ j, (a_i,c_i) ≠ (a_j,c_j). Input Input is given from Standard Input in the following format: N c_1 a_1 c_2 a_2 : c_{2N} a_{2N} Output Print the minimum number of operations required to achieve the objective. Examples Input 3 B 1 W 2 B 3 W 1 W 3 B 2 Output 4 Input 4 B 4 W 4 B 3 W 3 B 2 W 2 B 1 W 1 Output 18 Input 9 W 3 B 1 B 4 W 1 B 5 W 9 W 2 B 6 W 5 B 3 W 8 B 9 W 7 B 2 B 8 W 4 W 6 B 7 Output 41 "Correct Solution: ``` def main(): from sys import stdin input = stdin.readline n = int(input()) ab = [list(input().split()) for _ in [0]*(2*n)] ab = [(a == "B", int(b)-1) for a, b in ab] dct_b = [0]*n dct_w = [0]*n for i, (a, b) in enumerate(ab): if a: dct_b[b] = i else: dct_w[b] = i dp = [[10**9]*(n+1) for _ in [0]*(n+1)] # dp[i][j] = Bをi個、Wをj個置くための最小手数 dp[0][0] = 0 # 左i個にあるj以上のボールの数 lb = [[0]*(n+1) for _ in [0]*(2*n)] lw = [[0]*(n+1) for _ in [0]*(2*n)] for j in range(n): cnt1, cnt2 = 0, 0 for i, (a, b) in enumerate(ab): lb[i][j] = cnt1 lw[i][j] = cnt2 if a: if b >= j: cnt1 += 1 else: if b >= j: cnt2 += 1 dp = [[10**9]*(n+1) for _ in [0]*(n+1)] # dp[i][j] = Bをi個、Wをj個置くための最小手数 dp[0][0] = 0 for i in range(n+1): dp1 = dp[i] if i < n: dp2 = dp[i+1] t = dct_b[i] for j in range(n+1): d = dp1[j] if i < n: dp2[j] = min(dp2[j], d+lb[t][i]+lw[t][j]) if j < n: t2 = dct_w[j] dp1[j+1] = min(dp1[j+1], d+lb[t2][i]+lw[t2][j]) print(dp[n][n]) main() ```
108,497
Provide a correct Python 3 solution for this coding contest problem. There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black. Takahashi the human wants to achieve the following objective: * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it. * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it. In order to achieve this, he can perform the following operation: * Swap two adjacent balls. Find the minimum number of operations required to achieve the objective. Constraints * 1 ≤ N ≤ 2000 * 1 ≤ a_i ≤ N * c_i = `W` or c_i = `B`. * If i ≠ j, (a_i,c_i) ≠ (a_j,c_j). Input Input is given from Standard Input in the following format: N c_1 a_1 c_2 a_2 : c_{2N} a_{2N} Output Print the minimum number of operations required to achieve the objective. Examples Input 3 B 1 W 2 B 3 W 1 W 3 B 2 Output 4 Input 4 B 4 W 4 B 3 W 3 B 2 W 2 B 1 W 1 Output 18 Input 9 W 3 B 1 B 4 W 1 B 5 W 9 W 2 B 6 W 5 B 3 W 8 B 9 W 7 B 2 B 8 W 4 W 6 B 7 Output 41 "Correct Solution: ``` N = int(input()) MB = {}; MW = {} for i in range(2*N): c, a = input().split() c = "BW".index(c); a = int(a) if c: MW[a-1] = i else: MB[a-1] = i P = [[0]*(N+1) for i in range(N+1)] Q = [[0]*(N+1) for i in range(N+1)] for p in range(N): cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p)) P[p][0] = cnt for q in range(N): if MB[q] < MW[p]: cnt -= 1 P[p][q+1] = cnt for q in range(N): cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q)) Q[q][0] = cnt for p in range(N): if MW[p] < MB[q]: cnt -= 1 Q[q][p+1] = cnt dp = [[10**9]*(N+1) for i in range(N+1)] dp[0][0] = 0 for i in range(N): dp[i+1][0] = dp[i][0] + P[i][0] for i in range(N): dp[0][i+1] = dp[0][i] + Q[i][0] for i in range(N): for j in range(N): dp[i+1][j+1] = min(dp[i][j+1] + P[i][j+1], dp[i+1][j] + Q[j][i+1]) print(dp[N][N]) ```
108,498
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black. Takahashi the human wants to achieve the following objective: * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it. * For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it. In order to achieve this, he can perform the following operation: * Swap two adjacent balls. Find the minimum number of operations required to achieve the objective. Constraints * 1 ≤ N ≤ 2000 * 1 ≤ a_i ≤ N * c_i = `W` or c_i = `B`. * If i ≠ j, (a_i,c_i) ≠ (a_j,c_j). Input Input is given from Standard Input in the following format: N c_1 a_1 c_2 a_2 : c_{2N} a_{2N} Output Print the minimum number of operations required to achieve the objective. Examples Input 3 B 1 W 2 B 3 W 1 W 3 B 2 Output 4 Input 4 B 4 W 4 B 3 W 3 B 2 W 2 B 1 W 1 Output 18 Input 9 W 3 B 1 B 4 W 1 B 5 W 9 W 2 B 6 W 5 B 3 W 8 B 9 W 7 B 2 B 8 W 4 W 6 B 7 Output 41 Submitted Solution: ``` import sys input = sys.stdin.readline N = int(input()) c = [0] * (2 * N) a = [0] * (2 * N) for i in range(2 * N): c[i], a[i] = input().split() a[i] = int(a[i]) - 1 idx = [[0] * 2 for _ in range(N)] for i in range(2 * N): idx[a[i]][c[i] == 'W'] = i costB = [[0] * (N + 1) for _ in range(N)] for i in range(N): j = idx[i][0] cnt = 0 for k in range(i): if j < idx[k][0]: cnt += 1 costB[i][0] = j + cnt - i cnt2 = 0 for k in range(1, N + 1): if j < idx[k - 1][1]: cnt2 += 1 costB[i][k] = j + cnt + cnt2 - (i + k) costW = [[0] * (N + 1) for _ in range(N)] for i in range(N): j = idx[i][1] cnt = 0 for k in range(i): if j < idx[k][1]: cnt += 1 costW[i][0] = j + cnt - i cnt2 = 0 for k in range(1, N + 1): if j < idx[k - 1][0]: cnt2 += 1 costW[i][k] = j + cnt + cnt2 - (i + k) dp = [[10**18] * (N + 1) for _ in range(2 * N + 1)] dp[0][0] = 0 for i in range(1, 2 * N + 1): for j in range(max(0, i - N), min(i, N) + 1): dp[i][j] = dp[i-1][j] + costW[i-j-1][j] if j > 0: dp[i][j] = min(dp[i][j], dp[i-1][j-1] + costB[j-1][i-j]) print(dp[2*N][N]) ``` Yes
108,499