message
stringlengths
2
67k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
463
109k
cluster
float64
19
19
__index_level_0__
int64
926
217k
Provide tags and a correct Python 3 solution for this coding contest problem. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used.
instruction
0
7,772
19
15,544
Tags: brute force, constructive algorithms, greedy, math, probabilities Correct Solution: ``` t = int(input()) chisls = [2 ** (i + 1) - 2 for i in range(1, 72)] # print(chisls) for i in range(t): n = int(input()) if n % 2 == 1: print(-1) else: # print(1, end=' ') data = [1] n -= 2 p = len(chisls) - 1 while n > 0: while chisls[p] > n: p -= 1 # print(p) while n >= chisls[p]: data += [1] + [0 for j in range(p)] n -= chisls[p] if len(data) > 2000: break if len(data) > 2000: print(-1) else: print(len(data)) print(*data) ```
output
1
7,772
19
15,545
Provide tags and a correct Python 3 solution for this coding contest problem. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used.
instruction
0
7,773
19
15,546
Tags: brute force, constructive algorithms, greedy, math, probabilities Correct Solution: ``` import sys from math import gcd,sqrt,ceil,log2 from collections import defaultdict,Counter,deque from bisect import bisect_left,bisect_right import math sys.setrecursionlimit(2*10**5+10) import heapq from itertools import permutations # input=sys.stdin.readline # def print(x): # sys.stdout.write(str(x)+"\n") # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 aa='abcdefghijklmnopqrstuvwxyz' class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # import sys # import io, os # input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def get_sum(bit,i): s = 0 i+=1 while i>0: s+=bit[i] i-=i&(-i) return s def update(bit,n,i,v): i+=1 while i<=n: bit[i]+=v i+=i&(-i) def modInverse(b,m): g = math.gcd(b, m) if (g != 1): return -1 else: return pow(b, m - 2, m) def primeFactors(n): sa = [] # sa.add(n) while n % 2 == 0: sa.append(2) n = n // 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: sa.append(i) n = n // i # sa.add(n) if n > 2: sa.append(n) return sa def seive(n): pri = [True]*(n+1) p = 2 while p*p<=n: if pri[p] == True: for i in range(p*p,n+1,p): pri[i] = False p+=1 return pri def check_prim(n): if n<0: return False for i in range(2,int(sqrt(n))+1): if n%i == 0: return False return True def getZarr(string, z): n = len(string) # [L,R] make a window which matches # with prefix of s l, r, k = 0, 0, 0 for i in range(1, n): # if i>R nothing matches so we will calculate. # Z[i] using naive way. if i > r: l, r = i, i # R-L = 0 in starting, so it will start # checking from 0'th index. For example, # for "ababab" and i = 1, the value of R # remains 0 and Z[i] becomes 0. For string # "aaaaaa" and i = 1, Z[i] and R become 5 while r < n and string[r - l] == string[r]: r += 1 z[i] = r - l r -= 1 else: # k = i-L so k corresponds to number which # matches in [L,R] interval. k = i - l # if Z[k] is less than remaining interval # then Z[i] will be equal to Z[k]. # For example, str = "ababab", i = 3, R = 5 # and L = 2 if z[k] < r - i + 1: z[i] = z[k] # For example str = "aaaaaa" and i = 2, # R is 5, L is 0 else: # else start from R and check manually l = i while r < n and string[r - l] == string[r]: r += 1 z[i] = r - l r -= 1 def search(text, pattern): # Create concatenated string "P$T" concat = pattern + "$" + text l = len(concat) z = [0] * l getZarr(concat, z) ha = [] for i in range(l): if z[i] == len(pattern): ha.append(i - len(pattern) - 1) return ha # n,k = map(int,input().split()) # l = list(map(int,input().split())) # # n = int(input()) # l = list(map(int,input().split())) # # hash = defaultdict(list) # la = [] # # for i in range(n): # la.append([l[i],i+1]) # # la.sort(key = lambda x: (x[0],-x[1])) # ans = [] # r = n # flag = 0 # lo = [] # ha = [i for i in range(n,0,-1)] # yo = [] # for a,b in la: # # if a == 1: # ans.append([r,b]) # # hash[(1,1)].append([b,r]) # lo.append((r,b)) # ha.pop(0) # yo.append([r,b]) # r-=1 # # elif a == 2: # # print(yo,lo) # # print(hash[1,1]) # if lo == []: # flag = 1 # break # c,d = lo.pop(0) # yo.pop(0) # if b>=d: # flag = 1 # break # ans.append([c,b]) # yo.append([c,b]) # # # # elif a == 3: # # if yo == []: # flag = 1 # break # c,d = yo.pop(0) # if b>=d: # flag = 1 # break # if ha == []: # flag = 1 # break # # ka = ha.pop(0) # # ans.append([ka,b]) # ans.append([ka,d]) # yo.append([ka,b]) # # if flag: # print(-1) # else: # print(len(ans)) # for a,b in ans: # print(a,b) def mergeIntervals(arr): # Sorting based on the increasing order # of the start intervals arr.sort(key = lambda x: x[0]) # array to hold the merged intervals m = [] s = -10000 max = -100000 for i in range(len(arr)): a = arr[i] if a[0] > max: if i != 0: m.append([s,max]) max = a[1] s = a[0] else: if a[1] >= max: max = a[1] #'max' value gives the last point of # that particular interval # 's' gives the starting point of that interval # 'm' array contains the list of all merged intervals if max != -100000 and [s, max] not in m: m.append([s, max]) return m class SortedList: def __init__(self, iterable=[], _load=200): """Initialize sorted list instance.""" values = sorted(iterable) self._len = _len = len(values) self._load = _load self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)] self._list_lens = [len(_list) for _list in _lists] self._mins = [_list[0] for _list in _lists] self._fen_tree = [] self._rebuild = True def _fen_build(self): """Build a fenwick tree instance.""" self._fen_tree[:] = self._list_lens _fen_tree = self._fen_tree for i in range(len(_fen_tree)): if i | i + 1 < len(_fen_tree): _fen_tree[i | i + 1] += _fen_tree[i] self._rebuild = False def _fen_update(self, index, value): """Update `fen_tree[index] += value`.""" if not self._rebuild: _fen_tree = self._fen_tree while index < len(_fen_tree): _fen_tree[index] += value index |= index + 1 def _fen_query(self, end): """Return `sum(_fen_tree[:end])`.""" if self._rebuild: self._fen_build() _fen_tree = self._fen_tree x = 0 while end: x += _fen_tree[end - 1] end &= end - 1 return x def _fen_findkth(self, k): """Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`).""" _list_lens = self._list_lens if k < _list_lens[0]: return 0, k if k >= self._len - _list_lens[-1]: return len(_list_lens) - 1, k + _list_lens[-1] - self._len if self._rebuild: self._fen_build() _fen_tree = self._fen_tree idx = -1 for d in reversed(range(len(_fen_tree).bit_length())): right_idx = idx + (1 << d) if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]: idx = right_idx k -= _fen_tree[idx] return idx + 1, k def _delete(self, pos, idx): """Delete value at the given `(pos, idx)`.""" _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len -= 1 self._fen_update(pos, -1) del _lists[pos][idx] _list_lens[pos] -= 1 if _list_lens[pos]: _mins[pos] = _lists[pos][0] else: del _lists[pos] del _list_lens[pos] del _mins[pos] self._rebuild = True def _loc_left(self, value): """Return an index pair that corresponds to the first position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins lo, pos = -1, len(_lists) - 1 while lo + 1 < pos: mi = (lo + pos) >> 1 if value <= _mins[mi]: pos = mi else: lo = mi if pos and value <= _lists[pos - 1][-1]: pos -= 1 _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value <= _list[mi]: idx = mi else: lo = mi return pos, idx def _loc_right(self, value): """Return an index pair that corresponds to the last position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins pos, hi = 0, len(_lists) while pos + 1 < hi: mi = (pos + hi) >> 1 if value < _mins[mi]: hi = mi else: pos = mi _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value < _list[mi]: idx = mi else: lo = mi return pos, idx def add(self, value): """Add `value` to sorted list.""" _load = self._load _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len += 1 if _lists: pos, idx = self._loc_right(value) self._fen_update(pos, 1) _list = _lists[pos] _list.insert(idx, value) _list_lens[pos] += 1 _mins[pos] = _list[0] if _load + _load < len(_list): _lists.insert(pos + 1, _list[_load:]) _list_lens.insert(pos + 1, len(_list) - _load) _mins.insert(pos + 1, _list[_load]) _list_lens[pos] = _load del _list[_load:] self._rebuild = True else: _lists.append([value]) _mins.append(value) _list_lens.append(1) self._rebuild = True def discard(self, value): """Remove `value` from sorted list if it is a member.""" _lists = self._lists if _lists: pos, idx = self._loc_right(value) if idx and _lists[pos][idx - 1] == value: self._delete(pos, idx - 1) def remove(self, value): """Remove `value` from sorted list; `value` must be a member.""" _len = self._len self.discard(value) if _len == self._len: raise ValueError('{0!r} not in list'.format(value)) def pop(self, index=-1): """Remove and return value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) value = self._lists[pos][idx] self._delete(pos, idx) return value def bisect_left(self, value): """Return the first index to insert `value` in the sorted list.""" pos, idx = self._loc_left(value) return self._fen_query(pos) + idx def bisect_right(self, value): """Return the last index to insert `value` in the sorted list.""" pos, idx = self._loc_right(value) return self._fen_query(pos) + idx def count(self, value): """Return number of occurrences of `value` in the sorted list.""" return self.bisect_right(value) - self.bisect_left(value) def __len__(self): """Return the size of the sorted list.""" return self._len def __getitem__(self, index): """Lookup value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) return self._lists[pos][idx] def __delitem__(self, index): """Remove value at `index` from sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) self._delete(pos, idx) def __contains__(self, value): """Return true if `value` is an element of the sorted list.""" _lists = self._lists if _lists: pos, idx = self._loc_left(value) return idx < len(_lists[pos]) and _lists[pos][idx] == value return False def __iter__(self): """Return an iterator over the sorted list.""" return (value for _list in self._lists for value in _list) def __reversed__(self): """Return a reverse iterator over the sorted list.""" return (value for _list in reversed(self._lists) for value in reversed(_list)) def __repr__(self): """Return string representation of sorted list.""" return 'SortedList({0})'.format(list(self)) def ncr(n, r, p): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p def sol(n): seti = set() for i in range(1,int(sqrt(n))+1): if n%i == 0: seti.add(n//i) seti.add(i) return seti def lcm(a,b): return (a*b)//gcd(a,b) # # n,p = map(int,input().split()) # # s = input() # # if n <=2: # if n == 1: # pass # if n == 2: # pass # i = n-1 # idx = -1 # while i>=0: # z = ord(s[i])-96 # k = chr(z+1+96) # flag = 1 # if i-1>=0: # if s[i-1]!=k: # flag+=1 # else: # flag+=1 # if i-2>=0: # if s[i-2]!=k: # flag+=1 # else: # flag+=1 # if flag == 2: # idx = i # s[i] = k # break # if idx == -1: # print('NO') # exit() # for i in range(idx+1,n): # if # def moore_voting(l): count1 = 0 count2 = 0 first = 10**18 second = 10**18 n = len(l) for i in range(n): if l[i] == first: count1+=1 elif l[i] == second: count2+=1 elif count1 == 0: count1+=1 first = l[i] elif count2 == 0: count2+=1 second = l[i] else: count1-=1 count2-=1 for i in range(n): if l[i] == first: count1+=1 elif l[i] == second: count2+=1 if count1>n//3: return first if count2>n//3: return second return -1 def find_parent(u,parent): if u!=parent[u]: parent[u] = find_parent(parent[u],parent) return parent[u] def dis_union(n,e): par = [i for i in range(n+1)] rank = [1]*(n+1) for a,b in e: z1,z2 = find_parent(a,par),find_parent(b,par) if rank[z1]>rank[z2]: z1,z2 = z2,z1 if z1!=z2: par[z1] = z2 rank[z2]+=rank[z1] else: return a,b def dijkstra(n,tot,hash): hea = [[0,n]] dis = [10**18]*(tot+1) dis[n] = 0 boo = defaultdict(bool) check = defaultdict(int) while hea: a,b = heapq.heappop(hea) if boo[b]: continue boo[b] = True for i,w in hash[b]: if b == 1: c = 0 if (1,i,w) in nodes: c = nodes[(1,i,w)] del nodes[(1,i,w)] if dis[b]+w<dis[i]: dis[i] = dis[b]+w check[i] = c elif dis[b]+w == dis[i] and c == 0: dis[i] = dis[b]+w check[i] = c else: if dis[b]+w<=dis[i]: dis[i] = dis[b]+w check[i] = check[b] heapq.heappush(hea,[dis[i],i]) return check def power(x,y,p): res = 1 x = x%p if x == 0: return 0 while y>0: if (y&1) == 1: res*=x x = x*x y = y>>1 return res import sys from math import ceil,log2 INT_MAX = sys.maxsize def minVal(x, y) : return x if (x < y) else y def getMid(s, e) : return s + (e - s) // 2 def RMQUtil( st, ss, se, qs, qe, index) : if (qs <= ss and qe >= se) : return st[index] if (se < qs or ss > qe) : return INT_MAX mid = getMid(ss, se) return minVal(RMQUtil(st, ss, mid, qs, qe, 2 * index + 1), RMQUtil(st, mid + 1, se, qs, qe, 2 * index + 2)) def RMQ( st, n, qs, qe) : if (qs < 0 or qe > n - 1 or qs > qe) : print("Invalid Input") return -1 return RMQUtil(st, 0, n - 1, qs, qe, 0) def constructSTUtil(arr, ss, se, st, si) : if (ss == se) : st[si] = arr[ss] return arr[ss] mid = getMid(ss, se) st[si] = minVal(constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2)) return st[si] def constructST( arr, n) : x = (int)(ceil(log2(n))) max_size = 2 * (int)(2**x) - 1 st = [0] * (max_size) constructSTUtil(arr, 0, n - 1, st, 0) return st # t = int(input()) # for _ in range(t): # # n = int(input()) # l = list(map(int,input().split())) # # x,y = 0,10 # st = constructST(l, n) # # pre = [0] # suf = [0] # for i in range(n): # pre.append(max(pre[-1],l[i])) # for i in range(n-1,-1,-1): # suf.append(max(suf[-1],l[i])) # # # i = 1 # # print(pre,suf) # flag = 0 # x,y,z = -1,-1,-1 # # suf.reverse() # print(suf) # while i<len(pre): # # z = pre[i] # j = bisect_left(suf,z) # if suf[j] == z: # while i<n and l[i]<=z: # i+=1 # if pre[i]>z: # break # while j<n and l[n-j]<=z: # j+=1 # if suf[j]>z: # break # # j-=1 # print(i,n-j) # # break/ # if RMQ(st,n,i,j) == z: # c = i+j-i+1 # x,y,z = i,j-i+1,n-c # break # else: # i+=1 # # else: # i+=1 # # # # if x!=-1: # print('Yes') # print(x,y,z) # else: # print('No') # t = int(input()) # # for _ in range(t): # # def debug(n): # ans = [] # for i in range(1,n+1): # for j in range(i+1,n+1): # if (i*(j+1))%(j-i) == 0 : # ans.append([i,j]) # return ans # # # n = int(input()) # print(debug(n)) # import sys # input = sys.stdin.readline # import bisect # # t=int(input()) # for tests in range(t): # n=int(input()) # A=list(map(int,input().split())) # # LEN = len(A) # Sparse_table = [A] # # for i in range(LEN.bit_length()-1): # j = 1<<i # B = [] # for k in range(len(Sparse_table[-1])-j): # B.append(min(Sparse_table[-1][k], Sparse_table[-1][k+j])) # Sparse_table.append(B) # # def query(l,r): # [l,r)におけるminを求める. # i=(r-l).bit_length()-1 # 何番目のSparse_tableを見るか. # # return min(Sparse_table[i][l],Sparse_table[i][r-(1<<i)]) # (1<<i)個あれば[l, r)が埋まるので, それを使ってminを求める. # # LMAX=[A[0]] # for i in range(1,n): # LMAX.append(max(LMAX[-1],A[i])) # # RMAX=A[-1] # # for i in range(n-1,-1,-1): # RMAX=max(RMAX,A[i]) # # x=bisect.bisect(LMAX,RMAX) # #print(RMAX,x) # print(RMAX,x,i) # if x==0: # continue # # v=min(x,i-1) # if v<=0: # continue # # if LMAX[v-1]==query(v,i)==RMAX: # print("YES") # print(v,i-v,n-i) # break # # v-=1 # if v<=0: # continue # if LMAX[v-1]==query(v,i)==RMAX: # print("YES") # print(v,i-v,n-i) # break # else: # print("NO") # # # # # # # # # # t = int(input()) # # for _ in range(t): # # x = int(input()) # mini = 10**18 # n = ceil((-1 + sqrt(1+8*x))/2) # for i in range(-100,1): # z = x+-1*i # z1 = (abs(i)*(abs(i)+1))//2 # z+=z1 # # print(z) # n = ceil((-1 + sqrt(1+8*z))/2) # # y = (n*(n+1))//2 # # print(n,y,z,i) # mini = min(n+y-z,mini) # print(n+y-z,i) # # # print(mini) # t = int(input()) for _ in range(t): k = int(input()) ans = [] if k%2!=0: print(-1) continue ans = [1] su = 2 k1 = 2 while su-k!=0: su+=2**k1 if su>k: su-=2**k1 su+=2 ans.append(1) k1 = 2 continue ans.append(0) k1+=1 k-=su ans+=[1]*(k//2) if len(ans)>2000: print(-1) continue print(len(ans)) print(*ans) ```
output
1
7,773
19
15,547
Provide tags and a correct Python 3 solution for this coding contest problem. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used.
instruction
0
7,774
19
15,548
Tags: brute force, constructive algorithms, greedy, math, probabilities Correct Solution: ``` two = [] cur = 1 for i in range(100): two.append(cur) cur *= 2 t = int(input()) for _ in range(t): k = int(input()) if k % 2: print(-1) continue res = [] def get(x): a = 0 while 1: if two[a+3]-2 > x: break a += 1 res.append('1') res.extend(['0']*a) return x-two[a+2]+2 while k: k = get(k) n = len(res) if n > 2000: print(-1) continue print(n) print(' '.join(res)) ```
output
1
7,774
19
15,549
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used. Submitted Solution: ``` #from random import randint # # #arr=[1,1,1,1,1,1] #m=len(arr) #prevOne=[0 for _ in range(m)] #for i in range(m): # if arr[i]==1: # prevOne[i]=i # elif i>0: # prevOne[i]=prevOne[i-1] # #t=0 #n=10000 #for _ in range(n): # curr=0 # while curr<m: # if randint(1,2)==2: # curr+=1 # else: # curr=prevOne[curr] # t+=1 # #print(t/n) #each 1 contributes 2 #each 0 contributes 2**(1 + number of zeros in a row including itself) #k=n1*2 + sum(4*(2**l - 1)) where l is the number of consequtive 0s in a row def oneLineArrayPrint(arr): print(' '.join([str(x) for x in arr])) def calcTurnsForLZeroesInARow(L): return 4*(2**L-1) t=int(input()) for _ in range(t): k=int(input()) if k%2==1: print(-1) else: ans=[1] k-=2 while k>0: l=0 b=2000 while b>0: while calcTurnsForLZeroesInARow(l+b)<=k: l+=b b//=2 ans=ans+[0]*l k-=calcTurnsForLZeroesInARow(l) if k==0: break ans.append(1) k-=2 # print('here') print(len(ans)) oneLineArrayPrint(ans) ```
instruction
0
7,775
19
15,550
Yes
output
1
7,775
19
15,551
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used. Submitted Solution: ``` t = int(input()) add = 4 arr = [2] for i in range(70): arr += [arr[-1]+add] add *= 2 # print(arr) for _ in range(t): k = int(input()) ans = [1] flag = 1 while k != 0: # print(k, ans) flag = 0 for i in range(69, -1, -1): if arr[i] <= k: ans += [0]*i + [1] flag = 1 k -= arr[i] if flag == 0: ans = -1 break if ans != -1: ans.pop() print(len(ans)) print(*ans) else: print(ans) ```
instruction
0
7,776
19
15,552
Yes
output
1
7,776
19
15,553
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used. Submitted Solution: ``` import sys from collections import defaultdict as dd from collections import Counter as cc from queue import Queue import math import itertools try: sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') except: pass input = lambda: sys.stdin.readline().rstrip() for _ in range(int(input())): q=int(input()) if q%2==1: print(-1) else: w=[1] i=2 q-=2 while q>0: if 2*i<q: i*=2 q-=i w.append(0) else: i=2 q-=2 w.append(1) print(len(w)) print(*w) ```
instruction
0
7,777
19
15,554
Yes
output
1
7,777
19
15,555
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used. Submitted Solution: ``` from collections import deque import sys serie = "" d = deque() n = 2 d.append(n) while n<10**18: n = 2*n +2 d.append(n) r = 58 def binarySearch(l,r,x): if r >= l: mid = l + (r - l) // 2 if d[mid] == x: return mid elif d[mid] > x: return binarySearch(l, mid-1, x) else: return binarySearch(mid + 1, r, x) else: return l-1 def p(x,s = ""): if x == 0: return s i = binarySearch(0,r,x) s+="1 "+("0 "*i) x-=d[i] return p(x,s) t = int(sys.stdin.readline().strip()) while t>0: k = int(sys.stdin.readline().strip()) serie = "" if k%2 == 1: print(-1) else: s = p(k) print(len(s)//2) print(s) t-=1 ```
instruction
0
7,778
19
15,556
Yes
output
1
7,778
19
15,557
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used. Submitted Solution: ``` T = int(input()) for _ in range(T): k = int(input()) if k % 2: print(-1); continue res = [] for u in range(60, 1, -1): p = 2**u + 2 if k // p: res.append((u, k//p)) k %= p res.append((1, k//2)) ls = [] for u, cc in res: ls += sum([ [0]*(u-1)+[1] ]*cc, []) print(len(ls)) print(*ls) ```
instruction
0
7,779
19
15,558
No
output
1
7,779
19
15,559
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used. Submitted Solution: ``` mod = 10**9 + 7 def solve(): n = int(input()) ans = [] if n == 1: ans.append(-1) else: while n > 1 and len(ans) < 2000: if n % 2 == 0: ans.append(1) n //= 2; else: ans.append(0) n += 1 if n > 1: ans = [-1] print(len(ans)) print(*ans) t = 1 t = int(input()) while t > 0: solve() t -= 1 ```
instruction
0
7,780
19
15,560
No
output
1
7,780
19
15,561
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used. Submitted Solution: ``` import sys from sys import stdin tt = int(stdin.readline()) for loop in range(tt): k = int(stdin.readline()) ans = [] if k % 2 == 1: print (-1) continue tmp = 1 while k > 0: if k & (2**tmp) > 0: ans.append(1) for i in range(tmp-1): ans.append(0) k ^= 2**tmp tmp += 1 if len(ans) <= 2000: print (len(ans)) print (*ans) else: print (-1) ```
instruction
0
7,781
19
15,562
No
output
1
7,781
19
15,563
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Gildong is developing a game consisting of n stages numbered from 1 to n. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the n-th stage. There is at most one checkpoint on each stage, and there is always a checkpoint on the 1-st stage. At the beginning of the game, only the checkpoint on the 1-st stage is activated, and all other checkpoints are deactivated. When the player gets to the i-th stage that has a checkpoint, that checkpoint is activated. For each try of a stage, the player can either beat the stage or fail the stage. If they beat the i-th stage, the player is moved to the i+1-st stage. If they fail the i-th stage, the player is moved to the most recent checkpoint they activated, and they have to beat the stages after that checkpoint again. For example, assume that n = 4 and the checkpoints are on the 1-st and 3-rd stages. The player starts at the 1-st stage. If they fail on the 1-st stage, they need to retry the 1-st stage because the checkpoint on the 1-st stage is the most recent checkpoint they activated. If the player beats the 1-st stage, they're moved to the 2-nd stage. If they fail it, they're sent back to the 1-st stage again. If they beat both the 1-st stage and the 2-nd stage, they get to the 3-rd stage and the checkpoint on the 3-rd stage is activated. Now whenever they fail on the 3-rd stage, or the 4-th stage after beating the 3-rd stage, they're sent back to the 3-rd stage. If they beat both the 3-rd stage and the 4-th stage, they win the game. Gildong is going to build the stages to have equal difficulty. He wants you to find any series of stages and checkpoints using at most 2000 stages, where the [expected number](https://en.wikipedia.org/wiki/Expected_value) of tries over all stages is exactly k, for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 50). Each test case contains exactly one line. The line consists of a single integer k (1 ≤ k ≤ 10^{18}) — the expected number of tries over all stages Gildong wants to set for a player whose probability of beating each stage is exactly \cfrac{1}{2}. Output For each test case, print -1 if it's impossible to construct such a series of stages and checkpoints using at most 2000 stages. Otherwise, print two lines. The first line should contain a single integer n (1 ≤ n ≤ 2000) – the number of stages. The second line should contain n integers, where the i-th integer represents whether the i-th stage has a checkpoint. The i-th integer should be 0 if the i-th stage doesn't have a checkpoint, and 1 if it has a checkpoint. Note that the first integer must be 1 according to the description. Example Input 4 1 2 8 12 Output -1 1 1 4 1 1 1 1 5 1 1 0 1 1 Note In the first and the second case, we can see that the 'easiest' series of stages is to have 1 stage with a checkpoint. This already requires 2 tries in expectation, so it is impossible to make it to require only 1 try. In the third case, it takes 2 tries in expectation to beat each stage, and the player can always retry that stage without falling back to one of the previous stages if they fail it. Therefore the total expected number of tries is 8. Note that there exists an answer with fewer stages, but you are not required to minimize the number of stages used. Submitted Solution: ``` import math a = int(input()) for i in range(a): k = int(input()) if k %2 == 1: print("-1") else: ans = "" time = 0 while (k>0): n = int(math.log(k+2,2))-1 ans += "1 " k -= pow(2,n) n -= 1 time += 1 while(n>0): ans += "0 " k -= pow(2,n) n -= 1 time += 1 print(time) print(ans[:-1]) ```
instruction
0
7,782
19
15,564
No
output
1
7,782
19
15,565
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1
instruction
0
7,919
19
15,838
Tags: greedy Correct Solution: ``` from collections import defaultdict, deque from heapq import heappush, heappop from math import inf ri = lambda : map(int, input().split()) def sm(x): ans = 0 while x: ans += (x % 10) x //= 10 return ans def solve(): n = int(input()) A = list(ri()) ans = 0 cnt = defaultdict(int) for x in A: d = sm(x) if d % 3 == 0: if cnt[0] > 0: cnt[0] -= 1 ans += 1 else: cnt[0] += 1 elif cnt[3-(d%3)] > 0: cnt[3-(d%3)] -= 1 ans += 1 else: cnt[d%3] += 1 print(ans) t = 1 #t = int(input()) while t: t -= 1 solve() ```
output
1
7,919
19
15,839
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1
instruction
0
7,920
19
15,840
Tags: greedy Correct Solution: ``` d=[0,0,0];input() for i in map(int,input().split()):d[i%3]+=1 print(d[0]//2+min(d[1],d[2])) ```
output
1
7,920
19
15,841
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1
instruction
0
7,921
19
15,842
Tags: greedy Correct Solution: ``` a=input() b=map(int,input().split()) c=0 d=0 e=0 for i in b: if i%3==0: c+=1 elif i%3==1: d+=1 else: e+=1 print(min(d,e)+c//2) ```
output
1
7,921
19
15,843
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1
instruction
0
7,923
19
15,846
Tags: greedy Correct Solution: ``` n=int(input()) ar=list(map(lambda x:int(x)%3,input().split(' '))) print((ar.count(0)//2)+(min(ar.count(1),ar.count(2)))) ```
output
1
7,923
19
15,847
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1
instruction
0
7,924
19
15,848
Tags: greedy Correct Solution: ``` import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def input(): return sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- n=int(input()) a=list(map(int,input().split())) one=0 zero=0 two=0 for i in range(0,len(a)): if a[i]%3==0: zero+=1 if a[i]%3==1: one+=1 elif a[i]%3==2: two+=1 print(zero//2+min(one,two)) ```
output
1
7,924
19
15,849
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1
instruction
0
7,925
19
15,850
Tags: greedy Correct Solution: ``` import sys from array import array # noqa: F401 from collections import Counter def input(): return sys.stdin.buffer.readline().decode('utf-8') n = int(input()) cnt = Counter(x % 3 for x in map(int, input().split())) print(cnt[0] // 2 + min(cnt[1], cnt[2])) ```
output
1
7,925
19
15,851
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1
instruction
0
7,926
19
15,852
Tags: greedy Correct Solution: ``` s=int(input()) ar=[sum([int(x) for x in e])%3 for e in input().split()] x,y,z=ar.count(0),ar.count(1),ar.count(2) print(x//2+min(y,z)) ```
output
1
7,926
19
15,853
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1 Submitted Solution: ``` n=int(input()) arr=list(map(int,(input().split()))) a,b,c=0,0,0 for i in arr: if i%3==0: a+=1 elif i%3==1: b+=1 else: c+=1 print((a//2)+min(b,c)) ```
instruction
0
7,927
19
15,854
Yes
output
1
7,927
19
15,855
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1 Submitted Solution: ``` #from dust i have come dust i will be n=int(input()) a=list(map(int,input().split())) x=0 y=0 z=0 for i in range(n): if a[i]%3==0: x+=1 elif a[i]%3==1: y+=1 else: z+=1 t=(x//2)+min(y,z) print(t) ```
instruction
0
7,928
19
15,856
Yes
output
1
7,928
19
15,857
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1 Submitted Solution: ``` n = int(input()) ost = [0,0,0] for i in map(int, input().split()): ost[i%3] += 1 # print(ost) print(min(ost[1], ost[2])+(ost[0]//2)) ```
instruction
0
7,929
19
15,858
Yes
output
1
7,929
19
15,859
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1 Submitted Solution: ``` n = int(input()) l = [int(i) for i in input().split()] c1 = 0 c2 = 0 c3 = 0 ans = 0 for i in range(n): if (l[i]%3 == 0): c1 = c1 + 1 elif (l[i]%3 == 1): c2 = c2 + 1 else: c3 = c3 + 1 ans += c1//2 min1 = min(c2,c3) max1 = max(c2,c3) ans += min1 print (ans) ```
instruction
0
7,930
19
15,860
Yes
output
1
7,930
19
15,861
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1 Submitted Solution: ``` input() t = list(sum(int(i) for i in j) % 3 for j in input().split()) p = [0] * 3 for i in t: p[i] += 1 x = min(p[1], p[2]) y = p[1] + p[2] - x print(p[0] // 2 + x + y // 2) ```
instruction
0
7,931
19
15,862
No
output
1
7,931
19
15,863
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1 Submitted Solution: ``` a=input() b=map(int,input().split()) c=0 d=0 e=0 for i in b: print(i) if i%3==0: c+=1 elif i%3==1: d+=1 else: e+=1 print(min(d,e)+c//2) ```
instruction
0
7,932
19
15,864
No
output
1
7,932
19
15,865
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that? Input The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. Output Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. Examples Input 3 123 123 99 Output 1 Input 6 1 1 1 23 10 3 Output 1 Submitted Solution: ``` from collections import defaultdict, deque from heapq import heappush, heappop from math import inf ri = lambda : map(int, input().split()) def sm(x): ans = 0 while x: ans += (x % 10) x //= 10 return ans def solve(): n = int(input()) A = list(ri()) ans = 0 cnt = defaultdict(int) for x in A: d = sm(x) if d % 3 == 0: if cnt[0] > 1: cnt[0] -= 1 ans += 1 else: cnt[0] += 1 elif cnt[3-(d%3)] > 1: cnt[3-(d%3)] -= 1 ans += 1 else: cnt[d%3] += 1 print(ans) t = 1 #t = int(input()) while t: t -= 1 solve() ```
instruction
0
7,933
19
15,866
No
output
1
7,933
19
15,867
Provide a correct Python 3 solution for this coding contest problem. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996
instruction
0
8,200
19
16,400
"Correct Solution: ``` N,K = map(int,input().split()) R,S,P = map(int,input().split()) T = list(input()) d = {'r':P,'s':R,'p':S} ans = 0 for i,c in enumerate(T): if i < K or c != T[i-K]: ans += d[c] continue else: T[i] = '_' print(ans) ```
output
1
8,200
19
16,401
Provide a correct Python 3 solution for this coding contest problem. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996
instruction
0
8,201
19
16,402
"Correct Solution: ``` from itertools import groupby N, K = map(int, input().split()) R, S, P = map(int, input().split()) T = input() d = {"r": P, "s": R, "p": S} print(sum(sum(map(lambda p: d[p[0]]*((len(list(p[1]))+1)//2), groupby(T[k::K]))) for k in range(K))) ```
output
1
8,201
19
16,403
Provide a correct Python 3 solution for this coding contest problem. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996
instruction
0
8,202
19
16,404
"Correct Solution: ``` n, k = map(int, input().split()) r, s, p = map(int, input().split()) t = list(input()) dict = {"r":p, "s":r, "p":s} val = 0 for i in range(n): if i < k: val += dict[t[i]] elif t[i-k] != t[i]: val += dict[t[i]] else: t[i] = " " print(val) ```
output
1
8,202
19
16,405
Provide a correct Python 3 solution for this coding contest problem. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996
instruction
0
8,203
19
16,406
"Correct Solution: ``` n,k = map(int,input().split()) r,s,p = map(int,input().split()) t = list(input()) for i in range(k,n): if t[i]==t[i-k]: t[i]='' ans = t.count('r')*p+t.count('s')*r+t.count('p')*s print(ans) ```
output
1
8,203
19
16,407
Provide a correct Python 3 solution for this coding contest problem. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996
instruction
0
8,204
19
16,408
"Correct Solution: ``` n, k = map(int, input().split()) r, s, p = map(int, input().split()) t = list(input()) d = {'r': p, 's': r, 'p': s} point = 0 for i in range(k): point += d[t[i]] for i in range(k, n): if t[i] == t[i - k]: t[i] = 'x' continue point += d[t[i]] print(point) ```
output
1
8,204
19
16,409
Provide a correct Python 3 solution for this coding contest problem. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996
instruction
0
8,205
19
16,410
"Correct Solution: ``` n, k = map(int, input().split()) r, s, p = map(int, input().split()) d = {"r":p, "s":r, "p":s} t = list(input()) dp = [0]*n for i in range(n): if i>=k and t[i]==t[i-k]: dp[i] = 0 t[i] = "x" else: dp[i] = d[t[i]] print(sum(dp)) ```
output
1
8,205
19
16,411
Provide a correct Python 3 solution for this coding contest problem. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996
instruction
0
8,206
19
16,412
"Correct Solution: ``` N, K = map(int, input().split()) points = list(map(int, input().split())) hand = {'r': 0, 's': 1, 'p': 2} T = [hand[t] for t in input()] win = [False] * N ans = 0 for i in range(N): if i < K or T[i] != T[i-K] or not win[i-K]: win[i] = True ans += points[(T[i]+2) % 3] print(ans) ```
output
1
8,206
19
16,413
Provide a correct Python 3 solution for this coding contest problem. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996
instruction
0
8,207
19
16,414
"Correct Solution: ``` N,K=map(int,input().split()) R,S,P=map(int,input().split()) T=list(input()) for i in range(N-K): if T[K+i]==T[i]: T[K+i]="" ans=T.count("r")*P+T.count("s")*R+T.count("p")*S print(ans) ```
output
1
8,207
19
16,415
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996 Submitted Solution: ``` n,k=map(int,input().split()) r,s,p=map(int,input().split()) t=list(input()) cnt={'r':0,'s':0,'p':0} for i in range(n): if i<=k-1: cnt[t[i]]+=1 elif t[i]==t[i-k]: t[i]='o' continue else: cnt[t[i]]+=1 ans=p*cnt['r']+r*cnt['s']+s*cnt['p'] print(ans) ```
instruction
0
8,208
19
16,416
Yes
output
1
8,208
19
16,417
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996 Submitted Solution: ``` n, k = map(int, input().split()) r, s, p = map(int, input().split()) t = list(input()) score = {"r":p, "s":r, "p":s} ans = 0 for i in range(n): if i<k: ans += score[t[i]] else : if t[i] != t[i-k] : ans += score[t[i]] else : t[i]="x" print(ans) ```
instruction
0
8,209
19
16,418
Yes
output
1
8,209
19
16,419
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996 Submitted Solution: ``` n,k = map(int,input().split()) r,s,p = map(int,input().split()) t = list(input()) ans = 0 for i in range(k,n) : if t[i] == t[i-k] : t[i] = 'x' for i in range(n) : if t[i] == 'r' : ans += p elif t[i] == 's' : ans += r elif t[i] == 'p' : ans += s print(ans) ```
instruction
0
8,210
19
16,420
Yes
output
1
8,210
19
16,421
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996 Submitted Solution: ``` N,K=map(int,input().split()) R,S,P=map(int,input().split()) T=input() arr=[0]*N d={'r':P,'s':R,'p':S} for i,t in enumerate(T): if i-K>=0: if T[i-K]!=t: arr[i]=d[t] elif arr[i-K]==0: arr[i]=d[t] else: arr[i]=d[t] print(sum(arr)) ```
instruction
0
8,211
19
16,422
Yes
output
1
8,211
19
16,423
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996 Submitted Solution: ``` from collections import deque N, K = [int(x) for x in input().split()] r, s, p = [int(x) for x in input().split()] win_point = { 'r': r, 's': s, 'p': p, } next_hands = { 'r': ['s', 'p'], 's': ['r', 'p'], 'p': ['r', 's'], } enemy_hands = input() def can_win(enemy_hand, my_hands): # 勝てる場合にはTrueと勝てる手を教えてくれる if enemy_hand == 'r' and 'p' in my_hands: return True, 'p' if enemy_hand == 's' and 'r' in my_hands: return True, 'r' if enemy_hand == 'p' and 's' in my_hands: return True, 's' # 勝てる手がない場合 return False, None def choose_hand(enemy_hand, next_enemy_hand, now_hands): # 次の手に勝てるようにその手以外を選択する if next_enemy_hand == enemy_hand: # 次回も同じ手ならどっちでも良い return now_hands[0] win, hand = can_win(next_enemy_hand, enemy_hand) # 次の手と今の相手の手を戦わせる # 後の手が勝ったら今の手と同じものを出す # 今の手が勝ったら後の手と同じものと出す if win: return now_hands[enemy_hand] else: return now_hands[next_enemy_hand] point = 0 for index in range(K): target = index now_hands = ['r', 'p', 's'] for i in range(index, N, K): win, hand = can_win(enemy_hands[i], now_hands) if win: point += win_point[hand] now_hands = next_hands[hand] else: if i + K < N: next_enemy_hand = enemy_hands[i+K] now_hands = next_hands[choose_hand( enemy_hands[i], next_enemy_hand, now_hands)] print(point) ```
instruction
0
8,212
19
16,424
No
output
1
8,212
19
16,425
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996 Submitted Solution: ``` n, k = map(int, input().split()) r, s, p = map(int, input().split()) t = str(input()) ```
instruction
0
8,213
19
16,426
No
output
1
8,213
19
16,427
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996 Submitted Solution: ``` n, k = map(int, input().split()) R, S, P = map(int, input().split()) t = input() ans = 0 hand = [] for i in t: if i == "r": hand.append("p") if i == "s": hand.append("r") if i == "p": hand.append("s") for i, h in enumerate(t): if i >= k: if h == "r": if hand[i-k] != "p": ans += P else: hand[i] = "r" if h == "s": if hand[i-k] != "r": ans += R else: hand[i] = "s" if h == "p": if hand[i-k] != "s": ans += S else: hand[i] = "p" else: if h == "r": ans += P if h == "s": ans += R if h == "p": ans += S print(ans) ```
instruction
0
8,214
19
16,428
No
output
1
8,214
19
16,429
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows: * The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.) * Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss): * R points for winning with Rock; * S points for winning with Scissors; * P points for winning with Paper. * However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.) Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands. The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is `r`, the machine will play Rock in the i-th round. Similarly, `p` and `s` stand for Paper and Scissors, respectively. What is the maximum total score earned in the game by adequately choosing the hand to play in each round? Constraints * 2 \leq N \leq 10^5 * 1 \leq K \leq N-1 * 1 \leq R,S,P \leq 10^4 * N,K,R,S, and P are all integers. * |T| = N * T consists of `r`, `p`, and `s`. Input Input is given from Standard Input in the following format: N K R S P T Output Print the maximum total score earned in the game. Examples Input 5 2 8 7 6 rsrpr Output 27 Input 7 1 100 10 1 ssssppr Output 211 Input 30 5 325 234 123 rspsspspsrpspsppprpsprpssprpsr Output 4996 Submitted Solution: ``` n,k=map(int,input().split()) p,r,s=map(int,input().split()) mc=input() ans=0 for i in range(len(mc)): if i>=k and mc[i-k]!=mc[i]: if mc[i]=='r': ans+=r elif mc[i]=='p': ans+=p else: ans+=s ```
instruction
0
8,215
19
16,430
No
output
1
8,215
19
16,431
Provide a correct Python 3 solution for this coding contest problem. Consider the following game. There are k pairs of n cards with numbers from 1 to n written one by one. Shuffle these kn cards well to make piles of k cards and arrange them in a horizontal row. The i-th (k-card) pile from the left of the n piles created in this way is called "mountain i". <image> The game starts at mountain 1. Draw the top card of the pile (the drawn card is not returned to the original pile), and if the number written on that card is i, draw the top card of the pile i Pull. In this way, draw the top card of the pile numbered by the number written on the drawn card repeatedly, and if all the piles have no cards, it is successful. If there are still piles of cards left, but there are no more piles to draw next, it is a failure. If it fails in the middle, it ends with a failure, or the remaining card pile is left as it is (the pile number is also kept as it is) and the game is restarted. When restarting the game, the first card to draw is from the leftmost pile of the remaining piles (the top card of that pile is the first card to be drawn). After resuming, proceed with the game in the same way as before resuming, and if there are no cards in all the piles, it is a success. It is a failure. <image> Such a game shall be restarted up to m times. However, m is 0 or 1. In other words, it either does not restart once or restarts only once. The initial placement of cards differs depending on how you shuffle before the game starts. Of course, depending on the initial placement of the card, it may succeed without resuming, it may resume and succeed, or it may resume and fail. Since it is shuffled enough, we assume that all initial arrangements appear with the same probability, and we want to find the probability p that the restart will succeed within m times. Express this probability p as a decimal number, and create a program that finds and outputs to the decimal place r. However, output so that the following conditions are met. * If p × 10K becomes an integer when a sufficiently large positive integer K is taken, 0 continues from the middle of the decimal part, but that 0 should also be output. For example, if p = 3/8 = 0.375, then 0.37500 is output if r = 5, and 0.37 is output if r = 2. Similarly, when p = 1.0, for example, if r = 3, output 1.000. * For example, 0.150000 ... can be expressed as a recurring decimal 0.1499999 ..., but in such a case, the former expression is used. On the first line of the input file, the integers n, k, m, and r are written in this order with a blank as the delimiter. 1 ≤ n ≤ 10000, 1 ≤ k ≤ 100, m = 0 or m = 1, 1 ≤ r ≤ 10000. Input example 1 | Input example 2 | Input example 3 --- | --- | --- | 2 1 0 5 | 3 1 1 3 | 2 2 1 3 | Output example 1 | Output example 2 | Output example 3 0.50000 | 0.833 | 1.000 | input The input consists of multiple datasets. Input ends when n, k, m, and r are all 0. The number of datasets does not exceed 5. output For each dataset, p is output on one line as specified. Example Input 2 1 0 5 3 1 1 3 2 2 1 3 0 0 0 0 Output 0.50000 0.833 1.000
instruction
0
8,342
19
16,684
"Correct Solution: ``` # AOJ 0504: Card Game II # Python3 2018.7.1 bal4u from decimal import * while True: n, k, m, r = map(int, input().split()) if n == 0: break setcontext(Context(prec=r, rounding=ROUND_HALF_UP)) one = Decimal(1) ans = one/Decimal(n) if m == 1: s = 0 for i in range(1, n): s += one/Decimal(i) ans *= 1+s ans = str(ans)[:r+2] if len(ans) < r+2: ans += '0'*(r+2-len(ans)) print(ans) ```
output
1
8,342
19
16,685
Provide a correct Python 3 solution for this coding contest problem. Consider the following game. There are k pairs of n cards with numbers from 1 to n written one by one. Shuffle these kn cards well to make piles of k cards and arrange them in a horizontal row. The i-th (k-card) pile from the left of the n piles created in this way is called "mountain i". <image> The game starts at mountain 1. Draw the top card of the pile (the drawn card is not returned to the original pile), and if the number written on that card is i, draw the top card of the pile i Pull. In this way, draw the top card of the pile numbered by the number written on the drawn card repeatedly, and if all the piles have no cards, it is successful. If there are still piles of cards left, but there are no more piles to draw next, it is a failure. If it fails in the middle, it ends with a failure, or the remaining card pile is left as it is (the pile number is also kept as it is) and the game is restarted. When restarting the game, the first card to draw is from the leftmost pile of the remaining piles (the top card of that pile is the first card to be drawn). After resuming, proceed with the game in the same way as before resuming, and if there are no cards in all the piles, it is a success. It is a failure. <image> Such a game shall be restarted up to m times. However, m is 0 or 1. In other words, it either does not restart once or restarts only once. The initial placement of cards differs depending on how you shuffle before the game starts. Of course, depending on the initial placement of the card, it may succeed without resuming, it may resume and succeed, or it may resume and fail. Since it is shuffled enough, we assume that all initial arrangements appear with the same probability, and we want to find the probability p that the restart will succeed within m times. Express this probability p as a decimal number, and create a program that finds and outputs to the decimal place r. However, output so that the following conditions are met. * If p × 10K becomes an integer when a sufficiently large positive integer K is taken, 0 continues from the middle of the decimal part, but that 0 should also be output. For example, if p = 3/8 = 0.375, then 0.37500 is output if r = 5, and 0.37 is output if r = 2. Similarly, when p = 1.0, for example, if r = 3, output 1.000. * For example, 0.150000 ... can be expressed as a recurring decimal 0.1499999 ..., but in such a case, the former expression is used. On the first line of the input file, the integers n, k, m, and r are written in this order with a blank as the delimiter. 1 ≤ n ≤ 10000, 1 ≤ k ≤ 100, m = 0 or m = 1, 1 ≤ r ≤ 10000. Input example 1 | Input example 2 | Input example 3 --- | --- | --- | 2 1 0 5 | 3 1 1 3 | 2 2 1 3 | Output example 1 | Output example 2 | Output example 3 0.50000 | 0.833 | 1.000 | input The input consists of multiple datasets. Input ends when n, k, m, and r are all 0. The number of datasets does not exceed 5. output For each dataset, p is output on one line as specified. Example Input 2 1 0 5 3 1 1 3 2 2 1 3 0 0 0 0 Output 0.50000 0.833 1.000
instruction
0
8,343
19
16,686
"Correct Solution: ``` from decimal import Decimal, getcontext d1 = Decimal(1) while True: n, k, m, r = map(int, input().split()) if not n: break getcontext().prec = r + 1 ans = d1 / Decimal(n) if m: ans *= 1 + sum(d1 / Decimal(i) for i in range(1, n)) print('{{:.{}f}}'.format(r + 1).format(ans)[:-1]) ```
output
1
8,343
19
16,687
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Consider the following game. There are k pairs of n cards with numbers from 1 to n written one by one. Shuffle these kn cards well to make piles of k cards and arrange them in a horizontal row. The i-th (k-card) pile from the left of the n piles created in this way is called "mountain i". <image> The game starts at mountain 1. Draw the top card of the pile (the drawn card is not returned to the original pile), and if the number written on that card is i, draw the top card of the pile i Pull. In this way, draw the top card of the pile numbered by the number written on the drawn card repeatedly, and if all the piles have no cards, it is successful. If there are still piles of cards left, but there are no more piles to draw next, it is a failure. If it fails in the middle, it ends with a failure, or the remaining card pile is left as it is (the pile number is also kept as it is) and the game is restarted. When restarting the game, the first card to draw is from the leftmost pile of the remaining piles (the top card of that pile is the first card to be drawn). After resuming, proceed with the game in the same way as before resuming, and if there are no cards in all the piles, it is a success. It is a failure. <image> Such a game shall be restarted up to m times. However, m is 0 or 1. In other words, it either does not restart once or restarts only once. The initial placement of cards differs depending on how you shuffle before the game starts. Of course, depending on the initial placement of the card, it may succeed without resuming, it may resume and succeed, or it may resume and fail. Since it is shuffled enough, we assume that all initial arrangements appear with the same probability, and we want to find the probability p that the restart will succeed within m times. Express this probability p as a decimal number, and create a program that finds and outputs to the decimal place r. However, output so that the following conditions are met. * If p × 10K becomes an integer when a sufficiently large positive integer K is taken, 0 continues from the middle of the decimal part, but that 0 should also be output. For example, if p = 3/8 = 0.375, then 0.37500 is output if r = 5, and 0.37 is output if r = 2. Similarly, when p = 1.0, for example, if r = 3, output 1.000. * For example, 0.150000 ... can be expressed as a recurring decimal 0.1499999 ..., but in such a case, the former expression is used. On the first line of the input file, the integers n, k, m, and r are written in this order with a blank as the delimiter. 1 ≤ n ≤ 10000, 1 ≤ k ≤ 100, m = 0 or m = 1, 1 ≤ r ≤ 10000. Input example 1 | Input example 2 | Input example 3 --- | --- | --- | 2 1 0 5 | 3 1 1 3 | 2 2 1 3 | Output example 1 | Output example 2 | Output example 3 0.50000 | 0.833 | 1.000 | input The input consists of multiple datasets. Input ends when n, k, m, and r are all 0. The number of datasets does not exceed 5. output For each dataset, p is output on one line as specified. Example Input 2 1 0 5 3 1 1 3 2 2 1 3 0 0 0 0 Output 0.50000 0.833 1.000 Submitted Solution: ``` from decimal import Decimal, getcontext d1 = Decimal(1) while True: n, k, m, r = map(int, input().split()) if not n: break getcontext().prec = r + 1 ans = d1 / Decimal(n) if m: ans *= 1 + sum(d1 / Decimal(i) for i in range(1, n)) print('{{:.{}f}}'.format(r).format(ans)) ```
instruction
0
8,344
19
16,688
No
output
1
8,344
19
16,689
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Consider the following game. There are k pairs of n cards with numbers from 1 to n written one by one. Shuffle these kn cards well to make piles of k cards and arrange them in a horizontal row. The i-th (k-card) pile from the left of the n piles created in this way is called "mountain i". <image> The game starts at mountain 1. Draw the top card of the pile (the drawn card is not returned to the original pile), and if the number written on that card is i, draw the top card of the pile i Pull. In this way, draw the top card of the pile numbered by the number written on the drawn card repeatedly, and if all the piles have no cards, it is successful. If there are still piles of cards left, but there are no more piles to draw next, it is a failure. If it fails in the middle, it ends with a failure, or the remaining card pile is left as it is (the pile number is also kept as it is) and the game is restarted. When restarting the game, the first card to draw is from the leftmost pile of the remaining piles (the top card of that pile is the first card to be drawn). After resuming, proceed with the game in the same way as before resuming, and if there are no cards in all the piles, it is a success. It is a failure. <image> Such a game shall be restarted up to m times. However, m is 0 or 1. In other words, it either does not restart once or restarts only once. The initial placement of cards differs depending on how you shuffle before the game starts. Of course, depending on the initial placement of the card, it may succeed without resuming, it may resume and succeed, or it may resume and fail. Since it is shuffled enough, we assume that all initial arrangements appear with the same probability, and we want to find the probability p that the restart will succeed within m times. Express this probability p as a decimal number, and create a program that finds and outputs to the decimal place r. However, output so that the following conditions are met. * If p × 10K becomes an integer when a sufficiently large positive integer K is taken, 0 continues from the middle of the decimal part, but that 0 should also be output. For example, if p = 3/8 = 0.375, then 0.37500 is output if r = 5, and 0.37 is output if r = 2. Similarly, when p = 1.0, for example, if r = 3, output 1.000. * For example, 0.150000 ... can be expressed as a recurring decimal 0.1499999 ..., but in such a case, the former expression is used. On the first line of the input file, the integers n, k, m, and r are written in this order with a blank as the delimiter. 1 ≤ n ≤ 10000, 1 ≤ k ≤ 100, m = 0 or m = 1, 1 ≤ r ≤ 10000. Input example 1 | Input example 2 | Input example 3 --- | --- | --- | 2 1 0 5 | 3 1 1 3 | 2 2 1 3 | Output example 1 | Output example 2 | Output example 3 0.50000 | 0.833 | 1.000 | input The input consists of multiple datasets. Input ends when n, k, m, and r are all 0. The number of datasets does not exceed 5. output For each dataset, p is output on one line as specified. Example Input 2 1 0 5 3 1 1 3 2 2 1 3 0 0 0 0 Output 0.50000 0.833 1.000 Submitted Solution: ``` # AOJ 0504: Card Game II # Python3 2018.7.1 bal4u def calc(b, n): a = 1 for i in range(n+1): if a >= b: d[i] += a//b a %= b if a == 0: break a *= 10 ans = ['0']*10005 while True: n, k, m, r = map(int, input().split()) if n == 0: break r += 6 d = [0]*10005 calc(n, r) if m == 1: for i in range(1, n): calc(n*i, r) k = 0 for i in range(r, -1, -1): k += d[i] if k >= 10: ans[i] = str(k%10) k //= 10 else: ans[i] = str(k) k = 0 print(ans[0], '.', ''.join(ans[1:r-5]), sep='') ```
instruction
0
8,345
19
16,690
No
output
1
8,345
19
16,691
Provide tags and a correct Python 3 solution for this coding contest problem. Jeff has become friends with Furik. Now these two are going to play one quite amusing game. At the beginning of the game Jeff takes a piece of paper and writes down a permutation consisting of n numbers: p1, p2, ..., pn. Then the guys take turns to make moves, Jeff moves first. During his move, Jeff chooses two adjacent permutation elements and then the boy swaps them. During his move, Furic tosses a coin and if the coin shows "heads" he chooses a random pair of adjacent elements with indexes i and i + 1, for which an inequality pi > pi + 1 holds, and swaps them. But if the coin shows "tails", Furik chooses a random pair of adjacent elements with indexes i and i + 1, for which the inequality pi < pi + 1 holds, and swaps them. If the coin shows "heads" or "tails" and Furik has multiple ways of adjacent pairs to take, then he uniformly takes one of the pairs. If Furik doesn't have any pair to take, he tosses a coin one more time. The game ends when the permutation is sorted in the increasing order. Jeff wants the game to finish as quickly as possible (that is, he wants both players to make as few moves as possible). Help Jeff find the minimum mathematical expectation of the number of moves in the game if he moves optimally well. You can consider that the coin shows the heads (or tails) with the probability of 50 percent. Input The first line contains integer n (1 ≤ n ≤ 3000). The next line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation p. The numbers are separated by spaces. Output In a single line print a single real value — the answer to the problem. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6. Examples Input 2 1 2 Output 0.000000 Input 5 3 5 2 4 1 Output 13.000000 Note In the first test the sequence is already sorted, so the answer is 0.
instruction
0
8,629
19
17,258
Tags: combinatorics, dp, probabilities Correct Solution: ``` from sys import * n=int(stdin.readline().strip()) s1=stdin.readline().strip() a=list(map(int,s1.split())) if n==1: print('0') else: x=0 n0=1 while n0<n: n0=n0*2 b=[0]*(2*n0+10) '''for i in range (n-1): for j in range (i+1,n): if a[i]>a[j]: x+=1''' for i in range (n): t=a[i] u=0 k=1 j=1 while t>0: if (t>>j)<<j!=t: u=u+b[(n0+t-1)>>(j-1)] t=t-k k=k<<1 j=j+1 x=x+u j=n0+a[i]-1 while j>0: b[j]+=1 j=j>>1 x=((n*(n-1))//2)-x '''n=x//2 print(x,n,' !!!') r=x i=1 bi=n eps=0.0000001 if x>0: while (x+2*i)*bi*((0.5)**i)>eps: r=r+(x+2*i)*bi*((0.5)**i) #print(r) bi=(bi*(n+i))//(i+1) i=i+1 #print(bi,i) else: r=0 r=r*((0.5)**n) print("%.7f"%r)''' if x%2 ==1: print(2*x-1) else: print(2*x) ```
output
1
8,629
19
17,259
Provide tags and a correct Python 3 solution for this coding contest problem. Jeff has become friends with Furik. Now these two are going to play one quite amusing game. At the beginning of the game Jeff takes a piece of paper and writes down a permutation consisting of n numbers: p1, p2, ..., pn. Then the guys take turns to make moves, Jeff moves first. During his move, Jeff chooses two adjacent permutation elements and then the boy swaps them. During his move, Furic tosses a coin and if the coin shows "heads" he chooses a random pair of adjacent elements with indexes i and i + 1, for which an inequality pi > pi + 1 holds, and swaps them. But if the coin shows "tails", Furik chooses a random pair of adjacent elements with indexes i and i + 1, for which the inequality pi < pi + 1 holds, and swaps them. If the coin shows "heads" or "tails" and Furik has multiple ways of adjacent pairs to take, then he uniformly takes one of the pairs. If Furik doesn't have any pair to take, he tosses a coin one more time. The game ends when the permutation is sorted in the increasing order. Jeff wants the game to finish as quickly as possible (that is, he wants both players to make as few moves as possible). Help Jeff find the minimum mathematical expectation of the number of moves in the game if he moves optimally well. You can consider that the coin shows the heads (or tails) with the probability of 50 percent. Input The first line contains integer n (1 ≤ n ≤ 3000). The next line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation p. The numbers are separated by spaces. Output In a single line print a single real value — the answer to the problem. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6. Examples Input 2 1 2 Output 0.000000 Input 5 3 5 2 4 1 Output 13.000000 Note In the first test the sequence is already sorted, so the answer is 0.
instruction
0
8,630
19
17,260
Tags: combinatorics, dp, probabilities Correct Solution: ``` n=int(input().strip()) p=[0]+list(map(int,input().split())) c=[0]*(n+1) def lowbit(x): return x&(-x) def add(x,v): while x<=n: c[x]+=v x+=lowbit(x) def get(x): ans=0 while x: ans+=c[x] x-=lowbit(x) return ans ans=0 for i in range(n,0,-1): ans+=get(p[i]) add(p[i],1) if ans%2: print(2*ans-1) else: print(2*ans) ```
output
1
8,630
19
17,261
Provide tags and a correct Python 3 solution for this coding contest problem. Jeff has become friends with Furik. Now these two are going to play one quite amusing game. At the beginning of the game Jeff takes a piece of paper and writes down a permutation consisting of n numbers: p1, p2, ..., pn. Then the guys take turns to make moves, Jeff moves first. During his move, Jeff chooses two adjacent permutation elements and then the boy swaps them. During his move, Furic tosses a coin and if the coin shows "heads" he chooses a random pair of adjacent elements with indexes i and i + 1, for which an inequality pi > pi + 1 holds, and swaps them. But if the coin shows "tails", Furik chooses a random pair of adjacent elements with indexes i and i + 1, for which the inequality pi < pi + 1 holds, and swaps them. If the coin shows "heads" or "tails" and Furik has multiple ways of adjacent pairs to take, then he uniformly takes one of the pairs. If Furik doesn't have any pair to take, he tosses a coin one more time. The game ends when the permutation is sorted in the increasing order. Jeff wants the game to finish as quickly as possible (that is, he wants both players to make as few moves as possible). Help Jeff find the minimum mathematical expectation of the number of moves in the game if he moves optimally well. You can consider that the coin shows the heads (or tails) with the probability of 50 percent. Input The first line contains integer n (1 ≤ n ≤ 3000). The next line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation p. The numbers are separated by spaces. Output In a single line print a single real value — the answer to the problem. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6. Examples Input 2 1 2 Output 0.000000 Input 5 3 5 2 4 1 Output 13.000000 Note In the first test the sequence is already sorted, so the answer is 0.
instruction
0
8,631
19
17,262
Tags: combinatorics, dp, probabilities Correct Solution: ``` n=int(input()) b=list(map(int,input().split())) cnt=0 for i in range(n): j=i-1 while(j>=0): if b[j]>b[i]: cnt+=1 j+=-1 i+=-1 print(2*cnt-cnt%2) ```
output
1
8,631
19
17,263
Provide tags and a correct Python 3 solution for this coding contest problem. Jeff has become friends with Furik. Now these two are going to play one quite amusing game. At the beginning of the game Jeff takes a piece of paper and writes down a permutation consisting of n numbers: p1, p2, ..., pn. Then the guys take turns to make moves, Jeff moves first. During his move, Jeff chooses two adjacent permutation elements and then the boy swaps them. During his move, Furic tosses a coin and if the coin shows "heads" he chooses a random pair of adjacent elements with indexes i and i + 1, for which an inequality pi > pi + 1 holds, and swaps them. But if the coin shows "tails", Furik chooses a random pair of adjacent elements with indexes i and i + 1, for which the inequality pi < pi + 1 holds, and swaps them. If the coin shows "heads" or "tails" and Furik has multiple ways of adjacent pairs to take, then he uniformly takes one of the pairs. If Furik doesn't have any pair to take, he tosses a coin one more time. The game ends when the permutation is sorted in the increasing order. Jeff wants the game to finish as quickly as possible (that is, he wants both players to make as few moves as possible). Help Jeff find the minimum mathematical expectation of the number of moves in the game if he moves optimally well. You can consider that the coin shows the heads (or tails) with the probability of 50 percent. Input The first line contains integer n (1 ≤ n ≤ 3000). The next line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation p. The numbers are separated by spaces. Output In a single line print a single real value — the answer to the problem. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6. Examples Input 2 1 2 Output 0.000000 Input 5 3 5 2 4 1 Output 13.000000 Note In the first test the sequence is already sorted, so the answer is 0.
instruction
0
8,632
19
17,264
Tags: combinatorics, dp, probabilities Correct Solution: ``` #!/usr/bin/python3 import sys class CumTree: def __init__(self, a, b): self.a = a self.b = b self.count = 0 if a == b: return mid = (a + b) // 2 self.levo = CumTree(a, mid) self.desno = CumTree(mid+1, b) def manjsi(self, t): if self.a >= t: return 0 if self.b < t: return self.count return self.levo.manjsi(t) + self.desno.manjsi(t) def vstavi(self, t): if self.a <= t <= self.b: self.count += 1 if self.a == self.b: return self.levo.vstavi(t) self.desno.vstavi(t) n = int(sys.stdin.readline()) p = [int(x) for x in sys.stdin.readline().strip().split()] ct = CumTree(1, 4096) vsota = 0 while len(p) > 0: x = p.pop() vsota += ct.manjsi(x) ct.vstavi(x) k, d = vsota // 2, vsota % 2 print("%f" % (4*k + d)) ```
output
1
8,632
19
17,265
Provide tags and a correct Python 3 solution for this coding contest problem. Jeff has become friends with Furik. Now these two are going to play one quite amusing game. At the beginning of the game Jeff takes a piece of paper and writes down a permutation consisting of n numbers: p1, p2, ..., pn. Then the guys take turns to make moves, Jeff moves first. During his move, Jeff chooses two adjacent permutation elements and then the boy swaps them. During his move, Furic tosses a coin and if the coin shows "heads" he chooses a random pair of adjacent elements with indexes i and i + 1, for which an inequality pi > pi + 1 holds, and swaps them. But if the coin shows "tails", Furik chooses a random pair of adjacent elements with indexes i and i + 1, for which the inequality pi < pi + 1 holds, and swaps them. If the coin shows "heads" or "tails" and Furik has multiple ways of adjacent pairs to take, then he uniformly takes one of the pairs. If Furik doesn't have any pair to take, he tosses a coin one more time. The game ends when the permutation is sorted in the increasing order. Jeff wants the game to finish as quickly as possible (that is, he wants both players to make as few moves as possible). Help Jeff find the minimum mathematical expectation of the number of moves in the game if he moves optimally well. You can consider that the coin shows the heads (or tails) with the probability of 50 percent. Input The first line contains integer n (1 ≤ n ≤ 3000). The next line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation p. The numbers are separated by spaces. Output In a single line print a single real value — the answer to the problem. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6. Examples Input 2 1 2 Output 0.000000 Input 5 3 5 2 4 1 Output 13.000000 Note In the first test the sequence is already sorted, so the answer is 0.
instruction
0
8,633
19
17,266
Tags: combinatorics, dp, probabilities Correct Solution: ``` def count(a): n = len(a) cnt = 0 for i in range(n): for j in range(i+1, n): if a[i] > a[j]: cnt+=1 return cnt n = int(input()) p = list(map(int, input().split())) num = count(p) print(num*2 - num%2) ```
output
1
8,633
19
17,267