text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. AtCoder's head office consists of N rooms numbered 1 to N. For any two rooms, there is a direct passage connecting these rooms. For security reasons, Takahashi the president asked you to set a level for every passage, which is a positive integer and must satisfy the following condition: * For each room i\ (1 \leq i \leq N), if we leave Room i, pass through some passages whose levels are all equal and get back to Room i, the number of times we pass through a passage is always even. Your task is to set levels to the passages so that the highest level of a passage is minimized. Constraints * N is an integer between 2 and 500 (inclusive). Input Input is given from Standard Input in the following format: N Output Print one way to set levels to the passages so that the objective is achieved, as follows: a_{1,2} a_{1,3} ... a_{1,N} a_{2,3} ... a_{2,N} . . . a_{N-1,N} Here a_{i,j} is the level of the passage connecting Room i and Room j. If there are multiple solutions, any of them will be accepted. Example Input 3 Output 1 2 1 Submitted Solution: ``` N=int(input()) ans=[] for i in range(N-1): tmp=[] for j in range(i+1,N): if i%2 != j%2: tmp.append(1) else: tmp.append((i+2)//2+1) ans.append(tmp) for i in range(N-1): print(*ans[i]) ``` No
95,900
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. AtCoder's head office consists of N rooms numbered 1 to N. For any two rooms, there is a direct passage connecting these rooms. For security reasons, Takahashi the president asked you to set a level for every passage, which is a positive integer and must satisfy the following condition: * For each room i\ (1 \leq i \leq N), if we leave Room i, pass through some passages whose levels are all equal and get back to Room i, the number of times we pass through a passage is always even. Your task is to set levels to the passages so that the highest level of a passage is minimized. Constraints * N is an integer between 2 and 500 (inclusive). Input Input is given from Standard Input in the following format: N Output Print one way to set levels to the passages so that the objective is achieved, as follows: a_{1,2} a_{1,3} ... a_{1,N} a_{2,3} ... a_{2,N} . . . a_{N-1,N} Here a_{i,j} is the level of the passage connecting Room i and Room j. If there are multiple solutions, any of them will be accepted. Example Input 3 Output 1 2 1 Submitted Solution: ``` import sys input = sys.stdin.readline N = int(input()) res = [[0] * x for x in range(N - 1, 0, -1)] for x in range(N - 1): for y in range(N - 1 - x): b = 0 for k in range(10): if y + 1 < pow(2, k): break b = k + 1 res[x][y] = b for r in res: print(*r) ``` No
95,901
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. AtCoder's head office consists of N rooms numbered 1 to N. For any two rooms, there is a direct passage connecting these rooms. For security reasons, Takahashi the president asked you to set a level for every passage, which is a positive integer and must satisfy the following condition: * For each room i\ (1 \leq i \leq N), if we leave Room i, pass through some passages whose levels are all equal and get back to Room i, the number of times we pass through a passage is always even. Your task is to set levels to the passages so that the highest level of a passage is minimized. Constraints * N is an integer between 2 and 500 (inclusive). Input Input is given from Standard Input in the following format: N Output Print one way to set levels to the passages so that the objective is achieved, as follows: a_{1,2} a_{1,3} ... a_{1,N} a_{2,3} ... a_{2,N} . . . a_{N-1,N} Here a_{i,j} is the level of the passage connecting Room i and Room j. If there are multiple solutions, any of them will be accepted. Example Input 3 Output 1 2 1 Submitted Solution: ``` def f_d(): n = int(input()) for i in range(n-1): print(" ".join([str(i+1)]*(n-i-1))) if __name__ == "__main__": f_d() ``` No
95,902
Provide a correct Python 3 solution for this coding contest problem. Find the number, modulo 998244353, of sequences of length N consisting of 0, 1 and 2 such that none of their contiguous subsequences totals to X. Constraints * 1 \leq N \leq 3000 * 1 \leq X \leq 2N * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number, modulo 998244353, of sequences that satisfy the condition. Examples Input 3 3 Output 14 Input 8 6 Output 1179 Input 10 1 Output 1024 Input 9 13 Output 18402 Input 314 159 Output 459765451 "Correct Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip() N, X = map(int, input().split()) P = 998244353 A = [[0] * (2*i+1) for i in range(N+1)] # A[i][j] is the number of permutations of 1's and 2's whose length is i and whose sum is j A[0][0] = 1 B = [0] * (N + 1) # B[i] is the number of permutations of 1's and 2's with length i none of whose consecutive subsequenses is equal to X for i in range(N): for j in range(i, 2*i+1): A[i+1][j+1] += A[i][j] A[i+1][j+2] += A[i][j] if A[i+1][j+1] >= P: A[i+1][j+1] -= P if A[i+1][j+2] >= P: A[i+1][j+2] -= P # Type 1: Sum is below X for i in range(N+1): for j in range(i, min(2*i+1, X)): B[i] += A[i][j] # Type 2: Sum is 2X or more if X % 2: for i in range(X, N+1): B[i] += 1 # Type 3: X <= Sum < 2X for i in range(1, X): a = X - 1 - 2 * i if a < 0: continue for j in range((a+1) // 2, a + 1): k = j + 2 * i if k > N: break B[k] += A[j][a] if B[k] >= P: B[k] -= P ans = 0 for i, b in enumerate(B): ans = (ans + b * A[-1][-1-i]) % P print(ans) ```
95,903
Provide a correct Python 3 solution for this coding contest problem. Find the number, modulo 998244353, of sequences of length N consisting of 0, 1 and 2 such that none of their contiguous subsequences totals to X. Constraints * 1 \leq N \leq 3000 * 1 \leq X \leq 2N * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number, modulo 998244353, of sequences that satisfy the condition. Examples Input 3 3 Output 14 Input 8 6 Output 1179 Input 10 1 Output 1024 Input 9 13 Output 18402 Input 314 159 Output 459765451 "Correct Solution: ``` """ Writer: SPD_9X2 https://atcoder.jp/contests/tenka1-2019/tasks/tenka1_2019_f Xが偶数か奇数かは重要な気がする 長さL(<=N)の1,2で構成された文字列の問題、に変換できる O(L)で求められばおk dpか? X == 1なら、0,2のみで構成されてればおk X == 2なら、1が2つ以上入っていたら不可能 0 & 1が1つまで X == 3なら、2を入れる場合→1は入れられないが、2を好きなだけ入れていい 入れない場合→1を2個まで入れられる X == 4なら、 →やはり偶奇が重要か? とどく場合は X-1 → X+1の移動は強制 すなわち 座標1に行ってはいけない 初手は2に行くしかない 同様にX+2には行けない X+3に行くしかない 3には行けないから4に行くしかない つまり2以外不可?? 無限に移動を続ける場合は少なくともX=奇数 2のみ以外無理 偶数の場合はX以前に奇数回1をはさむ必要あり a→a+1と移動すると、X+a-1から先は移動できなくなる よって2Xは絶対超えられない あとは数え上げにどうやって落とすか Lは固定でいいと思うんだよな O(L)で解ければ勝ち dp ? 総和で場合分け or Lで場合分け 総和で場合分けする場合、当然Lは異なるのでまとめる必要がある Lの長さの移動の寄与にはNCLを掛ける必要がある 移動するときにLは1増えるんだよな… nC(l+1) = nCl * (n-l)/(l+1) 移動処理を一気に行えばlは統一できるからおk? 総和SはX以上の場合 X+1+2kしかありえない S-X以前は2のみ進軍可能、以降は自由 対称性? 1と2の数を全探索? ならばO(N**2)でいける 1がA個,2がB個の時、最初と最後に2をいくつか置く必要がある →後は残りの位置から1を置く場所を選べばよい →解けたっぽい? 場合分け頑張ろう 合計がX以下の場合→完全に自由 合計が2X以上の場合→Xが奇数の場合のみ all2が可能。それ以外は死 X以上2X以下の場合 → S-Xが奇数である必要あり まず2を(S-X+1)こ消費する(前後に置く分) あとは自由に順列を作ってどうぞ """ def modfac(n, MOD): f = 1 factorials = [1] for m in range(1, n + 1): f *= m f %= MOD factorials.append(f) inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv return factorials, invs def modnCr(n,r,mod,fac,inv): #上で求めたfacとinvsを引数に入れるべし(上の関数で与えたnが計算できる最大のnになる) return fac[n] * inv[n-r] * inv[r] % mod N,X = map(int,input().split()) mod = 998244353 fac,inv = modfac(N+10,mod) ans = 0 for two in range(N+1): for one in range(N+1): if one + two > N: break dist = one + two*2 zero = modnCr(N,one+two,mod,fac,inv) now = 0 if dist < X: now = modnCr(one+two,one,mod,fac,inv) * zero elif dist == X: continue elif dist < 2*X: if (dist-X) % 2 == 0: continue dtwo = two - (dist-X+1) if dtwo >= 0: now = modnCr(one+dtwo,one,mod,fac,inv) * zero elif X % 2 == 1 and one == 0: now = zero #print (one,two,now) ans += now ans %= mod print (ans) ```
95,904
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Find the number, modulo 998244353, of sequences of length N consisting of 0, 1 and 2 such that none of their contiguous subsequences totals to X. Constraints * 1 \leq N \leq 3000 * 1 \leq X \leq 2N * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number, modulo 998244353, of sequences that satisfy the condition. Examples Input 3 3 Output 14 Input 8 6 Output 1179 Input 10 1 Output 1024 Input 9 13 Output 18402 Input 314 159 Output 459765451 Submitted Solution: ``` MOD = 998244353 MAX_K = 3000 def ceil(a: int, b: int)->int: '''a/b の切り上げを計算する。 ''' if (a // b) * b == a: return a//b return a//b + 1 # __fact[n] = n! __fact = [0] * (MAX_K+1) def init_comb(): # initialize __fact __fact[0] = 1 for n in range(MAX_K): __fact[n+1] = ((n+1) * __fact[n]) % MOD def mod_inv(n: int, mod: int)->int: b, u, v = mod, 1, 0 while b > 0: t = n // b n -= t * b u -= t * v n, b = b, n u, v = v, u return (u+mod) % mod def comb(n: int, r: int)->int: '''nCr を計算する ''' res = __fact[n] res = (res * mod_inv(__fact[n-r], MOD)) % MOD res = (res * mod_inv(__fact[r], MOD)) % MOD return res def banned_x(N: int, X: int)->int: # 数列の全体の和を固定して調べる。 # 数列の和の最大値は全て 2 でできている数列の 2*N res = 1 # 0 だけでできている分 for S in range(1, 2*N+1): tmp = res if S < X: # 1, 2 を合計で k 個利用するとする。 # 1 を p 個、2 を q 個とすると # p+q=K; p+2q=S; # より # p=2K-S; q=S-K # となる。 # このとき、0, 1, 2 の並べ方は # comb(N,p) * comb(N-p,q) # である。 # また、0<=p; 0<=q より S/2<=k<=S が成り立つ。 for k in range(ceil(S, 2), min(S, N)+1): p, q = 2*k - S, S-k res += comb(N, p) * comb(N-p, q) res %= MOD elif S == X: # nothing continue elif (S-X) & 1: # 2 を置かなければならない場所が [0, only2) # と (only2, N] only2 = (S-(X-1))//2 # 1 が含まれない場合 if 2*only2 >= X-1: if X & 1: # 2 の置き方は N 個の位置から S//2 個 # 選ぶ組み合わせ。残りは 0 を置く res += comb(N, S//2) res %= MOD # 1 が含まれる場合 else: # SS は 1,2 なんでも置いていいゾーンの幅 SS = X-1-2*only2 # k,p,q の変数の意味は S<X の時と同じ # ただし、2 は 2*only2 の領域にも置くので # 2 の個数は SS-k+2*only2 になり、 # (N 個の位置から k+2*only2 個の位置を 1,2 用に確保) # x (k 個の位置から p 個の 1 を置く位置を確保) for k in range(ceil(SS, 2), min(N-2*only2, SS)+1): p, q = 2*k-SS, SS-k res += comb(N, k+2*only2) * comb(k, p) res %= MOD # print('S={}, res={}'.format(S, res-tmp)) return res if __name__ == "__main__": N, X = map(int, input().split()) init_comb() ans = banned_x(N, X) print(ans) ``` No
95,905
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Find the number, modulo 998244353, of sequences of length N consisting of 0, 1 and 2 such that none of their contiguous subsequences totals to X. Constraints * 1 \leq N \leq 3000 * 1 \leq X \leq 2N * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number, modulo 998244353, of sequences that satisfy the condition. Examples Input 3 3 Output 14 Input 8 6 Output 1179 Input 10 1 Output 1024 Input 9 13 Output 18402 Input 314 159 Output 459765451 Submitted Solution: ``` MOD = 998244353 MAX_K = 3000 def ceil(a: int, b: int)->int: '''a/b の切り上げを計算する。 ''' if (a // b) * b == a: return a//b return a//b + 1 # __fact[n] = n! __fact = [0] * (MAX_K+1) # __inv[n] = (n! の mod MOD に関する逆数) __inv = [0] * (MAX_K+1) def init_comb(): # initialize __fact __fact[0] = 1 __inv[0] = 1 for n in range(MAX_K): __fact[n+1] = ((n+1) * __fact[n]) % MOD __inv[n+1] = mod_inv((n+1) * __fact[n], MOD) def mod_inv(n: int, mod: int)->int: b, u, v = mod, 1, 0 while b > 0: t = n // b n -= t * b u -= t * v n, b = b, n u, v = v, u return (u+mod) % mod def comb(n: int, r: int)->int: '''nCr を計算する ''' res = __fact[n] res = (res * __inv[n-r]) % MOD res = (res * __inv[r]) % MOD return res def banned_x(N: int, X: int)->int: # 数列の全体の和を固定して調べる。 # 数列の和の最大値は全て 2 でできている数列の 2*N res = 1 # 0 だけでできている分 for S in range(1, 2*N+1): if S < X: # 1, 2 を合計で k 個利用するとする。 # 1 を p 個、2 を q 個とすると # p+q=K; p+2q=S; # より # p=2K-S; q=S-K # となる。 # このとき、0, 1, 2 の並べ方は # comb(N,p) * comb(N-p,q) # である。 # また、0<=p; 0<=q より S/2<=k<=S が成り立つ。 for k in range(ceil(S, 2), min(S, N)+1): p, q = 2*k - S, S-k res += comb(N, p) * comb(N-p, q) res %= MOD elif S == X: # nothing continue elif (S-X) & 1: # 2 を置かなければならない場所が [0, only2) # と (only2, N] only2 = (S-(X-1))//2 # 1 が含まれない場合 if 2*only2 >= X-1: if X & 1: # 2 の置き方は N 個の位置から S//2 個 # 選ぶ組み合わせ。残りは 0 を置く res += comb(N, S//2) res %= MOD # 1 が含まれる場合 else: # SS は 1,2 なんでも置いていいゾーンの幅 SS = X-1-2*only2 # k,p,q の変数の意味は S<X の時と同じ # ただし、2 は 2*only2 の領域にも置くので # 2 の個数は SS-k+2*only2 になり、 # (N 個の位置から k+2*only2 個の位置を 1,2 用に確保) # x (k 個の位置から p 個の 1 を置く位置を確保) for k in range(ceil(SS, 2), min(N-2*only2, SS)+1): p, q = 2*k-SS, SS-k res += comb(N, k+2*only2) * comb(k, p) res %= MOD # print('S={}, res={}'.format(S, res-tmp)) return res if __name__ == "__main__": N, X = map(int, input().split()) init_comb() ans = banned_x(N, X) print(ans) ``` No
95,906
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Find the number, modulo 998244353, of sequences of length N consisting of 0, 1 and 2 such that none of their contiguous subsequences totals to X. Constraints * 1 \leq N \leq 3000 * 1 \leq X \leq 2N * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number, modulo 998244353, of sequences that satisfy the condition. Examples Input 3 3 Output 14 Input 8 6 Output 1179 Input 10 1 Output 1024 Input 9 13 Output 18402 Input 314 159 Output 459765451 Submitted Solution: ``` a ``` No
95,907
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Find the number, modulo 998244353, of sequences of length N consisting of 0, 1 and 2 such that none of their contiguous subsequences totals to X. Constraints * 1 \leq N \leq 3000 * 1 \leq X \leq 2N * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number, modulo 998244353, of sequences that satisfy the condition. Examples Input 3 3 Output 14 Input 8 6 Output 1179 Input 10 1 Output 1024 Input 9 13 Output 18402 Input 314 159 Output 459765451 Submitted Solution: ``` import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np """ ・累積和の到達地点集合ごとに見る ・type1:X未満で終わる ・type2:X+1以上2X未満 ・type3:2X以上、これはXが奇数の場合のみ """ MOD = 998244353 N,X = map(int,read().split()) def cumprod(arr,MOD): L = len(arr); Lsq = int(L**.5+1) arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq) for n in range(1,Lsq): arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD for n in range(1,Lsq): arr[n] *= arr[n-1,-1]; arr[n] %= MOD return arr.ravel()[:L] def make_fact(U,MOD): x = np.arange(U,dtype=np.int64); x[0] = 1 fact = cumprod(x,MOD) x = np.arange(U,0,-1,dtype=np.int64); x[0] = pow(int(fact[-1]),MOD-2,MOD) fact_inv = cumprod(x,MOD)[::-1] return fact,fact_inv U = N+100 fact,fact_inv = make_fact(U,MOD) combN = fact[N] * fact_inv[:N+1] % MOD * fact_inv[N::-1] % MOD def F1(N,X): # X未満で終わる場合 def mult(P,Q): # 多項式の積 dP = len(P) - 1 dQ = len(Q) - 1 if dP < dQ: dP,dQ = dQ,dP P,Q = Q,P R = np.zeros(dP+dQ+1,np.int64) for n in range(dQ+1): R[n:n+dP+1] += Q[n] * P % MOD R %= MOD return R[:X] def power(P,n): if n == 0: return np.array([1],dtype=np.int64) Q = power(P,n//2) Q = mult(Q,Q) return mult(P,Q) if n&1 else Q P = np.array([1,1,1],np.int64) Q = power(P,N) return Q.sum() % MOD def F2(N,X): U = N+100 fact,fact_inv = make_fact(U,MOD) combN = fact[N] * fact_inv[:N+1] % MOD * fact_inv[N::-1] % MOD x = np.zeros(N+1,np.int64) # X+1+2nで終わる場合 for n in range(X): m = (X-1) - (2+2*n) if m < 0: break # 2+2n -> X-1に含まれる2の回数ごとに two = np.arange(m//2+1,dtype=np.int64) one = m - 2*two coef = fact[one+two] * fact_inv[one] % MOD * fact_inv[two] % MOD rest = N-one-two-(2*n+2) ind = rest>=0 rest = rest[ind]; coef = coef[ind] x[rest] += coef x %= MOD return (x * combN % MOD).sum() % MOD def F3(N,X): U = N+100 fact,fact_inv = make_fact(U,MOD) combN = fact[N] * fact_inv[:N+1] % MOD * fact_inv[N::-1] % MOD # 2X以上の偶数。Xは奇数で、+2を連打している場合 if X%2 == 0: return 0 if X > N: return 0 n = N-X+1 return combN[:n].sum() % MOD answer = (F1(N,X) + F2(N,X) + F3(N,X)) % MOD print(answer) ``` No
95,908
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 "Correct Solution: ``` N, X = map(int, input().split()) a, p = [1], [1] for i in range(N): a.append(a[i] * 2 + 3) p.append(p[i] * 2 + 1) def paty(L, n): if L == 0: return 0 if n <= 0 else 1 elif n <= 1 + a[L - 1]: return paty(L-1, n-1) else: return p[L-1] + 1 + paty(L-1, n-2-a[L-1]) print(paty(N, X)) ```
95,909
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 "Correct Solution: ``` N,X = map(int,input().split()) P = [1] H = [1] for i in range(1,51): P.append(P[i-1]*2+1) H.append(H[i-1]*2+3) def r(n,x): if x<=n: return 0 if x==H[n]: return P[n] if x>=H[n-1]+2: return P[n-1]+1+r(n-1,x-H[n-1]-2) else: return r(n-1,x-1) print(r(N,X)) ```
95,910
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 "Correct Solution: ``` n,x=map(int,input().split()) def p(n,x): if x<=n: return 0 if n==0: return 1 i=0 while 2**(2+i)-3+n-i<=x: i+=1 if 2**(i+1)-2+n-i==x: return 2**i-1 else: return 2**i+p(i-1,x-(2**(i+1)+n-i-2)-1) print(p(n,x)) ```
95,911
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 "Correct Solution: ``` n, x = map(int, input().split()) S, P = [1], [1] for i in range(n): S.append(S[i]*2+3) P.append(P[i]*2+1) def f(n, x): if n == 0: return 0 if x <= 0 else 1 elif x <= 1+S[n-1]: return f(n-1, x-1) else: return P[n-1] + 1 + f(n-1, x-2-S[n-1]) print(f(n, x)) ```
95,912
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 "Correct Solution: ``` n,x=map(int,input().split()) p=[1] a=[1] for i in range(0,n): p.append(p[i]*2+1) a.append(a[i]*2+3) def f(n,x): if n==0 and x<=0: return 0 elif n==0: return 1 elif x<=1+a[n-1]: return f(n-1,x-1) else: return p[n-1]+f(n-1,x-a[n-1]-2)+1 print(f(n,x)) ```
95,913
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 "Correct Solution: ``` N, X = map(int, input().split()) a = [2**(i+2) - 3 for i in range(N+1)] # 層の数 p = [2**(i+1) - 1 for i in range(N+1)] # パティの数 def f(N, X): if N == 0: return 1 if X > 0 else 0 elif X <= 1 + a[N-1]: return f(N-1, X-1) else: return p[N-1] + 1 + f(N-1, X-2-a[N-1]) print(f(N, X)) ```
95,914
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 "Correct Solution: ``` n,x=map(int,input().split()) a,p=[1],[1] for i in range(n): a.append(a[i]*2+3) p.append(p[i]*2+1) def f(n,x): if n==0: return 0 if x<=0 else 1 elif x<=a[n-1]+1: return f(n-1,x-1) else: return p[n-1]+1+f(n-1,x-2-a[n-1]) print(f(n,x)) ```
95,915
Provide a correct Python 3 solution for this coding contest problem. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 "Correct Solution: ``` N, X = map(int, input().split()) def count(N, X): if N == 0: return 1 if X == 1: return 0 elif 1 < X < 2**(N+1) - 1: return count(N-1, X-1) elif X == 2**(N+1) - 1: return 2**N elif 2**(N+1) - 1 < X: return 2**N + count(N-1, X-2**(N+1)+1) print(count(N, X)) ```
95,916
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 Submitted Solution: ``` N, X = map(int, input().split()) a, p = [1], [1] for i in range(N): a.append(a[i] * 2 + 3) p.append(p[i] * 2 + 1) def f(N, X): # X <= 0 や X > a_N を許容し解説本文から簡略化 if N == 0: return 0 if X <= 0 else 1 elif X <= 1 + a[N-1]: return f(N-1, X-1) else: return p[N-1] + 1 + f(N-1, X-2-a[N-1]) print(f(N, X)) ``` Yes
95,917
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 Submitted Solution: ``` def f(N, X): m = 2 ** (N + 1) - 1 if X <= N: return 0 elif N == 0: return 1 elif X < m: return f(N-1, X-1) else: return 2 ** N + f(N-1, X-m) N, X = map(int, input().split()) print(f(N, X)) ``` Yes
95,918
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 Submitted Solution: ``` def hamb(n,x): if x<=n:return 0 elif x==1 and n==0:return 1 elif x<=s[n-1]+1:return hamb(n-1,x-1) elif x==s[n]:return p[n] else:return hamb(n-1,x-2-s[n-1])+p[n-1]+1 n,x=map(int,input().split()) s=[0]*(n+1) p=[0]*(n+1) s[0]=1 p[0]=1 for i in range(n): s[i+1]=3+2*s[i] p[i+1]=1+2*p[i] print(hamb(n,x)) ``` Yes
95,919
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 Submitted Solution: ``` n,x=map(int, input().split()) b=[1]*51 p=[1]*51 for i in range(50): b[i+1]=2*b[i]+3 p[i+1]=2*p[i]+1 def eat(l,x): if x==0: return 0 if l==0: return 1 if x<b[l-1]+2: ans=eat(l-1,x-1) else: ans=p[l-1]+1+eat(l-1,x-2-b[l-1]) return ans print(eat(n,x)) ``` Yes
95,920
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 Submitted Solution: ``` N, X = map(int, input().split()) patty = [1] buns = [0] for i in range(0, N): patty.append(patty[i] * 2 + 1) buns.append(buns[i] * 2 + 2) # print(patty) # print(buns) burger_high = patty[N] + buns[N] def dfs(now_runrun, now_burger_high, dimension): if now_runrun == 1: return 0 elif now_runrun == now_burger_high // 2 + 1: return patty[dimension - 1] + 1 elif now_runrun == now_burger_high: return patty[dimension] elif now_runrun <= now_burger_high // 2: return dfs(now_runrun - 1, now_burger_high // 2, dimension - 1) else: return dfs(now_runrun - (patty[dimension - 1] + buns[dimension - 1]) - 2, now_burger_high // 2, dimension - 1) + patty[dimension - 1] + 1 print(dfs(X, burger_high, N)) ``` No
95,921
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 Submitted Solution: ``` # -*- coding: utf-8 -*- import sys import math N,X = list(map(int, input().rstrip().split())) #----- ## レベルnバーガーの、層の数 def material(n): return 2**(n+2)-3 ## レベルnバーガーの、Pattyの数 def patty(n): return 2**(n+1)-1 cnt=0 while N > 0: half = material(N) // 2 + 1 if X == half: cnt = patty(N-1) + 1 break elif X == 1: break elif X > half: cnt += patty(N-1) + 1 N -= 1 X -= half elif 1 < X < half: N -= 1 X -= 1 ## last if N == 0 and X == 1: cnt += 1 print(cnt) ``` No
95,922
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 Submitted Solution: ``` n,x=map(int,input().split()) l,p=[1],[1] for i in range(n): l+=[l[-1]*2+3] p+=[p[-1]*2+1] x-=1 a=0 while x>0: t=l[n]//2 if x>=t: a+=p[n-1]+1 x-=t x-=1 n-=1 print(a) ``` No
95,923
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In some other world, today is Christmas. Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing: * A level-0 burger is a patty. * A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom. For example, a level-1 burger and a level-2 burger look like `BPPPB` and `BBPPPBPBPPPBB` (rotated 90 degrees), where `B` and `P` stands for a bun and a patty. The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat? Constraints * 1 \leq N \leq 50 * 1 \leq X \leq ( the total number of layers in a level-N burger ) * N and X are integers. Input Input is given from Standard Input in the following format: N X Output Print the number of patties in the bottom-most X layers from the bottom of a level-N burger. Examples Input 2 7 Output 4 Input 1 1 Output 0 Input 50 4321098765432109 Output 2160549382716056 Submitted Solution: ``` n, x = map(int, input().split()) def pi(n): return 2**(n+1)-1 def ai(n): return 2**(n+2) - 3 def f(n, x): if N == 0: return 0 if X <= 0 else 1 elif (1 < x) and (x <= 1 + ai(n-1)): return f(n - 1, x - 1) elif x == 2 + ai(n - 1): return pi(n - 1) + 1 elif (2 + ai(n - 1) < x) and (x <= 2 + 2 * ai(n - 1)): return pi(n-1) + 1 + f(n-1, x-2-ai(n-1)) elif x >= ai(n): return pi(n) print(f(n, x)) ``` No
95,924
Provide a correct Python 3 solution for this coding contest problem. There is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex x_i and y_i. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter c_i. c_i = `W` represents the vertex is white; c_i = `B` represents the vertex is black. A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly: * Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex. * Invert the color of the vertex where she is currently. The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective? Constraints * 1 ≤ N ≤ 10^5 * 1 ≤ x_i,y_i ≤ N (1 ≤ i ≤ N-1) * The given graph is a tree. * c_i = `W` or c_i = `B`. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} c_1c_2..c_N Output Print the minimum number of seconds required to achieve the objective. Examples Input 5 1 2 2 3 2 4 4 5 WBBWW Output 5 Input 6 3 1 4 5 2 6 6 1 3 4 WWBWBB Output 7 Input 1 B Output 0 Input 20 2 19 5 13 6 4 15 6 12 19 13 19 3 11 8 3 3 20 16 13 7 14 3 17 7 8 10 20 11 9 8 18 8 2 10 1 6 13 WBWBWBBWWWBBWWBBBBBW Output 21 "Correct Solution: ``` """ https://atcoder.jp/contests/arc097/tasks/arc097_d 始点と終点関係ある? →逆にしても問題はない 黒い葉は完全に無視できる(落とせる) よって、葉はすべて白 葉以外は白黒どっちもあり得る すべての葉をめぐる最短経路? ある葉からスタートするのは自明っぽい? ある白からスタートするのはそう →自分を塗ってからdfsする →全方位木dp? 行きのみで帰らない場所が1つ存在するはず →始点と終点のパスとそこから生える木って感じのイメージ 足踏み(停止)の回数を極力少なくしたい →始点でも終点でもない場合、 e本の辺がつながってたらe回訪問は確定  e^colorが黒でない場合は+1 →つまり、各頂点で消費する回数は始点・終点でない限り常に一定? 終点はまず葉→始点も葉でおk 始点は足踏みなので1秒つかう 終点も1秒使う パス上の点は、e-1回訪問(ここが根本的に違う!) つまり、あるパスがあって その長さがL(両端除く)の時、最大でL減る ただし、パス上にあっても回数が減らない点もある B=0 W=1とするか (color^E) == 1 の点は、E-1回訪問になると足踏みがいらないから2減る つまり、そのような点が最大数あるパスを求めればいい """ import sys sys.setrecursionlimit(200000) from collections import deque MM = 0 def dfs(v,p): global MM if v != p and linknum[v] == 1: return 0 cl = [] for nex in lis[v]: if nex != p and exist[nex]: tmp = dfs(nex,v) cl.append(tmp) cl.sort() cl.reverse() if len(cl) == 1: MM = max(MM , cl[0]) else: if (linknum[v]+c[v])%2 == 1: MM = max(MM , cl[0] + cl[1] + 2) else: MM = max(MM , cl[0] + cl[1]) if (linknum[v]+c[v]) % 2 == 1: return cl[0]+ 2 else: return cl[0] N = int(input()) lis = [ [] for i in range(N) ] linknum = [0] * N for i in range(N-1): u,v = map(int,input().split()) u -= 1 v -= 1 lis[u].append(v) lis[v].append(u) linknum[u] += 1 linknum[v] += 1 ctmp = input() c = [] for i in ctmp: if i == "B": c.append(0) else: c.append(1) exist = [True] * N q = deque([]) for i in range(N): if linknum[i] <= 1 and c[i] == 0: q.append(i) while len(q) > 0: now = q.popleft() exist[now] = False linknum[now] = 0 for nex in lis[now]: linknum[nex] -= 1 if linknum[nex] == 1 and c[nex] == 0: q.append(nex) #print (exist) start = None for i in range(N): if exist[i]: start = i break else: print (0) sys.exit() if linknum[start] == 0: print (1) sys.exit() ans = 0 #print (linknum) for i in range(N): if exist[i]: if (linknum[i] + c[i]) % 2 == 0: ans += linknum[i] else: ans += linknum[i] + 1 MM = 0 pick = dfs(start,start) print (ans - MM) ```
95,925
Provide a correct Python 3 solution for this coding contest problem. There is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex x_i and y_i. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter c_i. c_i = `W` represents the vertex is white; c_i = `B` represents the vertex is black. A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly: * Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex. * Invert the color of the vertex where she is currently. The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective? Constraints * 1 ≤ N ≤ 10^5 * 1 ≤ x_i,y_i ≤ N (1 ≤ i ≤ N-1) * The given graph is a tree. * c_i = `W` or c_i = `B`. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} c_1c_2..c_N Output Print the minimum number of seconds required to achieve the objective. Examples Input 5 1 2 2 3 2 4 4 5 WBBWW Output 5 Input 6 3 1 4 5 2 6 6 1 3 4 WWBWBB Output 7 Input 1 B Output 0 Input 20 2 19 5 13 6 4 15 6 12 19 13 19 3 11 8 3 3 20 16 13 7 14 3 17 7 8 10 20 11 9 8 18 8 2 10 1 6 13 WBWBWBBWWWBBWWBBBBBW Output 21 "Correct Solution: ``` from collections import deque def first_cut(links, colors): tmp_links = links.copy() for v, neighbors in tmp_links.items(): while len(neighbors) == 1 and colors[v]: del links[v] par = neighbors.pop() links[par].remove(v) v = par neighbors = links[par] return links def diameter(links, flags): def dfs(s): fs = flags[s] d, v = 0, 0 q = deque(sorted((fs + flags[v], v, s) for v in links[s])) while q: d, v, a = q.popleft() for u in links[v]: if u == a: continue fu = flags[u] if fu: q.append((d + 1, u, v)) else: q.appendleft((d, u, v)) return d, v s = next(iter(links)) _, t = dfs(s) d, _ = dfs(t) return d def solve(links, colors): if all(colors): return 0 links = first_cut(links, colors) k = len(links) if k == 1: return 1 flags = {v: colors[v] ^ (len(link) % 2 == 0) for v, link in links.items()} euler_tour = 2 * (k - 1) + sum(flags.values()) return euler_tour - 2 * diameter(links, flags) n = int(input()) links = {i: set() for i in range(n)} for _ in range(n - 1): x, y = map(int, input().split()) x -= 1 y -= 1 links[x].add(y) links[y].add(x) colors = [c == 'B' for c in input()] print(solve(links, colors)) ```
95,926
Provide a correct Python 3 solution for this coding contest problem. There is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex x_i and y_i. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter c_i. c_i = `W` represents the vertex is white; c_i = `B` represents the vertex is black. A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly: * Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex. * Invert the color of the vertex where she is currently. The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective? Constraints * 1 ≤ N ≤ 10^5 * 1 ≤ x_i,y_i ≤ N (1 ≤ i ≤ N-1) * The given graph is a tree. * c_i = `W` or c_i = `B`. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} c_1c_2..c_N Output Print the minimum number of seconds required to achieve the objective. Examples Input 5 1 2 2 3 2 4 4 5 WBBWW Output 5 Input 6 3 1 4 5 2 6 6 1 3 4 WWBWBB Output 7 Input 1 B Output 0 Input 20 2 19 5 13 6 4 15 6 12 19 13 19 3 11 8 3 3 20 16 13 7 14 3 17 7 8 10 20 11 9 8 18 8 2 10 1 6 13 WBWBWBBWWWBBWWBBBBBW Output 21 "Correct Solution: ``` import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N = int(readline()) data = read().split() m = map(int,data[:-1]) XY = zip(m,m) C = [0] + [1 * (x == ord('W')) for x in data[-1]] graph = [set() for _ in range(N+1)] for x,y in XY: graph[x].add(y) graph[y].add(x) deg = [len(x) for x in graph] b_leaf = [i for i in range(N+1) if deg[i] == 1 and not C[i]] while b_leaf: x = b_leaf.pop() for y in graph[x]: graph[y].remove(x) deg[y] -= 1 if deg[y] == 1 and not C[y]: b_leaf.append(y) deg[x] = 0 graph[x].clear() V = set(i for i,E in enumerate(graph) if E) if len(V) <= 2: print(sum(C)) exit() root = None for v in V: if deg[v] > 1: root = v break parent = [0] * (N+1) order = [] stack = [root] while stack: x = stack.pop() order.append(x) for y in graph[x]: if y == parent[x]: continue parent[y] = x stack.append(y) """ ・Euler tourの状態から、パスをひとつ除く ・葉と葉を結ぶとしてよい。通ると2コスト安くなる点がある """ for i in range(N+1): C[i] ^= (1&deg[i]) cost_full = sum(deg) + sum(C) # C[i] == 1 となる i を最もたくさん含むパスを探す opt = 0 dp = [0] * (N+1) for v in order[::-1]: if deg[v] == 1: continue arr = sorted((dp[w] for w in graph[v] if w != parent[v]),reverse = True) + [0] x = arr[0] + arr[1] + C[v] if opt < x: opt = x dp[v] = arr[0] + C[v] answer = cost_full - 2 * opt print(answer) ```
95,927
Provide a correct Python 3 solution for this coding contest problem. There is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex x_i and y_i. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter c_i. c_i = `W` represents the vertex is white; c_i = `B` represents the vertex is black. A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly: * Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex. * Invert the color of the vertex where she is currently. The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective? Constraints * 1 ≤ N ≤ 10^5 * 1 ≤ x_i,y_i ≤ N (1 ≤ i ≤ N-1) * The given graph is a tree. * c_i = `W` or c_i = `B`. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} c_1c_2..c_N Output Print the minimum number of seconds required to achieve the objective. Examples Input 5 1 2 2 3 2 4 4 5 WBBWW Output 5 Input 6 3 1 4 5 2 6 6 1 3 4 WWBWBB Output 7 Input 1 B Output 0 Input 20 2 19 5 13 6 4 15 6 12 19 13 19 3 11 8 3 3 20 16 13 7 14 3 17 7 8 10 20 11 9 8 18 8 2 10 1 6 13 WBWBWBBWWWBBWWBBBBBW Output 21 "Correct Solution: ``` import sys from collections import deque input=sys.stdin.readline N=int(input()) edge=[[] for i in range(N)] for i in range(N-1): x,y=map(int,input().split()) edge[x-1].append(y-1) edge[y-1].append(x-1) c=input()[:N] deg=[len(edge[i]) for i in range(N)] leaf=set([]) for i in range(N): if deg[i]==1 and c[i]=="B": leaf.add(i) ban=set([]) while leaf: v=leaf.pop() ban.add(v) deg[v]=0 for nv in edge[v]: deg[nv]-=1 if deg[nv]==1 and c[nv]=="B": leaf.add(nv) for i in range(N): edge[i]=[nv for nv in edge[i] if nv not in ban] root=-1 for i in range(N): if i not in ban: root=i parent=[-2]*N deq=deque([(root,-1)]) node=[] while deq: v,pv=deq.popleft() parent[v]=pv node.append(v) for nv in edge[v]: if nv!=pv: deq.append((nv,v)) node=node[::-1] for i in range(N): edge[i]=[nv for nv in edge[i] if nv!=parent[i]] check=True for i in range(N): check&=(deg[i]<=0) if check: print(int(c[root]=="W")) exit() cond=[0]*N for v in range(N): if (deg[v]%2==1 and c[v]=="B") or (deg[v]%2==0 and c[v]=="W"): cond[v]+=1 else: cond[v]-=1 lower=[0]*N for v in node: res=0 for nv in edge[v]: res=max(res,lower[nv]) res+=1+cond[v] lower[v]=res upper=[0]*N node=node[::-1] for v in node: n=len(edge[v]) if n>1: left=[0]*n right=[0]*n for i in range(n-1): nv=edge[v][i] left[i]=max(left[i-1],lower[nv]+2+cond[v]) nv=edge[v][-1] upper[nv]=left[n-2]+cond[nv] right[n-1]=lower[nv]+2+cond[v] for i in range(n-2,0,-1): nv=edge[v][i] upper[nv]=max(left[i-1],right[i+1])+cond[nv] right[i]=max(right[i+1],lower[nv]+2+cond[v]) if edge[v][0]!=pv: nv=edge[v][0] upper[nv]=right[1]+cond[nv] if v!=root: for nv in edge[v]: upper[nv]=max(upper[nv],upper[v]+1+cond[nv]) base=sum(deg[i] for i in range(N))+sum(cond[i]==1 for i in range(N)) #print(deg) #print(base) #print(lower) #print(upper) #print(base) print(base-max(max(upper),max(lower))) ```
95,928
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex x_i and y_i. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter c_i. c_i = `W` represents the vertex is white; c_i = `B` represents the vertex is black. A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly: * Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex. * Invert the color of the vertex where she is currently. The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective? Constraints * 1 ≤ N ≤ 10^5 * 1 ≤ x_i,y_i ≤ N (1 ≤ i ≤ N-1) * The given graph is a tree. * c_i = `W` or c_i = `B`. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} c_1c_2..c_N Output Print the minimum number of seconds required to achieve the objective. Examples Input 5 1 2 2 3 2 4 4 5 WBBWW Output 5 Input 6 3 1 4 5 2 6 6 1 3 4 WWBWBB Output 7 Input 1 B Output 0 Input 20 2 19 5 13 6 4 15 6 12 19 13 19 3 11 8 3 3 20 16 13 7 14 3 17 7 8 10 20 11 9 8 18 8 2 10 1 6 13 WBWBWBBWWWBBWWBBBBBW Output 21 Submitted Solution: ``` import sys from collections import deque input=sys.stdin.readline N=int(input()) edge=[[] for i in range(N)] for i in range(N-1): x,y=map(int,input().split()) edge[x-1].append(y-1) edge[y-1].append(x-1) c=input()[:N] deg=[len(edge[i]) for i in range(N)] leaf=set([]) for i in range(N): if deg[i]==1 and c[i]=="B": leaf.add(i) ban=set([]) while leaf: v=leaf.pop() ban.add(v) deg[v]=0 for nv in edge[v]: deg[nv]-=1 if deg[nv]==1 and c[nv]=="B": leaf.add(nv) for i in range(N): edge[i]=[nv for nv in edge[i] if nv not in ban] root=-1 for i in range(N): if i not in ban: root=i parent=[-2]*N deq=deque([(root,-1)]) node=[] while deq: v,pv=deq.popleft() parent[v]=pv node.append(v) for nv in edge[v]: if nv!=pv: deq.append((nv,v)) node=node[::-1] for i in range(N): edge[i]=[nv for nv in edge[i] if nv!=parent[i]] check=True for i in range(N): check&=(deg[i]<=0) if check: print(int(c[root]=="W")) exit() cond=[0]*N for v in range(N): if (deg[v]%2==1 and c[v]=="B") or (deg[v]%2==0 and c[v]=="W"): cond[v]+=1 else: cond[v]-=1 lower=[0]*N for v in node: res=0 for nv in edge[v]: res=max(res,lower[nv]) res+=1+cond[v] lower[v]=res upper=[0]*N for v in node: n=len(edge[v]) if n>1: left=[0]*n right=[0]*n for i in range(n-1): nv=edge[v][i] left[i]=max(left[i-1],lower[nv]+2+cond[v]) nv=edge[v][-1] upper[nv]=left[n-2]+cond[nv] right[n-1]=lower[nv]+2+cond[v] for i in range(n-2,0,-1): nv=edge[v][i] upper[nv]=max(left[i-1],right[i+1])+cond[nv] right[i]=max(right[i+1],lower[nv]+2+cond[v]) if edge[v][0]!=pv: nv=edge[v][0] upper[nv]=right[1]+cond[nv] if v!=root: for nv in edge[v]: upper[nv]=max(upper[nv],upper[v]+1+cond[nv]) base=sum(deg[i] for i in range(N))+sum(cond[i]==1 for i in range(N)) #print(deg) #print(base) #print(lower) #print(upper) #print(base) print(base-max(max(upper),max(lower))) ``` No
95,929
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex x_i and y_i. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter c_i. c_i = `W` represents the vertex is white; c_i = `B` represents the vertex is black. A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly: * Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex. * Invert the color of the vertex where she is currently. The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective? Constraints * 1 ≤ N ≤ 10^5 * 1 ≤ x_i,y_i ≤ N (1 ≤ i ≤ N-1) * The given graph is a tree. * c_i = `W` or c_i = `B`. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} c_1c_2..c_N Output Print the minimum number of seconds required to achieve the objective. Examples Input 5 1 2 2 3 2 4 4 5 WBBWW Output 5 Input 6 3 1 4 5 2 6 6 1 3 4 WWBWBB Output 7 Input 1 B Output 0 Input 20 2 19 5 13 6 4 15 6 12 19 13 19 3 11 8 3 3 20 16 13 7 14 3 17 7 8 10 20 11 9 8 18 8 2 10 1 6 13 WBWBWBBWWWBBWWBBBBBW Output 21 Submitted Solution: ``` """ https://atcoder.jp/contests/arc097/tasks/arc097_d 始点と終点関係ある? →逆にしても問題はない 黒い葉は完全に無視できる(落とせる) よって、葉はすべて白 葉以外は白黒どっちもあり得る すべての葉をめぐる最短経路? ある葉からスタートするのは自明っぽい? ある白からスタートするのはそう →自分を塗ってからdfsする →全方位木dp? 行きのみで帰らない場所が1つ存在するはず →始点と終点のパスとそこから生える木って感じのイメージ 足踏み(停止)の回数を極力少なくしたい →始点でも終点でもない場合、 e本の辺がつながってたらe回訪問は確定  e^colorが黒でない場合は+1 →つまり、各頂点で消費する回数は始点・終点でない限り常に一定? 終点はまず葉→始点も葉でおk 始点は足踏みなので1秒つかう 終点も1秒使う パス上の点は、e-1回訪問(ここが根本的に違う!) つまり、あるパスがあって その長さがL(両端除く)の時、最大でL減る ただし、パス上にあっても回数が減らない点もある B=0 W=1とするか (color^E) == 1 の点は、E-1回訪問になると足踏みがいらないから2減る つまり、そのような点が最大数あるパスを求めればいい """ import sys sys.setrecursionlimit(200000) from collections import deque MM = 0 def dfs(v,p): global MM if v != p and linknum[v] == 1: return 0 cl = [] for nex in lis[v]: if nex != p and exist[nex]: tmp = dfs(nex,v) cl.append(tmp) cl.sort() cl.reverse() if len(cl) == 1: MM = max(MM , cl[0]) else: if (linknum[v]+c[v])%2 == 1: MM = max(MM , cl[0] + cl[1] + 2) else: MM = max(MM , cl[0] + cl[1]) if (linknum[v]+c[v]) % 2 == 1: return cl[0]+ 2 else: return cl[0] N = int(input()) lis = [ [] for i in range(N) ] linknum = [0] * N for i in range(N-1): u,v = map(int,input().split()) u -= 1 v -= 1 lis[u].append(v) lis[v].append(u) linknum[u] += 1 linknum[v] += 1 ctmp = input() c = [] for i in ctmp: if i == "B": c.append(0) else: c.append(1) exist = [True] * N q = deque([]) for i in range(N): if linknum[i] <= 1 and c[i] == 0: q.append(i) while len(q) > 0: now = q.popleft() exist[now] = False linknum[now] = 0 for nex in lis[now]: linknum[nex] -= 1 if linknum[nex] == 1 and c[nex] == 0: q.append(nex) #print (exist) start = None for i in range(N): if exist[i]: start = i break else: print (0) sys.exit() if linknum[start] == 0: print (1) sys.exit() ans = 0 #print (linknum) for i in range(N): if exist[i]: if (linknum[i] + c[i]) % 2 == 0: ans += linknum[i] else: ans += linknum[i] + 1 MM = 0 pick = dfs(start,start) print (ans - MM) ``` No
95,930
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex x_i and y_i. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter c_i. c_i = `W` represents the vertex is white; c_i = `B` represents the vertex is black. A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly: * Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex. * Invert the color of the vertex where she is currently. The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective? Constraints * 1 ≤ N ≤ 10^5 * 1 ≤ x_i,y_i ≤ N (1 ≤ i ≤ N-1) * The given graph is a tree. * c_i = `W` or c_i = `B`. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} c_1c_2..c_N Output Print the minimum number of seconds required to achieve the objective. Examples Input 5 1 2 2 3 2 4 4 5 WBBWW Output 5 Input 6 3 1 4 5 2 6 6 1 3 4 WWBWBB Output 7 Input 1 B Output 0 Input 20 2 19 5 13 6 4 15 6 12 19 13 19 3 11 8 3 3 20 16 13 7 14 3 17 7 8 10 20 11 9 8 18 8 2 10 1 6 13 WBWBWBBWWWBBWWBBBBBW Output 21 Submitted Solution: ``` from collections import deque def first_cut(links, colors): tmp_links = links.copy() for v, neighbors in tmp_links.items(): while len(neighbors) == 1 and colors[v]: del links[v] par = neighbors.pop() links[par].remove(v) v = par neighbors = links[par] return links def diameter(links): flags = {v: colors[v] ^ (len(link) % 2 == 0) for v, link in links.items()} s = next(iter(links)) fs = flags[s] v, d = 0, 0 q = deque(sorted((fs + flags[v], v, s) for v in links[s])) while q: d, v, a = q.popleft() for u in links[v]: if u == a: continue fu = flags[u] if fu: q.append((d + 1, u, v)) else: q.appendleft((d, u, v)) t = v ft = flags[t] q = deque(sorted((ft + flags[v], v, t) for v in links[s])) while q: d, v, a = q.popleft() for u in links[v]: if u == a: continue fu = flags[u] if fu: q.append((d + 1, u, v)) else: q.appendleft((d, u, v)) return d def solve(links, colors): if all(colors): return 0 if sum(colors) == len(colors) - 1: return 1 links = first_cut(links, colors) k = len(links) euler_tour = 2 * (k - 1) + sum(colors[v] ^ (len(link) % 2 == 0) for v, link in links.items()) return euler_tour - 2 * diameter(links) n = int(input()) links = {i: set() for i in range(n)} for _ in range(n - 1): x, y = map(int, input().split()) x -= 1 y -= 1 links[x].add(y) links[y].add(x) colors = [c == 'B' for c in input()] print(solve(links, colors)) ``` No
95,931
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex x_i and y_i. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter c_i. c_i = `W` represents the vertex is white; c_i = `B` represents the vertex is black. A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly: * Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex. * Invert the color of the vertex where she is currently. The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective? Constraints * 1 ≤ N ≤ 10^5 * 1 ≤ x_i,y_i ≤ N (1 ≤ i ≤ N-1) * The given graph is a tree. * c_i = `W` or c_i = `B`. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} c_1c_2..c_N Output Print the minimum number of seconds required to achieve the objective. Examples Input 5 1 2 2 3 2 4 4 5 WBBWW Output 5 Input 6 3 1 4 5 2 6 6 1 3 4 WWBWBB Output 7 Input 1 B Output 0 Input 20 2 19 5 13 6 4 15 6 12 19 13 19 3 11 8 3 3 20 16 13 7 14 3 17 7 8 10 20 11 9 8 18 8 2 10 1 6 13 WBWBWBBWWWBBWWBBBBBW Output 21 Submitted Solution: ``` input = __import__('sys').stdin.readline MIS = lambda: map(int,input().split()) n = int(input()) adj = [set() for i in range(n+1)] white = [False] for i in range(n-1): a, b = MIS() adj[a].add(b); adj[b].add(a) s = input().rstrip() for i in range(n): white.append(s[i] == 'W') # Corner cases if sum(white) <= 1: print(sum(white)); exit() # Remove black leaves stack = [i for i in range(1,n+1) if len(adj[i]) == 1 and not white[i]] while stack: p = stack.pop() pa = min(adj[p]) adj[pa].remove(p); adj[p].clear() if len(adj[pa]) == 1 and not white[pa]: stack.append(pa) # Simulate Euler tour root = 1 while not adj[root]: root+= 1 tadj = [[] for i in range(n+1)] vis = [False]*(n+1); vis[root] = True stack = [root] while stack: p = stack.pop() for q in adj[p]: if vis[q]: continue vis[q] = True; tadj[p].append(q) stack.append(q) for i in range(1, n+1): if adj[i] and len(tadj[i])%2 == 0: white[i] = not white[i] white[root] = not white[root] # Diameter def bfs(v): stack = [v] dist = [-1]*(n+1); dist[v] = white[v] while stack: p = stack.pop() for q in adj[p]: if dist[q] != -1: continue dist[q] = dist[p] + white[q] stack.append(q) return dist numedge = sum(map(len, adj)) d1 = bfs(root) u = d1.index(max(d1)) d2 = bfs(u) diam = max(d2) print(numedge - diam) ``` No
95,932
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has come up with the following problem. > You are given a sequence d of length N. Find the number of the undirected graphs with N vertices labeled 1,2,...,N satisfying the following conditions, modulo 10^{9} + 7: > > * The graph is simple and connected. > * The degree of Vertex i is d_i. > When 2 \leq N, 1 \leq d_i \leq N-1, {\rm Σ} d_i = 2(N-1), it can be proved that the answer to the problem is \frac{(N-2)!}{(d_{1} -1)!(d_{2} - 1)! ... (d_{N}-1)!}. Snuke is wondering what the answer is when 3 \leq N, 1 \leq d_i \leq N-1, { \rm Σ} d_i = 2N. Solve the problem under this condition for him. Constraints * 3 \leq N \leq 300 * 1 \leq d_i \leq N-1 * { \rm Σ} d_i = 2N Input Input is given from Standard Input in the following format: N d_1 d_2 ... d_{N} Output Print the answer. Examples Input 5 1 2 2 3 2 Output 6 Input 16 2 1 3 1 2 1 4 1 1 2 1 1 3 2 4 3 Output 555275958 Submitted Solution: ``` import sys import numpy as np import numba from numba import njit i8 = numba.int64 read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines MOD = 10**9 + 7 @njit((i8, ), cache=True) def fact_table(N): inv = np.empty(N, np.int64) inv[0] = 0 inv[1] = 1 for n in range(2, N): q, r = divmod(MOD, n) inv[n] = inv[r] * (-q) % MOD fact = np.empty(N, np.int64) fact[0] = 1 for n in range(1, N): fact[n] = n * fact[n - 1] % MOD fact_inv = np.empty(N, np.int64) fact_inv[0] = 1 for n in range(1, N): fact_inv[n] = fact_inv[n - 1] * inv[n] % MOD return fact, fact_inv, inv @njit((i8[::1], ), cache=True) def main(A): # key:サイクルに選んだ個数、(d-2)の和 # value:選んでいないもの:1/(d-1)!、選んだもの:1/(d-2)! の積の和 N = len(A) U = N + 10 dp = np.zeros((U, U), np.int64) dp[0, 0] = 1 fact, fact_inv, inv = fact_table(10**3) for d in A: newdp = np.zeros_like(dp) # 選ばない場合の遷移 newdp += dp * fact_inv[d - 1] % MOD # 選ぶ場合の遷移 if d >= 2: newdp[1:, d - 2:U] += dp[:-1, 0:U + 2 - d] * fact_inv[d - 2] % MOD dp = newdp % MOD ret = 0 for n in range(3, N + 1): for a in range(N + 1): x = a * fact[n - 1] % MOD * fact[N - n - 1] % MOD x = x * inv[2] % MOD * dp[n, a] % MOD ret += x return ret % MOD A = np.array(read().split(), np.int64)[1:] print(main(A)) ``` No
95,933
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous "Correct Solution: ``` x,a,b=map(int,input().split()) print("delicious"if a>=b else "safe" if b-a<=x else "dangerous") ```
95,934
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous "Correct Solution: ``` X, A, B = list(map(int, input().split())) print("delicious" if A >= B else "safe" if B - A <= X else "dangerous") ```
95,935
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous "Correct Solution: ``` x,a,b=map(int,input().split()) if a-b>=0: print("delicious") elif -x<=a-b<0: print("safe") else: print("dangerous") ```
95,936
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous "Correct Solution: ``` X,A,B=map(int,input().split()) print(["delicious" if -A+B<=0 else "safe","dangerous"][X<-A+B]) ```
95,937
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous "Correct Solution: ``` x, a, b = [int(s) for s in input().split()] print("delicious" if b <= a else ("safe" if b <= a + x else "dangerous")) ```
95,938
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous "Correct Solution: ``` x,a,b = map(int, input().split()) if b > a+x: print('dangerous') elif b > a: print('safe') else: print('delicious') ```
95,939
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous "Correct Solution: ``` a,b,c=map(int,input().split());print('delicious' if c<=b else 'safe' if c<=a+b else 'dangerous') ```
95,940
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous "Correct Solution: ``` x,a,b=map(int,input().split()) if b<=a:print('delicious') elif 0<(b-a)<=x:print('safe') else:print('dangerous') ```
95,941
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous Submitted Solution: ``` x,a,b = map(int, input().split()) if a+x < b: print("dangerous") elif a < b: print("safe") else: print("delicious") ``` Yes
95,942
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous Submitted Solution: ``` x,a,b = [int(i) for i in input().split()] print('delicious' if x >= b else 'safe' if x >= (b-a) else 'dangerous') ``` Yes
95,943
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous Submitted Solution: ``` X,A,B=map(int,input().split());print("dsdaaenflgeie cr io ou us s"[(B<=A)+(B<=A+X)::3]) ``` Yes
95,944
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous Submitted Solution: ``` a,b,c=map(int,input().split()) if c-b>a: print("dangerous") else: print("safe"if b<c else"delicious") ``` Yes
95,945
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous Submitted Solution: ``` x,a,b=map(int,input().split()) if a>=b: print('delicious') elif x>b-a: print('safe') else: print('dangerous') ``` No
95,946
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous Submitted Solution: ``` [a,b,c]=[int(x) for x in input().split()] if b<c: print("delicious") elif a+b<c: print("safe") else: print("dangerous") ``` No
95,947
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous Submitted Solution: ``` x,a,b = map(int, input().split()) if a-b >= 0: print("delicious") elif 0 > a-b and abs(a-b) <= x+1: print("safe") else: print("dangerous") ``` No
95,948
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier. He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though. Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious. Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it. Write a program that outputs `delicious` if he found it delicious, `safe` if he did not found it delicious but did not get a stomachache either, and `dangerous` if he got a stomachache. Constraints * 1 ≤ X,A,B ≤ 10^9 Input Input is given from Standard Input in the following format: X A B Output Print `delicious` if Takahashi found the food delicious; print `safe` if he neither found it delicious nor got a stomachache; print `dangerous` if he got a stomachache. Examples Input 4 3 6 Output safe Input 6 5 1 Output delicious Input 3 7 12 Output dangerous Submitted Solution: ``` x, a, b = map(int, input().split()) if b-a<=0: print('delicious') elif b-a <= x: print('safe') else: print('dangerouse') ``` No
95,949
Provide a correct Python 3 solution for this coding contest problem. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba "Correct Solution: ``` A, B, C = input().split(",") print(A, B, C) ```
95,950
Provide a correct Python 3 solution for this coding contest problem. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba "Correct Solution: ``` s = input().replace(',',' ') print(s) ```
95,951
Provide a correct Python 3 solution for this coding contest problem. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba "Correct Solution: ``` s=input() s_2=s.replace(",", " ") print(s_2) ```
95,952
Provide a correct Python 3 solution for this coding contest problem. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba "Correct Solution: ``` a=input().split(",") print(" ".join(a)) ```
95,953
Provide a correct Python 3 solution for this coding contest problem. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba "Correct Solution: ``` L = input().split(',') print(' '.join(L)) ```
95,954
Provide a correct Python 3 solution for this coding contest problem. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba "Correct Solution: ``` a,b,c = input().split(sep=",") print(a,b,c) ```
95,955
Provide a correct Python 3 solution for this coding contest problem. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba "Correct Solution: ``` S = input().split(',') print(*S,sep=' ') ```
95,956
Provide a correct Python 3 solution for this coding contest problem. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba "Correct Solution: ``` s=input() print(s[0:5],s[6:13],s[14:19]) ```
95,957
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba Submitted Solution: ``` s = str(input()) print(s.replace(",", " ")) ``` Yes
95,958
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba Submitted Solution: ``` S = input() print(S[:5], S[6:13], S[14:]) ``` Yes
95,959
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba Submitted Solution: ``` s = input().replace(',', ' ', 2) print(s) ``` Yes
95,960
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba Submitted Solution: ``` a = input() A = a.replace(","," ") print(A) ``` Yes
95,961
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba Submitted Solution: ``` s = input() print(s.replace(' ', ',')) ``` No
95,962
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba Submitted Solution: ``` [a, b] = map(int, input().split()) if a*3==b: print(1) elif b%3==0: print(b*3+1) else: print(b*3) ``` No
95,963
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba Submitted Solution: ``` def check(x, y, K, S): if x + y > S: return False for z in range(K+1): if x + y + z == S: return True return False K, S = list(map(int, input('').split())) count = 0 for x in range(K+1): for y in range(K+1): if check(x, y, K, S): count += 1 print(count) ``` No
95,964
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: `[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]`. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. Constraints * The length of s is 19. * The sixth and fourteenth characters in s are `,`. * The other characters in s are lowercase English letters. Input The input is given from Standard Input in the following format: s Output Print the string after the conversion. Examples Input happy,newyear,enjoy Output happy newyear enjoy Input haiku,atcoder,tasks Output haiku atcoder tasks Input abcde,fghihgf,edcba Output abcde fghihgf edcba Submitted Solution: ``` a = input() a[5] = " " a[13] = " " print(a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]+a[10]+a[11]+a[12]+a[13]+a[14]+a[15]+a[16]+a[17]+a[18]) ``` No
95,965
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 "Correct Solution: ``` def bfs(s: str, n: int, i: int, j: int, k: int): return k+int(s[j:]) if i==n-1 else bfs(s,n,i+1,i+1,k+int(s[j:i+1]))+bfs(s,n,i+1,j,k) s=input() n=len(s) print(bfs(s,n,0,0,0)) ```
95,966
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 "Correct Solution: ``` S = input() l = len(S) ans = 0 for i in range(l): for j in range(i+1,l+1): temp = int(S[i:j]) ans += temp * max(1,pow(2,i-1)) * max(1,pow(2,l-j-1)) print(ans) ```
95,967
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 "Correct Solution: ``` s = input() n = len(s) ans = 0 for i in range(2**(n-1)): key = "" for j in range(n-1): key = key + s[j] if (i>>j)&1: key = key + "+" key = key + s[n-1] ans += eval(key) print(ans) ```
95,968
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 "Correct Solution: ``` S = input() L = len(S)-1 a = 0 for i in range(2**L): T = S[0] for j in range(L): T+=(i>>j&1)*"+"+S[1+j] a+=eval(T) print(a) ```
95,969
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 "Correct Solution: ``` s = input() ans = 0 for i in range(2**(len(s)-1)): t = 0 for j in range(len(s)-1): if (i >> j)&1: ans += int(s[t:j+1]) t = j+1 ans += int(s[t:]) print(ans) ```
95,970
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 "Correct Solution: ``` s = input() l = len(s) def dfs(i, f): if i == l - 1: return sum(list(map(int, f.split('+')))) return dfs(i + 1, f + s[i + 1]) + dfs(i + 1, f + '+' + s[i + 1]) print(dfs(0, s[0])) ```
95,971
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 "Correct Solution: ``` s=input() l=len(s) def dfs(i,f): if i==l-1: return eval(f) else: return dfs(i+1, f+s[i+1]) + dfs(i+1,f+'+'+s[i+1]) print(dfs(0,s[0])) ```
95,972
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 "Correct Solution: ``` S = input() def func(S, i): if len(S) <= i: return eval(S) S1 = S[:i] + '+' + S[i:] return func(S, i + 1) + func(S1, i + 2) print(func(S, 1)) ```
95,973
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 Submitted Solution: ``` c,*s=input();n=len(s);print(sum(eval(c+''.join('+'*(b>>i&1)+s[i]for i in range(n)))for b in range(2**n))) ``` Yes
95,974
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 Submitted Solution: ``` S = input() L = len(S) def dfs(i, f): if i == L-1: return sum(map(int, f.split('+'))) return dfs(i+1, f+S[i+1]) + dfs(i+1, f+'+'+S[i+1]) print(dfs(0, S[0])) ``` Yes
95,975
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 Submitted Solution: ``` def dfs(i, f): if i == n - 1: return eval(f) return dfs(i + 1, f + s[i + 1]) + \ dfs(i + 1, f + "+" + s[i + 1]) s = input() n = len(s) print(dfs(0, s[0])) ``` Yes
95,976
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 Submitted Solution: ``` s = input() ans = 0 for i in range(0, len(s)): for j in range(i+1, len(s)+1): ans += int(s[i:j]) * max(1, 2**(i-1)) * max(1, 2**(len(s)-j-1)) print(ans) ``` Yes
95,977
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 Submitted Solution: ``` S = list(input()) cnt = 0 for i in range(1 << len(S)-1): f = S[0] for j, b in enumerate(format(i, 'b').zfill(len(S)-1)): if(b == '1'): f += '+' f += S[j+1] cnt += sum(map(int,f.split('+'))) print(cnt) ``` No
95,978
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 Submitted Solution: ``` print(dfs(0,s[0])) ``` No
95,979
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 Submitted Solution: ``` import sys import copy S = input() op_list = ["+"]*(len(S)-1) sum = 0 for i in range(2**len(op_list)): for j in range(len(op_list)): if (i >> j) & 1: op_list[j] = "" s = "" for k in range(len(op_list)): s = s + S[k] + op_list[k] sum = sum + eval(s) print(sum) ``` No
95,980
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S consisting of digits between `1` and `9`, inclusive. You can insert the letter `+` into some of the positions (possibly none) between two letters in this string. Here, `+` must not occur consecutively after insertion. All strings that can be obtained in this way can be evaluated as formulas. Evaluate all possible formulas, and print the sum of the results. Constraints * 1 \leq |S| \leq 10 * All letters in S are digits between `1` and `9`, inclusive. Input The input is given from Standard Input in the following format: S Output Print the sum of the evaluated value over all possible formulas. Examples Input 125 Output 176 Input 9999999999 Output 12656242944 Submitted Solution: ``` s = input() op_count = len(s) - 1 op_list = [] for i in range(2**op_count): op = [''] * op_count for j in range(op_count): if (i >> j) & 1: op[op_count - 1 - j] = '+' op_list.append(op) ans = 0 for i in range(2 * op_count): cur = 1 t = s for j in range(op_count): if op_list[i][j] == '+': t = t[:j + cur] + "+" + t[j + cur:] cur += 1 ans += eval(t) print(ans) ``` No
95,981
Provide a correct Python 3 solution for this coding contest problem. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 "Correct Solution: ``` while True: try: L = input().split() except: break S = [] for i in L: if i not in "+-*/": S.append(int(i)) elif i == "+": S.append(S.pop()+S.pop()) elif i == "-": t = S.pop() S.append(S.pop()-t) elif i == "*": S.append(S.pop()*S.pop()) else: t = S.pop() S.append(S.pop() / t) print("{:.6f}".format(S[0])) ```
95,982
Provide a correct Python 3 solution for this coding contest problem. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 "Correct Solution: ``` def get_input(): while True: try: yield ''.join(input()) except EOFError: break N = list(get_input()) for l in range(len(N)): p = N[l].split() s = [] for i in range(len(p)): if p[i] == "+": a = float(s.pop()) b = float(s.pop()) s.append(str(b+a)) elif p[i] == "-": a = float(s.pop()) b = float(s.pop()) s.append(str(b-a)) elif p[i] == "*": a = float(s.pop()) b = float(s.pop()) s.append(str(b*a)) elif p[i] == "/": a = float(s.pop()) b = float(s.pop()) s.append(str(b/a)) else: s.append(p[i]) print(s[0]) ```
95,983
Provide a correct Python 3 solution for this coding contest problem. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 "Correct Solution: ``` import re while True: try: f = input().split() except: break stack = [] for c in f: if re.match("-*[0-9]", c) is None: b, a = str(stack.pop()), str(stack.pop()) stack.append(float(eval(a+c+b))) else: stack.append(c) print(stack[0]) ```
95,984
Provide a correct Python 3 solution for this coding contest problem. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 "Correct Solution: ``` d = [] while True: try: lst = list(input().split()) for i in lst: if i == '+': num = d.pop() d[-1] = d[-1] + num elif i == '-': num = d.pop() d[-1] = d[-1] - num elif i == '*': num = d.pop() d[-1] = d[-1] * num elif i == '/': num = d.pop() d[-1] = d[-1] / num else: d.append(int(i)) print(d.pop()) except EOFError: break ```
95,985
Provide a correct Python 3 solution for this coding contest problem. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 "Correct Solution: ``` while True : try : lst = list(input().split()) except EOFError : break stack = [] for i in lst : if i == '+' : b = stack.pop(-1) a = stack.pop(-1) stack.append(a+b) elif i == '-' : b = stack.pop(-1) a = stack.pop(-1) stack.append(a-b) elif i == '*' : b = stack.pop(-1) a = stack.pop(-1) stack.append(a*b) elif i == '/' : b = stack.pop(-1) a = stack.pop(-1) stack.append(a/b) else : stack.append(int(i)) print('{:.8f}'.format(stack[0])) ```
95,986
Provide a correct Python 3 solution for this coding contest problem. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 "Correct Solution: ``` while True: try: a = input().split() stack = [] for inp in a: if inp == "+": x = stack.pop() y = stack.pop() stack.append(x+y) elif inp == "-": x = stack.pop() y = stack.pop() stack.append(y-x) elif inp == "*": x = stack.pop() y = stack.pop() stack.append(x*y) elif inp == "/": x = stack.pop() y = stack.pop() stack.append(y/x) else: stack.append(float(inp)) print(*stack) except: break ```
95,987
Provide a correct Python 3 solution for this coding contest problem. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 "Correct Solution: ``` import sys def isNum(a) : try: int(a) except: return False return True for line in sys.stdin: stack = [] task = line.strip().split(" ") ; for i in range(0, len(task) ) : if isNum(task[i]) : stack.append(task[i]) elif len(stack) != 0 : stack.append( str( eval( stack.pop(-2) + task[i] + stack.pop(-1) ) ) ) print("{:6f}".format(float(stack[0]))) ```
95,988
Provide a correct Python 3 solution for this coding contest problem. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 "Correct Solution: ``` def invpol(f): s = [] for c in f: if c == '+': a = s.pop() s.append(s.pop() + a) elif c == '-': a = s.pop() s.append(s.pop() - a) elif c == '*': a = s.pop() s.append(s.pop() * a) elif c == '/': a = s.pop() s.append(s.pop() / a) else: s.append(int(c)) return(s[0]) while True: try: f = input().strip().split() print("%.6f" % invpol(f)) except EOFError: break ```
95,989
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 Submitted Solution: ``` # -*- coding: utf-8 -*- import sys import os import math for s in sys.stdin: lst = s.split() # stack S = [] for c in lst: if c == '+': a = S.pop() b = S.pop() S.append(b + a) elif c == '-': a = S.pop() b = S.pop() S.append(b - a) elif c == '*': a = S.pop() b = S.pop() S.append(b * a) elif c == '/': a = S.pop() b = S.pop() S.append(b / a) else: S.append(float(c)) print(S[0]) ``` Yes
95,990
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 Submitted Solution: ``` import sys f = sys.stdin from collections import deque import operator func = {'+':operator.add,'-':operator.sub,'*':operator.mul,'/':operator.truediv } for line in f: stack = deque() for element in line.strip().split(): if element in func: b = stack.pop() a = stack.pop() stack.append(func[element](a, b)) else: stack.append(float(element)) print('{:.6f}'.format(stack[0])) ``` Yes
95,991
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 Submitted Solution: ``` while(1): try: a = [i for i in input().split()] stack = [] l = len(stack) for i in a: if i == "+": x = stack[l-2] y = stack[l-1] stack.pop() stack.pop() stack.append(x+y) elif i == "-": x = stack[l-2] y = stack[l-1] stack.pop() stack.pop() stack.append(x-y) elif i == "*": x = stack[l-2] y = stack[l-1] stack.pop() stack.pop() stack.append(x*y) elif i == "/": x = stack[l-2] y = stack[l-1] stack.pop() stack.pop() stack.append(x/y) else: stack.append(float(i)) print("{:.6f}".format(stack[0])) except EOFError: break ``` Yes
95,992
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 Submitted Solution: ``` while 1: try:s=list(input().split()) except:break a=[] for x in s: if x in ['+','-','*','/']: c,b=str(a.pop()),str(a.pop()) a+=[float(eval(b+x+c))] else:a+=[float(x)] print(*a) ``` Yes
95,993
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 Submitted Solution: ``` import sys def line():return sys.stdin.readline().strip() ops = {"+":lambda a,b:b + a, "-":lambda a,b:b - a, "*":lambda a,b:b * a, "/":lambda a,b:b / a} while True: stack = [] for s in line().split(): if s in ops: stack.append(ops[s](stack.pop(),stack.pop())) else: stack.append(int(s)) print(stack[0]) ``` No
95,994
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 Submitted Solution: ``` s=list();[[[s.append(eval('{2}{1}{0}'.format(s.pop(),i,s.pop()))if(i in'+-*/')else i)for i in input().split(' ')]and print(s.pop())]for x in __import__('itertools').count()] ``` No
95,995
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 Submitted Solution: ``` #!/usr/bin/env python3 import sys def calc_inverse_polish_notation_string(s): tokens = [token for token in s.split(' ') if token != ''] stack = [] for token in tokens: if token in ['+', '-', '*', '/']: val2 = stack.pop() val1 = stack.pop() result = eval("%s %s %s" % (val1, token, val2)) stack.append(result) else: stack.append(token) return stack[0] # if len(stack) != 1, it should throw an exception def main(): for line in sys.stdin.readlines(): print(calc_inverse_polish_notation_string(line)) if __name__ == "__main__": main() ``` No
95,996
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dr .: Peter, I've finally done it. Peter: See you again? What kind of silly invention is this time? Dr .: I finally came up with a revolutionary way to process mathematical formulas on a computer. Look at this table. Ordinary notation | Dr.'s "breakthrough" notation --- | --- 1 + 2 | 1 2 + 3 * 4 + 7 | 3 4 * 7 + 10 / (2 --12) | 10 2 12-/ (3-4) * (7 + 2 * 3) | 3 4 --7 2 3 * + * Peter: Yeah. Dr .: Fufufu. This alone won't tell you what it means to an inexperienced person. It's important from here. Peter: I mean ... Dr .: You know that computers have a data structure called a stack. Look, that's "first in, then out". Peter: Yes. I know, that ... Dr .: This groundbreaking notation uses that stack. For example, this 10 2 12-/, but process as follows. Processing target | 10 | 2 | 12 |-| / --- | --- | --- | --- | --- | --- | ↓ | ↓ | ↓ | ↓ 2-12 | ↓ 10 / -10 Stack | |. --- .. Ten | .. --- 2 Ten | 12 --- 2 Ten | .. --- -Ten Ten | .. --- .. -1 Dr .: How is it? You don't have to worry about parentheses or operator precedence, right? The word order is also "10 divided by 2 minus 12", which is somewhat similar to his Far Eastern island nation, Japanese. With this epoch-making invention, our laboratory is safe. Fafafa. Peter: I mean, Doctor. I learned this in the basic course at the University of Aizu when I was in Japan. Everyone had a simple program called "Reverse Polish Notation". Dr .: ... So, instead of Peter, I decided to teach this program to the doctor. Create a program that inputs the formula written in "Reverse Polish Notation" and outputs the calculation result. input Given multiple datasets. For each dataset, a formula in Reverse Polish Notation (a string of up to 80 characters with integers and arithmetic symbols separated by one blank character (half-width)) is given on one line. No formula is given that divides a value by 0 or a value that is as close to 0 as possible. The number of datasets does not exceed 50. output Output the calculation result (real number) on one line for each data set. The calculation result may include an error of 0.00001 or less. Example Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output -1.000000 -13.000000 0.000000 Submitted Solution: ``` import sys f = sys.stdin from collections import deque import operator func = {'+':operator.add,'-':operator.sub,'*':operator.mul,'/':operator.div } for line in f: stack = deque() for element in line.strip().split(): if element.isdigit(): stack.append(float(element)) else: b = stack.pop() a = stack.pop() stack.append(func[element](a, b)) print('{:.6f}'.format(stack[0])) ``` No
95,997
Provide a correct Python 3 solution for this coding contest problem. There is an ice cream shop named Ten Ice Cream. At this store, we always have 10 types of ice cream on the shelves. The store manager creates a daily graph showing how well ice cream is selling for reference in product development. For such a store manager, you decided to create a program that displays the number of each ice cream sold in a graph. Enter the total number of ice creams sold in a day and the number of ice creams sold, and create a program that outputs as many * (half-width asterisks) as the number sold for each type of ice cream. However, the type of ice cream is represented by an integer from 0 to 9. Also, for ice cream with zero sales, one- (half-width hyphen) is output. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: n c1 c2 :: cn The first line gives the total number of ice creams sold per day n (1 ≤ n ≤ 10000). The next n lines are given the i-th ice cream type ci (0 ≤ ci ≤ 9). The number of datasets does not exceed 20. Output For each input dataset, the number of sales is output in numerical order of each ice cream type. Example Input 15 2 6 7 0 1 9 8 7 3 8 9 4 8 2 2 3 9 1 5 0 Output * * *** * * - * ** *** ** - * - - - * - - - * "Correct Solution: ``` while True: N=int(input()) A=0 B=0 C=0 D=0 E=0 F=0 G=0 H=0 I=0 J=0 i=0 j=0 L=[] if N==0: break else: L=[int(input()) for _ in range (N)] for i in range (N): if L[i]==0: A+=1 elif L[i]==1: B+=1 elif L[i]==2: C+=1 elif L[i]==3: D+=1 elif L[i]==4: E+=1 elif L[i]==5: F+=1 elif L[i]==6: G+=1 elif L[i]==7: H+=1 elif L[i]==8: I+=1 else: J+=1 S=[A,B,C,D,E,F,G,H,I,J] for j in range(10): T=S[j] if T==0: print("-") else: k=0 print("*"*T) ```
95,998
Provide a correct Python 3 solution for this coding contest problem. There is an ice cream shop named Ten Ice Cream. At this store, we always have 10 types of ice cream on the shelves. The store manager creates a daily graph showing how well ice cream is selling for reference in product development. For such a store manager, you decided to create a program that displays the number of each ice cream sold in a graph. Enter the total number of ice creams sold in a day and the number of ice creams sold, and create a program that outputs as many * (half-width asterisks) as the number sold for each type of ice cream. However, the type of ice cream is represented by an integer from 0 to 9. Also, for ice cream with zero sales, one- (half-width hyphen) is output. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: n c1 c2 :: cn The first line gives the total number of ice creams sold per day n (1 ≤ n ≤ 10000). The next n lines are given the i-th ice cream type ci (0 ≤ ci ≤ 9). The number of datasets does not exceed 20. Output For each input dataset, the number of sales is output in numerical order of each ice cream type. Example Input 15 2 6 7 0 1 9 8 7 3 8 9 4 8 2 2 3 9 1 5 0 Output * * *** * * - * ** *** ** - * - - - * - - - * "Correct Solution: ``` while True: n= int(input()) if n== 0: break c= [int(input()) for _ in range(n)] for i in range(10): cc= c.count(i) print('-' if cc== 0 else cc*"*") ```
95,999