message
stringlengths
2
65.1k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
0
108k
cluster
float64
14
14
__index_level_0__
int64
0
217k
Provide a correct Python 3 solution for this coding contest problem. There are 2N people numbered 1 through 2N. The height of Person i is h_i. How many ways are there to make N pairs of people such that the following conditions are satisfied? Compute the answer modulo 998,244,353. * Each person is contained in exactly one pair. * For each pair, the heights of the two people in the pair are different. Two ways are considered different if for some p and q, Person p and Person q are paired in one way and not in the other. Constraints * 1 \leq N \leq 50,000 * 1 \leq h_i \leq 100,000 * All values in input are integers. Input Input is given from Standard Input in the following format: N h_1 : h_{2N} Output Print the answer. Examples Input 2 1 1 2 3 Output 2 Input 5 30 10 20 40 20 10 10 30 50 60 Output 516
instruction
0
45,933
14
91,866
"Correct Solution: ``` ROOT = 3 MOD = 998244353 roots = [pow(ROOT,(MOD-1)>>i,MOD) for i in range(24)] # 1 の 2^i 乗根 iroots = [pow(x,MOD-2,MOD) for x in roots] # 1 の 2^i 乗根の逆元 def untt(a,n): for i in range(n): m = 1<<(n-i-1) for s in range(1<<i): w_N = 1 s *= m*2 for p in range(m): a[s+p], a[s+p+m] = (a[s+p]+a[s+p+m])%MOD, (a[s+p]-a[s+p+m])*w_N%MOD w_N = w_N*roots[n-i]%MOD def iuntt(a,n): for i in range(n): m = 1<<i for s in range(1<<(n-i-1)): w_N = 1 s *= m*2 for p in range(m): a[s+p], a[s+p+m] = (a[s+p]+a[s+p+m]*w_N)%MOD, (a[s+p]-a[s+p+m]*w_N)%MOD w_N = w_N*iroots[i+1]%MOD inv = pow((MOD+1)//2,n,MOD) for i in range(1<<n): a[i] = a[i]*inv%MOD def convolution(a,b): la = len(a) lb = len(b) deg = la+lb-2 n = deg.bit_length() if min(la, lb) <= 50: if la < lb: la,lb = lb,la a,b = b,a res = [0]*(la+lb-1) for i in range(la): for j in range(lb): res[i+j] += a[i]*b[j] res[i+j] %= MOD return res N = 1<<n a += [0]*(N-len(a)) b += [0]*(N-len(b)) untt(a,n) untt(b,n) for i in range(N): a[i] = a[i]*b[i]%MOD iuntt(a,n) return a[:deg+1] def convolution_all(polys): if not polys: return [1] #polys.sort(key=lambda x:len(x)) N = len(polys) height = (N-1).bit_length() N0 = 1<<(height) data = [None]*(N0*2) data[N0:N0+N] = polys data[N0+N:] = [[1] for _ in range(N0-N)] #print(data) #print(polys) for k in range(N0-1,0,-1): data[k] = convolution(data[2*k], data[2*k+1]) return data[1] SIZE=10**5 + 10 #998244353 #ここを変更する inv = [0]*SIZE # inv[j] = j^{-1} mod MOD fac = [0]*SIZE # fac[j] = j! mod MOD finv = [0]*SIZE # finv[j] = (j!)^{-1} mod MOD fac[0] = fac[1] = 1 finv[0] = finv[1] = 1 for i in range(2,SIZE): fac[i] = fac[i-1]*i%MOD finv[-1] = pow(fac[-1],MOD-2,MOD) for i in range(SIZE-1,0,-1): finv[i-1] = finv[i]*i%MOD inv[i] = finv[i]*fac[i-1]%MOD def choose(n,r): # nCk mod MOD の計算 if 0 <= r <= n: return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD else: return 0 dfac = [0]*SIZE # fac[j] = j! mod MOD dfac[0] = dfac[1] = 1 for i in range(2,SIZE): dfac[i] = dfac[i-2]*i%MOD import sys readline = sys.stdin.readline read = sys.stdin.read n,*h = map(int, read().split()) from collections import Counter def get(k): # k 個の頂点からiペア作れる場合の数 res = [1] for i in range(k//2): res.append(res[-1]*choose(k-2*i,2)%MOD) for i in range(2,k//2 + 1): res[i] = res[i]*finv[i]%MOD return res d = Counter(h) res = [] for k,v in d.items(): if v >= 2: res.append(get(v)) if v > n: print(0) exit() bad = convolution_all(res) #print(bad) #bad = [1,5,7,3] ans = 0 sgn = 1 for i,ai in enumerate(bad): ans += sgn*(dfac[2*n-2*i-1] if i < n else 1)*ai ans %= MOD sgn *= -1 #print(ans) print(ans%MOD) ```
output
1
45,933
14
91,867
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N people numbered 1 through 2N. The height of Person i is h_i. How many ways are there to make N pairs of people such that the following conditions are satisfied? Compute the answer modulo 998,244,353. * Each person is contained in exactly one pair. * For each pair, the heights of the two people in the pair are different. Two ways are considered different if for some p and q, Person p and Person q are paired in one way and not in the other. Constraints * 1 \leq N \leq 50,000 * 1 \leq h_i \leq 100,000 * All values in input are integers. Input Input is given from Standard Input in the following format: N h_1 : h_{2N} Output Print the answer. Examples Input 2 1 1 2 3 Output 2 Input 5 30 10 20 40 20 10 10 30 50 60 Output 516 Submitted Solution: ``` ROOT = 3 MOD = 998244353 roots = [pow(ROOT,(MOD-1)>>i,MOD) for i in range(24)] # 1 の 2^i 乗根 iroots = [pow(x,MOD-2,MOD) for x in roots] # 1 の 2^i 乗根の逆元 def untt(a,n): for i in range(n): m = 1<<(n-i-1) for s in range(1<<i): w_N = 1 s *= m*2 for p in range(m): a[s+p], a[s+p+m] = (a[s+p]+a[s+p+m])%MOD, (a[s+p]-a[s+p+m])*w_N%MOD w_N = w_N*roots[n-i]%MOD def iuntt(a,n): for i in range(n): m = 1<<i for s in range(1<<(n-i-1)): w_N = 1 s *= m*2 for p in range(m): a[s+p], a[s+p+m] = (a[s+p]+a[s+p+m]*w_N)%MOD, (a[s+p]-a[s+p+m]*w_N)%MOD w_N = w_N*iroots[i+1]%MOD inv = pow((MOD+1)//2,n,MOD) for i in range(1<<n): a[i] = a[i]*inv%MOD def convolution(a,b): la = len(a) lb = len(b) deg = la+lb-2 n = deg.bit_length() if min(la, lb) <= 50: if la < lb: la,lb = lb,la a,b = b,a res = [0]*(la+lb-1) for i in range(la): for j in range(lb): res[i+j] += a[i]*b[j] res[i+j] %= MOD return res N = 1<<n a += [0]*(N-len(a)) b += [0]*(N-len(b)) untt(a,n) untt(b,n) for i in range(N): a[i] = a[i]*b[i]%MOD iuntt(a,n) return a[:deg+1] def convolution_all(polys): if not polys: return [1] polys.sort(key=lambda x:len(x)) N = len(polys) height = (N-1).bit_length() N0 = 1<<(height) data = [None]*(N0*2) data[N0:N0+N] = polys data[N0+N:] = [[1] for _ in range(N0-N)] #print(data) #print(polys) for k in range(N0-1,0,-1): data[k] = convolution(data[2*k], data[2*k+1]) return data[1] SIZE=10**5 + 10 #998244353 #ここを変更する inv = [0]*SIZE # inv[j] = j^{-1} mod MOD fac = [0]*SIZE # fac[j] = j! mod MOD finv = [0]*SIZE # finv[j] = (j!)^{-1} mod MOD fac[0] = fac[1] = 1 finv[0] = finv[1] = 1 for i in range(2,SIZE): fac[i] = fac[i-1]*i%MOD finv[-1] = pow(fac[-1],MOD-2,MOD) for i in range(SIZE-1,0,-1): finv[i-1] = finv[i]*i%MOD inv[i] = finv[i]*fac[i-1]%MOD def choose(n,r): # nCk mod MOD の計算 if 0 <= r <= n: return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD else: return 0 dfac = [0]*SIZE # fac[j] = j! mod MOD dfac[0] = dfac[1] = 1 for i in range(2,SIZE): dfac[i] = dfac[i-2]*i%MOD import sys readline = sys.stdin.readline read = sys.stdin.read n,*h = map(int, read().split()) from collections import Counter def get(k): # k 個の頂点からiペア作れる場合の数 res = [1] for i in range(k//2): res.append(res[-1]*choose(k-2*i,2)%MOD) for i in range(2,k//2 + 1): res[i] = res[i]*finv[i]%MOD return res d = Counter(h) res = [] for k,v in d.items(): if v >= 2: res.append(get(v)) if v > n: print(0) exit() bad = convolution_all(res) #print(bad) #bad = [1,5,7,3] ans = 0 sgn = 1 for i,ai in enumerate(bad): ans += sgn*(dfac[2*n-2*i-1] if i < n else 1)*ai ans %= MOD sgn *= -1 #print(ans) print(ans%MOD) ```
instruction
0
45,938
14
91,876
Yes
output
1
45,938
14
91,877
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are 2N people numbered 1 through 2N. The height of Person i is h_i. How many ways are there to make N pairs of people such that the following conditions are satisfied? Compute the answer modulo 998,244,353. * Each person is contained in exactly one pair. * For each pair, the heights of the two people in the pair are different. Two ways are considered different if for some p and q, Person p and Person q are paired in one way and not in the other. Constraints * 1 \leq N \leq 50,000 * 1 \leq h_i \leq 100,000 * All values in input are integers. Input Input is given from Standard Input in the following format: N h_1 : h_{2N} Output Print the answer. Examples Input 2 1 1 2 3 Output 2 Input 5 30 10 20 40 20 10 10 30 50 60 Output 516 Submitted Solution: ``` import sys readline = sys.stdin.readline from collections import Counter MOD = 998244353 def fac(limit): fac = [1]*limit for i in range(2,limit): fac[i] = i * fac[i-1]%MOD faci = [None]*limit faci[-1] = pow(fac[-1], MOD -2, MOD) for i in range(-2, -limit-1, -1): faci[i] = faci[i+1] * (limit + i + 1) % MOD return fac, faci fac, faci = fac(234139) def comb(a, b): if not a >= b >= 0: return 0 return fac[a]*faci[b]*faci[a-b]%MOD def perm(a, b): if not a >= b >= 0: return 0 return fac[a]*faci[a-b]%MOD pr = 3 LS = 20 class NTT: def __init__(self): self.N0 = 1<<LS omega = pow(pr, (MOD-1)//self.N0, MOD) omegainv = pow(omega, MOD-2, MOD) self.w = [0]*(self.N0//2) self.winv = [0]*(self.N0//2) self.w[0] = 1 self.winv[0] = 1 for i in range(1, self.N0//2): self.w[i] = (self.w[i-1]*omega)%MOD self.winv[i] = (self.winv[i-1]*omegainv)%MOD used = set() for i in range(self.N0//2): if i in used: continue j = 0 for k in range(LS-1): j |= (i>>k&1) << (LS-2-k) used.add(j) self.w[i], self.w[j] = self.w[j], self.w[i] self.winv[i], self.winv[j] = self.winv[j], self.winv[i] def _fft(self, A): M = len(A) bn = 1 hbs = M>>1 while hbs: for j in range(hbs): A[j], A[j+hbs] = A[j] + A[j+hbs], A[j] - A[j+hbs] if A[j] > MOD: A[j] -= MOD if A[j+hbs] < 0: A[j+hbs] += MOD for bi in range(1, bn): wi = self.w[bi] for j in range(bi*hbs*2, bi*hbs*2+hbs): A[j], A[j+hbs] = (A[j] + wi*A[j+hbs])%MOD, (A[j] - wi*A[j+hbs])%MOD bn <<= 1 hbs >>= 1 def _ifft(self, A): M = len(A) bn = M>>1 hbs = 1 while bn: for j in range(hbs): A[j], A[j+hbs] = A[j] + A[j+hbs], A[j] - A[j+hbs] if A[j] > MOD: A[j] -= MOD if A[j+hbs] < 0: A[j+hbs] += MOD for bi in range(1, bn): winvi = self.winv[bi] for j in range(bi*hbs*2, bi*hbs*2+hbs): A[j], A[j+hbs] = (A[j] + A[j+hbs])%MOD, winvi*(A[j] - A[j+hbs])%MOD bn >>= 1 hbs <<= 1 def convolve(self, A, B): LA = len(A) LB = len(B) LC = LA+LB-1 M = 1<<(LC-1).bit_length() A += [0]*(M-LA) B += [0]*(M-LB) self._fft(A) self._fft(B) C = [0]*M for i in range(M): C[i] = A[i]*B[i]%MOD self._ifft(C) minv = pow(M, MOD-2, MOD) for i in range(LC): C[i] = C[i]*minv%MOD return C[:LC] return C def inverse(self, A): LA = len(A) dep = (LA-1).bit_length() M = 1<<dep A += [0]*(M-LA) g = [pow(A[0], MOD-2, MOD)] for n in range(dep): dl = 1<<(n+1) f = A[:dl] fg = self.convolve(f, g[:])[:dl] fgg = self.convolve(fg, g[:])[:dl] ng = [None]*dl for i in range(dl//2): ng[i] = (2*g[i]-fgg[i])%MOD for i in range(dl//2, dl): ng[i] = MOD-fgg[i] g = ng[:] return g[:LA] def integral(self, A): A = [1] + [A[i]*faci[i+2]%MOD for i in range(len(A))] N = int(readline()) H = [int(readline()) for _ in range(2*N)] Ch = list(Counter(H).values()) M = len(Ch) table = [[] for _ in range(M)] for c in Ch: res = [0]*(c//2 + 1) for i in range(c//2 + 1): #res[i] = perm(c, 2*i)*faci[i]%MOD res[i] = comb(c, 2*i)*fac[2*i]*faci[i]%MOD table.append(res) FT = NTT() for i in range(M-1, 0, -1): table[i] = FT.convolve(table[2*i], table[2*i+1]) ans = 0 for i in range(len(table[1])): ans = (ans + (-1 if i&1 else 1)*table[1][i]*comb(N, i)*fac[2*N-2*i]*fac[i]) % MOD ans = ans*pow((MOD+1)//2, N, MOD)*faci[N]%MOD print(ans) ```
instruction
0
45,941
14
91,882
No
output
1
45,941
14
91,883
Provide a correct Python 3 solution for this coding contest problem. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11
instruction
0
45,944
14
91,888
"Correct Solution: ``` N = int(input()) P = list(map(int, input().split())) dp = [[min(i, N - i - 1, j, N - j - 1) for j in range(N)] for i in range(N)] presence = [[1 for j in range(N)] for i in range(N)] ans = 0 for i in range(len(P)): P[i] -= 1 x, y = divmod(P[i], N) ans += dp[x][y] presence[x][y] = 0 Q = [(x, y)] while Q: x, y = Q.pop() new_value = dp[x][y] + presence[x][y] if 1 <= x and dp[x - 1][y] > new_value: dp[x - 1][y] = new_value Q.append((x - 1, y)) if x < N - 1 and dp[x + 1][y] > new_value: dp[x + 1][y] = new_value Q.append((x + 1, y)) if 1 <= y and dp[x][y - 1] > new_value: dp[x][y - 1] = new_value Q.append((x, y - 1)) if y < N - 1 and dp[x][y + 1] > new_value: dp[x][y + 1] = new_value Q.append((x, y + 1)) print(ans) ```
output
1
45,944
14
91,889
Provide a correct Python 3 solution for this coding contest problem. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11
instruction
0
45,945
14
91,890
"Correct Solution: ``` N=int(input()) plist=list(map(int,input().split())) sit=[[1]*N for _ in range(N)] dist=[[min(i,j,N-1-i,N-1-j) for j in range(N)] for i in range(N)] answer=0 for p in plist: i=(p-1)//N j=(p-1)%N #print(i,j) sit[i][j]=0 answer+=dist[i][j] queue=[(i,j,dist[i][j])] d=0 while queue: x,y,d=queue.pop() if x>0 and dist[x-1][y]>d: dist[x-1][y]=d queue.append((x-1,y,d+sit[x-1][y])) if x<N-1 and dist[x+1][y]>d: dist[x+1][y]=d queue.append((x+1,y,d+sit[x+1][y])) if y>0 and dist[x][y-1]>d: dist[x][y-1]=d queue.append((x,y-1,d+sit[x][y-1])) if y<N-1 and dist[x][y+1]>d: dist[x][y+1]=d queue.append((x,y+1,d+sit[x][y+1])) print(answer) ```
output
1
45,945
14
91,891
Provide a correct Python 3 solution for this coding contest problem. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11
instruction
0
45,946
14
91,892
"Correct Solution: ``` import sys input = sys.stdin.readline from collections import deque n = int(input()) a = list(map(int,input().split())) n += 2 ls = [0 for i in range(n*n)] for i in range(n): for j in range(n): ls[i*n+j] = min(i,j,n-1-i,n-1-j)-1 ans = 0 st = [0]*(n*n) for i in a: x,y = divmod(i-1,n-2) i = (x+1)*n+(y+1) ans += ls[i] st[i]=1 q = deque([i]) while q: j = q.popleft() for k in (j-n,j+n,j+1,j-1): if st[j]: if ls[j] < ls[k]: ls[k] = ls[j] q.append(k) elif ls[j]+1 < ls[k]: ls[k] = ls[j]+1 q.append(k) print(ans) ```
output
1
45,946
14
91,893
Provide a correct Python 3 solution for this coding contest problem. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11
instruction
0
45,947
14
91,894
"Correct Solution: ``` from collections import deque n=int(input()) *p,=map(int, input().split()) for i in range(n*n):p[i]-=1 seats=[min(i//n,i%n,n-(i//n)-1,n-(i%n)-1) for i in range(n*n)] exist=[1]*(n*n) dxy=(n,-n,1,-1) ans=0 q=deque() for pi in p: ans+=seats[pi] exist[pi]=0 q.append(pi) while q: i=q.popleft() for dx in dxy: idx=i+dx if 0<=idx<n*n : if seats[i]+exist[i]<seats[idx]: seats[idx]=seats[i]+exist[i] q.append(idx) print(ans) ```
output
1
45,947
14
91,895
Provide a correct Python 3 solution for this coding contest problem. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11
instruction
0
45,948
14
91,896
"Correct Solution: ``` def main(): import sys input = sys.stdin.readline n = int(input()) l = list(map(int,input().split())) l = [((i-1)//n, (i-1) % n) for i in l] check = [[1 for j in range(n)] for i in range(n)] d = [[min(i, n-i-1, j, n-j-1) for j in range(n)] for i in range(n)] ans = 0 for x,y in l: check[x][y] = 0 ans += d[x][y] q = [] q.append((x,y,d[x][y])) while q: bx,by,z = q.pop() if bx != 0: if d[bx-1][by] > z: d[bx-1][by] = z q.append((bx-1,by,z+check[bx-1][by])) if bx != n-1: if d[bx+1][by] > z: d[bx+1][by] = z q.append((bx+1,by,z+check[bx+1][by])) if by != 0: if d[bx][by-1] > z: d[bx][by-1] = z q.append((bx,by-1,z+check[bx][by-1])) if by != n-1: if d[bx][by+1] > z: d[bx][by+1] = z q.append((bx,by+1,z+check[bx][by+1])) print(ans) if __name__ =="__main__": main() ```
output
1
45,948
14
91,897
Provide a correct Python 3 solution for this coding contest problem. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11
instruction
0
45,949
14
91,898
"Correct Solution: ``` import sys input = sys.stdin.readline n = int(input()) p = list(map(int, input().split())) distance = [[min(i, j, n - i - 1, n - j - 1) for i in range(n)] for j in range(n)] check = [[1] * n for i in range(n)] p = [((i - 1) // n, (i - 1) % n) for i in p] def move(x,y,nx,ny): if distance[nx][ny] > distance[x][y] + check[x][y]: distance[nx][ny] = distance[x][y] + check[x][y] d.append((nx,ny)) ans = 0 for i, j in p: ans += distance[j][i] d=[] d.append((j,i)) check[j][i] = 0 while len(d): x, y = d.pop() if x!=0: move(x,y,x-1,y) if x!=n-1: move(x,y,x+1,y) if y!=0: move(x,y,x,y-1) if y!=n-1: move(x,y,x,y+1) print(ans) ```
output
1
45,949
14
91,899
Provide a correct Python 3 solution for this coding contest problem. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11
instruction
0
45,950
14
91,900
"Correct Solution: ``` cin = open(0).read().strip().split('\n') n = int(cin[0]) p = list(map(int, cin[1].split(' '))) space = [1]*(n*n) dist = [0]*(n*n) for r in range(n): for c in range(n): dist[r*n+c] = min(r, (n-1)-r, c, (n-1)-c) ans = 0 for pi in p: pi -= 1 q = [pi] ans += dist[pi] space[pi] = 0 while q: pp = q.pop() for dxi in [-n,-1,1,n]: if dxi == 1 and pp % n==n-1: continue if dxi == -1 and pp % n ==n: continue if 0<=pp+dxi<n*n and dist[pp+dxi] > dist[pp]+space[pp]: q.append(pp+dxi) dist[pp+dxi] = dist[pp]+space[pp] print(ans) ```
output
1
45,950
14
91,901
Provide a correct Python 3 solution for this coding contest problem. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11
instruction
0
45,951
14
91,902
"Correct Solution: ``` N, = map(int, input().split()) R = [0] * (N**2+1) V = [0] * (N**2+1) X = list(map(int, input().split())) for ai in range(N**2): i, j = divmod(ai, N) R[ai+1] = min([i, N-i-1, j, N-j-1]) r = 0 for a in X: r += R[a] V[a] = 1 stack=[a] while stack: v = stack.pop() ccs = [] if v>N: ccs.append(v-N) if v<N**2+1-N: ccs.append(v+N) if v%N: ccs.append(v+1) if v%N!=1: ccs.append(v-1) for u in ccs: if R[v]>=R[u] or ((1-V[v]) and R[v]+1 >= R[u]): continue R[u] -= 1 stack.append(u) print(r) ```
output
1
45,951
14
91,903
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11 Submitted Solution: ``` def N(): return int(input()) def NM():return map(int,input().split()) def L():return list(NM()) def LN(n):return [N() for i in range(n)] def LL(n):return [L() for i in range(n)] import sys sys.setrecursionlimit(10**6) n=N() p=L() size=(n+1)**2+1 ans=[0]*size for i in range(1,n+1): for j in range(1,n+1): k=i*(n+1)+j ans[k]=min(i-1,j-1,n-i,n-j) c=[1]*size di=[-1,1,n+1,-n-1] def f(k,i): if k+i>=size: return if ans[k]+c[k]<ans[k+i]: ans[k+i]-=1 q.append(k+i) t=0 for i in p: y=i%n if y==0: y=n x=(i+n-1)//n k=x*(n+1)+y c[k]=0 t+=ans[k] q=[k] while q: k=q.pop() f(k,1) f(k,-1) f(k,n+1) f(k,-n-1) print(t) ```
instruction
0
45,952
14
91,904
Yes
output
1
45,952
14
91,905
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11 Submitted Solution: ``` N, = map(int, input().split()) R = [0] * (N**2+1) V = [0] * (N**2+1) X = list(map(int, input().split())) for ai in range(N**2): i, j = divmod(ai, N) R[ai+1] = min([i, N-i-1, j, N-j-1]) r = 0 for a in X: r += R[a] V[a] = 1 stack=[a] while stack: v = stack.pop() if v>N and (R[v]<R[v-N] and (V[v] or R[v]+1<R[v-N])): R[v-N] -= 1 stack.append(v-N) if v<N**2+1-N and (R[v]<R[v+N] and (V[v] or R[v]+1<R[v+N])): R[v+N] -= 1 stack.append(v+N) if v%N and (R[v]<R[v+1] and (V[v] or R[v]+1<R[v+1])): R[v+1] -= 1 stack.append(v+1) if v%N-1 and (R[v]<R[v-1] and (V[v] or R[v]+1<R[v-1])): R[v-1] -= 1 stack.append(v-1) print(r) ```
instruction
0
45,953
14
91,906
Yes
output
1
45,953
14
91,907
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11 Submitted Solution: ``` import os import itertools 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 def test(): ans_hist = [INF] * (N ** 2) ans = 0 dist = [INF] * (N ** 2) for i, p in enumerate(P): p -= 1 ph, pw = p // N, p % N d = min(ph, pw, N - ph - 1, N - pw - 1) j = i - 1 while j >= 0: q = P[j] - 1 qh, qw = q // N, q % N d = min(d, dist[q] + abs(ph - qh) + abs(pw - qw) - 1) j -= 1 dist[p] = d ans_hist[p] = d j = i - 1 while j >= 0: q = P[j] - 1 qh, qw = q // N, q % N dist[q] = min(dist[q], dist[p] + abs(ph - qh) + abs(pw - qw) - 1) j -= 1 ans += d # print(p, dist) print(ans) # print(dist) # print(np.array(ans_hist, dtype=int).reshape(N, N)) return ans N = int(sys.stdin.buffer.readline()) P = list(map(int, sys.stdin.buffer.readline().split())) # 解説 dist = [INF] * len(P) for h, w in itertools.product(range(N), range(N)): dist[h * N + w] = min(h, w, N - h - 1, N - w - 1) present = [1] * len(P) ans = 0 for p in P: p -= 1 h, w = p // N, p % N present[p] = 0 ans += dist[p] stack = [(p, dist[p])] while stack: p, d = stack.pop() h, w = p // N, p % N d += present[p] for dh, dw in ((0, 1), (1, 0), (0, -1), (-1, 0)): if 0 <= h + dh < N and 0 <= w + dw < N: q = (h + dh) * N + w + dw if d < dist[q]: dist[q] = d stack.append((q, d)) print(ans) # test() ```
instruction
0
45,954
14
91,908
Yes
output
1
45,954
14
91,909
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11 Submitted Solution: ``` # -*- coding: utf-8 -*- import sys from collections import deque sys.setrecursionlimit(10**9) INF=10**18 MOD=10**9+7 input=lambda: sys.stdin.readline().rstrip() YesNo=lambda b: bool([print('Yes')] if b else print('No')) YESNO=lambda b: bool([print('YES')] if b else print('NO')) int1=lambda x:int(x)-1 N=int(input()) M=N**2 P=tuple(map(int1,input().split())) d=[0]*M for i in range(N): for j in range(N): d[i*N+j]=min(min(i,N-1-i),min(j,N-1-j)) used=[1]*M ans=0 que=[] for p in P: ans+=d[p] used[p]=0 que.append(p) while que: x=que.pop() for ddx in (-1,1,-N,N): nx=x+ddx if 0<=nx<M: if used[x]==0 and d[nx]-d[x]>=1: d[nx]-=1 que.append(nx) elif used[x]==1 and d[nx]-d[x]>=2: d[nx]-=1 que.append(nx) print(ans) ```
instruction
0
45,955
14
91,910
Yes
output
1
45,955
14
91,911
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11 Submitted Solution: ``` n=int(input()) l=list(map(int, input().split())) l1=[[0 for i in range(n)] for j in range(n)] k=1 cnt=0 for i in range(n): for j in range(n): l1[i][j]=k k=k+1 for i in l: r=i/n if r!=int(r): r=int(r) else: r=int(r)-1 c=(i%n)-1 if c==-1: c=n-1 if r==0 or r==n-1 or c==0 or c==n-1: l1[r][c]=0 else: val1=0 val2=0 val3=0 val4=0 num1=0 num2=0 num3=0 num4=0 for h in range(r): if l1[h][c]!=0: val1=1 num1=num1+1 for h in range(r+1,n): if val1==0: break if l1[h][c]!=0: val2=1 num2=num2+1 if num2>=num1: break for h in range(c): if val1==0 or val2==0: break if l1[r][h]!=0: val3=1 num3=num3+1 if num3>=num1 or num3>=num2: break for h in range(c+1,n): if val1==0 or val2==0 or val3==0: break if l1[r][h]!=0: val4=1 num4=num4+1 if num4>=num3 or num4>=num2 or num4>=num1: break if val1==1 and val2==1 and val3==1 and val4==1: l9=[] l9.append(num1) l9.append(num2) l9.append(num3) l9.append(num4) cnt=cnt+min(l9) l1[r][c]=0 print(cnt) ```
instruction
0
45,956
14
91,912
No
output
1
45,956
14
91,913
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11 Submitted Solution: ``` from numba import njit import numpy as np import sys input = sys.stdin.buffer.readline @njit('i4, i4, i4, i4[:,:], i4[:,:]') def dfs(N, h, w, minDist, isFilled): dist = minDist[h][w] + isFilled[h][w] if h + 1 < N and minDist[h + 1][w] > dist: minDist[h + 1][w] = dist dfs(N, h + 1, w, minDist, isFilled) if h - 1 >= 0 and minDist[h - 1][w] > dist: minDist[h - 1][w] = dist dfs(N, h - 1, w, minDist, isFilled) if w + 1 < N and minDist[h][w + 1] > dist: minDist[h][w + 1] = dist dfs(N, h, w + 1, minDist, isFilled) if w - 1 < N and minDist[h][w - 1] > dist: minDist[h][w - 1] = dist dfs(N, h, w - 1, minDist, isFilled) @njit('i4(i4, i4[:])') def solve(N, P): minDist = np.ones((N, N), dtype=np.int32) isFilled = np.ones((N, N), dtype=np.int32) for h in range(N): for w in range(N): minDist[h][w] = min(h, N - h - 1, w, N - w - 1) ans = 0 for p in P: h, w = divmod(p, N) ans += minDist[h][w] isFilled[h][w] = False dfs(N, h, w, minDist, isFilled) return ans N = int(input()) P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32) print(solve(N, P)) ```
instruction
0
45,957
14
91,914
No
output
1
45,957
14
91,915
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11 Submitted Solution: ``` import sys,queue,math,copy,itertools,bisect,collections,heapq def main(): sys.setrecursionlimit(10**7) INF = 10**18 MOD = 10**9 + 7 LI = lambda : [int(x) for x in sys.stdin.readline().split()] _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()] NI = lambda : int(sys.stdin.readline()) SI = lambda : sys.stdin.readline().rstrip() N = NI() P = LI() s = [[0] * N for _ in range(N)] for i in range(N): for j in range(N): s[i][j] = min(i,N-1-i,j,N-1-j) r = [[1] * N for _ in range(N)] ans = 0 for p in P: x = (p-1)//N y = (p-1) % N ans += s[x][y] r[x][y] = 0 q = collections.deque() q.append((x,y,s[x][y])) while q: a,b,c = q.pop() nc = c + r[a][b] for da,db in ((1,0),(0,1),(-1,0),(0,-1)): na = a + da; nb = b + db if 0 < na < N-1 and 0 < nb < N-1: if s[na][nb] <= nc: continue s[na][nb] = nc q.append((na,nb,nc)) print(ans) if __name__ == '__main__': main() ```
instruction
0
45,958
14
91,916
No
output
1
45,958
14
91,917
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2. At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever. Compute the number of pairs of viewers (x, y) such that y will hate x forever. Constraints * 2 \le N \le 500 * The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \\{1, 2, \dots, N^2\\}. Input The input is given from Standard Input in the format N P_1 P_2 \cdots P_{N^2} Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Output If ans is the number of pairs of viewers described in the statement, you should print on Standard Output ans Examples Input 3 1 3 7 9 5 4 8 6 2 Output 1 Input 4 6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8 Output 3 Input 6 11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30 Output 11 Submitted Solution: ``` from collections import deque def hate(x,y): cur_hate = 0 cur_q = deque() next_q = deque() cur_q.append((x,y)) already = set() while True: while cur_q: x,y = cur_q.popleft() for i in range(4): next_x = x+dx[i] next_y = y+dy[i] if not (next_x,next_y) in already: if grid[next_y][next_x] == "o": return cur_hate if grid[next_y][next_x] == "#": next_q.append((next_x,next_y)) else: cur_q.append((next_x,next_y)) already.add((x,y)) cur_hate += 1 cur_q = next_q next_q = deque() n = int(input()) grid = [] grid.append(["o"]*(n+2)) for i in range(n): grid.append(["o"]+["#"]*n+["o"]) grid.append(["o"]*(n+2)) dx = [0,1,0,-1] dy = [1,0,-1,0] ans = 0 p = list(map(int,input().split())) for i in range(n**2): pos = p[i] x = (pos-1)%n+1 y = (pos-1)//n+1 ans += hate(x,y) if hate(x,y) == 0: grid[y][x] = "o" else: grid[y][x] = "." print(ans) ```
instruction
0
45,959
14
91,918
No
output
1
45,959
14
91,919
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. ButCoder Inc. is a startup company whose main business is the development and operation of the programming competition site "ButCoder". There are N members of the company including the president, and each member except the president has exactly one direct boss. Each member has a unique ID number from 1 to N, and the member with the ID i is called Member i. The president is Member 1, and the direct boss of Member i (2 ≤ i ≤ N) is Member b_i (1 ≤ b_i < i). All the members in ButCoder have gathered in the great hall in the main office to take a group photo. The hall is very large, and all N people can stand in one line. However, they have been unable to decide the order in which they stand. For some reason, all of them except the president want to avoid standing next to their direct bosses. How many ways are there for them to stand in a line that satisfy their desires? Find the count modulo 10^9+7. Constraints * 2 ≤ N ≤ 2000 * 1 ≤ b_i < i (2 ≤ i ≤ N) Input Input is given from Standard Input in the following format: N b_2 b_3 : b_N Output Print the number of ways for the N members to stand in a line, such that no member except the president is next to his/her direct boss, modulo 10^9+7. Examples Input 4 1 2 3 Output 2 Input 3 1 2 Output 0 Input 5 1 1 3 3 Output 8 Input 15 1 2 3 1 4 2 7 1 8 2 8 1 8 2 Output 97193524 Submitted Solution: ``` # -*- coding: utf-8 -*- numAns = 0 class Node: def __init__(self,init): self.addr = None self.boss = init self.subordinate = [] def printBoss(self): print(self.boss) def showTree(self,tmp_node): print(self.addr,self.boss,self.subordinate) for sub in self.subordinate: # print("-",end="") tmp_node[sub-1].showTree(tmp_node) def findTree(self,N,relation,tmp_node): relation.append(self.addr) if N == len(relation): global numAns numAns = (numAns+1) % 1000000007 relation.pop() return list = [True for i in range(N)] list[int(self.addr)-1] = False if self.boss is not None: list[int(self.boss)-1] = False for sub in self.subordinate: list[int(sub)-1] = False for rel in relation: list[int(rel)-1] = False for i in range(len(list)): if list[i] is True: tmp_node[i].findTree(N,relation,tmp_node) relation.pop() def input_boss(boss): N = input() for i in range(int(N)-1): boss.append(input()) return N def make_tree(nodes,boss): for num in boss: nodes.append(Node(num)) nodes.insert(0,Node(None)) nodes[0].addr = 1 for i in range(1,len(nodes)): nodes[i].addr = i+1 tmp_boss = int(nodes[i].boss)-1 nodes[tmp_boss].subordinate.append(i+1) # for node in nodes: # print(node.addr,node.boss,node.subordinate) if __name__ == '__main__': boss =[] N = input_boss(boss) nodes = [] make_tree(nodes,boss) # nodes[0].showTree(nodes) for i in range(len(nodes)): relation = [] nodes[i].findTree(int(N),relation,nodes) print(numAns) ```
instruction
0
46,040
14
92,080
No
output
1
46,040
14
92,081
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. ButCoder Inc. is a startup company whose main business is the development and operation of the programming competition site "ButCoder". There are N members of the company including the president, and each member except the president has exactly one direct boss. Each member has a unique ID number from 1 to N, and the member with the ID i is called Member i. The president is Member 1, and the direct boss of Member i (2 ≤ i ≤ N) is Member b_i (1 ≤ b_i < i). All the members in ButCoder have gathered in the great hall in the main office to take a group photo. The hall is very large, and all N people can stand in one line. However, they have been unable to decide the order in which they stand. For some reason, all of them except the president want to avoid standing next to their direct bosses. How many ways are there for them to stand in a line that satisfy their desires? Find the count modulo 10^9+7. Constraints * 2 ≤ N ≤ 2000 * 1 ≤ b_i < i (2 ≤ i ≤ N) Input Input is given from Standard Input in the following format: N b_2 b_3 : b_N Output Print the number of ways for the N members to stand in a line, such that no member except the president is next to his/her direct boss, modulo 10^9+7. Examples Input 4 1 2 3 Output 2 Input 3 1 2 Output 0 Input 5 1 1 3 3 Output 8 Input 15 1 2 3 1 4 2 7 1 8 2 8 1 8 2 Output 97193524 Submitted Solution: ``` # -*- coding: utf-8 -*- numAns = 0 class Node: def __init__(self,init): self.addr = None self.boss = init self.subordinate = [] def printBoss(self): print(self.boss) def showTree(self,tmp_node): print(self.addr,self.boss,self.subordinate) for sub in self.subordinate: # print("-",end="") tmp_node[sub-1].showTree(tmp_node) def findTree(self,N,relation,tmp_node): relation.append(self.addr) if N == len(relation): global numAns numAns += 1 relation.pop() return list = [True for i in range(N)] list[int(self.addr)-1] = False if self.boss is not None: list[int(self.boss)-1] = False for sub in self.subordinate: list[int(sub)-1] = False for rel in relation: list[int(rel)-1] = False for i in range(len(list)): if list[i] is True: tmp_node[i].findTree(N,relation,tmp_node) relation.pop() def input_boss(boss): N = input() for i in range(int(N)-1): boss.append(input()) return N def make_tree(nodes,boss): for num in boss: nodes.append(Node(num)) nodes.insert(0,Node(None)) nodes[0].addr = 1 for i in range(1,len(nodes)): nodes[i].addr = i+1 tmp_boss = int(nodes[i].boss)-1 nodes[tmp_boss].subordinate.append(i+1) # for node in nodes: # print(node.addr,node.boss,node.subordinate) if __name__ == '__main__': boss =[] N = input_boss(boss) nodes = [] make_tree(nodes,boss) # nodes[0].showTree(nodes) for i in range(len(nodes)): relation = [] nodes[i].findTree(int(N),relation,nodes) print(numAns) ```
instruction
0
46,041
14
92,082
No
output
1
46,041
14
92,083
Provide a correct Python 3 solution for this coding contest problem. There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i. N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than L_i, or not less than R_i. Naturally, only one person can sit in the same chair. It may not be possible for all of them to sit in their favorite chairs, if nothing is done. Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions. Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs. Constraints * 1 ≤ N,M ≤ 2 × 10^5 * 0 ≤ L_i < R_i ≤ M + 1(1 ≤ i ≤ N) * All input values are integers. Input Input is given from Standard Input in the following format: N M L_1 R_1 : L_N R_N Output Print the minimum required number of additional chairs. Examples Input 4 4 0 3 2 3 1 3 3 4 Output 0 Input 7 6 0 7 1 5 3 6 2 7 1 6 2 6 3 7 Output 2 Input 3 1 1 2 1 2 1 2 Output 2 Input 6 6 1 6 1 6 1 5 1 5 2 6 2 6 Output 2
instruction
0
46,045
14
92,090
"Correct Solution: ``` class LazySegmentTree(): def __init__(self,n,init,merge_func=min,ide_ele=10**18): self.n=(n-1).bit_length() self.merge_func=merge_func self.ide_ele=ide_ele self.data=[0 for i in range(1<<(self.n+1))] self.lazy=[0 for i in range(1<<(self.n+1))] for i in range(n): self.data[2**self.n+i]=init[i] for i in range(2**self.n-1,0,-1): self.data[i]=self.merge_func(self.data[2*i],self.data[2*i+1]) def propagate_above(self,i): m=i.bit_length()-1 for bit in range(m,0,-1): v=i>>bit add=self.lazy[v] self.lazy[v]=0 self.data[2*v]+=add self.data[2*v+1]+=add self.lazy[2*v]+=add self.lazy[2*v+1]+=add def remerge_above(self,i): while i: i>>=1 self.data[i]=self.merge_func(self.data[2*i],self.data[2*i+1])+self.lazy[i] def update(self,l,r,x): l+=1<<self.n r+=1<<self.n l0=l//(l&-l) r0=r//(r&-r)-1 while l<r: self.data[l]+=x*(l&1) self.lazy[l]+=x*(l&1) l+=(l&1) self.data[r-1]+=x*(r&1) self.lazy[r-1]+=x*(r&1) l>>=1 r>>=1 self.remerge_above(l0) self.remerge_above(r0) def query(self,l,r): l+=1<<self.n r+=1<<self.n l0=l//(l&-l) r0=r//(r&-r)-1 self.propagate_above(l0) self.propagate_above(r0) res=self.ide_ele while l<r: if l&1: res=self.merge_func(res,self.data[l]) l+=1 if r&1: res=self.merge_func(res,self.data[r-1]) l>>=1 r>>=1 return res import sys input=sys.stdin.buffer.readline ide_ele=10**18 N,M=map(int,input().split()) init=[0 for i in range(M+2)] for i in range(1,M+1): init[0]+=1 init[i+1]-=1 for i in range(1,M+1): init[i]+=init[i-1] init[-1]=0 LST=LazySegmentTree(M+2,init) hito=[tuple(map(int,input().split())) for i in range(N)] hito.sort() add=M-N for l,r in hito: LST.update(0,r+1,-1) m=LST.query(l+1,M+2)+l add=min(m,add) print(max(-add,0)) ```
output
1
46,045
14
92,091
Provide a correct Python 3 solution for this coding contest problem. There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i. N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than L_i, or not less than R_i. Naturally, only one person can sit in the same chair. It may not be possible for all of them to sit in their favorite chairs, if nothing is done. Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions. Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs. Constraints * 1 ≤ N,M ≤ 2 × 10^5 * 0 ≤ L_i < R_i ≤ M + 1(1 ≤ i ≤ N) * All input values are integers. Input Input is given from Standard Input in the following format: N M L_1 R_1 : L_N R_N Output Print the minimum required number of additional chairs. Examples Input 4 4 0 3 2 3 1 3 3 4 Output 0 Input 7 6 0 7 1 5 3 6 2 7 1 6 2 6 3 7 Output 2 Input 3 1 1 2 1 2 1 2 Output 2 Input 6 6 1 6 1 6 1 5 1 5 2 6 2 6 Output 2
instruction
0
46,049
14
92,098
"Correct Solution: ``` import sys input=sys.stdin.readline N,M=map(int,input().split()) # N: 処理する区間の長さ INF = 2**31-1 LV = (M+2-1).bit_length() N0 = 2**LV data = [0]*(2*N0) lazy = [0]*(2*N0) def gindex(l, r): L = (l + N0) >> 1; R = (r + N0) >> 1 lc = 0 if l & 1 else (L & -L).bit_length() rc = 0 if r & 1 else (R & -R).bit_length() for i in range(LV): if rc <= i: yield R if L < R and lc <= i: yield L L >>= 1; R >>= 1 # 遅延伝搬処理 def propagates(*ids): for i in reversed(ids): v = lazy[i-1] if not v: continue lazy[2*i-1] += v; lazy[2*i] += v data[2*i-1] += v; data[2*i] += v lazy[i-1] = 0 # 区間[l, r)にxを加算 def update(l, r, x): *ids, = gindex(l, r) propagates(*ids) L = N0 + l; R = N0 + r while L < R: if R & 1: R -= 1 lazy[R-1] += x; data[R-1] += x if L & 1: lazy[L-1] += x; data[L-1] += x L += 1 L >>= 1; R >>= 1 for i in ids: data[i-1] = min(data[2*i-1], data[2*i]) # 区間[l, r)内の最小値を求める def query(l, r): propagates(*gindex(l, r)) L = N0 + l; R = N0 + r s = INF while L < R: if R & 1: R -= 1 s = min(s, data[R-1]) if L & 1: s = min(s, data[L-1]) L += 1 L >>= 1; R >>= 1 return s for i in range(1,M+1): update(0,i+1,1) add=M-N hito=[] for i in range(N): L,R=map(int,input().split()) hito.append((L,R)) hito.sort() #test=[query(i,i+1) for i in range(M+2)] #print(test) for l,r in hito: update(0,r+1,-1) #test=[query(i,i+1) for i in range(M+2)] #print(test) m=query(l+1,M+2)+l add=min(m,add) print(max(-add,0)) ```
output
1
46,049
14
92,099
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i. N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than L_i, or not less than R_i. Naturally, only one person can sit in the same chair. It may not be possible for all of them to sit in their favorite chairs, if nothing is done. Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions. Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs. Constraints * 1 ≤ N,M ≤ 2 × 10^5 * 0 ≤ L_i < R_i ≤ M + 1(1 ≤ i ≤ N) * All input values are integers. Input Input is given from Standard Input in the following format: N M L_1 R_1 : L_N R_N Output Print the minimum required number of additional chairs. Examples Input 4 4 0 3 2 3 1 3 3 4 Output 0 Input 7 6 0 7 1 5 3 6 2 7 1 6 2 6 3 7 Output 2 Input 3 1 1 2 1 2 1 2 Output 2 Input 6 6 1 6 1 6 1 5 1 5 2 6 2 6 Output 2 Submitted Solution: ``` from operator import add class LazySegmentTree(): def __init__(self,n,init,merge=min,merge_unit=10**18,operate=add,operate_unit=0): self.merge=merge self.merge_unit=merge_unit self.operate=operate self.operate_unit=operate_unit self.n=(n-1).bit_length() self.data=[0 for i in range(1<<(self.n+1))] self.lazy=[0 for i in range(1<<(self.n+1))] for i in range(n): self.data[2**self.n+i]=init[i] for i in range(2**self.n-1,0,-1): self.data[i]=self.merge(self.data[2*i],self.data[2*i+1]) def propagate_above(self,i): m=i.bit_length()-1 for bit in range(m,0,-1): v=i>>bit add=self.lazy[v] self.lazy[v]=0 self.data[2*v]=self.operate(self.data[2*v],add) self.data[2*v+1]=self.operate(self.data[2*v+1],add) self.lazy[2*v]=self.operate(self.lazy[2*v],add) self.lazy[2*v+1]=self.operate(self.lazy[2*v+1],add) def remerge_above(self,i): while i: i>>=1 self.data[i]=self.operate(self.merge(self.data[2*i],self.data[2*i+1]),self.lazy[i]) def update(self,l,r,x): l+=1<<self.n r+=1<<self.n l0=l//(l&-l) r0=r//(r&-r)-1 while l<r: if l&1: self.data[l]=self.operate(self.data[l],x) self.lazy[l]=self.operate(self.lazy[l],x) l+=1 if r&1: self.data[r-1]=self.operate(self.data[r-1],x) self.lazy[r-1]=self.operate(self.lazy[r-1],x) l>>=1 r>>=1 self.remerge_above(l0) self.remerge_above(r0) def query(self,l,r): l+=1<<self.n r+=1<<self.n l0=l//(l&-l) r0=r//(r&-r)-1 self.propagate_above(l0) self.propagate_above(r0) res=self.merge_unit while l<r: if l&1: res=self.merge(res,self.data[l]) l+=1 if r&1: res=self.merge(res,self.data[r-1]) l>>=1 r>>=1 return res import sys input=sys.stdin.buffer.readline ide_ele=10**18 N,M=map(int,input().split()) init=[0 for i in range(M+2)] for i in range(1,M+1): init[0]+=1 init[i+1]-=1 for i in range(1,M+1): init[i]+=init[i-1] init[-1]=0 LST=LazySegmentTree(M+2,init) hito=[tuple(map(int,input().split())) for i in range(N)] hito.sort() add=M-N for l,r in hito: LST.update(0,r+1,-1) m=LST.query(l+1,M+2)+l add=min(m,add) print(max(-add,0)) ```
instruction
0
46,051
14
92,102
Yes
output
1
46,051
14
92,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i. N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than L_i, or not less than R_i. Naturally, only one person can sit in the same chair. It may not be possible for all of them to sit in their favorite chairs, if nothing is done. Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions. Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs. Constraints * 1 ≤ N,M ≤ 2 × 10^5 * 0 ≤ L_i < R_i ≤ M + 1(1 ≤ i ≤ N) * All input values are integers. Input Input is given from Standard Input in the following format: N M L_1 R_1 : L_N R_N Output Print the minimum required number of additional chairs. Examples Input 4 4 0 3 2 3 1 3 3 4 Output 0 Input 7 6 0 7 1 5 3 6 2 7 1 6 2 6 3 7 Output 2 Input 3 1 1 2 1 2 1 2 Output 2 Input 6 6 1 6 1 6 1 5 1 5 2 6 2 6 Output 2 Submitted Solution: ``` # https://tjkendev.github.io/procon-library/python/max_flow/dinic.html # Dinic's algorithm from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow # coding: utf-8 # Your code here! import sys read = sys.stdin.read readline = sys.stdin.readline n,m,*lr = map(int,read().split()) mp = iter(lr) D = Dinic(n+3*m+8) s = n+3*m+8-2 g = n+3*m+8-1 for i in range(n+m+2,n+2*m+3): D.add_edge(i, i-m-2, 1) D.add_edge(i, i+1, 1) D.add_edge(n+2*m+3, n+m+1, 1) for i in range(n+2*m+4,n+3*m+5): D.add_edge(i,i-2*m-4, 1) D.add_edge(i+1, i, 1) D.add_edge(n+3*m+5, n+m+1, 1) for i in range(n): D.add_edge(s,i,1) for i in range(n+1,n+m+1): D.add_edge(i,g,1) for i,(l,r) in enumerate(zip(mp,mp)): D.add_edge(i, n+2*m+4+l, 1) D.add_edge(i, n+m+2+r, 1) ans = D.flow(s, g) print(n-ans) ```
instruction
0
46,055
14
92,110
No
output
1
46,055
14
92,111
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i. N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than L_i, or not less than R_i. Naturally, only one person can sit in the same chair. It may not be possible for all of them to sit in their favorite chairs, if nothing is done. Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions. Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs. Constraints * 1 ≤ N,M ≤ 2 × 10^5 * 0 ≤ L_i < R_i ≤ M + 1(1 ≤ i ≤ N) * All input values are integers. Input Input is given from Standard Input in the following format: N M L_1 R_1 : L_N R_N Output Print the minimum required number of additional chairs. Examples Input 4 4 0 3 2 3 1 3 3 4 Output 0 Input 7 6 0 7 1 5 3 6 2 7 1 6 2 6 3 7 Output 2 Input 3 1 1 2 1 2 1 2 Output 2 Input 6 6 1 6 1 6 1 5 1 5 2 6 2 6 Output 2 Submitted Solution: ``` import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") 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)] # 区間加算、区間最大 class LazySeg: def __init__(self, aa): inf = 10 ** 16 n = 1 << (len(aa) - 1).bit_length() tree = [-inf] * (2 * n - 1) lazy = [0] * (2 * n - 1) tree[n - 1:n - 1 + len(aa)] = aa for u in range(n - 2, -1, -1): tree[u] = max(tree[u * 2 + 1], tree[u * 2 + 2]) self.tree = tree self.lazy = lazy self.n = n self.inf = inf def prop(self, u, l, r): self.tree[u] += self.lazy[u] if l + 1 < r: self.lazy[u * 2 + 1] += self.lazy[u] self.lazy[u * 2 + 2] += self.lazy[u] self.lazy[u] = 0 def add(self, l, r, a, u=0, tl=0, tr=-1): if tr == -1: tr = self.n if r <= tl or tr <= l: return if l <= tl and tr <= r: self.lazy[u] += a self.prop(u, tl, tr) return tm = (tl + tr) // 2 self.add(l, r, a, u * 2 + 1, tl, tm) self.add(l, r, a, u * 2 + 2, tm, tr) self.tree[u] = max(self.tree[u * 2 + 1], self.tree[u * 2 + 2]) def max(self, l, r, u=0, tl=0, tr=-1): res = -self.inf if tr == -1: tr = self.n if r <= tl or tr <= l: return res self.prop(u, tl, tr) if l <= tl and tr <= r: return self.tree[u] tm = (tl + tr) // 2 ret = self.max(l, r, u * 2 + 1, tl, tm) if ret > res: res = ret ret = self.max(l, r, u * 2 + 2, tm, tr) if ret > res: res = ret return res def main(): n,m=MI() lr=[LI() for _ in range(n)] lr.sort() #print(lr) aa=list(range(-m,1)) st=LazySeg(aa) ans=n-m for i,[l,r] in enumerate(lr): st.add(0,r,1) if i+1<n and lr[i+1][0]==l:continue #print(st.max(1,m+2),l) over=st.max(0,m+1)-l if over>ans:ans=over print(ans) main() ```
instruction
0
46,056
14
92,112
No
output
1
46,056
14
92,113
Provide a correct Python 3 solution for this coding contest problem. I have a lot of friends. Every friend is very small. I often go out with my friends. Put some friends in your backpack and go out together. Every morning I decide which friends to go out with that day. Put friends one by one in an empty backpack. I'm not very strong. Therefore, there is a limit to the weight of friends that can be carried at the same time. I keep friends so that I don't exceed the weight limit. The order in which you put them in depends on your mood. I won't stop as long as I still have friends to put in. I will never stop. …… By the way, how many patterns are there in total for the combination of friends in the backpack? Input N W w1 w2 .. .. .. wn On the first line of input, the integer N (1 ≤ N ≤ 200) and the integer W (1 ≤ W ≤ 10,000) are written in this order, separated by blanks. The integer N represents the number of friends, and the integer W represents the limit of the weight that can be carried at the same time. It cannot be carried if the total weight is greater than W. The next N lines contain an integer that represents the weight of the friend. The integer wi (1 ≤ wi ≤ 10,000) represents the weight of the i-th friend. Output How many possible combinations of friends are finally in the backpack? Divide the total number by 1,000,000,007 and output the remainder. Note that 1,000,000,007 are prime numbers. Note that "no one is in the backpack" is counted as one. Examples Input 4 8 1 2 7 9 Output 2 Input 4 25 20 15 20 15 Output 4 Input 6 37 5 9 13 18 26 33 Output 6
instruction
0
46,130
14
92,260
"Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**13 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] def f(n,w): a = sorted([I() for _ in range(n)], reverse=True) dp = [0] * (w+1) dp[0] = 1 sm = sum(a) if sm <= w: return 1 r = 0 for c in a: sm -= c if w >= sm: r += sum(dp[max(0,w-sm-c+1):w-sm+1]) r %= mod for j in range(w-c,-1,-1): dp[j+c] += dp[j] dp[j+c] %= mod return r % mod while 1: n,m = LI() if n == 0: break rr.append(f(n,m)) break return '\n'.join(map(str,rr)) print(main()) ```
output
1
46,130
14
92,261
Provide a correct Python 3 solution for this coding contest problem. I have a lot of friends. Every friend is very small. I often go out with my friends. Put some friends in your backpack and go out together. Every morning I decide which friends to go out with that day. Put friends one by one in an empty backpack. I'm not very strong. Therefore, there is a limit to the weight of friends that can be carried at the same time. I keep friends so that I don't exceed the weight limit. The order in which you put them in depends on your mood. I won't stop as long as I still have friends to put in. I will never stop. …… By the way, how many patterns are there in total for the combination of friends in the backpack? Input N W w1 w2 .. .. .. wn On the first line of input, the integer N (1 ≤ N ≤ 200) and the integer W (1 ≤ W ≤ 10,000) are written in this order, separated by blanks. The integer N represents the number of friends, and the integer W represents the limit of the weight that can be carried at the same time. It cannot be carried if the total weight is greater than W. The next N lines contain an integer that represents the weight of the friend. The integer wi (1 ≤ wi ≤ 10,000) represents the weight of the i-th friend. Output How many possible combinations of friends are finally in the backpack? Divide the total number by 1,000,000,007 and output the remainder. Note that 1,000,000,007 are prime numbers. Note that "no one is in the backpack" is counted as one. Examples Input 4 8 1 2 7 9 Output 2 Input 4 25 20 15 20 15 Output 4 Input 6 37 5 9 13 18 26 33 Output 6
instruction
0
46,131
14
92,262
"Correct Solution: ``` N, W = map(int, input().split()) w = [] for i in range(N): w.append(int(input())) w.sort() summ = 0 ans = 0 for num, wi in enumerate(w): if num == N - 1: if sum(w) - w[-1] <= W and W - sum(w) - w[-1] < w[-1]: ans += 1 break dp = [[0] * (W + 1) for i in range(N - num)] dp[0][0] = 1 for n in range(N - num - 1): dpn1 = dp[n + 1] dpn = dp[n] for k in range(W + 1): if k - w[num + n + 1] >= 0: dpn1[k] = dpn[k] + dpn[k - w[num + n + 1]] else: dpn1[k] = dpn[k] for i in range(max(W - summ - wi + 1,0), W - summ + 1): ans += dp[-1][i] ans %= 10 ** 9 + 7 summ += wi if W < summ: break print(ans%(10**9+7)) ```
output
1
46,131
14
92,263
Provide a correct Python 3 solution for this coding contest problem. I have a lot of friends. Every friend is very small. I often go out with my friends. Put some friends in your backpack and go out together. Every morning I decide which friends to go out with that day. Put friends one by one in an empty backpack. I'm not very strong. Therefore, there is a limit to the weight of friends that can be carried at the same time. I keep friends so that I don't exceed the weight limit. The order in which you put them in depends on your mood. I won't stop as long as I still have friends to put in. I will never stop. …… By the way, how many patterns are there in total for the combination of friends in the backpack? Input N W w1 w2 .. .. .. wn On the first line of input, the integer N (1 ≤ N ≤ 200) and the integer W (1 ≤ W ≤ 10,000) are written in this order, separated by blanks. The integer N represents the number of friends, and the integer W represents the limit of the weight that can be carried at the same time. It cannot be carried if the total weight is greater than W. The next N lines contain an integer that represents the weight of the friend. The integer wi (1 ≤ wi ≤ 10,000) represents the weight of the i-th friend. Output How many possible combinations of friends are finally in the backpack? Divide the total number by 1,000,000,007 and output the remainder. Note that 1,000,000,007 are prime numbers. Note that "no one is in the backpack" is counted as one. Examples Input 4 8 1 2 7 9 Output 2 Input 4 25 20 15 20 15 Output 4 Input 6 37 5 9 13 18 26 33 Output 6
instruction
0
46,132
14
92,264
"Correct Solution: ``` N, W = map(int, input().split()) MOD = 10**9 + 7 ws = [int(input()) for i in range(N)] ws.sort(reverse=1) su = sum(ws) dp = [0]*(W+1) dp[0] = 1 ans = 0 if su <= W: ans += 1 for i in range(N): w = ws[i] su -= w ans += sum(dp[max(W+1-w-su, 0):max(W+1-su, 0)]) % MOD for j in range(W, w-1, -1): dp[j] += dp[j-w] dp[j] %= MOD print(ans % MOD) ```
output
1
46,132
14
92,265
Provide tags and a correct Python 3 solution for this coding contest problem. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2.
instruction
0
46,350
14
92,700
Tags: data structures, dp, math, probabilities Correct Solution: ``` # by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase def main(): mod = 998244353 div = pow(100,mod-2,mod) n = int(input()) p = list(map(lambda xx:(int(xx)*div)%mod,input().split())) p1 = list(map(lambda xx:1-xx+mod if xx > 1 else 1-xx,p)) X = 1 for i in range(n): X = (X*p[i])%mod an,pr = 0,1 for i in range(1,n+1): an = (an+i*pr*p1[i-1])%mod pr = (pr*p[i-1])%mod an = (an+n*X)%mod an = (an*pow(X,mod-2,mod))%mod print(an) # Fast IO Region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ```
output
1
46,350
14
92,701
Provide tags and a correct Python 3 solution for this coding contest problem. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2.
instruction
0
46,351
14
92,702
Tags: data structures, dp, math, probabilities Correct Solution: ``` n = int(input()) exp = 0 M = 998244353 for pi in map(int, input().split()): exp = (exp+1)*100*pow(pi, M-2, M) %M print(int(exp%M)) ```
output
1
46,351
14
92,703
Provide tags and a correct Python 3 solution for this coding contest problem. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2.
instruction
0
46,352
14
92,704
Tags: data structures, dp, math, probabilities Correct Solution: ``` prime=998244353 def power(x, y, prime): b = bin(y)[2:] start = x answer = 1 for i in range(len(b)): b2 = b[len(b)-1-i] if b2=='1': answer = (answer*start) % prime start = (start*start) % prime return answer def gcd(a, b): if a > b: a, b = b, a if b % a==0: return a return gcd(b % a, a) def add(f1, f2): p1, q1 = f1 p2, q2 = f2 num = p1*q2+p2*q1 den = q1*q2 g = gcd(num, den) return [num//g, den//g] def mult(f1, f2): p1, q1 = f1 p2, q2 = f2 num = p1*p2 den = q1*q2 g = gcd(num, den) return [num//g, den//g] def process(A): num = 100 den = 1 for p in A[:-1]: den = p*den num = 100*(num+den) num, den = num % prime, den % prime den = (den*A[-1]) % prime # g = gcd(num, den) # num, den = num//g, den//g # print(num, den) P, Q = num % prime, den % prime Qinv = power(Q, prime-2, prime) answer = (P*Qinv) % prime return answer n = int(input()) A = [int(x) for x in input().split()] print(process(A)) ```
output
1
46,352
14
92,705
Provide tags and a correct Python 3 solution for this coding contest problem. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2.
instruction
0
46,353
14
92,706
Tags: data structures, dp, math, probabilities Correct Solution: ``` def solve(): s = input() if 'aa' in s or 'bb' in s or 'cc' in s: print(-1) return syms = s + '@' ans = ['@'] for i, sym in enumerate(s): if sym != '?': ans.append(sym) continue for x in 'abc': if x != ans[-1] and x != syms[i + 1]: ans.append(x) break print(''.join(ans[1:])) def solveb(): n = int(input()) perm = [int(x) for x in input().split()] num___idx = [-1 for _ in range(n + 1)] for i, num in enumerate(perm): num___idx[num] = i curr_max = -1 curr_min = 2 * n num___pretty = [0 for _ in range(n + 1)] for num in range(1, n + 1): curr_max = max(num___idx[num], curr_max) curr_min = min(num___idx[num], curr_min) if curr_max - curr_min + 1 == num: num___pretty[num] = 1 print(*num___pretty[1:], sep='') def solvec(): n = int(input()) rank___problems_nr = [int(x) for x in input().split()] weird_prefsums = [0] last_num = rank___problems_nr[0] for num in rank___problems_nr: if num != last_num: last_num = num weird_prefsums.append(weird_prefsums[-1]) weird_prefsums[-1] += 1 gold = weird_prefsums[0] silvers = 0 i = 1 for i in range(1, len(weird_prefsums)): x = weird_prefsums[i] if x - gold > gold: silvers = x - gold break bronzes = 0 for j in range(i, len(weird_prefsums)): x = weird_prefsums[j] if x > n / 2: break if x - gold - silvers > gold: bronzes = x - gold - silvers if bronzes == 0 or silvers == 0: print(0, 0, 0) return print(gold, silvers, bronzes) def solved(): a, b, c, d = (int(x) for x in input().split()) ab_len = min(a, b) a -= ab_len b -= ab_len cd_len = min(c, d) c -= cd_len d -= cd_len if a == 1 and cd_len == 0 and d == 0 and c == 0: print('YES') print('0 1 ' * ab_len + '0') return if d == 1 and ab_len == 0 and a == 0 and b == 0: print('YES') print('3 ' + '2 3 ' * cd_len) return if a > 0 or d > 0: print('NO') return cb_len = min(b, c) b -= cb_len c -= cb_len if b > 1 or c > 1: print('NO') return print('YES') print('1 ' * b + '0 1 ' * ab_len + '2 1 ' * cb_len + '2 3 ' * cd_len + '2' * c) def get_me(prob, mod): # return 100, prob return (100 * pow(prob, mod - 2, mod)) % mod def solvee(): n = int(input()) mod = 998244353 idx___prob = [int(x) for x in input().split()] curr_me = get_me(idx___prob[0], mod) for prob in idx___prob[1:]: me = get_me(prob, mod) curr_me *= me curr_me %= mod curr_me += me curr_me %= mod # curr_q_me = pow(curr_q_me, mod - 2, mod) print(curr_me) if __name__ == '__main__': solvee() ```
output
1
46,353
14
92,707
Provide tags and a correct Python 3 solution for this coding contest problem. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2.
instruction
0
46,354
14
92,708
Tags: data structures, dp, math, probabilities Correct Solution: ``` n = int(input()) p = list(map(int, input().split())) ans = 0 size = 998244353 for p in p: ans = (ans+1) % size ans = (((ans*100) % size)*pow(p, size-2, size)) % size print(ans) ```
output
1
46,354
14
92,709
Provide tags and a correct Python 3 solution for this coding contest problem. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2.
instruction
0
46,355
14
92,710
Tags: data structures, dp, math, probabilities Correct Solution: ``` # import random import time import sys # random.seed(a=3742967) # f = open('test.txt', 'w+') # f.write("200000\n") # for i in range(0, 200000): # f.write(f'{random.randint(1, 99)} ') # f.write('\n') # f.seek(0) f = sys.stdin t1 = time.time() n = int(f.readline()) MOD = 998244353 prob = list(map(int, f.readline().split())) num = pow(100, n, MOD) new_term = num inv_100 = pow(100, MOD-2, MOD) for x in prob[:-1]: new_term = (new_term*x*inv_100) % MOD num = (num + new_term) % MOD t4 = time.time() den = new_term*prob[-1]*inv_100 inverse = pow(den, MOD - 2, MOD) print((num*inverse) % MOD) # print("--- %s seconds ---" % (time.time() - t1)) ```
output
1
46,355
14
92,711
Provide tags and a correct Python 3 solution for this coding contest problem. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2.
instruction
0
46,356
14
92,712
Tags: data structures, dp, math, probabilities Correct Solution: ``` mod=998244353 n=int(input()) ar=list(map(int,input().split())) m,mul=1,[1] div=pow(100,mod-2,mod) for i in ar: mul.append((mul[-1]*i*div)%mod) num=0 den=pow(mul[n],mod-2,mod) for i in mul: num+=i num%=mod print((num*den-1)%mod) ```
output
1
46,356
14
92,713
Provide tags and a correct Python 3 solution for this coding contest problem. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2.
instruction
0
46,357
14
92,714
Tags: data structures, dp, math, probabilities Correct Solution: ``` input() p=list(map(int,input().split())) mod=998244353 f=0 for i in p: f=100*(f+1)*pow(i,mod-2,mod)%mod print(f) ```
output
1
46,357
14
92,715
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2. Submitted Solution: ``` n = int(input()) a = list(map(int,input().split())) mod = 998244353 rng = 1100 inv = [0,1] for i in range(2,rng): inv.append(pow(i,mod-2,mod)) acc = [0]*n acc[-1] = 100*inv[a[-1]] for i in range(n-1)[::-1]: acc[i] = acc[i+1]*100*inv[a[i]]%mod print(sum(acc)%mod) ```
instruction
0
46,358
14
92,716
Yes
output
1
46,358
14
92,717
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2. Submitted Solution: ``` mod = 998244353 def inv_mod(n): return pow(n, mod - 2, mod) n = int(input()) p = [int(x) for x in input().split()] res = 0 for i in p: res = (res + 1) * 100 * inv_mod(i) % mod print(res) ```
instruction
0
46,359
14
92,718
Yes
output
1
46,359
14
92,719
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2. Submitted Solution: ``` M = 998244353 n=input() p=list(map(int, input().split(' '))) num=0 denum=1 for i in p: num=(num+denum)*100%M denum=denum*i%M #print(num, denum) from math import gcd g=gcd(num,denum) num=num//g denum=denum//g denum=pow(denum,M-2,M) print(num*denum%M) ```
instruction
0
46,360
14
92,720
Yes
output
1
46,360
14
92,721
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2. Submitted Solution: ``` import sys MOD = 998244353 def powmod(a, b): if b == 0: return 1 res = powmod(a, b // 2) res *= res res %= MOD if b & 1: res *= a res %= MOD return res def inv(n): return powmod(n, MOD - 2) _100 = inv(100) n = int(input()) p = list(map(lambda x: int(x) * _100 % MOD, input().split())) if n == 1: print(inv(p[0])) sys.exit(0) pp = [0] * n pp[0] = p[0] for i in range(1, n): pp[i] = pp[i - 1] * p[i] % MOD k = 0 q = inv(pp[n - 2]) for i in range(0, n - 1): c = q if i: c *= pp[i - 1] c %= MOD k += c k %= MOD res = (k + 1) * inv(p[-1]) res %= MOD print(res) ```
instruction
0
46,361
14
92,722
Yes
output
1
46,361
14
92,723
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2. Submitted Solution: ``` import sys import math from collections import defaultdict from collections import deque from itertools import combinations from itertools import permutations input = lambda : sys.stdin.readline().rstrip() read = lambda : list(map(int, input().split())) def write(*args, sep="\n"): for i in args: sys.stdout.write("{}{}".format(i, sep)) INF = float('inf') MOD = int(1e9 + 7) YES = "YES" NO = "NO" n = int(input()) p = [0] + read() p = list(map(lambda x : x*pow(100, MOD-2, MOD)%MOD, p)) A = [0] * (n+1) for i in range(2, n+1): A[i] = (A[i-1] - 1) * pow(p[i-1], MOD-2, MOD) % MOD #print(p) #print(A) print((1 - A[n]) * pow(p[n], MOD-2, MOD) % MOD) ```
instruction
0
46,362
14
92,724
No
output
1
46,362
14
92,725
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2. Submitted Solution: ``` n = int(input()) exp = 0 M = 998244353 for pi in map(int, input().split()): exp = (100 / pi * exp + 100/pi) % M print(exp%M) ```
instruction
0
46,363
14
92,726
No
output
1
46,363
14
92,727
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2. Submitted Solution: ``` n = int(input()) exp = 0 M = 998244353 for pi in map(int, input().split()): exp = int((100 / pi * exp + 100/pi)) % M print(int(exp%M)) ```
instruction
0
46,364
14
92,728
No
output
1
46,364
14
92,729
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n. Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: * The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; * In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy. This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Input The first line contains one integer n (1≤ n≤ 2⋅ 10^5) — the number of mirrors. The second line contains n integers p_1, p_2, …, p_n (1 ≤ p_i ≤ 100). Output Print the answer modulo 998244353 in a single line. Examples Input 1 50 Output 2 Input 3 10 20 50 Output 112 Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2. Submitted Solution: ``` prime=998244353 def power(x, y, prime): b = bin(y)[2:] start = x answer = 1 for i in range(len(b)): b2 = b[len(b)-1-i] if b2=='1': answer = (answer*start) % prime start = (start*start) % prime return start def gcd(a, b): if a > b: a, b = b, a if b % a==0: return a return gcd(b % a, a) def add(f1, f2): p1, q1 = f1 p2, q2 = f2 num = p1*q2+p2*q1 den = q1*q2 g = gcd(num, den) return [num//g, den//g] def mult(f1, f2): p1, q1 = f1 p2, q2 = f2 num = p1*p2 den = q1*q2 g = gcd(num, den) return [num//g, den//g] def process(A): num = [1, 1] den = [1, 1] for p in A[:-1]: den = mult(den, [p, 100]) num = add(num, den) den = mult(den, [A[-1], 100]) p2, q2 = den P, Q = mult(num, [q2, p2]) Qinv = power(Q, prime-2, prime) answer = (P*Qinv) % prime return answer n = int(input()) A = [int(x) for x in input().split()] print(process(A)) ```
instruction
0
46,365
14
92,730
No
output
1
46,365
14
92,731
Provide tags and a correct Python 3 solution for this coding contest problem. It's a walking tour day in SIS.Winter, so t groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another. Initially, some students are angry. Let's describe a group of students by a string of capital letters "A" and "P": * "A" corresponds to an angry student * "P" corresponds to a patient student Such string describes the row from the last to the first student. Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index i in the string describing a group then they will throw a snowball at the student that corresponds to the character with index i+1 (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them. <image> Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry. Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group. Input The first line contains a single integer t — the number of groups of students (1 ≤ t ≤ 100). The following 2t lines contain descriptions of groups of students. The description of the group starts with an integer k_i (1 ≤ k_i ≤ 100) — the number of students in the group, followed by a string s_i, consisting of k_i letters "A" and "P", which describes the i-th group of students. Output For every group output single integer — the last moment a student becomes angry. Examples Input 1 4 PPAP Output 1 Input 3 12 APPAPPPAPPPP 3 AAP 3 PPA Output 4 1 0 Note In the first test, after 1 minute the state of students becomes PPAA. After that, no new angry students will appear. In the second tets, state of students in the first group is: * after 1 minute — AAPAAPPAAPPP * after 2 minutes — AAAAAAPAAAPP * after 3 minutes — AAAAAAAAAAAP * after 4 minutes all 12 students are angry In the second group after 1 minute, all students are angry.
instruction
0
46,366
14
92,732
Tags: greedy, implementation Correct Solution: ``` for i in range(int(input())): n = int(input()) a = list(input()) ans, flag, j, pointer = 0, 0, 0, 0 freq = [0]*len(a) for i in range(len(a)): if (a[i] == "A"): pointer = 1 if (a[i] == "P" and i != 0 and pointer == 1): if (flag == 0): freq[j] += 1 else: j += 1 flag = 1 else: if (flag == 1): pass else: j += 1 flag = 0 print(max(freq)) ```
output
1
46,366
14
92,733
Provide tags and a correct Python 3 solution for this coding contest problem. It's a walking tour day in SIS.Winter, so t groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another. Initially, some students are angry. Let's describe a group of students by a string of capital letters "A" and "P": * "A" corresponds to an angry student * "P" corresponds to a patient student Such string describes the row from the last to the first student. Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index i in the string describing a group then they will throw a snowball at the student that corresponds to the character with index i+1 (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them. <image> Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry. Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group. Input The first line contains a single integer t — the number of groups of students (1 ≤ t ≤ 100). The following 2t lines contain descriptions of groups of students. The description of the group starts with an integer k_i (1 ≤ k_i ≤ 100) — the number of students in the group, followed by a string s_i, consisting of k_i letters "A" and "P", which describes the i-th group of students. Output For every group output single integer — the last moment a student becomes angry. Examples Input 1 4 PPAP Output 1 Input 3 12 APPAPPPAPPPP 3 AAP 3 PPA Output 4 1 0 Note In the first test, after 1 minute the state of students becomes PPAA. After that, no new angry students will appear. In the second tets, state of students in the first group is: * after 1 minute — AAPAAPPAAPPP * after 2 minutes — AAAAAAPAAAPP * after 3 minutes — AAAAAAAAAAAP * after 4 minutes all 12 students are angry In the second group after 1 minute, all students are angry.
instruction
0
46,367
14
92,734
Tags: greedy, implementation Correct Solution: ``` I=input exec(int(I())*"I();print(max(0,0,*map(len,I().split('A')[1:])));") #JSR ```
output
1
46,367
14
92,735
Provide tags and a correct Python 3 solution for this coding contest problem. It's a walking tour day in SIS.Winter, so t groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another. Initially, some students are angry. Let's describe a group of students by a string of capital letters "A" and "P": * "A" corresponds to an angry student * "P" corresponds to a patient student Such string describes the row from the last to the first student. Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index i in the string describing a group then they will throw a snowball at the student that corresponds to the character with index i+1 (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them. <image> Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry. Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group. Input The first line contains a single integer t — the number of groups of students (1 ≤ t ≤ 100). The following 2t lines contain descriptions of groups of students. The description of the group starts with an integer k_i (1 ≤ k_i ≤ 100) — the number of students in the group, followed by a string s_i, consisting of k_i letters "A" and "P", which describes the i-th group of students. Output For every group output single integer — the last moment a student becomes angry. Examples Input 1 4 PPAP Output 1 Input 3 12 APPAPPPAPPPP 3 AAP 3 PPA Output 4 1 0 Note In the first test, after 1 minute the state of students becomes PPAA. After that, no new angry students will appear. In the second tets, state of students in the first group is: * after 1 minute — AAPAAPPAAPPP * after 2 minutes — AAAAAAPAAAPP * after 3 minutes — AAAAAAAAAAAP * after 4 minutes all 12 students are angry In the second group after 1 minute, all students are angry.
instruction
0
46,368
14
92,736
Tags: greedy, implementation Correct Solution: ``` for _ in range(int(input())): n=int(input()) a=list(input()) t=[] f=0 c=0 while(a!=t): t=a[:] c+=1 for i in range(n-1,0,-1): if(a[i-1]=='A'): a[i]='A' #print(i) print(c-1) ```
output
1
46,368
14
92,737
Provide tags and a correct Python 3 solution for this coding contest problem. It's a walking tour day in SIS.Winter, so t groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another. Initially, some students are angry. Let's describe a group of students by a string of capital letters "A" and "P": * "A" corresponds to an angry student * "P" corresponds to a patient student Such string describes the row from the last to the first student. Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index i in the string describing a group then they will throw a snowball at the student that corresponds to the character with index i+1 (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them. <image> Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry. Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group. Input The first line contains a single integer t — the number of groups of students (1 ≤ t ≤ 100). The following 2t lines contain descriptions of groups of students. The description of the group starts with an integer k_i (1 ≤ k_i ≤ 100) — the number of students in the group, followed by a string s_i, consisting of k_i letters "A" and "P", which describes the i-th group of students. Output For every group output single integer — the last moment a student becomes angry. Examples Input 1 4 PPAP Output 1 Input 3 12 APPAPPPAPPPP 3 AAP 3 PPA Output 4 1 0 Note In the first test, after 1 minute the state of students becomes PPAA. After that, no new angry students will appear. In the second tets, state of students in the first group is: * after 1 minute — AAPAAPPAAPPP * after 2 minutes — AAAAAAPAAAPP * after 3 minutes — AAAAAAAAAAAP * after 4 minutes all 12 students are angry In the second group after 1 minute, all students are angry.
instruction
0
46,369
14
92,738
Tags: greedy, implementation Correct Solution: ``` import math class Read: @staticmethod def string(): return input() @staticmethod def int(): return int(input()) @staticmethod def list(sep=' '): return input().split(sep) @staticmethod def list_int(sep=' '): return list(map(int, input().split(sep))) def solve(): k = Read.int() a = input() s = False count = 0 local_count = 0 for i in a: if i == 'A': s = True if local_count > count: count = local_count local_count = 0 elif s: local_count += 1 if s and local_count > count: count = local_count print(count) # query_count = 1 query_count = Read.int() while query_count: query_count -= 1 solve() ```
output
1
46,369
14
92,739
Provide tags and a correct Python 3 solution for this coding contest problem. It's a walking tour day in SIS.Winter, so t groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another. Initially, some students are angry. Let's describe a group of students by a string of capital letters "A" and "P": * "A" corresponds to an angry student * "P" corresponds to a patient student Such string describes the row from the last to the first student. Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index i in the string describing a group then they will throw a snowball at the student that corresponds to the character with index i+1 (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them. <image> Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry. Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group. Input The first line contains a single integer t — the number of groups of students (1 ≤ t ≤ 100). The following 2t lines contain descriptions of groups of students. The description of the group starts with an integer k_i (1 ≤ k_i ≤ 100) — the number of students in the group, followed by a string s_i, consisting of k_i letters "A" and "P", which describes the i-th group of students. Output For every group output single integer — the last moment a student becomes angry. Examples Input 1 4 PPAP Output 1 Input 3 12 APPAPPPAPPPP 3 AAP 3 PPA Output 4 1 0 Note In the first test, after 1 minute the state of students becomes PPAA. After that, no new angry students will appear. In the second tets, state of students in the first group is: * after 1 minute — AAPAAPPAAPPP * after 2 minutes — AAAAAAPAAAPP * after 3 minutes — AAAAAAAAAAAP * after 4 minutes all 12 students are angry In the second group after 1 minute, all students are angry.
instruction
0
46,370
14
92,740
Tags: greedy, implementation Correct Solution: ``` T = int(input()) for test in range(T): n = int(input()) s = input() ans = 0 tans = 0 if 'A' not in s: print(0) continue j = s.index('A') for i in range(j, n): if s[i] == 'P': tans += 1 else: ans = max(tans, ans) tans = 0 print(max(tans, ans)) ```
output
1
46,370
14
92,741