message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Provide a correct Python 3 solution for this coding contest problem. There are N arrays. The length of each array is M and initially each array contains integers (1,2,...,M) in this order. Mr. Takahashi has decided to perform Q operations on those N arrays. For the i-th (1≤i≤Q) time, he performs the following operation. * Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5,4,3,2,1), this array becomes (2,5,4,3,1). Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations. Determine if it is possible or not. Constraints * 2≤N≤10^5 * 2≤M≤10^5 * 1≤Q≤10^5 * 1≤a_i≤M Input The input is given from Standard Input in the following format: N M Q a_1 a_2 ... a_Q Output Print `Yes` if it is possible to make N arrays exactly the same after performing the Q operations. Otherwise, print `No`. Examples Input 2 2 3 2 1 2 Output Yes Input 3 2 3 2 1 2 Output No Input 2 3 3 3 2 1 Output Yes Input 3 3 6 1 2 2 3 3 3 Output No
instruction
0
75,774
12
151,548
"Correct Solution: ``` def reads(offset = 0): return [int(i) - offset for i in input().split(' ')] def Judge(vector): length = len(vector)-1 for i in range(length): if(vector[i] > vector[i+1]): return 0 return 1 (N, M) = reads() Q = int(input()) A = reads(1) pos = [-1] * M pat = [] freq = [0] * (M+1) freq[0] = N found = set() counter = 0 for i in A[::-1]: if (i not in found): pat.append(i) found.add(i) pos[i] = counter freq[counter] -= 1 counter += 1 freq[counter] += 1 elif (freq[pos[i]] > 0): freq[pos[i]] -= 1 freq[pos[i]+1] += 1 for i in range(M): if (pos[i] == -1): pat.append(i) for i in range(M+1): if (freq[i] != 0): start = i break print("Yes") if Judge(pat[start:]) else print("No") ```
output
1
75,774
12
151,549
Provide a correct Python 3 solution for this coding contest problem. There are N arrays. The length of each array is M and initially each array contains integers (1,2,...,M) in this order. Mr. Takahashi has decided to perform Q operations on those N arrays. For the i-th (1≤i≤Q) time, he performs the following operation. * Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5,4,3,2,1), this array becomes (2,5,4,3,1). Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations. Determine if it is possible or not. Constraints * 2≤N≤10^5 * 2≤M≤10^5 * 1≤Q≤10^5 * 1≤a_i≤M Input The input is given from Standard Input in the following format: N M Q a_1 a_2 ... a_Q Output Print `Yes` if it is possible to make N arrays exactly the same after performing the Q operations. Otherwise, print `No`. Examples Input 2 2 3 2 1 2 Output Yes Input 3 2 3 2 1 2 Output No Input 2 3 3 3 2 1 Output Yes Input 3 3 6 1 2 2 3 3 3 Output No
instruction
0
75,775
12
151,550
"Correct Solution: ``` import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) N,M = map(int,input().split()) Q = int(input()) A = [int(x) for x in input().split()] used = [False] * (M+1) arr = [] for x in A[::-1]: if used[x]: continue used[x] = True arr.append(x) for x in range(1,M+1): if used[x]: continue arr.append(x) x_to_i = {x:i for i,x in enumerate(arr,1)} made = [N] + [0] * M for x in A[::-1]: i = x_to_i[x] if made[i-1] > 0: made[i-1] -= 1 made[i] += 1 L = 0 prev = M+100 for x in arr[::-1]: if x >= prev: break prev = x L += 1 x = sum(made[::-1][:L+1]) answer = 'Yes' if x == N else 'No' print(answer) ```
output
1
75,775
12
151,551
Provide a correct Python 3 solution for this coding contest problem. There are N arrays. The length of each array is M and initially each array contains integers (1,2,...,M) in this order. Mr. Takahashi has decided to perform Q operations on those N arrays. For the i-th (1≤i≤Q) time, he performs the following operation. * Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5,4,3,2,1), this array becomes (2,5,4,3,1). Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations. Determine if it is possible or not. Constraints * 2≤N≤10^5 * 2≤M≤10^5 * 1≤Q≤10^5 * 1≤a_i≤M Input The input is given from Standard Input in the following format: N M Q a_1 a_2 ... a_Q Output Print `Yes` if it is possible to make N arrays exactly the same after performing the Q operations. Otherwise, print `No`. Examples Input 2 2 3 2 1 2 Output Yes Input 3 2 3 2 1 2 Output No Input 2 3 3 3 2 1 Output Yes Input 3 3 6 1 2 2 3 3 3 Output No
instruction
0
75,776
12
151,552
"Correct Solution: ``` import sys readline = sys.stdin.readline import sys from itertools import product readline = sys.stdin.readline def check1(A): K = max(A) table = [-1]*(K+1) for i in range(Q): table[A[i]] = i if -1 in table: return False if any(t1-t2 < 0 for t1, t2 in zip(table, table[1:])): return False return True INF = 10**9+7 def check2(A): stack = [] used = set() for a in A[::-1] + list(range(M)): if a not in used: used.add(a) stack.append(a) C = dict() for i in range(M): C[stack[i]] = i table = [0]*M + [INF] for a in A[::-1]: if table[C[a]] < table[C[a]-1]: table[C[a]] += 1 used = set() ch = [] for i in range(M): if table[i] < N: break ch.append(stack[i]) used.add(stack[i]) for i in range(M): if i not in used: used.add(i) ch.append(i) return stack == ch N, M = map(int, readline().split()) Q = int(readline()) A = list(map(lambda x: int(x)-1, readline().split())) ans = 'Yes' if not check1(A): if not check2(A): ans = 'No' print(ans) ```
output
1
75,776
12
151,553
Provide a correct Python 3 solution for this coding contest problem. There are N arrays. The length of each array is M and initially each array contains integers (1,2,...,M) in this order. Mr. Takahashi has decided to perform Q operations on those N arrays. For the i-th (1≤i≤Q) time, he performs the following operation. * Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5,4,3,2,1), this array becomes (2,5,4,3,1). Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations. Determine if it is possible or not. Constraints * 2≤N≤10^5 * 2≤M≤10^5 * 1≤Q≤10^5 * 1≤a_i≤M Input The input is given from Standard Input in the following format: N M Q a_1 a_2 ... a_Q Output Print `Yes` if it is possible to make N arrays exactly the same after performing the Q operations. Otherwise, print `No`. Examples Input 2 2 3 2 1 2 Output Yes Input 3 2 3 2 1 2 Output No Input 2 3 3 3 2 1 Output Yes Input 3 3 6 1 2 2 3 3 3 Output No
instruction
0
75,777
12
151,554
"Correct Solution: ``` def reads(offset = 0): return [int(i) - offset for i in input().split(' ')] def Judge(vector): length = len(vector)-1 for i in range(length): if(vector[i] > vector[i+1]): return 0 return 1 (N, M) = reads() Q = int(input()) A = reads(1) pos = [-1] * M pat = [] freq = [0] * (M+1) freq[0] = N found = set() counter = 0 for i in A[::-1]: if (i not in found): pat.append(i) found.add(i) pos[i] = counter counter += 1 for i in range(M): if (pos[i] == -1): pat.append(i) for i in A[::-1]: temp = pos[i] if (freq[temp] > 0): freq[temp] -= 1 freq[temp+1] += 1 for i in range(M+1): if (freq[i] != 0): start = i break print("Yes") if Judge(pat[start:]) else print("No") ```
output
1
75,777
12
151,555
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N arrays. The length of each array is M and initially each array contains integers (1,2,...,M) in this order. Mr. Takahashi has decided to perform Q operations on those N arrays. For the i-th (1≤i≤Q) time, he performs the following operation. * Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5,4,3,2,1), this array becomes (2,5,4,3,1). Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations. Determine if it is possible or not. Constraints * 2≤N≤10^5 * 2≤M≤10^5 * 1≤Q≤10^5 * 1≤a_i≤M Input The input is given from Standard Input in the following format: N M Q a_1 a_2 ... a_Q Output Print `Yes` if it is possible to make N arrays exactly the same after performing the Q operations. Otherwise, print `No`. Examples Input 2 2 3 2 1 2 Output Yes Input 3 2 3 2 1 2 Output No Input 2 3 3 3 2 1 Output Yes Input 3 3 6 1 2 2 3 3 3 Output No Submitted Solution: ``` def reads(offset = 0): return [int(i) - offset for i in input().split(' ')] def Judge(vector): length = len(vector)-1 for i in range(length): if(vector[i] > vector[i+1]): return 0 return 1 (N, M) = reads() Q = int(input()) A = reads(1) pos = [-1] * M pat = [] freq = [0] * (M+1) freq[0] = N found = set() for i in A[::-1]: if (i not in found): pat.append(i) found.add(i) pos[i] = counter for i in range(M): if (pos[i] == -1): pat.append(i) for i in A[::-1]: temp = pos[i] if (freq[temp] > 0): freq[temp] -= 1 freq[temp+1] += 1 for i in range(M+1): if (freq[i] != 0): start = i break print("Yes") if Judge(pat[start:]) else print("No") ```
instruction
0
75,778
12
151,556
No
output
1
75,778
12
151,557
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N arrays. The length of each array is M and initially each array contains integers (1,2,...,M) in this order. Mr. Takahashi has decided to perform Q operations on those N arrays. For the i-th (1≤i≤Q) time, he performs the following operation. * Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5,4,3,2,1), this array becomes (2,5,4,3,1). Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations. Determine if it is possible or not. Constraints * 2≤N≤10^5 * 2≤M≤10^5 * 1≤Q≤10^5 * 1≤a_i≤M Input The input is given from Standard Input in the following format: N M Q a_1 a_2 ... a_Q Output Print `Yes` if it is possible to make N arrays exactly the same after performing the Q operations. Otherwise, print `No`. Examples Input 2 2 3 2 1 2 Output Yes Input 3 2 3 2 1 2 Output No Input 2 3 3 3 2 1 Output Yes Input 3 3 6 1 2 2 3 3 3 Output No Submitted Solution: ``` printd = print printd= lambda *args:0 #print N, M = [int(x) for x in input().split()] Q = int(input()) a = [int(x) - 1 for x in input().split()] target = [] for x in list(reversed(a)) + list(range(M)): if x not in target: target.append(x) printd(target) assert len(target) == M target_length =M-1 for i in range(len(target) - 1, 0, -1): if target[i] > target[i-1]: target_length-=1 else: break printd(target_length) can_trash = set() b = [0 for _ in range(N)] for x in reversed(a): _flag =False printd('x:',x) for i, value in enumerate(b): printd(i, value, x, b) if value < M and target[value] == x: b[i] = value + 1 printd('value add ',b) can_trash.add(x) _flag = True break if _flag: continue if x == 0: if any(val == len(target) for val in b): can_trash.add(0) if x in can_trash: continue else: print('No') exit() if all(x >= target_length for x in b): print('Yes') else: print("No") ```
instruction
0
75,779
12
151,558
No
output
1
75,779
12
151,559
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N arrays. The length of each array is M and initially each array contains integers (1,2,...,M) in this order. Mr. Takahashi has decided to perform Q operations on those N arrays. For the i-th (1≤i≤Q) time, he performs the following operation. * Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5,4,3,2,1), this array becomes (2,5,4,3,1). Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations. Determine if it is possible or not. Constraints * 2≤N≤10^5 * 2≤M≤10^5 * 1≤Q≤10^5 * 1≤a_i≤M Input The input is given from Standard Input in the following format: N M Q a_1 a_2 ... a_Q Output Print `Yes` if it is possible to make N arrays exactly the same after performing the Q operations. Otherwise, print `No`. Examples Input 2 2 3 2 1 2 Output Yes Input 3 2 3 2 1 2 Output No Input 2 3 3 3 2 1 Output Yes Input 3 3 6 1 2 2 3 3 3 Output No Submitted Solution: ``` """ Writer: SPD_9X2 https://atcoder.jp/contests/code-festival-2016-quala/tasks/codefestival_2016_qualA_e 最後の配列の状態は何で決定されるか。 同じaが同じ配列に複数回採用されたとき、効果があるのは最後に 採用したaのみ。(それ以前は合っても無くても同じになる) 最初、1が採用状態になっていると考えてよい。 →おそらく気にすべきは配列aのみ(Nこの配列は考慮しなくてよい) 1以外は、出現するなら少なくともN個出現しなければ不可能 2,3だけで考えてみよう N==3のとき 322233 →不可能(2が先頭が1つ生まれてしまう) 232233 →可能 →終了後の配列の数字は、最後に出現した順に前から並ぶ →よってaを見れば構成後の数列は分かる →後ろからaを見ていって、先に登場したやつより後に登場したほうが数が越えたらダメ →ただし、N個目以降は完全無視する →終了時に数がNに達していないのがあったらダメ(1を除く?) いや例3… →初期状態、の扱い方を考え直さなきゃな… aの前に M…M(N個),M-1…M-1,…,1…1 があると考える すると、aを後ろから見ていった時に先に登場したやつより後に登場したほうが数が越えてはいけない、 だけの条件になる!! """ import sys N,M = map(int,input().split()) Q = int(input()) A = list(map(int,input().split())) dic = {} lis = [] flag = True a = [[M-i,N] for i in range(M)] for i in range(Q): a.append([A[i],1]) a.reverse() for i in range(len(a)): if a[i][0] not in dic: ind = len(dic) dic[a[i][0]] = ind lis.append(a[i][1]) else: ind = dic[a[i][0]] lis[ind] = min(N,lis[ind] + a[i][1]) if ind != 0 and lis[ind] > lis[ind-1]: print ("No") sys.exit() print ("Yes") ```
instruction
0
75,780
12
151,560
No
output
1
75,780
12
151,561
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N arrays. The length of each array is M and initially each array contains integers (1,2,...,M) in this order. Mr. Takahashi has decided to perform Q operations on those N arrays. For the i-th (1≤i≤Q) time, he performs the following operation. * Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5,4,3,2,1), this array becomes (2,5,4,3,1). Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations. Determine if it is possible or not. Constraints * 2≤N≤10^5 * 2≤M≤10^5 * 1≤Q≤10^5 * 1≤a_i≤M Input The input is given from Standard Input in the following format: N M Q a_1 a_2 ... a_Q Output Print `Yes` if it is possible to make N arrays exactly the same after performing the Q operations. Otherwise, print `No`. Examples Input 2 2 3 2 1 2 Output Yes Input 3 2 3 2 1 2 Output No Input 2 3 3 3 2 1 Output Yes Input 3 3 6 1 2 2 3 3 3 Output No Submitted Solution: ``` from collections import defaultdict def ReadInput(): return [int(a) for a in input().split(' ')] (N, M) = ReadInput() Q = int(input()) Array = ReadInput() def ifEqual(x): temp = None for i in x: if (temp == None): temp = x[i] continue elif (x[i] != temp): print('ifEqual is 0') return 0 return temp def check(Array, N, M, *args): Count = defaultdict(int) Array.reverse() if ("reverse") not in args: for i in Array: if (i == 1 or Count[i] < Count[i-1]): Count[i] += 1 else: return 0 Count.pop(N) if(ifEqual(Count)): return 1 else: return 0 else: for i in Array: if (i == N or Count[i] < Count[i+1]): Count[i] += 1 else: return 0 Count.pop(1) if(ifEqual(Count) >= M): return 1 else return 0 if (check(Array, N, M) or check(Array, N, M, "reverse")): print("Yes") else: print("No") ```
instruction
0
75,781
12
151,562
No
output
1
75,781
12
151,563
Provide tags and a correct Python 3 solution for this coding contest problem. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
instruction
0
75,921
12
151,842
Tags: binary search, greedy Correct Solution: ``` def main(): n, m = map(int, input().split()) a = tuple(map(int, input().split())) l_, r = -1, m - 1 while l_ + 1 < r: m_ = (l_ + r) // 2 ai1 = 0 for ai in a: if (m + ai1 - ai) % m > m_: if ai < ai1: l_ = m_ break ai1 = ai else: r = m_ print(r) main() ```
output
1
75,921
12
151,843
Provide tags and a correct Python 3 solution for this coding contest problem. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
instruction
0
75,922
12
151,844
Tags: binary search, greedy Correct Solution: ``` #First we notice that the number of overall operations is equal to the maximum number #of operations used on a single index.Next we notice that, if we can solve the problem #in <= X operations, then we can also solve it in <= X + 1 operations. #This is a monotonic sequence, so we can solve for the minimum value of X with binary search. #we have to check wether each element can be changed into #its previous element(count) in the list using a specified number of operations #if its not possible then we have to check wether current element is greater #than the previosu element , if it is so, then we have no need to change #as the list is already in non decreaing form, then we simply replace #count with the current element #if it is lesser than count, then there is no way to make the element #greater than or equal to the previous element using specified number of #operations and we return to the binary search part to get the next no. of #operations def checker(ij): count=0 for i in range(len(l)): #print(count) #print(((count-l[i])%m),ij) if ((count-l[i])%m)>=ij: if l[i]>count: count=l[i] else: return False #print(l) return True n,m=input().split() n,m=[int(n),int(m)] l=[int(i) for i in input().split()] beg=-1 last=m while beg<last-1: mid=(beg+last)//2 counter=checker(mid) #print(beg,last) if counter: last=mid else: beg=mid print(beg) ```
output
1
75,922
12
151,845
Provide tags and a correct Python 3 solution for this coding contest problem. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
instruction
0
75,923
12
151,846
Tags: binary search, greedy Correct Solution: ``` import sys N, M = map(int, input().split(' ')) A = list(map(int, sys.stdin.readline().split(' '))) def check() : last = 0 for i in range(N) : if A[i] < last : if last - A[i] > m : break elif A[i] > last : if A[i] + m < M or (A[i] + m) % M < last : last = A[i] else : return True return False l = -1 r = M-1 while(l + 1 < r) : m = (l+r) // 2 if check() : r = m else : l = m print(r) ```
output
1
75,923
12
151,847
Provide tags and a correct Python 3 solution for this coding contest problem. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
instruction
0
75,924
12
151,848
Tags: binary search, greedy Correct Solution: ``` n , k = map(int , input().split()) A = list(map(int , input().split())) l = 0 r = k + 1 ans = k + 1 for _ in range(20): B = [0] * n m = (l + r)//2 min1 = 0 fl = 0 #print(m) for i in range(n): if A[i] > min1 : if k - A[i] + min1 <= m: B[i] = min1 else: min1 = A[i] B[i] = A[i] else: if min1 - A[i] > m: fl = 1 break else: B[i] = min1 if fl == 0: #print(B) for i in range( n -1 ): if B[i] > B[i + 1]: break fl = 1 if fl == 0: r = m ans = min(ans , m) else: l = m else: l = m print(ans) ```
output
1
75,924
12
151,849
Provide tags and a correct Python 3 solution for this coding contest problem. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
instruction
0
75,925
12
151,850
Tags: binary search, greedy Correct Solution: ``` import heapq import sys import bisect from collections import defaultdict from itertools import product n, m = list(map(int, input().split())) arr = list(map(int, input().split())) l, r = -1, m def check_v(v): M = 0 for el in arr: if el <= M: if el + v >= M: continue else: return False else: if el + v >= M + m: continue else: M = el return True while r - l > 1: mid = (l + r) // 2 if check_v(mid): r = mid else: l = mid print(r) ```
output
1
75,925
12
151,851
Provide tags and a correct Python 3 solution for this coding contest problem. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
instruction
0
75,926
12
151,852
Tags: binary search, greedy Correct Solution: ``` import copy n, m = map(int, input().split()) a = list(map(int, input().split())) l = -1 r = 300001 def check(x): success = True last = 0 if (a[0] + x >= m) else a[0] for i in range(1, n): now = a[i] if (now < last): now = min(last, now + x) if (a[i] + x >= m + last): now = last if (now < last): return False last = now return True while (r - l > 1): mid = (l + r) // 2 if (check(mid)): r = mid else: l = mid print(r) ```
output
1
75,926
12
151,853
Provide tags and a correct Python 3 solution for this coding contest problem. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
instruction
0
75,927
12
151,854
Tags: binary search, greedy Correct Solution: ``` n,m = map(int,input().split()) a = list(map(int,input().split())) def Check(x): y = 0 for i in range(n): if a[i] <= y and a[i]+x >= y: continue elif a[i]+x >= m and a[i]+x-m>=y: continue elif a[i]+x < y : return False else: y = a[i] return True ng = -1 ok = 10**9 mid=0 while(abs(ok-ng)>1): mid = (ok+ng)//2 if Check(mid): ok = mid else: ng = mid print(ok) ```
output
1
75,927
12
151,855
Provide tags and a correct Python 3 solution for this coding contest problem. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
instruction
0
75,928
12
151,856
Tags: binary search, greedy Correct Solution: ``` import sys,math def read_int(): return int(sys.stdin.readline().strip()) def read_int_list(): return list(map(int,sys.stdin.readline().strip().split())) def read_string(): return sys.stdin.readline().strip() def read_string_list(delim=" "): return sys.stdin.readline().strip().split(delim) ###### Author : Samir Vyas ####### ###### Write Code Below ####### n,m = read_int_list() arr = read_int_list() def is_ok(maxi): global arr prev = 0 for i in range(len(arr)): if prev <= arr[i] : k = prev+m-arr[i] #if no ops is more than maxi then make it prev if k > maxi: prev = arr[i] else: k = prev-arr[i] if k > maxi: return 0 return 1 l,r = 0,m while l<=r: mid = l+((r-l)//2) if is_ok(mid): r = mid-1 else: l = mid+1 if is_ok(l): print(l) else: print(r) ```
output
1
75,928
12
151,857
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1. Submitted Solution: ``` from sys import stdin ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) si = lambda: input() msi = lambda: map(int, stdin.readline().split()) lsi = lambda: list(msi()) n,m=li() a=li() l,r=0,m while(l<r): M=(l+r)//2 ai1=0 for ai in a: if (m-ai+ai1)%m>M: if ai<ai1: l=M+1 break ai1=ai else: r=M print(l) ```
instruction
0
75,929
12
151,858
Yes
output
1
75,929
12
151,859
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1. Submitted Solution: ``` # by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase def check(mid,a,n,m): maxi = m-1 for i in range(n-1,-1,-1): if a[i] > maxi: r = a[i]+mid if r >= m: r -= m else: return 0 maxi = min(maxi,r) else: maxi = min(maxi,a[i]+mid) return 1 def main(): n,m = map(int,input().split()) a = list(map(int,input().split())) hi,lo,ans = m,0,m while hi >= lo: mid = (hi+lo)//2 if check(mid,a,n,m): hi = mid-1 ans = mid else: lo = mid+1 print(ans) # Fast IO Region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ```
instruction
0
75,930
12
151,860
Yes
output
1
75,930
12
151,861
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1. Submitted Solution: ``` # AC import sys class Main: def __init__(self): self.buff = None self.index = 0 def next(self): if self.buff is None or self.index == len(self.buff): self.buff = self.next_line() self.index = 0 val = self.buff[self.index] self.index += 1 return val def next_line(self): return sys.stdin.readline().split() def next_ints(self): return [int(x) for x in sys.stdin.readline().split()] def next_int(self): return int(self.next()) def solve(self): n, m = self.next_ints() x = self.next_ints() low = 0 high = m while high - low > 1: mid = (high + low) // 2 if self.test(mid, x, m): high = mid else: low = mid print(low if self.test(low, x, m) else high) def test(self, st, xs, m): xx = 0 for x in xs: fr = x to = x + st if to >= m: to -= m if to >= fr: if xx > to: return False xx = max(xx, fr) elif to < xx < fr: xx = fr return True if __name__ == '__main__': Main().solve() ```
instruction
0
75,931
12
151,862
Yes
output
1
75,931
12
151,863
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1. Submitted Solution: ``` n, m = map(int, input().split()) ass = list(map(int,input().split())) def done(a): return all(x < y for (x,y) in zip(a[:-1], a[1:])) maxops = m-1 minops = 0 while maxops > minops: cops = (maxops+minops)//2 base = 0 ok = True for a in ass: if base > a and (base - a) > cops: ok = False break elif (base - a) % m > cops: base = a if ok: maxops = cops else: minops = cops+1 print (maxops) ```
instruction
0
75,932
12
151,864
Yes
output
1
75,932
12
151,865
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1. Submitted Solution: ``` n,m=map(int,input().split()) a=list(map(int,input().split())) l,r=-1,m-1 while l+1<r: mid,p,f=(l+r)//2,0,1 for i in a: if (m-i+p)%m>mid: if i<p: f=0 break p=i if f: r=mid else: l=mid print (mid) ```
instruction
0
75,933
12
151,866
No
output
1
75,933
12
151,867
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1. Submitted Solution: ``` n,m=list(map(int,input().split())) ns=list(map(int,input().split())) def check(x): if ns[0]+x>=m: minn=0 else: minn=ns[0] for c in ns: if minn>=c: if c+x>=minn: continue else: return False else: if c+x<m: minn=c continue else: if (c+x)%m>=minn: continue else: return False return True p=True for i in range(n-1): if ns[i+1]<ns[i]: p=False break if p: print(0) quit() l,r=0,m-1 while r-l>1: mm=(r+l)//2 if check(mm): r=mm else: l=mm print(r) ```
instruction
0
75,934
12
151,868
No
output
1
75,934
12
151,869
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1. Submitted Solution: ``` import sys import math as mt input=sys.stdin.buffer.readline t=1 def check(x1): prev=0 #print(x1,m) for i in range(n): #print(111,prev,i,arr[i],) if arr[i]>prev: x=m-(arr[i]-prev) if x>x1: prev=arr[i] if arr[i]<prev: x=(prev-arr[i]) if x>x1: return False return True #t=int(input()) for _ in range(t): #n=int((input()) n,m=map(int,input().split()) arr=list(map(int,input().split())) l,r=0,m ans=m while (1<r-l): mid=(l+r)//2 #print(l,r,mid,check(mid)) if (check(mid)): ans=min(ans,mid) r=mid-1 else: l=mid+1 if (check(ans-1)): ans-=1 print(ans) ```
instruction
0
75,935
12
151,870
No
output
1
75,935
12
151,871
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i < m) — the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1. Submitted Solution: ``` import copy n, m = map(int, input().split()) a = list(map(int, input().split())) l = -1 r = 300001 def check(x): global a global m success = True if (a[0] + x >= m): a[0] = 0 last = a[0] for i in range(1, len(a)): now = a[i] if (now < last): now = min(last, now + x) if (a[i] + x >= m + last): now = last if (now < last): return False last = now return True while (r - l > 1): mid = (l + r) // 2 if (check(mid)): r = mid else: l = mid print(r) ```
instruction
0
75,936
12
151,872
No
output
1
75,936
12
151,873
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1].
instruction
0
75,990
12
151,980
Tags: combinatorics, dp Correct Solution: ``` from math import factorial as f def C(n,k): return f(n)//(f(k)*f(n-k)) n,m=map(int,input().split(' ')) print(C(n+2*m-1,2*m)%(10**9+7)) ```
output
1
75,990
12
151,981
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1].
instruction
0
75,991
12
151,982
Tags: combinatorics, dp Correct Solution: ``` from math import factorial as f;n,m=map(int,input().split());print(f(2*m+n-1)//f(2*m)//f(n-1)%(7+10**9)); ```
output
1
75,991
12
151,983
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1].
instruction
0
75,992
12
151,984
Tags: combinatorics, dp Correct Solution: ``` from math import factorial n, m = [int(x) for x in input().split()] print((factorial(n + 2 * m - 1) // (factorial(2 * m) * factorial(n - 1))) % (10 ** 9 + 7)) ```
output
1
75,992
12
151,985
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1].
instruction
0
75,993
12
151,986
Tags: combinatorics, dp Correct Solution: ``` import math a,b=[int(ele) for ele in input().split()] inamo=10**9+7 res=(math.factorial(a+b*2-1)//math.factorial(a-1)//math.factorial(b*2))%inamo print(res) ```
output
1
75,993
12
151,987
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1].
instruction
0
75,994
12
151,988
Tags: combinatorics, dp Correct Solution: ``` from math import factorial as fact mod = 1000000007 def ncr(n,k): return fact(n) // (fact(k) * fact(n - k)) n,m=map(int,input().split()) print(ncr(n+2*m-1,2*m)%mod) ```
output
1
75,994
12
151,989
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1].
instruction
0
75,995
12
151,990
Tags: combinatorics, dp Correct Solution: ``` MOD = 10 ** 9 + 7 n, sz = map(int, input().split()) waysA = [[0 for _ in range(n + 1)] for _ in range(sz + 1)] waysB = [[0 for _ in range(n + 1)] for _ in range(sz + 1)] for size in range(1, sz + 1): for cur in range(1, n + 1): if size == 1: waysA[size][cur] = 1 waysB[size][cur] = 1 continue for prev in range(1, cur + 1): waysA[size][cur] += waysA[size - 1][prev] waysA[size][cur] %= MOD for prev in range(cur, n + 1): waysB[size][cur] += waysB[size - 1][prev] waysB[size][cur] %= MOD ret = 0 for a in range(1, n + 1): for b in range(a, n + 1): ret += (waysA[sz][a] * waysB[sz][b]) ret %= MOD print(ret) ```
output
1
75,995
12
151,991
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1].
instruction
0
75,996
12
151,992
Tags: combinatorics, dp Correct Solution: ``` import math mod=10**9+7 n,m=map(int,input().split()) print((math.factorial(2*m+n-1)//((math.factorial(2*m))*math.factorial(n-1)))%mod) ```
output
1
75,996
12
151,993
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1].
instruction
0
75,997
12
151,994
Tags: combinatorics, dp Correct Solution: ``` from math import factorial mod = 10**9 + 7 def C(n,k): return factorial(n)//(factorial(k)*factorial(n-k)) n,m=map(int,input().split(' ')) print(C(n+2*m-1,2*m)%mod) ```
output
1
75,997
12
151,995
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1]. Submitted Solution: ``` inp = input() n = int(inp.split(' ')[0]) m = int(inp.split(' ')[1]) nn = n-1+2*m k = 2*m er=1 for i in range(nn): er*=i+1 for i in range(nn-k): er//=i+1 for i in range(k): er//=i+1 print(er % 1000000007); ```
instruction
0
75,998
12
151,996
Yes
output
1
75,998
12
151,997
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1]. Submitted Solution: ``` ''' @Date : 2020-04-07 14:37:45 @Author : ssyze @Description : ''' from math import factorial as f mod = 10**9 + 7 n, m = map(int, input().split()) def C(n, k): return f(n) // (f(k) * f(n - k)) print(int(C(n + 2*m - 1, 2*m)) % mod) ```
instruction
0
76,000
12
152,000
Yes
output
1
76,000
12
152,001
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1]. Submitted Solution: ``` '''input 10 1 ''' # A coding delight from sys import stdin, stdout import gc gc.disable() input = stdin.readline import math # main starts n, m = list(map(int, input().split())) mod = 10 ** 9 + 7 dp1 = [[1 for x in range(m + 1)] for y in range(n + 1)] dp2 = [[1 for y in range(m + 1)] for y in range(n + 1)] for j in range(2, m + 1): for i in range(1, n + 1): temp = 0 for k in range(1, i + 1): temp += dp1[k][j - 1] temp %= mod dp1[i][j] = temp temp = 0 for k in range(i, n + 1): temp += dp2[k][j - 1] temp %= mod dp2[i][j] = temp # print(dp1) # print(dp2) ans = 0 for i in range(1, n + 1): for k in range(i, n + 1): ans += dp1[i][m] * dp2[k][m] ans %= mod print(ans) ```
instruction
0
76,001
12
152,002
Yes
output
1
76,001
12
152,003
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1]. Submitted Solution: ``` n,k = list(map(int,input().split())) mod = 10**9 + 7 fact = [1]*(2*k+n+1) for i in range(1,len(fact)): fact[i] = (fact[i-1] * i)%mod fact = fact[1:] print((fact[(2*k+n-2)] // (fact[2*k-1] * fact[(2*k+n-1-2*k-1)]) )%mod) ```
instruction
0
76,002
12
152,004
No
output
1
76,002
12
152,005
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1]. Submitted Solution: ``` ''' @Date : 2020-04-07 14:37:45 @Author : ssyze @Description : ''' from math import factorial as f mod = 10**9 + 7 n, m = map(int, input().split()) def C(n, k): return f(n) / (f(k) * f(n - k)) print(int(C(n + 2*m - 1, 2*m)) % mod) ```
instruction
0
76,003
12
152,006
No
output
1
76,003
12
152,007
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1]. Submitted Solution: ``` n,m=map(int,input().split()) dp=[[0 for i in range(n+2)] for j in range(2*m+2)] for i in range(n): dp[1][i+1]=1 for i in range(2,2*m + 1): for j in range(1,n+1): dp[i][j]=sum(dp[i-1][1:j+1]) print(sum(dp[2*m][1:n])) ```
instruction
0
76,004
12
152,008
No
output
1
76,004
12
152,009
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers n and m. Calculate the number of pairs of arrays (a, b) such that: * the length of both arrays is equal to m; * each element of each array is an integer between 1 and n (inclusive); * a_i ≤ b_i for any index i from 1 to m; * array a is sorted in non-descending order; * array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 10^9+7. Input The only line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 10^9+7. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are 5 suitable arrays: * a = [1, 1], b = [2, 2]; * a = [1, 2], b = [2, 2]; * a = [2, 2], b = [2, 2]; * a = [1, 1], b = [2, 1]; * a = [1, 1], b = [1, 1]. Submitted Solution: ``` n,m=map(int,input().split()) dp=[0 for i in range(max(2*m+1,n+ 2*m))] dp[0]=1 mod=pow(10,9)+7 for i in range(1,len(dp)): dp[i]=(dp[i-1]*i)%mod dp[i]=dp[i]%mod print(dp[n+m-1]//(dp[2*m] * dp[n-m-1])) ```
instruction
0
76,005
12
152,010
No
output
1
76,005
12
152,011
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3.
instruction
0
76,038
12
152,076
Tags: binary search, combinatorics, dp, math, number theory, sortings Correct Solution: ``` n, p = list(map(int, input().split(' '))) a = list(map(int, input().split(' '))) a.sort() result = [] _min = 0 for i in range(len(a) - 1, -1, -1): if _min <= a[i] - i: _min = a[i] - i _max = a[-1] for i in range(len(a) - 1, p-2, -1): if _max >= p - 2 - i + a[i]: _max = p - 2 - i + a[i] result = max(_max - _min + 1, 0) print(result) if result > 0: for i in range(_min, _max + 1): print(i, end=' ') """ print('max ', _max) print('min ', _min) """ ```
output
1
76,038
12
152,077
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3.
instruction
0
76,039
12
152,078
Tags: binary search, combinatorics, dp, math, number theory, sortings Correct Solution: ``` lis = input().split() n,p = int(lis[0]),int(lis[1]) lis = input().split() a = [0]*n for i in range(n): a[i] = int(lis[i]) a.sort() xminn = a[0] for i in range(1,n): xminn = max(a[i]-i,xminn) for i in range(n): a[i] = min(xminn - a[i] + 1 + i , i+1) lenn = n i = p while(i<=n): lenn = min(i-a[i-1],lenn) i+=p if(lenn==0): print(0) else: notAllowed = [False]*lenn for i in range(n): uplim = min(a[i]+lenn-1,i+1) val = ((a[i]-1)//p + 1)*p if(val<=uplim): notAllowed[val-a[i]] = True good = [] for i in range(lenn): if (not notAllowed[i]): good.append(xminn+i) print(len(good)) for i in good: print(i,end=" ") print() ```
output
1
76,039
12
152,079
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3.
instruction
0
76,040
12
152,080
Tags: binary search, combinatorics, dp, math, number theory, sortings Correct Solution: ``` def places(num,v): if num>=v+n: return 0 if num<v: return n return (n-(num-v)) def check(num): for i in range(n-1,-1,-1): count = max(0, places(b[i],num) - ((n-1)-i)) # print (num,count) if count==0: return True if count%p==0: return False return False def check2(num): for i in range(n-1,-1,-1): count = max(0, places(b[i],num) - ((n-1)-i)) # print (num,count) if count%p==0: return True return False n,p = map(int,input().split()) a = list(map(int,input().split())) b = sorted(a) minn = b[0] maxx = b[-1] low = 0 high = maxx-1 while low<high: mid = (low+high)//2 if check(mid): low = mid+1 else: high = mid-1 if check(low): start = low+1 else: start = low low = start high = maxx-1 while low<high: mid = (low+high)//2 if not check2(mid): low = mid + 1 else: high = mid - 1 if check2(low): end = low - 1 else: end = low ans = [] for i in range(start,end+1): ans.append(i) print (len(ans)) print (*ans) ```
output
1
76,040
12
152,081
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3.
instruction
0
76,041
12
152,082
Tags: binary search, combinatorics, dp, math, number theory, sortings Correct Solution: ``` mod = 1000000007 eps = 10**-9 def main(): import sys from collections import Counter input = sys.stdin.readline N, p = map(int, input().split()) A = list(map(int, input().split())) A.sort() """ C = Counter(A) for a in A: if C[a] >= p: print(0) print("\n") exit() """ x0 = -1 for i in range(N): x0 = max(x0, A[i] - i) x1 = A[p-1] """ if x0 >= x1: print(0) print("\n") exit() """ dic = {} i = 0 for x in range(x0, x0 + 2*10**5+1): while True: if i == N-1: break if A[i+1] <= x: i += 1 else: break dic[x] = i+1 ok = x0-1 ng = x1 mid = (ok+ng)//2 while ng - ok > 1: x = mid flg = 1 for i in range(N): if (dic[x+i] - i) % p == 0: flg = 0 break if flg: ok = mid else: ng = mid mid = (ok+ng)//2 print(ok - x0 + 1) X = list(range(x0, ok+1)) print(*X) if __name__ == '__main__': main() ```
output
1
76,041
12
152,083
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3.
instruction
0
76,042
12
152,084
Tags: binary search, combinatorics, dp, math, number theory, sortings Correct Solution: ``` n, p = map(int, input().split()) a = list(map(int, input().split())) a.sort() for i in range(n): a[i] -= i l = max(a) for i in range(n): a[i] += p - 1 r = min(a[p - 1:]) print(max(r - l, 0)) print(*list(range(l, r))) ```
output
1
76,042
12
152,085
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3.
instruction
0
76,043
12
152,086
Tags: binary search, combinatorics, dp, math, number theory, sortings Correct Solution: ``` n,p = map(int,input().split()) l = list(map(int,input().split())) l.sort() m = l[-1] b = [0] * (2*n-1) ind = 0 for i in range(2*n-1): while ind < n and l[ind] <= i+m-n+1: ind += 1 b[i] = ind chuje = [0] * p for i in range(m-n+1,m+1): chuje[(i-b[i-m+n-1])%p] += 1 dobre = [] for x in range(m-n+1,m): if not chuje[x%p]: dobre.append(x) chuje[(x-b[x-m+n-1])%p] -= 1 chuje[(x+n-b[x-m +2*n-1])%p] += 1 print(len(dobre)) print(*dobre) ```
output
1
76,043
12
152,087
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3.
instruction
0
76,044
12
152,088
Tags: binary search, combinatorics, dp, math, number theory, sortings Correct Solution: ``` import sys, math,os from io import BytesIO, IOBase #data = BytesIO(os.read(0,os.fstat(0).st_size)).readline # from bisect import bisect_left as bl, bisect_right as br, insort # from heapq import heapify, heappush, heappop from collections import defaultdict as dd, deque, Counter # from itertools import permutations,combinations # from decimal import Decimal def data(): return sys.stdin.readline().strip() def mdata(): return list(map(int, data().split())) def outl(var): sys.stdout.write(' '.join(map(str, var)) + '\n') def out(var): sys.stdout.write(str(var) + '\n') #sys.setrecursionlimit(100000 + 1) INF = 10**9 mod = 998244353 n,p=mdata() A=mdata() A.sort() m=0 m1=-INF l=[] for i in range(n): m=max(m,A[i]-i) for i in range(p-1,n): m1=max(m1,i-A[i]+1) i=m while True: if m1+i>=p: break l.append(i) i+=1 out(len(l)) outl(l) ```
output
1
76,044
12
152,089
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3.
instruction
0
76,045
12
152,090
Tags: binary search, combinatorics, dp, math, number theory, sortings Correct Solution: ``` import sys input=sys.stdin.readline import bisect n,p=map(int,input().split()) a=[int(i) for i in input().split() if i!='\n'] a.sort() #print(a) j,k=a[0],a[0] for i in range(1,n): if a[i]-k>1: j+=((a[i]-k)-1) k=a[i] else: k+=1 #print(j) unique=[1] for i in range(1,n): unique.append(unique[-1]+1) mina=a[-1] #print(unique) for i in range(n): bis=bisect.bisect(unique,(i+p)) #print(bis,a[bis-1]) mina=min(mina,a[bis-1]-i) #print(mina) if bis==n: break out=list(range(j,mina)) sys.stdout.write(str(len(out))+'\n') out=' '.join(map(str,out)) sys.stdout.write(out) ```
output
1
76,045
12
152,091
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3. Submitted Solution: ``` def ican(a, x): k = x wr = 0 for i in range(len(a)): if a[i] <= k: wr += 1 else: wr -= a[i] - k k = a[i] if wr < 0: return False wr += 1 return True def bnpleft(a): r = max(a) l = -1 while r - l > 1: h = (r+l)//2 if ican(a, h): r = h else: l = h return l def fa(a, x, p): k = x wr = 0 for i in range(len(a)): if a[i] <= k: wr += 1 else: nk = wr // p * p wr -= a[i] - k if wr < nk: return False k = a[i] if wr < 0: return False wr += 1 if wr >= p: return False else: return True def bnpr(a,p,left): l = left r = max(a) while r - l > 1: h = (r+l)//2 if fa(a, h, p): l = h else: r = h return l def solve(): n, p = map(int, input().split()) lst = list(map(int,input().split())) lst.sort() ll = bnpleft(lst) rr = bnpr(lst,p,ll) print(rr-ll) for i in range(ll + 1, rr + 1): print(i, end=" ") for i in range(1): solve() ```
instruction
0
76,046
12
152,092
Yes
output
1
76,046
12
152,093
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3. Submitted Solution: ``` import sys lines = sys.stdin.readlines() # (N, K) = map(int, lines[0].strip().split(" ")) (n, p) = map(int, lines[0].strip().split(" ")) arr = list(map(int, lines[1].strip().split(" "))) arr.sort() counter = {} for a in arr: if a not in counter:counter[a] = 0 counter[a] += 1 lower = max(arr[-1] - n + 1, 1) a, b, c, d = lower-1, -1, -1, arr[-1] + 1 def check(val): pt = 0 cnt = 0 while pt < n and arr[pt] <= val: cnt += 1; pt += 1 if cnt >= p: return 1 while cnt > 0: val += 1 cnt -= 1 if val in counter: cnt += counter[val] if cnt >= p: return 1 if val >= arr[-1]: break if ( cnt <= 0 or val < arr[-1]): return -1 else: return 0 exist = False while(a<d-1): mid = (a+d)//2 res = check(mid) if res == 1: d = mid elif res == -1: a = mid else: exist = True; break if exist: b = mid; c = mid while(a < b-1): mid = (a+b)//2 res = check(mid) if res == 0: b = mid else: a = mid while(c < d-1): mid = (c+d)//2 if check(mid) == 1: d = mid else: c = mid if exist: print(c-b+1) print(' '.join(map(str, range(b, c+1)))) else: print(0); print() ```
instruction
0
76,047
12
152,094
Yes
output
1
76,047
12
152,095
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies. Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: * If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. * The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define f(x) as the number of valid permutations for the integer x. You are given n, a and a prime number p ≤ n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x. Your task is to solve this problem made by Akari. Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10^5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). Output In the first line, print the number of good integers x. In the second line, output all good integers x in the ascending order. It is guaranteed that the number of good integers x does not exceed 10^5. Examples Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 Note In the first test, p=2. * If x ≤ 2, there are no valid permutations for Yuzu. So f(x)=0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. * If x = 3, \{1,2,3\} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good. * If x = 4, \{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good. * If x ≥ 5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3. In the third test, for all positive integers x the value f(x) is divisible by p = 3. Submitted Solution: ``` from bisect import * from collections import * from math import gcd,ceil,sqrt,floor,inf from heapq import * from itertools import * from operator import add,mul,sub,xor,truediv,floordiv from functools import * #------------------------------------------------------------------------ import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #------------------------------------------------------------------------ def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) #------------------------------------------------------------------------ from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc farr=[1] ifa=[] def fact(x,mod=0): if mod: while x>=len(farr): farr.append(farr[-1]*len(farr)%mod) else: while x>=len(farr): farr.append(farr[-1]*len(farr)) return farr[x] def ifact(x,mod): global ifa ifa.append(pow(farr[-1],mod-2,mod)) for i in range(x,0,-1): ifa.append(ifa[-1]*i%mod) ifa=ifa[::-1] def per(i,j,mod=0): if i<j: return 0 if not mod: return fact(i)//fact(i-j) return farr[i]*ifa[i-j]%mod def com(i,j,mod=0): if i<j: return 0 if not mod: return per(i,j)//fact(j) return per(i,j,mod)*ifa[j]%mod def catalan(n): return com(2*n,n)//(n+1) def linc(f,t,l,r): while l<r: mid=(l+r)//2 if t>f(mid): l=mid+1 else: r=mid return l def rinc(f,t,l,r): while l<r: mid=(l+r+1)//2 if t<f(mid): r=mid-1 else: l=mid return l def ldec(f,t,l,r): while l<r: mid=(l+r)//2 if t<f(mid): l=mid+1 else: r=mid return l def rdec(f,t,l,r): while l<r: mid=(l+r+1)//2 if t>f(mid): r=mid-1 else: l=mid return l def isprime(n): for i in range(2,int(n**0.5)+1): if n%i==0: return False return True def binfun(x): c=0 for w in arr: c+=ceil(w/x) return c def lowbit(n): return n&-n def inverse(a,m): a%=m if a<=1: return a return ((1-inverse(m,a)*m)//a)%m class BIT: def __init__(self,arr): self.arr=arr self.n=len(arr)-1 def update(self,x,v): while x<=self.n: self.arr[x]+=v x+=x&-x def query(self,x): ans=0 while x: ans+=self.arr[x] x&=x-1 return ans ''' class SMT: def __init__(self,arr): self.n=len(arr)-1 self.arr=[0]*(self.n<<2) self.lazy=[0]*(self.n<<2) def Build(l,r,rt): if l==r: self.arr[rt]=arr[l] return m=(l+r)>>1 Build(l,m,rt<<1) Build(m+1,r,rt<<1|1) self.pushup(rt) Build(1,self.n,1) def pushup(self,rt): self.arr[rt]=self.arr[rt<<1]+self.arr[rt<<1|1] def pushdown(self,rt,ln,rn):#lr,rn表区间数字数 if self.lazy[rt]: self.lazy[rt<<1]+=self.lazy[rt] self.lazy[rt<<1|1]=self.lazy[rt] self.arr[rt<<1]+=self.lazy[rt]*ln self.arr[rt<<1|1]+=self.lazy[rt]*rn self.lazy[rt]=0 def update(self,L,R,c,l=1,r=None,rt=1):#L,R表示操作区间 if r==None: r=self.n if L<=l and r<=R: self.arr[rt]+=c*(r-l+1) self.lazy[rt]+=c return m=(l+r)>>1 self.pushdown(rt,m-l+1,r-m) if L<=m: self.update(L,R,c,l,m,rt<<1) if R>m: self.update(L,R,c,m+1,r,rt<<1|1) self.pushup(rt) def query(self,L,R,l=1,r=None,rt=1): if r==None: r=self.n #print(L,R,l,r,rt) if L<=l and R>=r: return self.arr[rt] m=(l+r)>>1 self.pushdown(rt,m-l+1,r-m) ans=0 if L<=m: ans+=self.query(L,R,l,m,rt<<1) if R>m: ans+=self.query(L,R,m+1,r,rt<<1|1) return ans ''' class DSU:#容量+路径压缩 def __init__(self,n): self.c=[-1]*n def same(self,x,y): return self.find(x)==self.find(y) def find(self,x): if self.c[x]<0: return x self.c[x]=self.find(self.c[x]) return self.c[x] def union(self,u,v): u,v=self.find(u),self.find(v) if u==v: return False if self.c[u]<self.c[v]: u,v=v,u self.c[u]+=self.c[v] self.c[v]=u return True def size(self,x): return -self.c[self.find(x)] class UFS:#秩+路径 def __init__(self,n): self.parent=[i for i in range(n)] self.ranks=[0]*n def find(self,x): if x!=self.parent[x]: self.parent[x]=self.find(self.parent[x]) return self.parent[x] def union(self,u,v): pu,pv=self.find(u),self.find(v) if pu==pv: return False if self.ranks[pu]>=self.ranks[pv]: self.parent[pv]=pu if self.ranks[pv]==self.ranks[pu]: self.ranks[pu]+=1 else: self.parent[pu]=pv def Prime(n): c=0 prime=[] flag=[0]*(n+1) for i in range(2,n+1): if not flag[i]: prime.append(i) c+=1 for j in range(c): if i*prime[j]>n: break flag[i*prime[j]]=prime[j] if i%prime[j]==0: break return prime def dij(s,graph): d={} d[s]=0 heap=[(0,s)] seen=set() while heap: dis,u=heappop(heap) if u in seen: continue for v in graph[u]: if v not in d or d[v]>d[u]+graph[u][v]: d[v]=d[u]+graph[u][v] heappush(heap,(d[v],v)) return d def GP(it): return [(ch,len(list(g))) for ch,g in groupby(it)] class DLN: def __init__(self,val): self.val=val self.pre=None self.next=None t=1 for i in range(t): n,p=RL() a=RLL() a.sort() mi=max(a[i]-i for i in range(n)) ma=a[-1]-1 if mi>ma: print(0) print() exit() j=cur=0 res=[] for i in range(mi,ma+n): while j<n and a[j]<=i: j+=1 cur+=1 res.append((i-cur)%p) c=Counter() ans=[] for r in range(n): c[res[r]]+=1 if c[mi%p]==0: ans.append(mi) r=n for x in range(mi+1,ma+1): c[res[r]]+=1 c[res[r-n]]-=1 if c[x%p]==0: ans.append(x) r+=1 print(len(ans)) print(*ans) ''' sys.setrecursionlimit(200000) import threading threading.stack_size(10**8) t=threading.Thread(target=main) t.start() t.join() ''' ''' sys.setrecursionlimit(200000) import threading threading.stack_size(10**8) t=threading.Thread(target=main) t.start() t.join() ''' ```
instruction
0
76,048
12
152,096
Yes
output
1
76,048
12
152,097