text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries: * Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring? Constraints * 2 \leq N \leq 10^5 * 1 \leq Q \leq 10^5 * S is a string of length N. * Each character in S is `A`, `C`, `G` or `T`. * 1 \leq l_i < r_i \leq N Input Input is given from Standard Input in the following format: N Q S l_1 r_1 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query. Example Input 8 3 ACACTACG 3 7 2 3 1 8 Output 2 0 3 Submitted Solution: ``` n,q=map(int,input().split()) s=input() a=[0]*n for i in range(n): if i: a[i]=a[i-1]+(s[i-1:i+1]=='AC') for _ in range(q): l,r=map(int,input().split()) print(a[r-1]-a[l-1]) ``` Yes
1,500
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries: * Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring? Constraints * 2 \leq N \leq 10^5 * 1 \leq Q \leq 10^5 * S is a string of length N. * Each character in S is `A`, `C`, `G` or `T`. * 1 \leq l_i < r_i \leq N Input Input is given from Standard Input in the following format: N Q S l_1 r_1 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query. Example Input 8 3 ACACTACG 3 7 2 3 1 8 Output 2 0 3 Submitted Solution: ``` f = lambda : map(int,input().split()) n, q = f() s = input() ln = [0]*n for i in range(1,n): ln[i] = ln[i-1] + (s[i-1:i+1]=='AC') for _ in range(q): l, r = f() print(ln[r-1]-ln[l-1]) ``` Yes
1,501
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries: * Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring? Constraints * 2 \leq N \leq 10^5 * 1 \leq Q \leq 10^5 * S is a string of length N. * Each character in S is `A`, `C`, `G` or `T`. * 1 \leq l_i < r_i \leq N Input Input is given from Standard Input in the following format: N Q S l_1 r_1 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query. Example Input 8 3 ACACTACG 3 7 2 3 1 8 Output 2 0 3 Submitted Solution: ``` n,q=map(int,input().split()) s=input() b=[0]*n for i in range(1,n): if s[i-1]+s[i]=='AC': b[i]=b[i-1]+1 else: b[i]=b[i-1] for i in range(q): l,r=map(int,input().split()) print(b[r-1]-b[l-1]) ``` Yes
1,502
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries: * Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring? Constraints * 2 \leq N \leq 10^5 * 1 \leq Q \leq 10^5 * S is a string of length N. * Each character in S is `A`, `C`, `G` or `T`. * 1 \leq l_i < r_i \leq N Input Input is given from Standard Input in the following format: N Q S l_1 r_1 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query. Example Input 8 3 ACACTACG 3 7 2 3 1 8 Output 2 0 3 Submitted Solution: ``` N, Q = map(int, input().split()) S = input() t = [0] * N for i in range(N - 1): t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0) for i in range(Q): l, r = map(int, input().split()) print(t[r - 1] - t[l - 1]) ``` Yes
1,503
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries: * Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring? Constraints * 2 \leq N \leq 10^5 * 1 \leq Q \leq 10^5 * S is a string of length N. * Each character in S is `A`, `C`, `G` or `T`. * 1 \leq l_i < r_i \leq N Input Input is given from Standard Input in the following format: N Q S l_1 r_1 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query. Example Input 8 3 ACACTACG 3 7 2 3 1 8 Output 2 0 3 Submitted Solution: ``` #入力 N ,Q = list(map(int, input().split())) S = input() lr_array = [] for i in range(Q): lr_array.append(list(map(int, input().split()))) #本体 patent_var = 0 ans = 0 blank_array = [0] * (N+2) for j in range(N): if S[j] == "A" and S[j+1]=="C": patent_var += 1 blank_array[j+2] = patent_var else: blank_array[j+2] = patent_var for k in range(Q): left = lr_array[k][0] right = lr_array[k][1] ans = blank_array[right] - blank_array[left] print(ans) ans = 0 ``` No
1,504
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries: * Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring? Constraints * 2 \leq N \leq 10^5 * 1 \leq Q \leq 10^5 * S is a string of length N. * Each character in S is `A`, `C`, `G` or `T`. * 1 \leq l_i < r_i \leq N Input Input is given from Standard Input in the following format: N Q S l_1 r_1 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query. Example Input 8 3 ACACTACG 3 7 2 3 1 8 Output 2 0 3 Submitted Solution: ``` n, q = map(int, input().split()) s = input() # 先に回数を数える。累積和的に hoge = [s[0:(i + 1)].count('AC') for i in range(n)] for _ in range(q + 1): l, r = map(int, input().split()) print(hoge[r - 1] - hoge[l - 1]) ``` No
1,505
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries: * Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring? Constraints * 2 \leq N \leq 10^5 * 1 \leq Q \leq 10^5 * S is a string of length N. * Each character in S is `A`, `C`, `G` or `T`. * 1 \leq l_i < r_i \leq N Input Input is given from Standard Input in the following format: N Q S l_1 r_1 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query. Example Input 8 3 ACACTACG 3 7 2 3 1 8 Output 2 0 3 Submitted Solution: ``` #encoding: utf-8 n, q = map(int, input().split()) s = input() for i in range(q): l, r = list(map(int, input().split())) print(s[l - 1:r].count("AC")) ``` No
1,506
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries: * Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring? Constraints * 2 \leq N \leq 10^5 * 1 \leq Q \leq 10^5 * S is a string of length N. * Each character in S is `A`, `C`, `G` or `T`. * 1 \leq l_i < r_i \leq N Input Input is given from Standard Input in the following format: N Q S l_1 r_1 : l_Q r_Q Output Print Q lines. The i-th line should contain the answer to the i-th query. Example Input 8 3 ACACTACG 3 7 2 3 1 8 Output 2 0 3 Submitted Solution: ``` n, q = map(int, input().split()) s = input() ac = [0 for i in range(n)] for i in range(1, n - 1): if s[i - 1] == 'A' and s[i] == 'C': ac[i] = 1 for i in range(n - 1): ac[i + 1] += ac[i] ac = [0] + ac for i in range(q): l, r = map(int, input().split()) l -= 1 r -= 1 print(ac[r + 1] - ac[l]) ``` No
1,507
Provide a correct Python 3 solution for this coding contest problem. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 "Correct Solution: ``` import sys input = sys.stdin.readline MOD = pow(10, 9) + 7 def main(): n = int(input()) alist = list(map(int, input().split())) nn = 1 for i in range(2, n+1): nn = nn * i % MOD rev = [pow(i, MOD-2, MOD) * nn % MOD for i in range(1, n+1)] for i in range(1, n): rev[i] = (rev[i] + rev[i-1]) % MOD ans = 0 for i in range(n): ans += alist[i] * (rev[i] + rev[n-i-1] - rev[0]) % MOD ans %= MOD print(ans) if __name__ == '__main__': main() ```
1,508
Provide a correct Python 3 solution for this coding contest problem. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 "Correct Solution: ``` def multiply(x, y): return (x * y) % mod def power(x, y): if y == 0: return 1 elif y == 1: return x elif x == 1: return 1 elif x == 0: return 0 else: # print(mod) tmp = power(x, y // 2) return (multiply(tmp, tmp) * [1, x][y % 2]) % mod N = int(input()) A = list(map(int, input().split())) mod = 10 ** 9 + 7 plist = [0] nfact = 1 N tmp = 0 for i in range(1, N): tmp = (tmp + power(i + 1, mod - 2)) % mod nfact = (nfact * (i + 1)) % mod plist.append(tmp) ans = 0 # print(plist) # print(nfact) for i, a in enumerate(A): # print(i, N - i - 1, N) tmp = (1 + plist[i] + plist[N - i - 1]) % mod ans = (ans + multiply(multiply(a, tmp), nfact)) % mod print(ans) ```
1,509
Provide a correct Python 3 solution for this coding contest problem. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 "Correct Solution: ``` N = int(input()) A = [int(a) for a in input().split()] P = 10**9+7 def inv(a): return pow(a, P-2, P) s = 0 for i in range(N): s += inv(i+1) ans = 0 for i in range(N): ans += s * A[i] ans %= P s += inv(i+2) - inv(N-i) for i in range(1, N+1): ans = ans * i % P print(ans) ```
1,510
Provide a correct Python 3 solution for this coding contest problem. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 "Correct Solution: ``` inv=lambda x:pow(x, m - 2, m) N = int(input()) A = [int(x) for x in input().split()] f = [1, 1] m = 10**9+7 for i in range(2, N + 1): f+=[f[-1] * i % m] s = [0] for i in range(1, N + 1): s+=[(s[-1]+f[N]*inv(i))%m] a = 0 for i in range(N): a+=(s[i+1]+s[N-i]-f[N])*A[i] a%=m print(a) ```
1,511
Provide a correct Python 3 solution for this coding contest problem. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 "Correct Solution: ``` mod=10**9+7 N=int(input()) a=[int(i) for i in input().split()] F=1 for i in range(1,N+1): F=(F*i)%mod def power(x,y): if y==0: return 1 elif y==1: return x%mod elif y%2==0: return power(x,y//2)**2%mod else: return (power(x,y//2)**2)*x%mod inv=[0]*(N) for i in range(N): inv[i]=power(i+1,mod-2) S_inv=[0]*(N+1) for i in range(N): S_inv[i+1]=S_inv[i]+inv[i] ans=0 for i in range(N): p=S_inv[i+1]+S_inv[N-i]-1 ans=(ans+a[i]*F*p)%mod print(ans) ```
1,512
Provide a correct Python 3 solution for this coding contest problem. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 "Correct Solution: ``` def prepare(n, MOD): f = 1 factorials = [1] for m in range(1, n + 1): f *= m f %= MOD factorials.append(f) inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv return factorials, invs def solve(n, aaa): factorials, invs = prepare(n, MOD) fn = factorials[n] coefs = [fn * invs[i + 1] * factorials[i] % MOD for i in range(1, n)] acc = [0] for c in coefs: tmp = (acc[-1] + c) % MOD acc.append(tmp) ans = 0 for i, a in enumerate(aaa): ans += (acc[i] + acc[n - i - 1] + fn) * a ans %= MOD return ans MOD = 1000000007 n = int(input()) aaa = list(map(int, input().split())) print(solve(n, aaa)) ```
1,513
Provide a correct Python 3 solution for this coding contest problem. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 "Correct Solution: ``` n = int(input()) A = [int(i) for i in input().split()] p = 10 ** 9 + 7 def fact(n, p=10**9 + 7): f = 1 for i in range(1, n+1): f *= i f %= p return f def get_inv(n, p=10**9 + 7): inv = [0, 1] for i in range(2, n+1): inv.append(-(p//i * inv[p%i]) % p) return inv f = fact(n) inv = get_inv(n) csum = [0] for i in range(1, n+1): csum.append((csum[-1] + inv[i]) % p) ans = 0 for i, a in enumerate(A): ans += (csum[i+1] + csum[n-i] - csum[1] + p) % p * a ans %= p print(ans * f % p) ```
1,514
Provide a correct Python 3 solution for this coding contest problem. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 "Correct Solution: ``` # B N = int(input()) A_list = list(map(int, input().split())) LARGE = 10**9+7 def pinv(p, k): return pow(k, p-2, p) Npow = 1 for i in range(2, N+1): Npow = Npow*i % LARGE K = 0 for i in range(N): K += pinv(LARGE, i+1) res = (K*A_list[0]) % LARGE for i in range(1, N): K -= pinv(LARGE, N+1-i) K += pinv(LARGE, i+1) res = (res + K*A_list[i]) % LARGE print(res*Npow % LARGE) ```
1,515
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 Submitted Solution: ``` N = int(input()) *A, = map(int, input().split()) MOD = 10**9 + 7 S = [0]*(N+1) r = 0 for i in range(1, N+1): S[i] = r = (r + pow(i, MOD-2, MOD)) % MOD ans = 0 for i in range(N): ans += A[i] * (S[i+1] + S[N-i] - 1) for i in range(1, N+1): ans = ans * i % MOD print(ans) ``` Yes
1,516
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 Submitted Solution: ``` N = int(input()) A = list(map(int,input().split())) mod = 10**9+7 def inv(n): n %= mod return pow(n,mod-2,mod) H = [0]*(N+1) for n in range(1,N+1): H[n] = (inv(n)+H[n-1])%mod N_f = 1 for n in range(1,N+1): N_f = N_f*n%mod ans = 0 for n in range(1,N+1): ans += A[n-1]*(H[N-n+1]+H[n]-1) ans %= mod ans *= N_f ans %= mod print(ans) ``` Yes
1,517
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 Submitted Solution: ``` def main(): n = int(input()) a = list(map(int, input().split())) mod, fact, m, npr, now = 10**9+7, [1, 1], [0, 1], [1]*(n+1), 1 for i in range(2, n+1): fact.append(fact[-1]*i % mod) for i in range(n, 0, -1): npr[i-1] = npr[i]*i % mod for i in range(2, n+1): m.append(fact[i]+now) now = (m[-1]+now*i) % mod m = [i*j % mod for i, j in zip(npr, m)] ans = (sum([j*(m[n-i]+m[i+1]) for i, j in enumerate(a)])-sum(a)*fact[n]) % mod print(ans) main() ``` Yes
1,518
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 Submitted Solution: ``` # -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, product, permutations from operator import add, sub sys.setrecursionlimit(10000) mod = 10**9 + 7 def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap def mul(a, b): return ((a % mod) * (b % mod)) % mod def power(x, y): if y == 0: return 1 elif y == 1: return x % mod elif y % 2 == 0: return power(x, y//2)**2 % mod else: return power(x, y//2)**2 * x % mod def div(a, b): return mul(a, power(b, mod-2)) @mt def slv(N, A): def perm(n): p = 1 for i in range(1, n+1): p *= i p %= mod return p pn = perm(N) sp = [0] for i in range(1, N+1): sp.append((sp[-1] + div(pn, i)) % mod) ans = mul(sp[-1], A[0]) for i in range(1, N): ans += mul(sp[i+1] - sp[1], A[i]) ans %= mod ans += mul(sp[N-i] - sp[0], A[i]) ans %= mod return ans def main(): N = read_int() A = read_int_n() print(slv(N, A)) if __name__ == '__main__': main() ``` Yes
1,519
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 Submitted Solution: ``` import sys fin = sys.stdin.readline def factorial(n, mod): if n == 0: return 1 else: return n * factorial(n - 1, mod) % mod MOD = 10**9 + 7 N = int(fin()) A_list = [int(elem) for elem in fin().split()] fac_N = factorial(N, MOD) inv_nums = [fac_N * pow(i, MOD - 2, MOD) % MOD for i in range(1, N + 1)] cuml_inv_nums = [inv_nums[0]] for inv_num in inv_nums[1:]: cuml_inv_nums.append((cuml_inv_nums[-1] + inv_num) % MOD) ans = 0 for i, A in enumerate(A_list): ans += A * (cuml_inv_nums[i] + cuml_inv_nums[N - 1 - i] - cuml_inv_nums[0]) % MOD ans %= MOD print(ans) ``` No
1,520
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 Submitted Solution: ``` import numpy as np n = int(input()) li = list(map(int, input().split())) def weight(li,ansli): if li == []: return a = sum(li) for j in range(len(li)): a = a*(j+1) ansli.append(a) for i in range(len(li)): weight(li[:i],ansli) weight(li[i+1:],ansli) return ansli print(sum(weight(li,[]))%(10**9+7)) ``` No
1,521
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 Submitted Solution: ``` #! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # """ AGC028 B """ import itertools n = int(input()) ali = list(map(int, input().split())) cut = 10**9+7 def modInverse(a, b, divmod=divmod): r0, r1, s0, s1 = a, b, 1, 0 while r1 != 0: q, rtmp = divmod(r0, r1) stmp = s0-q*s1 r0, s0 = r1, s1 r1, s1 = rtmp, stmp return s0 % cut def factorial(n): nn = 1 for i in range(2, n+1): nn = nn*i return(nn) invlist = [modInverse(i, cut) for i in range(1, n+1)] accuminv = list(itertools.accumulate(invlist, func=lambda x, y: x+y % cut)) ans = 0 nfact = factorial(n) for i in range(n): sump = accuminv[i]+accuminv[n-1-i] - 1 ans += ali[i]*sump % cut ans = ans*nfact % cut print(ans) ``` No
1,522
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923 Submitted Solution: ``` N = int(input()) w = [int(i) for i in input().split())] def f(n): i = 1 for j in range(0,n): i = i*(j+1) return i def c(l): n = len(l) if n == 0 return 0 elif n == 1: return l[0] else: t = f(n)*sum(l) for i in range(0,n): a = l[0:i] b = l[i+1:n] t = t + (f(n-1)/f(i))*c(a) + (f(n-1)/f(n-i-1))*c(b) return int(t) print(c(w)) ``` No
1,523
Provide a correct Python 3 solution for this coding contest problem. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES "Correct Solution: ``` H, W = map(int, input().split()) Ss = [input() for _ in range(H)] # 行の入れ替えパターンを生成する(中央付近から埋めていく) def dfs(iR): # 全て埋まったら、判定に移る if iR < 0: return check() # 未使用の行を検索する iF = flgs.index(False) Rs[iR] = iF - offset flgs[iF] = True # ペアの相手を決めて、次のペア生成に移る ans = False for iF2, flg in enumerate(flgs): if not flg: Rs[H - 1 - iR] = iF2 - offset flgs[iF2] = True ans = ans or dfs(iR - 1) flgs[iF2] = False flgs[iF] = False return ans # 与えられた行の入れ替えパターンに対して、列の入れ替えのみで点対称にできるかを判定する def check(): Ts = [Ss[R] for R in Rs] Ts = list(map(list, zip(*Ts))) # (W+1)/2列目を使用可能かどうか if W % 2: flgCenter = True else: flgCenter = False # 各列に対して、処理を行う Used = [False] * W for j, T in enumerate(Ts): if Used[j]: continue for j2, T2 in enumerate(Ts[j + 1:], j + 1): # 上下反転したような未使用の列が存在するならば、次の列へ if not Used[j2] and T[::-1] == T2: Used[j2] = True break else: # 自身が上下対称、かつ、(W+1)/2列目を使用可能ならば、次の列へ if T[::-1] == T and flgCenter == True: flgCenter = False else: # この入れ替えパターンでは不可能と判定 return False return True if H % 2: # Hが奇数ならば、先頭にダミーを付加 flgs = [False] * (H + 1) offset = 1 else: flgs = [False] * H offset = 0 Rs = [-1] * H if dfs((H - 1) // 2): print('YES') else: print('NO') ```
1,524
Provide a correct Python 3 solution for this coding contest problem. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES "Correct Solution: ``` def check(field): tr = list(map(''.join, zip(*field))) paired = set() center = -1 for i, col1 in enumerate(tr): if i in paired: continue for j, col2 in enumerate(tr[i + 1:], start=i + 1): if j in paired: continue if col1 == col2[::-1]: paired.add(i) paired.add(j) break else: if center == -1 and col1 == col1[::-1]: center = i else: return False return True def arrange_row(field, new_field, i, remain): if len(remain) == 0: return check(new_field) j = remain.pop() new_field[i] = field[j] for k in list(remain): remain.remove(k) new_field[h - i - 1] = field[k] result = arrange_row(field, new_field, i + 1, remain) if result: return True remain.add(k) remain.add(j) return False def solve(h, w, field): new_field = [None] * h remaining_row = set(range(h)) if h % 2 == 0: return arrange_row(field, new_field, 0, remaining_row) for i in list(remaining_row): remaining_row.remove(i) new_field[h // 2] = field[i] result = arrange_row(field, new_field, 0, remaining_row) if result: return True remaining_row.add(i) return False h, w = map(int, input().split()) field = [input() for _ in range(h)] print('YES' if solve(h, w, field) else 'NO') ```
1,525
Provide a correct Python 3 solution for this coding contest problem. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES "Correct Solution: ``` import os import sys if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(10 ** 9) INF = float("inf") IINF = 10 ** 18 MOD = 10 ** 9 + 7 # MOD = 998244353 H, W = list(map(int, sys.stdin.buffer.readline().split())) S = [sys.stdin.buffer.readline().decode().rstrip() for _ in range(H)] # ペアのとり方は 10395 通り # a = math.factorial(12) / (2 ** 6) / math.factorial(6) # print(a) # 10395 def test(groups): seen = [False] * 6 order = [-1] * len(groups) for i, g in enumerate(groups): if g == -1: # 真ん中 order[W // 2] = i continue if not seen[g]: order[g] = i seen[g] = True else: order[~g] = i # print(groups, order) # return False rows = [] for s in S: row = '' for i in order: row += s[i] rows.append(row) ok = [False] * H for i in range(H): if ok[i]: continue rev = rows[i][::-1] for j in range(i + 1, H): if not ok[j] and rows[j] == rev: ok[i] = ok[j] = True break ng_cnt = ok.count(False) if ng_cnt == 0: return True if ng_cnt == 1: i = ok.index(False) return rows[i] == rows[i][::-1] return False def solve(groups, depth=0): if depth == W // 2: return test(groups) i = 0 for _ in range(2 if W % 2 == 1 else 1): while groups[i] != -1: i += 1 groups[i] = depth for j in range(i + 1, W): if groups[j] != -1: continue groups[j] = depth if solve(groups, depth + 1): return True groups[j] = -1 groups[i] = -1 i += 1 return False groups = [-1] * W ok = solve(groups) if ok: print('YES') else: print('NO') ```
1,526
Provide a correct Python 3 solution for this coding contest problem. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES "Correct Solution: ``` H,W=map(int,input().split()) G=[list(input()) for i in range(H)] G_t=[list(x) for x in list(zip(*G))] def Check(G,H,W): Paired_y=[False]*H for y1 in range(H): if Paired_y[y1]: continue for y2 in range(H): if y1==y2 or Paired_y[y2]: continue Paired_x=[False]*W for x1 in range(W): if Paired_x[x1]: continue for x2 in range(W): if x1==x2 or Paired_x[x2]: continue if G[y1][x1]==G[y2][x2] and G[y1][x2]==G[y2][x1]: Paired_x[x1]=True Paired_x[x2]=True break if W%2==1: if Paired_x.count(False)==1: r=Paired_x.index(False) if G[y1][r]==G[y2][r]: Paired_y[y1]=True Paired_y[y2]=True break else: if Paired_x.count(False)==0: Paired_y[y1]=True Paired_y[y2]=True break if H%2==1: if Paired_y.count(False)==1: return True else: return False else: if Paired_y.count(False)==0: return True else: return False if Check(G,H,W) and Check(G_t,W,H): print("YES") else: print("NO") ```
1,527
Provide a correct Python 3 solution for this coding contest problem. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES "Correct Solution: ``` H, W = map(int, input().split()) S = [input() for i in range(H)] def check(l): u = [0]*W mid = W % 2 for i in range(W): if u[i]: continue for j in range(i+1, W): for p, q in l: sp = S[p]; sq = S[q] if sp[i] != sq[j] or sp[j] != sq[i]: break else: u[j] = 1 break else: if mid: for p, q in l: sp = S[p]; sq = S[q] if sp[i] != sq[i]: break else: mid = 0 continue break else: return 1 return 0 def make(c, l, u, p): while c in u: c += 1 if c == H: if check(l): print("YES") exit(0) return if p: l.append((c, c)) make(c+1, l, u, 0) l.pop() for i in range(c+1, H): if i in u: continue u.add(i) l.append((c, i)) make(c+1, l, u, p) l.pop() u.remove(i) make(0, [], set(), H%2) print("NO") ```
1,528
Provide a correct Python 3 solution for this coding contest problem. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES "Correct Solution: ``` import sys import copy sys.setrecursionlimit(10 ** 6) def main(): def per(s, a=[]): if not s: return [a] if len(s) % 2: cs = copy.deepcopy(s) res = [] for u in cs: s.remove(u) res += per(s, [u] + a) s.add(u) return res u = min(s) s.remove(u) cs = copy.deepcopy(s) res = [] for uu in cs: if uu < u: continue s.remove(uu) res += per(s, a + [u, uu]) s.add(uu) s.add(u) return res ord_a = ord("a") h, w = map(int, input().split()) t0 = [[ord(c) - ord_a for c in input()] for _ in range(h)] h_set = set(range(h)) pattern = per(h_set) # print(t0) # print(pattern) b = h % 2 for p in pattern: t1 = [[] for _ in range(h)] if b: t1[h // 2] = t0[p[0]] i = 0 for ii in range(b, h, 2): t1[i] = t0[p[ii]] t1[h - 1 - i] = t0[p[ii + 1]] i += 1 # print(p) # print(t1) fin = [False] * w mid = (w % 2 == 1) br = False for j, c0 in enumerate(zip(*t1)): if fin[j]: continue fin[j] = True c0 = c0[::-1] for jj, c1 in enumerate(zip(*t1)): if jj <= j: continue if fin[jj]: continue if c0 == c1: fin[jj] = True break else: if mid and c0 == c0[::-1]: mid = False else: br = True break if br: continue print("YES") exit() print("NO") main() ```
1,529
Provide a correct Python 3 solution for this coding contest problem. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES "Correct Solution: ``` # この解法は嘘を含む from itertools import groupby H, W = map(int, input().split()) S_ = ["" for _ in range(W)] T_ = [] for _ in range(H): s = input() T_.append(s) for i, c in enumerate(s): S_[i] += c T = [sorted(t) for t in T_] S = [sorted(s) for s in S_] cnt = 0 for _, g in groupby(sorted(T)): if len(list(g))%2: cnt += 1 if H%2 < cnt: print("NO") exit() cnt = 0 for _, g in groupby(sorted(S)): if len(list(g))%2: cnt += 1 if W%2 < cnt: print("NO") exit() if W%2 or H%2: print("YES") exit() T1 = [] T2 = [] for i, (_, t) in enumerate(sorted(zip(T, T_))): if i%2: T1.append(t) else: T2.append(t) T_p = T1 + T2[::-1] S_pp = ["" for _ in range(W)] for t in T_p: for i, c in enumerate(t): S_pp[i] += c S1 = [] S2 = [] for i, (_, s) in enumerate(sorted(zip(S, S_pp))): if i%2: S1.append(s) else: S2.append(s) S_p = S1 + S2[::-1] for s1, s2 in zip(S_p, S_p[::-1]): for c1, c2 in zip(s1, s2[::-1]): if c1!=c2: print("NO") exit() print("YES") ```
1,530
Provide a correct Python 3 solution for this coding contest problem. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES "Correct Solution: ``` from collections import Counter n,m = map(int,input().split()) grid = [list(input()) for i in range(n)] def jc(a,b): l = len(a) used = [0]*l for i in range(l): if used[i]: continue for j in range(i+1,l): if used[j]: continue if a[i] == b[j] and b[i] == a[j]: used[i] = 1 used[j] = 1 break if used.count(0) <= l%2: return True else: return False def judge(a): h = len(a) w = len(a[0]) used = [0]*h for i in range(h): if used[i]: continue ci = Counter(a[i]) for j in range(i+1,h): if used[j]: continue cj = Counter(a[j]) if ci == cj: if jc(a[i],a[j]): used[i] = 1 used[j] = 1 break if used.count(0) <= h%2: return True else: return False gt = list(zip(*grid)) if judge(grid) & judge(gt): print("YES") else: print("NO") ```
1,531
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES Submitted Solution: ``` import sys from itertools import permutations from random import randrange readline = sys.stdin.readline mod = 10**9+9 base = 2009 def rollinhash(S): N = len(S) has = 0 for i in range(N): s = S[i] has = (has*base + s)%mod return has phr = [] while len(set(phr)) != 26: phr = [randrange(1, 10**9) for _ in range(26)] def shash(S): N = len(S) has = 0 for i in range(N): has = has + phr[S[i]]*phr[S[N-1-i]] return has H, W = map(int, readline().split()) G = [list(map(ord, readline().strip())) for _ in range(H)] ans = 'NO' fh = H//2 ch = H-fh lir = list(range(H)) seen = set() for k in permutations(lir, ch): k = list(k) sk = set(k) for i in range(H): if i not in sk: k.append(i) sh = shash(k) if sh in seen: continue seen.add(sh) G2 = [] for j in k: G2.append(G[j]) G2 = list(map(list, zip(*G2))) G2h = [rollinhash(g) for g in G2] G2hi = [rollinhash(g[::-1]) for g in G2] used = [False]*W w1 = 0 match = W//2 while match and w1 < W: if used[w1]: w1 += 1 continue sa = G2hi[w1] for w2 in range(w1+1, W): if used[w2]: continue if G2h[w2] == sa: used[w1] = True used[w2] = True match -= 1 break w1 += 1 if sum(used) >= W-1: if sum(used) == W-1: mid = used.index(False) if G2h[mid] == G2hi[mid]: ans = 'YES' else: ans = 'YES' if ans == 'YES': break print(ans) ``` Yes
1,532
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES Submitted Solution: ``` from collections import defaultdict def main(): h, w = map(int, input().split()) s = [] for _ in range(h): row = [] s.append(row) for c in input().strip(): row.append(ord(c)) c_row = defaultdict(int) for row in s: c_row[tuple(sorted(row))] += 1 if sum(c % 2 for c in c_row.values()) > h % 2: print('NO') return c_col = defaultdict(int) for col in zip(*s): c_col[tuple(sorted(col))] += 1 if sum(c % 2 for c in c_col.values()) > w % 2: print('NO') return print('YES') main() ``` No
1,533
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES Submitted Solution: ``` import sys import itertools H,W=map(int,input().split()) mat=[] for i in range(H): s=list(input()) mat.append(s) #print(mat) perm_h_list=list(itertools.permutations(range(H))) perm_w_list=list(itertools.permutations(range(W))) #print(perm_h_list) #print(perm_w_list) for perm_h in perm_h_list: for perm_w in perm_w_list: sym=True for i in range(H): for j in range(W): if mat[perm_h[i]][perm_w[j]] != mat[perm_h[H-1-i]][perm_w[W-1-j]]: sym=False break if not sym: break else: print("YES") sys.exit() else: print("NO") ``` No
1,534
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES Submitted Solution: ``` from collections import Counter def check(field): center = None prev = None for i, row in enumerate(field): if prev is None: prev = row continue if prev == row: prev = None continue if center is not None: return False center = prev prev = row if center is not None: cnt = Counter(center) cntcnt = Counter(c % 2 for c in cnt.values()) if cntcnt[1] > 1: return False return True h, w = map(int, input().split()) field = [] for i in range(h): field.append(list(map(ord, input()))) field1 = sorted(sorted(row) for row in field) field2 = sorted(sorted(col) for col in zip(*field)) print('YES' if check(field1) and check(field2) else 'NO') ``` No
1,535
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two different rows and swap them. Or, choose two different columns and swap them. Snuke wants this grid to be symmetric. That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal. Determine if Snuke can achieve this objective. Constraints * 1 \leq H \leq 12 * 1 \leq W \leq 12 * |S_i| = W * S_i consists of lowercase English letters. Input Input is given from Standard Input in the following format: H W S_1 S_2 : S_H Output If Snuke can make the grid symmetric, print `YES`; if he cannot, print `NO`. Examples Input 2 3 arc rac Output YES Input 3 7 atcoder regular contest Output NO Input 12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv Output YES Submitted Solution: ``` n, m = map(int,input().split()) d = {i: {chr(i): 0 for i in range(ord('a'), ord('z')+1)} for i in range(n)} d2 = {i: {chr(i): 0 for i in range(ord('a'), ord('z')+1)} for i in range(m)} for i in range(n): s = input() for j in s: d[i][j] += 1 for j in range(len(s)): d2[j][s[j]] += 1 k = [0 for i in range(n)] k2 = [0 for i in range(m)] f = True for i in range(n): for j in range(n): if d[i] == d[j]: k[i] += 1 for i in range(m): for j in range(m): if d2[i] == d2[j]: k2[i] += 1 k2 = sorted(k2) k = sorted(k) if n % 2: if len(k) > 1: if k[1] < 2: f = False else: if k[0] < 2: f = False if m % 2: if len(k2) > 1: if k2[1] < 2: f = False else: if k2[0] < 2: f = False if f: print('YES') else: print('NO') ``` No
1,536
Provide a correct Python 3 solution for this coding contest problem. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = "Correct Solution: ``` x, y = input().split();print("=" if x==y else "<>"[x>y::2]) ```
1,537
Provide a correct Python 3 solution for this coding contest problem. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = "Correct Solution: ``` a,b = input().split() if a>b: print(">") if a == b: print("=") if a<b: print("<") ```
1,538
Provide a correct Python 3 solution for this coding contest problem. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = "Correct Solution: ``` a,b=map(str,input().split()) print('>'if a>b else'<'if a<b else'=') ```
1,539
Provide a correct Python 3 solution for this coding contest problem. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = "Correct Solution: ``` x,y=input().split();print("=><"[(x>y)-(x<y)]) ```
1,540
Provide a correct Python 3 solution for this coding contest problem. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = "Correct Solution: ``` a,b=input().split() print(">" if ord(a) > ord(b) else "<" if ord(a) < ord(b) else "=") ```
1,541
Provide a correct Python 3 solution for this coding contest problem. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = "Correct Solution: ``` a,b=input().split();print('>=<'[(a<b)+(a<=b)]) ```
1,542
Provide a correct Python 3 solution for this coding contest problem. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = "Correct Solution: ``` x,y=input().split() if x>y: a='>' elif x<y: a='<' else: a='=' print(a) ```
1,543
Provide a correct Python 3 solution for this coding contest problem. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = "Correct Solution: ``` x, y = input().split() print("<" if x < y else (">" if x > y else "=")) ```
1,544
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = Submitted Solution: ``` x,y=input().split() if x>y: print(">") elif y>x: print("<") else: print("=") ``` Yes
1,545
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = Submitted Solution: ``` a,b=input().split();print(['=<'[a<b],'>'][a>b]) ``` Yes
1,546
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = Submitted Solution: ``` x,y=input().split() if x<y: print("<") elif x>y: print(">") else: print("=") ``` Yes
1,547
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = Submitted Solution: ``` X, Y = input().split() print('>' if X>Y else '<' if X<Y else '=') ``` Yes
1,548
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = Submitted Solution: ``` I=input() s=I.split(" ") #print(s) c=sorted(s) #print(c) if s==c and s[0]!=s[1]: print("<") if s!=c and s[0]!=s[1]: print(">") else: print("=") ``` No
1,549
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = Submitted Solution: ``` s1=input() s2=input() s1=s1.lower() s2=s2.lower() flag=0 if(len(s1)>len(s2)): a=len(s2) else: a=len(s1) for i in range(0,a): if(s1[i]==s2[i]): flag=flag+1 elif(s1[i]>s2[i]): print('>') break; elif(s1[i]<s2[i]): print('<') break; if(flag==a): if(len(s1)>len(s2)): print(">") elif(len(s1)<len(s2)): print("<") else: print("=") ``` No
1,550
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = Submitted Solution: ``` x,y = map(int, input().split()) a=0 b=0 if x=="A": a=10 elif x=="B": a=11 elif x=="C": a=12 elif x=="D": a=13: elif x=="E": a=14 elif x=="F": a=15 if y=="A": b=10 elif y=="B": b=11 elif x=="C": b=12 elif x=="D": b=13 elif x=="E": b=14 elif x=="F": b=15 if a<b: print("<") elif a>b: print(">") elif a==b: print("=") ``` No
1,551
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. When X and Y are seen as hexadecimal numbers, which is larger? Constraints * Each X and Y is `A`, `B`, `C`, `D`, `E` or `F`. Input Input is given from Standard Input in the following format: X Y Output If X is smaller, print `<`; if Y is smaller, print `>`; if they are equal, print `=`. Examples Input A B Output < Input E C Output > Input F F Output = Submitted Solution: ``` import sys import math # 入力 a, b, c = map(int, input().split()) # 計算 if a < (b + 2*c): print("a >= b + 2*cとなるようにしてください") sys.exit() elif a == (b + 2*c): print("1") else: max = math.floor((a + c) / (b + 2*c)) print(max) ``` No
1,552
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 "Correct Solution: ``` class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i def main(): import sys read = sys.stdin.read N, K = (int(i) for i in input().split()) A = [int(s) for s in read().rstrip().split('\n')] from itertools import accumulate S = list(accumulate([0] + A)) B = [s - K*i for i, s in enumerate(S)] c = {b: i+1 for i, b in enumerate(sorted(set(B)))} f = Bit(N+1) ans = 0 for j in range(N+1): ans += f.sum(c[B[j]]) f.add(c[B[j]], 1) print(ans) if __name__ == '__main__': main() ```
1,553
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 "Correct Solution: ``` from bisect import bisect_left import sys input = sys.stdin.readline class BinaryIndexedTree(): def __init__(self, N): self.N = N self.arr = [0] * (N+1) def query(self, i): ret = 0 i += 1 while i: ret += self.arr[i] lsb = i & (-i) i -= lsb return ret def add(self, i, v): i += 1 while i < self.N+1: lsb = i & (-i) self.arr[i] += v i += lsb def search(self, x): lo = -1 hi = self.N while lo < hi: mid = (lo+hi)//2 if self.query(mid) < x: lo = mid + 1 else: hi = mid return lo def main(): N, K = map(int, input().split()) A = [int(input()) for _ in range(N)] B = [0] * (N+1) for i in range(N): B[i+1] = B[i] + A[i] - K sortedB = sorted(B) C = [0] * (N+1) bit = BinaryIndexedTree(N+1) ans = 0 for i in range(N+1): x = bisect_left(sortedB, B[i]) C[i] = x ans += bit.query(x) bit.add(C[i], 1) print(ans) if __name__ == "__main__": main() ```
1,554
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 "Correct Solution: ``` from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify from bisect import bisect_right, bisect_left import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor, gamma, log from operator import mul from functools import reduce sys.setrecursionlimit(2147483647) INF = 1 << 100 def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 1000000007 class BIT: def __init__(self, size): self.bit = [0] * size self.size = size self.total = 0 def add(self, i, w): x = i + 1 while x <= self.size: self.bit[x - 1] += w x += x & -x return def sum(self, i): res = 0 x = i + 1 while x: res += self.bit[x - 1] x -= x & -x return res n, k = LI() A = IR(n) A = [a - k for a in A] A = [0] + list(accumulate(A)) D = {} for i, v in enumerate(sorted(set(A))): D[v] = i for i in range(n + 1): A[i] = D[A[i]] bit = BIT(max(A) + 1) c = 0 for a in A: c += bit.sum(a) bit.add(a, 1) print(c) ```
1,555
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 "Correct Solution: ``` from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor, cos, radians, pi, sin from operator import mul from functools import reduce from operator import mul sys.setrecursionlimit(2147483647) INF = 10 ** 13 def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 10 ** 9 + 7 class BIT: def __init__(self, size): self.bit = [0] * size self.size = size self.total = 0 def add(self, i, w): x = i + 1 self.total += w while x <= self.size: self.bit[x - 1] += w x += x & -x return def sum(self, i): res = 0 x = i + 1 while x: res += self.bit[x - 1] x -= x & -x return res n, k = LI() A = IR(n) A = [0] + list(accumulate([a - k for a in A])) D = {v:i for i,v in enumerate(sorted(A))} bit = BIT(len(A)) c = 0 for a in A: c += bit.sum(D[a]) bit.add(D[a], 1) print(c) ```
1,556
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 "Correct Solution: ``` from itertools import accumulate from bisect import bisect_right N, K = map(int, input().split()) A = [-19] + [int(input()) for i in range(N)] diff = [a - K for a in A] diff = list(accumulate(diff)) class BinaryIndexedTree: def __init__(self, n): self.size = n self.bit = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.bit[i] i -= (i & -i) return s def add(self, i, x): while i <= self.size: self.bit[i] += x i += (i & -i) def reset(self): self.bit = [0] * (self.size + 1) BIT = BinaryIndexedTree(N + 1) temp = sorted(diff) order = [bisect_right(temp, d) for d in diff] ans = 0 for x in order: ans += BIT.sum(x) BIT.add(x, 1) print(ans) ```
1,557
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 "Correct Solution: ``` from bisect import bisect_left class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n+1) def sum(self, i): # [0, i) s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): # i > 0 assert i > 0 while i <= self.size: self.tree[i] += x i += i & -i n, k = map(int, input().split()) a = [int(input()) for _ in range(n)] d = [0] * (n+1) d2 = {0} for i in range(n): d[i+1] = d[i] + a[i] - k d2.add(d[i+1]) d2 = sorted(d2) bit = Bit(len(d2)) ans = 0 for i in range(n+1): d2_i = bisect_left(d2, d[i]) ans += bit.sum(d2_i+1) bit.add(d2_i+1, 1) print(ans) ```
1,558
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 "Correct Solution: ``` import sys from copy import deepcopy F = sys.stdin N, K = map(int, F.readline().strip("\n").split()) A = [None] * (N + 1) A[0] = 0 A_order = {0} for a in range(N): A[a+1] = int(F.readline().strip("\n")) - K + A[a] A_order |= {A[a+1]} maxA = len(A_order) A_order = list(A_order) A_order.sort() compressed_A = dict() for i in range(maxA): compressed_A[A_order[i]] = i bit = [0] * (maxA + 1) total_inversion = 0 for i in range(N+1): b = compressed_A[A[i]] + 1 while b <= maxA + 1: bit[b - 1] += 1 b += b & -b #lower sum lower = compressed_A[A[i]] + 1 lower_sum = 0 while lower > 0: lower_sum += bit[lower - 1] lower -= lower & -lower total_inversion += i + 1 - lower_sum print(N * (N + 1) // 2 - total_inversion) ```
1,559
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 "Correct Solution: ``` n,k=map(int,input().split()) b=[0]*(n+1) for i in range(n): b[i+1]=b[i]+int(input())-k a=sorted(set(b)) s={a[i-1]:i for i in range(1,len(a)+1)} c=[] for i in b: c.append(s[i]) b=[0]*(len(a)+1) def f(x): c=0 while x>0: c+=b[x] x-=x&-x return c def g(x): while x<len(a)+1: b[x]+=1 x+=x&-x e=0 for i in range(n+1): e+=f(c[i]) g(c[i]) print(e) ```
1,560
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 Submitted Solution: ``` import sys input = sys.stdin.readline n, k = [int(item) for item in input().split()] a = [0] for i in range(n): a.append(int(input()) - k) for i in range(1, n+1): a[i] += a[i - 1] a_set = sorted(list(set(a))) comp = dict() for i, item in enumerate(a_set): comp[item] = i for i, item in enumerate(a): a[i] = comp[item] n = len(a_set) + 5 bit = [0] * (n + 1) # Add w to ax def bit_add(x, w): while x <= n: bit[x] += w x += x & -x # Sum a1 to ax def bit_sum(x): ret = 0 while x > 0: ret += bit[x] x -= x & -x return ret ans = 0 for item in a: ans += bit_sum(item + 1) bit_add(item + 1, 1) print(ans) ``` Yes
1,561
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 Submitted Solution: ``` import bisect from itertools import accumulate # python template for atcoder1 import sys sys.setrecursionlimit(10**9) input = sys.stdin.readline def solve(): N, K = map(int, input().split()) L = [int(input()) for _ in range(N)] L_acc = [0]+list(accumulate(L)) for i in range(N+1): L_acc[i] -= (i)*K L_sort = list(sorted(set(L_acc))) L_comp = [-1]*(N+1) for i in range(N+1): key = L_acc[i] ind = bisect.bisect_left(L_sort, key) L_comp[i] = ind # BIT bit = [0]*(N+1) def sum_bit(i): s = 0 while i > 0: s += bit[i] i -= i & (-i) return s def add(i, x): while i <= N: bit[i] += x i += i & (-i) ans = 0 for i, l in enumerate(L_comp): if l == 0: ans += N-i continue ans += sum_bit(l) add(l, 1) print(ans) solve() ``` Yes
1,562
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 Submitted Solution: ``` import sys input = sys.stdin.readline class BIT1(): """ Binary Indexed Tree (1-indexed) """ def __init__(self, n): self.n = n self.bit = [0] * (self.n + 1) self.data = [0] * (self.n + 1) def add(self, idx, x): # add x to idx-th element # idx: 1-indexed self.data[idx] += x while idx <= self.n: self.bit[idx] += x idx += (idx & (-idx)) def sum(self, idx): # get sum of [1, idx] # idx: 1-indexed s = 0 while idx: s += self.bit[idx] idx -= (idx & (-idx)) return s n, k = map(int, input().split()) a = [int(input()) for _ in range(n)] s = [0] * (n + 1) for i in range(n): s[i + 1] = s[i] + a[i] t = [s[i] - k * i for i in range(n + 1)] # 座標圧縮 zipped = {} for i, xi in enumerate(sorted(set(t))): zipped[xi] = i + 1 m = len(zipped) t2 = [0] * (n + 1) for i in range(n + 1): t2[i] = zipped[t[i]] ans = 0 bit = BIT1(m) for i in range(n + 1): ans += bit.sum(t2[i]) bit.add(t2[i], 1) print(ans) ``` Yes
1,563
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 Submitted Solution: ``` from collections import defaultdict n, k = map(int, input().split()) a = [int(input()) - k for _ in range(n)] s = [0] * (n + 1) for i in range(n): s[i + 1] = s[i] + a[i] mp = defaultdict(int) b = sorted(list(set(s))) num = 1 for c in b: mp[c] = num num += 1 m = len(mp) d = [0] * (m + 1) def add(i): while i <= m: d[i] += 1 i += i & -i def qqq(i): res = 0 while i > 0: res += d[i] i -= i & -i return res ans = 0 for i in range(n + 1): ans += qqq(mp[s[i]]) add(mp[s[i]]) print(ans) ``` Yes
1,564
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 Submitted Solution: ``` n, k = map(int, input().rstrip("\n").split(" ")) from collections import defaultdict h = [] total = 0 prev_counter = defaultdict(int) for i in range(n): value = int(input()) counter = defaultdict(int) diff = value - k if diff >= 0: total += 1 counter[diff] = 1 for key, count in prev_counter.items(): if (key + diff) >= 0: total += count counter[key + diff] += count prev_counter = counter print(total ) ``` No
1,565
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 Submitted Solution: ``` import sys,bisect,math input = sys.stdin.readline # i が与えられたとき a1~aiまでの和を計算する( O(log n) ) def bitsum(bit,i): s = 0 while i > 0: s += bit[i] i -= i&(-i) return s # i と x が与えられたとき ai += x とする ( O(log n) ) def bitadd(bit,i,x): while i <= n: bit[i] += x i += i&(-i) return bit n,k = map(int,input().split()) a = [int(input()) for i in range(n)] #b[j+1]-b[i]:a[i]~a[j] b = [0] for i in range(n): b.append(b[-1]+a[i]) #c[i]:b[i]-k*i c = [0]*(n+1) for i in range(n+1): c[i] = b[i]-k*i d = sorted(c) for i in range(n+1): k = bisect.bisect_left(d,c[i]) d[k] = d[k] c[i] = k+1 #print(c) n_ = int(math.log2(n+1)) + 1 bit = [0] * (2**n_+1) ass = 0 for j in range(n+1): ass += j-bitsum(bit,c[j]) #bitsum(bit,a[j]): j-1番目までの中でa[j]以下のもの bit = bitadd(bit,c[j],1) print(n*(n+1)//2-ass) ``` No
1,566
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 Submitted Solution: ``` class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i N, K = map(int, input().split()) ac = [0] * (N + 1) eps = 10 ** -6 place = {0: 0} for i in range(N): a = int(input()) - K ac[i+1] = ac[i] + a + eps * (i+1) place[ac[i+1]] = i+1 ac.sort() ac_cmpr = [0] * (N+1) for i in range(N+1): ac_cmpr[place[ac[i]]] = i + 1 bit = Bit(N+1) bit.add(ac_cmpr[0], 1) ans = 0 for i in range(1, N+1): ans += bit.sum(ac_cmpr[i]) bit.add(ac_cmpr[i], 1) print(ans) ``` No
1,567
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13 Submitted Solution: ``` import sys input=sys.stdin.readline N,K=map(int,input().split()) A=[0] for i in range(N): a=int(input()) A.append(a-K) B=[A[0]] for i in range(1,N+1): B.append(B[-1]+A[i]) #print(A) #print(B) ans=0 for i in range(1,N+1): for j in range(i,N+1): if B[j]-B[i-1]>=0: ans+=1 print(ans) ``` No
1,568
Provide a correct Python 3 solution for this coding contest problem. There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i). We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|). You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes. When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes. Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass. Constraints * 2≦N≦10^5 * 1≦x_i, y_i≦10^9 * 1≦a < b≦N * When i ≠ j, (x_i, y_i) ≠ (x_j, y_j) * x_i and y_i are integers. Input The input is given from Standard Input in the following format: N a b x_1 y_1 : x_N y_N Output Print the number of the pairs of pinholes that can be pointed by the compass. Examples Input 5 1 2 1 1 4 3 6 1 5 5 4 8 Output 4 Input 6 2 3 1 3 5 3 3 5 8 4 4 7 2 5 Output 4 Input 8 1 2 1 5 4 3 8 2 4 7 8 8 3 3 6 6 4 8 Output 7 "Correct Solution: ``` import bisect import sys from collections import defaultdict class RandomizedBinarySearchTree: def __init__(self): self.children = [[-1], [-1]] self.values = [0] self.counts = [0] self.root = 0 def merge(self, left_root, right_root): children = self.children counts = self.counts li = left_root ri = right_root stack = [] switch = 0 while li != 0 and ri != 0: if switch: stack.append((li, 1)) li = children[1][li] else: stack.append((ri, 0)) ri = children[0][ri] switch ^= 1 i = li if li != 0 else ri while stack: pi, is_right = stack.pop() children[is_right][pi] = i counts[pi] = counts[children[0][pi]] + counts[children[1][pi]] + 1 i = pi return i def split(self, root, x): i = root lefts, rights = self.children values = self.values counts = self.counts l_stack = [] r_stack = [] while i != 0: if x < values[i]: r_stack.append(i) i = lefts[i] else: l_stack.append(i) i = rights[i] li, ri = 0, 0 while l_stack: pi = l_stack.pop() rights[pi] = li counts[pi] = counts[lefts[pi]] + counts[li] + 1 li = pi while r_stack: pi = r_stack.pop() lefts[pi] = ri counts[pi] = counts[ri] + counts[rights[pi]] + 1 ri = pi return li, ri def insert(self, x): ni = len(self.values) self.children[0].append(0) self.children[1].append(0) self.values.append(x) self.counts.append(1) li, ri = self.split(self.root, x) self.root = self.merge(self.merge(li, ni), ri) def delete(self, x): li, mri = self.split(self.root, x - 1) mi, ri = self.split(mri, x) if mi == 0: self.root = self.merge(li, ri) return self.root = self.merge(li, ri) return def upper_bound(self, x, default=-1): i = self.root lefts, rights = self.children values = self.values counts = self.counts y = default c = counts[i] j = 0 while i != 0: if x < values[i]: y = values[i] c = j + counts[lefts[i]] i = lefts[i] else: j += counts[lefts[i]] + 1 i = rights[i] return y, c def lower_bound(self, x, default=-1): i = self.root lefts, rights = self.children values = self.values counts = self.counts y = default c = counts[i] j = 0 while i != 0: if x <= values[i]: y = values[i] c = j + counts[lefts[i]] i = lefts[i] else: j += counts[lefts[i]] + 1 i = rights[i] return y, c def get_k_th(self, k, default=-1): i = self.root children = self.children values = self.values counts = self.counts if counts[i] <= k: return default j = k while i != 0: left_count = counts[children[0][i]] if left_count == j: return values[i] elif left_count > j: i = children[0][i] else: j -= left_count + 1 i = children[1][i] return default def debug_print(self): print('Lefts ', self.children[0]) print('Rights', self.children[1]) print('Values', self.values) print('Counts', self.counts) self._debug_print(self.root, 0) def _debug_print(self, i, depth): if i != -1: self._debug_print(self.children[0][i], depth + 1) print(' ' * depth, self.values[i], self.counts[i]) self._debug_print(self.children[1][i], depth + 1) def x_check(x_dict, nx, y, d, stack, stacked): counter = 0 if nx in x_dict: rbst, lst = x_dict[nx] ny, _ = rbst.lower_bound(y - d, y + d + 1) while ny <= y + d: z = (nx << 32) | ny if z not in stacked: stack.append(z) stacked.add(z) rbst.delete(ny) ny, _ = rbst.lower_bound(y - d, y + d + 1) i = bisect.bisect_left(lst, y - d) j = bisect.bisect(lst, y + d) counter = j - i return counter def y_check(y_dict, ny, x, d, stack, stacked): counter = 0 if ny in y_dict: rbst, lst = y_dict[ny] nx, _ = rbst.lower_bound(x - d, x + d + 1) while nx <= x + d: z = (nx << 32) | ny if z not in stacked: stack.append(z) stacked.add(z) rbst.delete(nx) nx, _ = rbst.lower_bound(x - d, x + d + 1) i = bisect.bisect(lst, x - d) j = bisect.bisect_left(lst, x + d) counter = j - i return counter n, a, b, *xy = map(int, sys.stdin.buffer.read().split()) xxx = xy[0::2] yyy = xy[1::2] x_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []]) y_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []]) OFFSET = 10 ** 9 MASK = (1 << 32) - 1 for i in range(n): x = xxx[i] - yyy[i] + OFFSET y = xxx[i] + yyy[i] x_dict[x][0].insert(y) y_dict[y][0].insert(x) x_dict[x][1].append(y) y_dict[y][1].append(x) for _, lst in x_dict.values(): lst.sort() for _, lst in y_dict.values(): lst.sort() a -= 1 b -= 1 ax_, ay_ = xxx[a], yyy[a] bx_, by_ = xxx[b], yyy[b] ax, ay = ax_ - ay_ + OFFSET, ax_ + ay_ bx, by = bx_ - by_ + OFFSET, bx_ + by_ az = (ax << 32) | ay bz = (bx << 32) | by d = max(abs(ax - bx), abs(ay - by)) x_dict[ax][0].delete(ay) x_dict[bx][0].delete(by) y_dict[ay][0].delete(ax) y_dict[by][0].delete(bx) stack = [az, bz] stacked = {az, bz} ans = 0 while stack: z = stack.pop() y = z & MASK x = z >> 32 ans += x_check(x_dict, x - d, y, d, stack, stacked) ans += x_check(x_dict, x + d, y, d, stack, stacked) ans += y_check(y_dict, y - d, x, d, stack, stacked) ans += y_check(y_dict, y + d, x, d, stack, stacked) print(ans // 2) ```
1,569
Provide a correct Python 3 solution for this coding contest problem. There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i). We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|). You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes. When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes. Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass. Constraints * 2≦N≦10^5 * 1≦x_i, y_i≦10^9 * 1≦a < b≦N * When i ≠ j, (x_i, y_i) ≠ (x_j, y_j) * x_i and y_i are integers. Input The input is given from Standard Input in the following format: N a b x_1 y_1 : x_N y_N Output Print the number of the pairs of pinholes that can be pointed by the compass. Examples Input 5 1 2 1 1 4 3 6 1 5 5 4 8 Output 4 Input 6 2 3 1 3 5 3 3 5 8 4 4 7 2 5 Output 4 Input 8 1 2 1 5 4 3 8 2 4 7 8 8 3 3 6 6 4 8 Output 7 "Correct Solution: ``` N, a, b = map(int, input().split()); a -= 1; b -= 1 P = [] Q = [] for i in range(N): x, y = map(int, input().split()) P.append((x-y, x+y, i)) Q.append((x+y, x-y, i)) d = max(abs(P[a][0] - P[b][0]), abs(P[a][1] - P[b][1])) *parent, = range(N) def root(x): if x == parent[x]: return x y = parent[x] = root(parent[x]) return y def unite(x, y): px = root(x); py = root(y) if px < py: parent[py] = px else: parent[px] = py C = [0]*N D = [0]*N def check(P0, i0, j0): return abs(P0[i0][0] - P0[j0][0]) == abs(P0[i0][1] - P0[j0][1]) def solve(P0): P = P0[:] P.sort() s = t = 0; prev = -1 for i in range(N): x, y, i0 = P[i] while t < N and P[t][0] < x-d or (P[t][0] == x-d and P[t][1] <= y+d): t += 1 while s < N and (P[s][0] < x-d or (P[s][0] == x-d and P[s][1] < y-d)): s += 1 if s < t: j0 = P[s][2] unite(i0, j0) if check(P0, i0, j0): D[i0] += 1 else: C[i0] += 1 if s < t-1: j0 = P[t-1][2] if check(P0, i0, j0): D[i0] += 1 C[i0] += t-s-2 else: C[i0] += t-s-1 for j in range(max(prev, s), t-1): unite(P[j][2], P[j+1][2]) prev = t-1 solve(P) solve(Q) S = T = 0 r = root(a) for i in range(N): if root(i) == r: S += C[i]; T += D[i] print(S + T//2) ```
1,570
Provide a correct Python 3 solution for this coding contest problem. There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i). We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|). You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes. When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes. Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass. Constraints * 2≦N≦10^5 * 1≦x_i, y_i≦10^9 * 1≦a < b≦N * When i ≠ j, (x_i, y_i) ≠ (x_j, y_j) * x_i and y_i are integers. Input The input is given from Standard Input in the following format: N a b x_1 y_1 : x_N y_N Output Print the number of the pairs of pinholes that can be pointed by the compass. Examples Input 5 1 2 1 1 4 3 6 1 5 5 4 8 Output 4 Input 6 2 3 1 3 5 3 3 5 8 4 4 7 2 5 Output 4 Input 8 1 2 1 5 4 3 8 2 4 7 8 8 3 3 6 6 4 8 Output 7 "Correct Solution: ``` import sys input = sys.stdin.readline from collections import defaultdict import bisect INF = 10 ** 12 N,a,b = map(int,input().split()) X_to_Y = defaultdict(lambda: [-INF,INF]) Y_to_X = defaultdict(lambda: [-INF,INF]) for i in range(1,N+1): x,y = map(int,input().split()) x,y = x+y,x-y X_to_Y[x].append(y) Y_to_X[y].append(x) if i == a: sx,sy = x,y elif i == b: tx,ty = x,y R = max(abs(sx-tx), abs(sy-ty)) for key, arr in X_to_Y.items(): arr.sort() L = len(arr) move_right = list(range(1,L)) + [None] X_to_Y[key] = [arr,move_right] for key, arr in Y_to_X.items(): arr.sort() L = len(arr) move_right = list(range(1,L)) + [None] Y_to_X[key] = [arr,move_right] equiv_class = set([(sx,sy), (tx,ty)]) answer = 0 task = [(sx,sy), (tx,ty)] while task: x,y = task.pop() # 既出の元も含めて辺の個数を数える # 未出の元を同値に追加 # x,y両方満たすものは、x側にだけ入れる for x1 in [x-R,x+R]: if not (x1 in X_to_Y): continue arr, move_right = X_to_Y[x1] left = bisect.bisect_left(arr, y-R) right = bisect.bisect_right(arr, y+R) # [left, right) が辺で結べるターゲット answer += right - left # 辺の個数 i = left while i < right: y1 = arr[i] if (x1,y1) not in equiv_class: equiv_class.add((x1,y1)) task.append((x1,y1)) # なるべく、既に居ない元は見ないで済ませるように next_i = move_right[i] if next_i >= right: break move_right[i] = right i = next_i for y1 in [y-R,y+R]: if not y1 in Y_to_X: continue arr, move_right = Y_to_X[y1] left = bisect.bisect_left(arr, x-R+1) right = bisect.bisect_right(arr, x+R-1) # [left, right) が辺で結べるターゲット answer += right - left # 辺の個数 i = left while i < right: x1 = arr[i] if (x1,y1) not in equiv_class: equiv_class.add((x1,y1)) task.append((x1,y1)) # なるべく、既に居ない元は見ないで済ませるように next_i = move_right[i] if next_i >= right: break move_right[i] = right i = next_i answer //= 2 # 両方向から辺を見た print(answer) ```
1,571
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i). We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|). You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes. When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes. Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass. Constraints * 2≦N≦10^5 * 1≦x_i, y_i≦10^9 * 1≦a < b≦N * When i ≠ j, (x_i, y_i) ≠ (x_j, y_j) * x_i and y_i are integers. Input The input is given from Standard Input in the following format: N a b x_1 y_1 : x_N y_N Output Print the number of the pairs of pinholes that can be pointed by the compass. Examples Input 5 1 2 1 1 4 3 6 1 5 5 4 8 Output 4 Input 6 2 3 1 3 5 3 3 5 8 4 4 7 2 5 Output 4 Input 8 1 2 1 5 4 3 8 2 4 7 8 8 3 3 6 6 4 8 Output 7 Submitted Solution: ``` import bisect import sys from collections import defaultdict class RandomizedBinarySearchTree: def __init__(self): self.children = [[-1], [-1]] self.values = [0] self.counts = [0] self.indices = set() self.root = 0 def merge(self, left_root, right_root): children = self.children counts = self.counts li = left_root ri = right_root stack = [] switch = 0 while li != 0 and ri != 0: if switch: stack.append((li, 1)) li = children[1][li] else: stack.append((ri, 0)) ri = children[0][ri] switch ^= 1 i = li if li != 0 else ri while stack: pi, is_right = stack.pop() children[is_right][pi] = i counts[pi] = counts[children[0][pi]] + counts[children[1][pi]] + 1 i = pi return i def split(self, root, x): i = root lefts, rights = self.children values = self.values counts = self.counts l_stack = [] r_stack = [] while i != 0: if x < values[i]: r_stack.append(i) i = lefts[i] else: l_stack.append(i) i = rights[i] li, ri = 0, 0 while l_stack: pi = l_stack.pop() rights[pi] = li counts[pi] = counts[lefts[pi]] + counts[li] + 1 li = pi while r_stack: pi = r_stack.pop() lefts[pi] = ri counts[pi] = counts[ri] + counts[rights[pi]] + 1 ri = pi return li, ri def insert(self, x): if self.indices: ni = self.indices.pop() self.children[0][ni] = self.children[1][ni] = 0 self.values[ni] = x self.counts[ni] = 1 else: ni = len(self.values) self.children[0].append(0) self.children[1].append(0) self.values.append(x) self.counts.append(1) li, ri = self.split(self.root, x) self.root = self.merge(self.merge(li, ni), ri) def delete(self, x): li, mri = self.split(self.root, x - 1) mi, ri = self.split(mri, x) if mi == 0: self.root = self.merge(li, ri) return self.indices.add(mi) self.root = self.merge(li, ri) return def upper_bound(self, x, default=-1): i = self.root lefts, rights = self.children values = self.values counts = self.counts y = default c = counts[i] j = 0 while i != 0: if x < values[i]: y = values[i] c = j + counts[lefts[i]] i = lefts[i] else: j += counts[lefts[i]] + 1 i = rights[i] return y, c def lower_bound(self, x, default=-1): i = self.root lefts, rights = self.children values = self.values counts = self.counts y = default c = counts[i] j = 0 while i != 0: if x <= values[i]: y = values[i] c = j + counts[lefts[i]] i = lefts[i] else: j += counts[lefts[i]] + 1 i = rights[i] return y, c def get_k_th(self, k, default=-1): i = self.root children = self.children values = self.values counts = self.counts if counts[i] <= k: return default j = k while i != 0: left_count = counts[children[0][i]] if left_count == j: return values[i] elif left_count > j: i = children[0][i] else: j -= left_count + 1 i = children[1][i] return default def debug_print(self): print('Lefts ', self.children[0]) print('Rights', self.children[1]) print('Values', self.values) print('Counts', self.counts) self._debug_print(self.root, 0) def _debug_print(self, i, depth): if i != -1: self._debug_print(self.children[0][i], depth + 1) print(' ' * depth, self.values[i], self.counts[i]) self._debug_print(self.children[1][i], depth + 1) def x_check(x_dict, rev_dict, nx, y, d, stack, stacked): counter = 0 if nx in x_dict: rbst, lst = x_dict[nx] ny, _ = rbst.lower_bound(y - d, y + d + 1) while ny <= y + d: q = rev_dict[nx, ny] if q not in stacked: stack.append(q) stacked.add(q) rbst.delete(ny) ny, _ = rbst.lower_bound(y - d, y + d + 1) i = bisect.bisect_left(lst, y - d) j = bisect.bisect(lst, y + d) counter = j - i return counter def y_check(y_dict, rev_dict, ny, x, d, stack, stacked): counter = 0 if ny in y_dict: rbst, lst = y_dict[ny] nx, _ = rbst.lower_bound(x - d, x + d + 1) while nx <= x + d: q = rev_dict[nx, ny] if q not in stacked: stack.append(q) stacked.add(q) rbst.delete(nx) nx, _ = rbst.lower_bound(x - d, x + d + 1) i = bisect.bisect(lst, x - d) j = bisect.bisect_left(lst, x + d) counter = j - i return counter n, a, b, *xy = map(int, sys.stdin.buffer.read().split()) xxx = xy[0::2] yyy = xy[1::2] x_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []]) y_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []]) ppp = [] rev_dict = {} for i in range(n): x = xxx[i] - yyy[i] y = xxx[i] + yyy[i] x_dict[x][0].insert(y) y_dict[y][0].insert(x) x_dict[x][1].append(y) y_dict[y][1].append(x) ppp.append((x, y)) rev_dict[x, y] = i for _, lst in x_dict.values(): lst.sort() for _, lst in y_dict.values(): lst.sort() a -= 1 b -= 1 ax, ay = ppp[a] bx, by = ppp[b] d = max(abs(ax - bx), abs(ay - by)) x_dict[ax][0].delete(ay) x_dict[bx][0].delete(by) y_dict[ay][0].delete(ax) y_dict[by][0].delete(bx) stack = [a, b] stacked = {a, b} ans = 0 while stack: p = stack.pop() x, y = ppp[p] ans += x_check(x_dict, rev_dict, x - d, y, d, stack, stacked) ans += x_check(x_dict, rev_dict, x + d, y, d, stack, stacked) ans += y_check(y_dict, rev_dict, y - d, x, d, stack, stacked) ans += y_check(y_dict, rev_dict, y + d, x, d, stack, stacked) print(ans // 2) ``` No
1,572
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i). We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|). You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes. When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes. Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass. Constraints * 2≦N≦10^5 * 1≦x_i, y_i≦10^9 * 1≦a < b≦N * When i ≠ j, (x_i, y_i) ≠ (x_j, y_j) * x_i and y_i are integers. Input The input is given from Standard Input in the following format: N a b x_1 y_1 : x_N y_N Output Print the number of the pairs of pinholes that can be pointed by the compass. Examples Input 5 1 2 1 1 4 3 6 1 5 5 4 8 Output 4 Input 6 2 3 1 3 5 3 3 5 8 4 4 7 2 5 Output 4 Input 8 1 2 1 5 4 3 8 2 4 7 8 8 3 3 6 6 4 8 Output 7 Submitted Solution: ``` import bisect import sys import random from collections import defaultdict class RandomizedBinarySearchTree: def __init__(self): self.children = [[-1], [-1]] self.values = [0] self.counts = [0] self.indices = set() self.root = 0 def merge(self, left_root, right_root): children = self.children counts = self.counts li = left_root ri = right_root stack = [] while li != 0 and ri != 0: if random.randrange(counts[li] + counts[ri]) < counts[li]: stack.append((li, 1)) li = children[1][li] else: stack.append((ri, 0)) ri = children[0][ri] i = li if li != 0 else ri while stack: pi, is_right = stack.pop() children[is_right][pi] = i counts[pi] = counts[children[0][pi]] + counts[children[1][pi]] + 1 i = pi return i def split(self, root, x): i = root lefts, rights = self.children values = self.values counts = self.counts l_stack = [] r_stack = [] while i != 0: if x < values[i]: r_stack.append(i) i = lefts[i] else: l_stack.append(i) i = rights[i] li, ri = 0, 0 while l_stack: pi = l_stack.pop() rights[pi] = li counts[pi] = counts[lefts[pi]] + counts[li] + 1 li = pi while r_stack: pi = r_stack.pop() lefts[pi] = ri counts[pi] = counts[ri] + counts[rights[pi]] + 1 ri = pi return li, ri def insert(self, x): if self.indices: ni = self.indices.pop() self.children[0][ni] = self.children[1][ni] = 0 self.values[ni] = x self.counts[ni] = 1 else: ni = len(self.values) self.children[0].append(0) self.children[1].append(0) self.values.append(x) self.counts.append(1) li, ri = self.split(self.root, x) self.root = self.merge(self.merge(li, ni), ri) def delete(self, x): li, mri = self.split(self.root, x - 1) mi, ri = self.split(mri, x) if mi == 0: self.root = self.merge(li, ri) return self.indices.add(mi) self.root = self.merge(li, ri) return def upper_bound(self, x, default=-1): i = self.root lefts, rights = self.children values = self.values counts = self.counts y = default c = counts[i] j = 0 while i != 0: if x < values[i]: y = values[i] c = j + counts[lefts[i]] i = lefts[i] else: j += counts[lefts[i]] + 1 i = rights[i] return y, c def lower_bound(self, x, default=-1): i = self.root lefts, rights = self.children values = self.values counts = self.counts y = default c = counts[i] j = 0 while i != 0: if x <= values[i]: y = values[i] c = j + counts[lefts[i]] i = lefts[i] else: j += counts[lefts[i]] + 1 i = rights[i] return y, c def get_k_th(self, k, default=-1): i = self.root children = self.children values = self.values counts = self.counts if counts[i] <= k: return default j = k while i != 0: left_count = counts[children[0][i]] if left_count == j: return values[i] elif left_count > j: i = children[0][i] else: j -= left_count + 1 i = children[1][i] return default def debug_print(self): print('Lefts ', self.children[0]) print('Rights', self.children[1]) print('Values', self.values) print('Counts', self.counts) self._debug_print(self.root, 0) def _debug_print(self, i, depth): if i != -1: self._debug_print(self.children[0][i], depth + 1) print(' ' * depth, self.values[i], self.counts[i]) self._debug_print(self.children[1][i], depth + 1) def x_check(x_dict, rev_dict, nx, y, d): ret = set() counter = 0 if nx in x_dict: rbst, lst = x_dict[nx] ny, _ = rbst.lower_bound(y - d, y + d + 1) while ny <= y + d: q = rev_dict[nx, ny] ret.add(q) rbst.delete(ny) ny, _ = rbst.lower_bound(y - d, y + d + 1) i = bisect.bisect_left(lst, y - d) j = bisect.bisect(lst, y + d) counter = j - i return ret, counter def y_check(y_dict, rev_dict, ny, x, d): ret = set() counter = 0 if ny in y_dict: rbst, lst = y_dict[ny] nx, _ = rbst.lower_bound(x - d, x + d + 1) while nx <= x + d: q = rev_dict[nx, ny] ret.add(q) rbst.delete(nx) nx, _ = rbst.lower_bound(x - d, x + d + 1) i = bisect.bisect(lst, x - d) j = bisect.bisect_left(lst, x + d) counter = j - i return ret, counter n, a, b, *xy = map(int, sys.stdin.buffer.read().split()) xxx = xy[0::2] yyy = xy[1::2] x_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []]) y_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []]) ppp = [] rev_dict = {} for i in range(n): x = xxx[i] - yyy[i] y = xxx[i] + yyy[i] x_dict[x][0].insert(y) y_dict[y][0].insert(x) x_dict[x][1].append(y) y_dict[y][1].append(x) ppp.append((x, y)) rev_dict[x, y] = i for _, lst in x_dict.values(): lst.sort() for _, lst in y_dict.values(): lst.sort() a -= 1 b -= 1 ax, ay = ppp[a] bx, by = ppp[b] d = max(abs(ax - bx), abs(ay - by)) x_dict[ax][0].delete(ay) x_dict[bx][0].delete(by) y_dict[ay][0].delete(ax) y_dict[by][0].delete(bx) stack = [a, b] stacked = {a, b} ans = 0 while stack: p = stack.pop() x, y = ppp[p] qqq = set() qs, c = x_check(x_dict, rev_dict, x - d, y, d) qqq.update(qs) ans += c qs, c = x_check(x_dict, rev_dict, x + d, y, d) qqq.update(qs) ans += c qs, c = y_check(y_dict, rev_dict, y - d, x, d) qqq.update(qs) ans += c qs, c = y_check(y_dict, rev_dict, y + d, x, d) qqq.update(qs) ans += c for q in qqq: if q not in stacked: stack.append(q) stacked.add(q) print(ans // 2) ``` No
1,573
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i). We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|). You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes. When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes. Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass. Constraints * 2≦N≦10^5 * 1≦x_i, y_i≦10^9 * 1≦a < b≦N * When i ≠ j, (x_i, y_i) ≠ (x_j, y_j) * x_i and y_i are integers. Input The input is given from Standard Input in the following format: N a b x_1 y_1 : x_N y_N Output Print the number of the pairs of pinholes that can be pointed by the compass. Examples Input 5 1 2 1 1 4 3 6 1 5 5 4 8 Output 4 Input 6 2 3 1 3 5 3 3 5 8 4 4 7 2 5 Output 4 Input 8 1 2 1 5 4 3 8 2 4 7 8 8 3 3 6 6 4 8 Output 7 Submitted Solution: ``` import numpy as np n, a, b = map(int, input().split()) holes = np.fromiter((complex(*map(int, input().split())) for _ in range(n)), dtype=complex) ab = holes[a - 1] - holes[b - 1] d = abs(ab.real) + abs(ab.imag) links = [set() for _ in range(n)] for i in range(0, n, 100): xy1, xy2 = np.meshgrid(holes, holes[i:i + 100]) diff = xy1 - xy2 dists = abs(diff.real) + abs(diff.imag) for x, y in zip(*np.where(dists == d)): links[x].add(y) visited = set() queue = [a - 1] while queue: p = queue.pop() if p in visited: continue visited.add(p) queue.extend(q for q in links[p] if q not in visited) print(sum(p < q for p in visited for q in links[p])) ``` No
1,574
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i). We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|). You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes. When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes. Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass. Constraints * 2≦N≦10^5 * 1≦x_i, y_i≦10^9 * 1≦a < b≦N * When i ≠ j, (x_i, y_i) ≠ (x_j, y_j) * x_i and y_i are integers. Input The input is given from Standard Input in the following format: N a b x_1 y_1 : x_N y_N Output Print the number of the pairs of pinholes that can be pointed by the compass. Examples Input 5 1 2 1 1 4 3 6 1 5 5 4 8 Output 4 Input 6 2 3 1 3 5 3 3 5 8 4 4 7 2 5 Output 4 Input 8 1 2 1 5 4 3 8 2 4 7 8 8 3 3 6 6 4 8 Output 7 Submitted Solution: ``` import sys from collections import defaultdict sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): def dist(i,j): x0,y0=xy[i] x1,y1=xy[j] return abs(x0-x1)+abs(y0-y1) def dfs(i): res=0 x,y=xy[i] zx,zy=x//d,y//d for dx,dy in [(0,0),(1,0),(0,1),(1,1),(-1,0),(0,-1),(-1,-1),(-1,1),(1,-1)]: for j in zone[zx+dx,zx+dy]: if fin[i,j]:continue if dist(i,j)==d: fin[i,j]=fin[j,i]=True res+=1 if not vis[j]: vis[j]=True res+=dfs(j) return res n,a,b=MI() a,b=a-1,b-1 xy=LLI(n) d=dist(a,b) zone=defaultdict(list) for i,(x,y) in enumerate(xy):zone[x//d,y//d].append(i) #print(zone) vis=[False]*n fin=defaultdict(bool) print(dfs(a)) main() ``` No
1,575
Provide a correct Python 3 solution for this coding contest problem. Snuke got an integer sequence from his mother, as a birthday present. The sequence has N elements, and the i-th of them is i. Snuke performs the following Q operations on this sequence. The i-th operation, described by a parameter q_i, is as follows: * Take the first q_i elements from the sequence obtained by concatenating infinitely many copy of the current sequence, then replace the current sequence with those q_i elements. After these Q operations, find how many times each of the integers 1 through N appears in the final sequence. Constraints * 1 ≦ N ≦ 10^5 * 0 ≦ Q ≦ 10^5 * 1 ≦ q_i ≦ 10^{18} * All input values are integers. Input The input is given from Standard Input in the following format: N Q q_1 : q_Q Output Print N lines. The i-th line (1 ≦ i ≦ N) should contain the number of times integer i appears in the final sequence after the Q operations. Examples Input 5 3 6 4 11 Output 3 3 3 2 0 Input 10 10 9 13 18 8 10 10 9 19 22 27 Output 7 4 4 3 3 2 2 2 0 0 "Correct Solution: ``` import bisect,sys input=sys.stdin.readline N,Q=map(int,input().split()) que=[(N,0)] for i in range(Q): q=int(input()) que.append((q,i+1)) que.sort(reverse=True) ext=[] while que: q,id=que.pop() if not ext: ext.append((q,id)) else: if ext[-1][1]<id: if ext[-1][0]==q: ext.pop() ext.append((q,id)) Q=len(ext) data=[1]*Q data[0]=ext[0][0] edge=[[] for i in range(Q)] nowext=[ext[0][0]] for i in range(1,Q): q=ext[i][0] rest=q while True: id=bisect.bisect_right(nowext,rest) if id==0: break else: edge[id-1].append((i,rest//nowext[id-1])) rest%=nowext[id-1] nowext.append(ext[i][0]) data[i]=rest #print(edge) dp=[1]*Q for i in range(Q-2,-1,-1): temp=0 for nv,c in edge[i]: temp+=dp[nv]*c dp[i]=temp #print(dp) #print(data) minus=[0]*(ext[0][0]+1) for i in range(Q): minus[data[i]]+=dp[i] base=sum(dp) for i in range(1,N+1): if i-1<=ext[0][0]: base-=minus[i-1] print(base) ```
1,576
Provide a correct Python 3 solution for this coding contest problem. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA "Correct Solution: ``` while True: d, w, h = map(int, input().split()) if d == 0: break n = int(input()) a, b = sorted([d, w, h])[:2] t = pow(pow(a, 2) + pow(b, 2), 0.5) // 2 for _ in range(n): print("OK" if int(input()) > t else "NA") ```
1,577
Provide a correct Python 3 solution for this coding contest problem. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA "Correct Solution: ``` while True: Size_lis = list(map(int,input().split())) if Size_lis == [0,0,0]: break n = int(input()) Size_lis.remove(max(Size_lis)) m_r = (Size_lis[0] / 2) ** 2 + (Size_lis[1] / 2) ** 2 for i in range(n): r = int(input()) if m_r >= r ** 2: print("NA") else: print("OK") ```
1,578
Provide a correct Python 3 solution for this coding contest problem. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA "Correct Solution: ``` while 1: try: dwh=list(map(int,input().split())) if dwh[0]==dwh[1]==dwh[2]==0:break dwh.sort() std=(dwh[0]**2+dwh[1]**2)**0.5/2 n=int(input()) for i in range(n): r=int(input()) if std<r:print("OK") else:print("NA") except:break ```
1,579
Provide a correct Python 3 solution for this coding contest problem. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA "Correct Solution: ``` while True: d, w, h = (int(x) for x in input().split()) if d == 0: break q = int(input()) vmin = min(d * d + w * w, d * d + h * h, w * w + h * h) for i in range(0,q): r = int(input()) ans = 'NA' if (2 * r) ** 2 > vmin: ans = 'OK' print(ans) ```
1,580
Provide a correct Python 3 solution for this coding contest problem. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA "Correct Solution: ``` while 1: cheese=list(map(int,input().split())) if cheese[0]==0:break n=int(input()) cheese=sorted(cheese) for i in range(n): radius = int(input()) if radius*2>(cheese[0]**2+cheese[1]**2)**0.5:print("OK") else:print("NA") ```
1,581
Provide a correct Python 3 solution for this coding contest problem. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA "Correct Solution: ``` import math output = [] while True: depth, width, height = [int(item) for item in input().split(" ")] if depth == 0 and width == 0 and height == 0: break cheeseRadius = math.sqrt((width / 2)**2 + (height / 2)**2) inputCount = int(input()) for lp in range(inputCount): entranceRadius = int(input()) if cheeseRadius < entranceRadius: output.append("OK") else: output.append("NA") print("\n".join(output)) ```
1,582
Provide a correct Python 3 solution for this coding contest problem. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA "Correct Solution: ``` while True: d, w, h = sorted(map(int, input().split())) if d == w == h == 0: break dw = (d**2 + w**2) n = int(input()) for i in range(n): y = int(input()) if (2*y)**2 > dw: print("OK") else: print("NA") ```
1,583
Provide a correct Python 3 solution for this coding contest problem. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA "Correct Solution: ``` while 1: a,b,_=sorted(map(int,input().split())) if a==0:break c=(a**2+b**2)**0.5//2 for _ in range(int(input())):print('OK' if c<int(input()) else 'NA') ```
1,584
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA Submitted Solution: ``` def get_input(): while True: try: yield ''.join(input()) except EOFError: break while True: d = [int(i) for i in input().split()] if d[0] == 0 and d[1] == 0 and d[2] == 0: break d.sort() cheese = d[0] * d[0] + d[1] * d[1] N = int(input()) for l in range(N): R = int(input())*2 if cheese < R*R: print("OK") else: print("NA") ``` Yes
1,585
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA Submitted Solution: ``` import math while True : d, w, h = map(int, input().split()) if d == 0 and w == 0 and h == 0 : break length = [d, w, h] len_1 = sorted([d, w, h])[0] len_2 = sorted([d, w, h])[1] max_d = math.sqrt(len_1**2 + len_2**2) n = int(input()) for i in range(n) : r = int(input()) if 2 * r > max_d : print("OK") else : print("NA") ``` Yes
1,586
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA Submitted Solution: ``` while True: ch = list(map(int,input().split())) if (0 in ch): break ch.sort() n = int(input()) a = ch[0] b = ch[1] d = a**2 + b**2 for i in range(n): r = int(input()) R = 2*r if (R**2 > d): print('OK') else: print('NA') ``` Yes
1,587
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA Submitted Solution: ``` while True: d, w,h =list(map(int, input().split())) if d ==0 and w==0 and h==0: break m = min(d*d+w*w, d*d+h*h, w*w+h*h) for i in range(int(input())): r = int(input()) if (2*r)**2 >m: print("OK") else: print("NA") ``` Yes
1,588
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA Submitted Solution: ``` import math while True: d,w,h = map(int,input().split()) if d == 0: break n = int(input()) dist = [d**2+w**2, d**2+h**2, w**2+h**2] leng = min(dist) for i in range(n): if int(input())-leng > 0: print('OK') else: print('NA') ``` No
1,589
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA Submitted Solution: ``` import itertools def ch(l,r): t=itertools.combinations(l,2) for i in t: if i[0]**2+i[1]**2<4*r**2: return True return False l=list(map(int,input().split())) o=[ch(l,int(input())) for i in range(int(input()))] input() [print("OK") if i else print("NA") for i in o] ``` No
1,590
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA Submitted Solution: ``` import sys for a in sys.stdin: if' 'in a: s=list(map(int,a.split())) c=0 while 1: t=s;s=[t.count(e)for e in t] if t==s:break c+=1 print(c);print(*s) ``` No
1,591
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive? Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA". You may assume that the number of holes is less than 10000. Input The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows: A B C n R1 R2 . . Rn n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole. Output For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole. Example Input 10 6 8 5 4 8 6 2 5 0 0 0 Output NA OK OK NA NA Submitted Solution: ``` import sys for a in sys.stdin: if'0'in a:break if' 'in a: s=list(map(int,a.split())) c=0 while 1: t=s;s=[t.count(e)for e in t] if t==s:break c+=1 print(c);print(*s) ``` No
1,592
Provide a correct Python 3 solution for this coding contest problem. An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as: $ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $ On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows. <image> This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $. The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows: $ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $ $ 7 + 6i + 7j + 8k $ $ + 14i + 12i ^ 2 + 14ij + 16ik $ $ + 21j + 18ji + 21j ^ 2 + 24jk $ $ + 28k + 24ki + 28kj + 32k ^ 2 $ By applying the table above $ = -58 + 16i + 36j + 32k $ It will be. Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $. input Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: $ n $ $ data_1 $ $ data_2 $ :: $ data_n $ The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format: $ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $ All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50. output Prints the product of a given set of quaternions for each dataset. Example Input 2 1 2 3 4 7 6 7 8 5 6 7 8 3 2 3 4 0 Output -58 16 36 32 -50 32 28 48 "Correct Solution: ``` while 1: n=int(input()) if n==0:break for _ in range(n): x1,y1,z1,w1,x2,y2,z2,w2=map(int,input().split()) print((x1*x2) - (y1*y2) -(z1*z2) -(w1*w2), (x1*y2) + (y1*x2) + (z1*w2) - (w1*z2), (x1*z2) - (y1*w2) + (z1*x2) + (w1*y2), (x1*w2) + (y1*z2) - (z1*y2) + (w1*x2)) ```
1,593
Provide a correct Python 3 solution for this coding contest problem. An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as: $ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $ On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows. <image> This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $. The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows: $ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $ $ 7 + 6i + 7j + 8k $ $ + 14i + 12i ^ 2 + 14ij + 16ik $ $ + 21j + 18ji + 21j ^ 2 + 24jk $ $ + 28k + 24ki + 28kj + 32k ^ 2 $ By applying the table above $ = -58 + 16i + 36j + 32k $ It will be. Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $. input Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: $ n $ $ data_1 $ $ data_2 $ :: $ data_n $ The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format: $ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $ All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50. output Prints the product of a given set of quaternions for each dataset. Example Input 2 1 2 3 4 7 6 7 8 5 6 7 8 3 2 3 4 0 Output -58 16 36 32 -50 32 28 48 "Correct Solution: ``` # from sys import exit while(True): N = int(input()) if N == 0: break for _ in range(N): a, b, c, d, A, B, C, D = [int(n) for n in input().split()] z = a*A - b*B - c*C - d*D i = a*B + b*A + c*D - d*C j = a*C - b*D + c*A + d*B k = a*D + b*C - c*B + d*A print(z, i, j, k) # a = [int(input()) for _ in range(N)] # S = str(input()) # L = len(S) # T = str(input()) ```
1,594
Provide a correct Python 3 solution for this coding contest problem. An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as: $ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $ On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows. <image> This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $. The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows: $ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $ $ 7 + 6i + 7j + 8k $ $ + 14i + 12i ^ 2 + 14ij + 16ik $ $ + 21j + 18ji + 21j ^ 2 + 24jk $ $ + 28k + 24ki + 28kj + 32k ^ 2 $ By applying the table above $ = -58 + 16i + 36j + 32k $ It will be. Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $. input Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: $ n $ $ data_1 $ $ data_2 $ :: $ data_n $ The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format: $ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $ All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50. output Prints the product of a given set of quaternions for each dataset. Example Input 2 1 2 3 4 7 6 7 8 5 6 7 8 3 2 3 4 0 Output -58 16 36 32 -50 32 28 48 "Correct Solution: ``` while 1: n = int(input()) if n == 0: break for _ in range(n): data = list(map(int, input().split())) c = data[0]*data[4] - data[1]*data[5] - \ data[2]*data[6] - data[3]*data[7] i = data[0]*data[5] + data[1]*data[4] + \ data[2]*data[7] - data[3]*data[6] j = data[0]*data[6] - data[1]*data[7] + \ data[2]*data[4] + data[3]*data[5] k = data[0]*data[7] + data[1]*data[6] - \ data[2]*data[5] + data[3]*data[4] print(c, i, j, k) ```
1,595
Provide a correct Python 3 solution for this coding contest problem. An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as: $ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $ On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows. <image> This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $. The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows: $ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $ $ 7 + 6i + 7j + 8k $ $ + 14i + 12i ^ 2 + 14ij + 16ik $ $ + 21j + 18ji + 21j ^ 2 + 24jk $ $ + 28k + 24ki + 28kj + 32k ^ 2 $ By applying the table above $ = -58 + 16i + 36j + 32k $ It will be. Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $. input Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: $ n $ $ data_1 $ $ data_2 $ :: $ data_n $ The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format: $ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $ All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50. output Prints the product of a given set of quaternions for each dataset. Example Input 2 1 2 3 4 7 6 7 8 5 6 7 8 3 2 3 4 0 Output -58 16 36 32 -50 32 28 48 "Correct Solution: ``` while 1: N = int(input()) if N == 0: break P = [ [1, 2, 3, 4], [2, -1, 4, -3], [3, -4, -1, 2], [4, 3, -2, -1], ] for i in range(N): *X, = map(int, input().split()) X0 = X[:4]; X1 = X[4:] Y = [0]*4 for i, x0 in enumerate(X0): for j, x1 in enumerate(X1): z = P[i][j] if z > 0: Y[z-1] += x0*x1 else: Y[-z-1] -= x0*x1 print(*Y) ```
1,596
Provide a correct Python 3 solution for this coding contest problem. An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as: $ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $ On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows. <image> This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $. The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows: $ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $ $ 7 + 6i + 7j + 8k $ $ + 14i + 12i ^ 2 + 14ij + 16ik $ $ + 21j + 18ji + 21j ^ 2 + 24jk $ $ + 28k + 24ki + 28kj + 32k ^ 2 $ By applying the table above $ = -58 + 16i + 36j + 32k $ It will be. Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $. input Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: $ n $ $ data_1 $ $ data_2 $ :: $ data_n $ The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format: $ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $ All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50. output Prints the product of a given set of quaternions for each dataset. Example Input 2 1 2 3 4 7 6 7 8 5 6 7 8 3 2 3 4 0 Output -58 16 36 32 -50 32 28 48 "Correct Solution: ``` import sys f = sys.stdin index = [[0,1,2,3], [1,0,3,2], [2,3,0,1], [3,2,1,0]] sign = [[1, 1, 1, 1], [1,-1, 1,-1], [1,-1,-1, 1], [1, 1,-1,-1]] while True: n = int(f.readline()) if n == 0: break for _ in range(n): ab = list(map(int, f.readline().split())) ret = [0] * 4 for i, a in enumerate(ab[:4]): for j, b in enumerate(ab[4:]): ret[index[i][j]] += sign[i][j] * a * b print(*ret) ```
1,597
Provide a correct Python 3 solution for this coding contest problem. An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as: $ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $ On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows. <image> This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $. The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows: $ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $ $ 7 + 6i + 7j + 8k $ $ + 14i + 12i ^ 2 + 14ij + 16ik $ $ + 21j + 18ji + 21j ^ 2 + 24jk $ $ + 28k + 24ki + 28kj + 32k ^ 2 $ By applying the table above $ = -58 + 16i + 36j + 32k $ It will be. Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $. input Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: $ n $ $ data_1 $ $ data_2 $ :: $ data_n $ The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format: $ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $ All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50. output Prints the product of a given set of quaternions for each dataset. Example Input 2 1 2 3 4 7 6 7 8 5 6 7 8 3 2 3 4 0 Output -58 16 36 32 -50 32 28 48 "Correct Solution: ``` b = [[1,2,3,4],[2,-1,4,-3],[3,-4,-1,2],[4,3,-2,-1]] while 1: n = int(input()) if n == 0:break for _ in range(n): x = tuple(map(int, input().split())) x1, x2 = x[:4], x[4:] a = [0,0,0,0] for i in range(4): for j in range(4): if b[i][j] > 0:a[b[i][j] - 1] += x1[i] * x2[j] else:a[-b[i][j] - 1] -= x1[i] * x2[j] print(*a) ```
1,598
Provide a correct Python 3 solution for this coding contest problem. An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as: $ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $ On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows. <image> This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $. The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows: $ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $ $ 7 + 6i + 7j + 8k $ $ + 14i + 12i ^ 2 + 14ij + 16ik $ $ + 21j + 18ji + 21j ^ 2 + 24jk $ $ + 28k + 24ki + 28kj + 32k ^ 2 $ By applying the table above $ = -58 + 16i + 36j + 32k $ It will be. Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $. input Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: $ n $ $ data_1 $ $ data_2 $ :: $ data_n $ The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format: $ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $ All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50. output Prints the product of a given set of quaternions for each dataset. Example Input 2 1 2 3 4 7 6 7 8 5 6 7 8 3 2 3 4 0 Output -58 16 36 32 -50 32 28 48 "Correct Solution: ``` while True : n = int(input()) if n == 0 : break for i in range(n) : x1, y1, z1, w1, x2, y2, z2, w2 = map(int, input().split()) print((x1*x2 - y1*y2 - z1*z2 - w1*w2), (x1*y2 + x2*y1 + z1*w2 - z2*w1), (x1*z2 - y1*w2 + x2*z1 + y2*w1), (x1*w2 + y1*z2 - y2*z1 + x2*w1)) ```
1,599