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 tags and a correct Python 3 solution for this coding contest problem.
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly <image> times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
Input
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Output
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic. | instruction | 0 | 16,299 | 12 | 32,598 |
Tags: greedy, implementation, math
Correct Solution:
```
def main():
R=lambda:map(int,input().split())
n,k=R()
a=list(R())
m=n//k
ans=0
for i in range(k):
c=[n,0,0]
for j in range(m):
c[a[j*k+i]]+=1
ans+=min(c)
print(ans)
main()
``` | output | 1 | 16,299 | 12 | 32,599 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly <image> times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
Input
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Output
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic. | instruction | 0 | 16,300 | 12 | 32,600 |
Tags: greedy, implementation, math
Correct Solution:
```
from math import *
[n, k] = [int(i) for i in input().split()]
arr = [int(i) - 1 for i in input().split()]
arr2 = [[0 for i in range(2)] for j in range(k)]
r = 0
for i in range(n // k):
for j in range(k):
arr2[j][arr[i * k + j]] += 1
for j in range(k):
r += min(arr2[j][0], arr2[j][1])
print(r)
``` | output | 1 | 16,300 | 12 | 32,601 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly <image> times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
Input
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Output
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.
Submitted Solution:
```
read = lambda:map(int, input().split())
n,k = read()
m = n//k
a = list(read())
result = 0
for i in range(k):
t = a[i:n:k]
s = t.count(1)
result += min(s, m-s)
print(result)
``` | instruction | 0 | 16,301 | 12 | 32,602 |
Yes | output | 1 | 16,301 | 12 | 32,603 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly <image> times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
Input
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Output
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.
Submitted Solution:
```
from collections import Counter
I = lambda: map(int, input().split())
n, k = I()
A = list(I())
print(sum(n//k-max(Counter(A[i::k]).values()) for i in range(k)))
``` | instruction | 0 | 16,302 | 12 | 32,604 |
Yes | output | 1 | 16,302 | 12 | 32,605 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly <image> times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
Input
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Output
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.
Submitted Solution:
```
n, k = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
ans = 0
for base in range(k):
ones = twos = 0
for x in range(n//k):
if a[x * k + base] == 1:
ones += 1
else:
twos += 1
ans += min(ones, twos)
print(ans)
``` | instruction | 0 | 16,303 | 12 | 32,606 |
Yes | output | 1 | 16,303 | 12 | 32,607 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly <image> times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
Input
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Output
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.
Submitted Solution:
```
I=lambda:map(int,input().split())
n,k=I()
a=[0]*n
i=0
for v in I():a[i]+=v-1;i=(i+1)%k
print(sum(map(lambda x:min(x,n//k-x),a)))
``` | instruction | 0 | 16,304 | 12 | 32,608 |
Yes | output | 1 | 16,304 | 12 | 32,609 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly <image> times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
Input
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Output
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.
Submitted Solution:
```
aa = input().split()
n = int(aa[0])
k = int(aa[1])
a = input().split()
master = []
new = []
for i in range(1, n + 1):
if i % k == 0:
new.append(a[i - 1])
master.append(new)
new = []
else:
new.append(a[i-1])
def count(list, item):
count = 0
for c in list:
if c == item:
count += 1
return count
def common(list):
counts = []
maxcount = 0
for stuff in list:
if count(list, stuff) > maxcount and count(list, stuff) > 1:
maxitem = stuff
maxcount = count(list, stuff)
if maxcount == 0:
maxitem = 'none'
return maxitem
if n == k:
answer = 0
else:
if common(master) == 'none':
reference = master[0]
else:
reference = common(master)
answer = 0
for i in range(1, len(master)):
if master[i] != reference:
for j in range(len(master[i])):
if master[i][j] != reference[j]:
answer += 1
print(answer)
``` | instruction | 0 | 16,305 | 12 | 32,610 |
No | output | 1 | 16,305 | 12 | 32,611 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly <image> times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
Input
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Output
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.
Submitted Solution:
```
n,q=map(int,input().split())
a=list(map(int,input().split()))
c=[]
c1=0
for i in range(1,n+1):
if(n%i==0 and i<=q):
r=a[:i]
# print(r)
j=i
c1=0
while(j<len(a)):
y=a[j:j+len(r)]
j=j+len(r)
# print(r,y)
for k in range(len(r)):
if(r[k]!=y[k]):
c1=c1+1
c.append(c1)
c.pop()
k1=a.count(1)
k2=a.count(2)
# print(c,k1,k2)
if(n==q):
print(0)
elif(len(c)>0):
print(min(min(c),min(k1,k2)))
else:
print(min(k1,k2))
``` | instruction | 0 | 16,306 | 12 | 32,612 |
No | output | 1 | 16,306 | 12 | 32,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly <image> times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
Input
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Output
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.
Submitted Solution:
```
line = input().split()
n = int(line[0])
k = int(line[1])
line = input().split()
arr = [int(i) for i in line]
matrix = []
new_arr = []
for i in range(n+1):
if i % k == 0 and new_arr:
matrix.append(new_arr)
new_arr = []
if i == n:
break
new_arr.append(arr[i])
# print(f"matrix: {matrix}")
changes = 0
for c in range(k): # n/k is number of sub arrays
one_cnt = 0
two_cnt = 0
for r in range(n//k):
# print(f"row {r} col {c} value: {matrix[r][c]}")
if matrix[r][c] == 2:
two_cnt += 1
else:
one_cnt += 1
# print(f"twos in {c}th column: {two_cnt}")
# print(f"ones in {c}th column: {one_cnt}")
if one_cnt != 0 and two_cnt != 0: # a change needs to be made
changes += abs(one_cnt - two_cnt)
# print("\n\n")
print(changes)
``` | instruction | 0 | 16,307 | 12 | 32,614 |
No | output | 1 | 16,307 | 12 | 32,615 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.
Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly <image> times consecutively. In other words, array a is k-periodic, if it has period of length k.
For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.
For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
Input
The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.
Output
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
Examples
Input
6 2
2 1 2 2 2 1
Output
1
Input
8 4
1 1 2 1 1 1 2 1
Output
0
Input
9 3
2 1 1 1 2 1 1 1 2
Output
3
Note
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].
In the second sample, the given array already is 4-periodic.
In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.
Submitted Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(k):
t1, t2 = 0, 0
for j in range(i, n, k):
print(j, a[j])
if a[j] == 1:
t1 += 1
else:
t2 += 1
ans += min(t1, t2)
print(ans)
``` | instruction | 0 | 16,308 | 12 | 32,616 |
No | output | 1 | 16,308 | 12 | 32,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.
A sequence of l integers b1, b2, ..., bl (1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is called good if each number divides (without a remainder) by the next number in the sequence. More formally <image> for all i (1 ≤ i ≤ l - 1).
Given n and k find the number of good sequences of length k. As the answer can be rather large print it modulo 1000000007 (109 + 7).
Input
The first line of input contains two space-separated integers n, k (1 ≤ n, k ≤ 2000).
Output
Output a single integer — the number of good sequences of length k modulo 1000000007 (109 + 7).
Examples
Input
3 2
Output
5
Input
6 4
Output
39
Input
2 1
Output
2
Note
In the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3]. | instruction | 0 | 16,313 | 12 | 32,626 |
Tags: combinatorics, dp, number theory
Correct Solution:
```
n, k = map(int, input().split())
dp = []
for i in range(k+1):
dp.append([])
for j in range(n+1):
dp[-1].append(0)
for i in range(1, n+1):
dp[1][i] = 1
for m in range(1, k):
for i in range(1, n+1):
for j in range(i, n+1, i):
dp[m+1][j] += dp[m][i]
dp[m+1][j]%=1000000007
print(sum(dp[k])%1000000007)
``` | output | 1 | 16,313 | 12 | 32,627 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.
A sequence of l integers b1, b2, ..., bl (1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is called good if each number divides (without a remainder) by the next number in the sequence. More formally <image> for all i (1 ≤ i ≤ l - 1).
Given n and k find the number of good sequences of length k. As the answer can be rather large print it modulo 1000000007 (109 + 7).
Input
The first line of input contains two space-separated integers n, k (1 ≤ n, k ≤ 2000).
Output
Output a single integer — the number of good sequences of length k modulo 1000000007 (109 + 7).
Examples
Input
3 2
Output
5
Input
6 4
Output
39
Input
2 1
Output
2
Note
In the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3]. | instruction | 0 | 16,314 | 12 | 32,628 |
Tags: combinatorics, dp, number theory
Correct Solution:
```
import os,sys;from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno();self.buffer = BytesIO();self.writable = "x" in file.mode or "r" not in file.mode;self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:break
ptr = self.buffer.tell();self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE));self.newlines = b.count(b"\n") + (not b);ptr = self.buffer.tell();self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:os.write(self._fd, self.buffer.getvalue());self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file);self.flush = self.buffer.flush;self.writable = self.buffer.writable;self.write = lambda s: self.buffer.write(s.encode("ascii"));self.read = lambda: self.buffer.read().decode("ascii");self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
try:sys.stdin,sys.stdout=open('in.txt','r'),open('out.txt','w')
except:pass
ii1=lambda:int(sys.stdin.readline().strip()) # for interger
is1=lambda:sys.stdin.readline().strip() # for str
iia=lambda:list(map(int,sys.stdin.readline().strip().split())) # for List[int]
isa=lambda:sys.stdin.readline().strip().split() # for List[str]
mod=int(1e9 + 7);from collections import *;from math import *
# abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# sys.setrecursionlimit(4000000)
###################### Start Here ######################
from functools import lru_cache
# from collections import defaultdict as dd
from collections import deque as dq
x, y = iia()
divs = [[] for _ in range(x+1)]
for n in range(1, x+1):
for k in range(n, x+1, n):
divs[k].append(n)
dp = [[0 for n in range(y+1)] for k in range(x+1)]
for n in range(1, y+1):
dp[1][n] = 1
for n in range(1, x+1):
dp[n][1] = 1
mod = int(1e9 + 7)
for n in range(2, x+1):
for k in range(2, y+1):
for dv in divs[n]:
dp[n][k] += dp[dv][k-1]
dp[n][k] %= mod
res = 0
for n in dp:
res += n[-1]
res %= mod
print(res)
``` | output | 1 | 16,314 | 12 | 32,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.
A sequence of l integers b1, b2, ..., bl (1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is called good if each number divides (without a remainder) by the next number in the sequence. More formally <image> for all i (1 ≤ i ≤ l - 1).
Given n and k find the number of good sequences of length k. As the answer can be rather large print it modulo 1000000007 (109 + 7).
Input
The first line of input contains two space-separated integers n, k (1 ≤ n, k ≤ 2000).
Output
Output a single integer — the number of good sequences of length k modulo 1000000007 (109 + 7).
Examples
Input
3 2
Output
5
Input
6 4
Output
39
Input
2 1
Output
2
Note
In the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3]. | instruction | 0 | 16,315 | 12 | 32,630 |
Tags: combinatorics, dp, number theory
Correct Solution:
```
def mlt(): return map(int, input().split())
x, y = mlt()
divs = [[] for _ in range(x+1)]
for n in range(1, x+1):
for k in range(n, x+1, n):
divs[k].append(n)
dp = [[0 for n in range(y+1)] for k in range(x+1)]
for n in range(1, y+1):
dp[1][n] = 1
for n in range(1, x+1):
dp[n][1] = 1
mod = int(1e9 + 7)
for n in range(2, x+1):
for k in range(2, y+1):
for dv in divs[n]:
dp[n][k] += dp[dv][k-1]
dp[n][k] %= mod
res = 0
for n in dp:
res += n[-1]
res %= mod
print(res)
``` | output | 1 | 16,315 | 12 | 32,631 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.
A sequence of l integers b1, b2, ..., bl (1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is called good if each number divides (without a remainder) by the next number in the sequence. More formally <image> for all i (1 ≤ i ≤ l - 1).
Given n and k find the number of good sequences of length k. As the answer can be rather large print it modulo 1000000007 (109 + 7).
Input
The first line of input contains two space-separated integers n, k (1 ≤ n, k ≤ 2000).
Output
Output a single integer — the number of good sequences of length k modulo 1000000007 (109 + 7).
Examples
Input
3 2
Output
5
Input
6 4
Output
39
Input
2 1
Output
2
Note
In the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3]. | instruction | 0 | 16,316 | 12 | 32,632 |
Tags: combinatorics, dp, number theory
Correct Solution:
```
Mod=10**9+7
n,k=map(int,input().split())
dp=[[0]*(n+1) for i in range(k+1)]
l=[[] for _ in range(n+1)]
for i in range(1,n+1):
dp[1][i]=1
for j in range(1,i+1):
if i%j==0:
l[i].append(j)
for j in range(2,k+1):
for i in range(1,n+1):
for le in range(len(l[i])):
dp[j][i]+=dp[j-1][l[i][le]]
dp[j][i]%=Mod
ans=0
for i in range(1,n+1):
ans+=dp[k][i]
ans%=Mod
print(ans)
``` | output | 1 | 16,316 | 12 | 32,633 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.
A sequence of l integers b1, b2, ..., bl (1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is called good if each number divides (without a remainder) by the next number in the sequence. More formally <image> for all i (1 ≤ i ≤ l - 1).
Given n and k find the number of good sequences of length k. As the answer can be rather large print it modulo 1000000007 (109 + 7).
Input
The first line of input contains two space-separated integers n, k (1 ≤ n, k ≤ 2000).
Output
Output a single integer — the number of good sequences of length k modulo 1000000007 (109 + 7).
Examples
Input
3 2
Output
5
Input
6 4
Output
39
Input
2 1
Output
2
Note
In the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3]. | instruction | 0 | 16,317 | 12 | 32,634 |
Tags: combinatorics, dp, number theory
Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
n,k = LI()
t = [0] * n
t[0] = 1
for _ in range(k):
u = [0] * n
for i in range(n):
b = t[i] % mod
for j in range(i,n,i+1):
u[j] += b
t = u
return sum(t) % mod
print(main())
``` | output | 1 | 16,317 | 12 | 32,635 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.
A sequence of l integers b1, b2, ..., bl (1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is called good if each number divides (without a remainder) by the next number in the sequence. More formally <image> for all i (1 ≤ i ≤ l - 1).
Given n and k find the number of good sequences of length k. As the answer can be rather large print it modulo 1000000007 (109 + 7).
Input
The first line of input contains two space-separated integers n, k (1 ≤ n, k ≤ 2000).
Output
Output a single integer — the number of good sequences of length k modulo 1000000007 (109 + 7).
Examples
Input
3 2
Output
5
Input
6 4
Output
39
Input
2 1
Output
2
Note
In the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3].
Submitted Solution:
```
def mlt(): return map(int, input().split())
x, y = mlt()
divs = [[] for _ in range(x+1)]
for n in range(1, x+1):
for k in range(n, x+1, n):
divs[k].append(n)
dp = [[0 for n in range(y+1)] for k in range(x+1)]
for n in range(1, y+1):
dp[1][n] = 1
for n in range(1, x+1):
dp[n][1] = 1
mod = int(1e9 + 7)
for n in range(2, x+1):
for k in range(2, y+1):
for dv in divs[n]:
dp[n][k] += dp[dv][k-1]
dp[n][k] %= mod
res = 0
for n in dp:
res += n[-1]
print(res)
``` | instruction | 0 | 16,318 | 12 | 32,636 |
No | output | 1 | 16,318 | 12 | 32,637 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.
A sequence of l integers b1, b2, ..., bl (1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is called good if each number divides (without a remainder) by the next number in the sequence. More formally <image> for all i (1 ≤ i ≤ l - 1).
Given n and k find the number of good sequences of length k. As the answer can be rather large print it modulo 1000000007 (109 + 7).
Input
The first line of input contains two space-separated integers n, k (1 ≤ n, k ≤ 2000).
Output
Output a single integer — the number of good sequences of length k modulo 1000000007 (109 + 7).
Examples
Input
3 2
Output
5
Input
6 4
Output
39
Input
2 1
Output
2
Note
In the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3].
Submitted Solution:
```
Mod=10**+7
n,k=map(int,input().split())
dp=[[0]*(n+1) for i in range(k+1)]
l=[[] for _ in range(n+1)]
for i in range(1,n+1):
dp[1][i]=1
for j in range(1,i+1):
if i%j==0:
l[i].append(j)
for j in range(2,k+1):
for i in range(1,n+1):
for le in range(len(l[i])):
dp[j][i]+=dp[j-1][l[i][le]]
dp[j][i]%=Mod
ans=0
for i in range(1,n+1):
ans+=dp[k][i]
ans%=Mod
print(ans)
``` | instruction | 0 | 16,319 | 12 | 32,638 |
No | output | 1 | 16,319 | 12 | 32,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7 | instruction | 0 | 16,962 | 12 | 33,924 |
Tags: binary search, brute force, combinatorics, constructive algorithms, dp, two pointers
Correct Solution:
```
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
mod=998244353
a=a[::-1]
b=b[::-1]
i,j=0,0
ans=1
while ans and j<m:
cnt=0
while i<n and a[i]>=b[j]:
if cnt:
cnt+=1
elif a[i]==b[j]:
cnt=1
i+=1
if j==m-1:
cnt=min(cnt,1)
if i<=n-1:
cnt=0
ans=(ans*cnt)%mod
j+=1
print(ans)
``` | output | 1 | 16,962 | 12 | 33,925 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7 | instruction | 0 | 16,963 | 12 | 33,926 |
Tags: binary search, brute force, combinatorics, constructive algorithms, dp, two pointers
Correct 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 isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
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
#print(flag)
return flag
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
def nb(i,j):
for ni,nj in [[i+1,j],[i-1,j],[i,j-1],[i,j+1]]:
if 0<=ni<n and 0<=nj<m:
yield ni,nj
@bootstrap
def gdfs(r,p):
if len(g[r])==1 and p!=-1:
yield None
for ch in g[r]:
if ch!=p:
yield gdfs(ch,r)
yield None
t=1
for i in range(t):
n,m=RL()
a=RLL()
b=RLL()
mod=998244353
ind=[0]*m
ans=0
j=m-1
f=1
for i in range(n-1,-1,-1):
if j>=0:
if a[i]<b[j]:
f=0
break
elif a[i]==b[j]:
ind[j]=i
j-=1
else:
if a[i]<b[0]:
f=0
break
if j!=-1:
f=0
if f:
ans=1
for i in range(m-1):
for j in range(ind[i+1],-1,-1):
if a[j]<b[i+1]:
break
tmp=ind[i+1]-j
ans=tmp*ans%mod
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()
'''
``` | output | 1 | 16,963 | 12 | 33,927 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7 | instruction | 0 | 16,964 | 12 | 33,928 |
Tags: binary search, brute force, combinatorics, constructive algorithms, dp, two pointers
Correct Solution:
```
from sys import stdin, stdout
# 6 3
# 12 10 | 20 20 25 30
# 0 4 3 2 1 0
# 0 1 0 2 0 0
#
# 10 12 [10] 50 30 [20] 40 [30]
# 10 20 30
def two_arrasy(n, m, a, b):
r = n-1
res = 1
for i in range(m-1, -1, -1):
#print(b[i])
while r >= 0 and a[r] > b[i]:
r -= 1
if r < 0 or b[i] != a[r]:
return 0
l = r
while l >= 0 and a[l] >= b[i]:
l -= 1
if l < 0 and i != 0:
return 0
if i == 0:
if l >= 0 and a[l] < b[i]:
return 0
else:
break
res *= (r-l)
res %= 998244353
r = l
return res
if __name__ == '__main__':
(n, m) = list(map(int, stdin.readline().split()))
a = list(map(int, stdin.readline().split()))
b = list(map(int, stdin.readline().split()))
res = two_arrasy(n, m, a, b)
stdout.write(str(res) + '\n')
``` | output | 1 | 16,964 | 12 | 33,929 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7 | instruction | 0 | 16,965 | 12 | 33,930 |
Tags: binary search, brute force, combinatorics, constructive algorithms, dp, two pointers
Correct Solution:
```
mod=998244353
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=1
i=n-1
for j in range(m-1,-1,-1):
l,r=-1,-1
while i!=-1:
if a[i]<b[j]:
print(0)
exit()
if a[i]==b[j]:
r=i
break
i-=1
else:
print(0)
exit()
while i!=-1:
if a[i]<b[j]:
l=i+1
if j==0:
print(0)
exit()
break
i-=1
else:
if j!=0:
print(0)
exit()
if j!=0:
ans*=(r-l+1)
ans%=mod
#print(l,r)
print(ans)
``` | output | 1 | 16,965 | 12 | 33,931 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7 | instruction | 0 | 16,966 | 12 | 33,932 |
Tags: binary search, brute force, combinatorics, constructive algorithms, dp, two pointers
Correct Solution:
```
MOD = 998244353
n, m = map(int, input().split())
d = dict()
a = [0] + list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(m):
d[b[i]] = i
c = [-1] * m
for i, x in enumerate(a):
if x in d:
c[d[x]] = i
idx = n
ans = 1
for i in range(m - 1, -1, -1):
while b[i] <= a[idx]:
idx -= 1
if c[i] <= idx:
print(0)
exit(0)
if i > 0:
ans *= c[i] - idx
elif idx > 0:
print(0)
exit(0)
ans %= MOD
print(ans)
``` | output | 1 | 16,966 | 12 | 33,933 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7 | instruction | 0 | 16,967 | 12 | 33,934 |
Tags: binary search, brute force, combinatorics, constructive algorithms, dp, two pointers
Correct Solution:
```
def getint():
return int(input())
def getint2():
return map(int,input().split())
def getintlist():
return (map(int,input().split()))
def getstring():
return input()
n,m=getint2()
a=list(getintlist())
b=list(getintlist())
for i in range(n-2,-1,-1):
if a[i]>a[i+1]:
a[i]=a[i+1]
ans=1
mod=998244353
if a[0]!=b[0]:
print(0)
else:
r,ans,l=n,1,0
for b in b[::-1]:
while r-1>=0 and a[r-1]>b:r-=1
if(r==0 or a[r-1]!=b):
ans=0
break
l=r-1
while(l-1>=0 and a[l-1]==b):
l-=1
if l:
ans=ans*(r-l)%mod
r=l
if(l):
ans=0
print(ans)
``` | output | 1 | 16,967 | 12 | 33,935 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7 | instruction | 0 | 16,968 | 12 | 33,936 |
Tags: binary search, brute force, combinatorics, constructive algorithms, dp, two pointers
Correct Solution:
```
def main():
n, m = map(int, input().split())
aa, bb = (list(map(int, input().split())) for _ in 'ab')
res, term, i, start = 1, bb[0], n - 1, 0
for b in reversed(bb):
while i >= 0 and aa[i] > b:
i -= 1
if i >= 0 and aa[i] == b:
start = i
else:
res = 0
break
while i >= 0 and aa[i] >= b:
i -= 1
if b == term:
break
res = res * (start - i) % 998_244_353
print(res * (i < 0))
if __name__ == '__main__':
main()
``` | output | 1 | 16,968 | 12 | 33,937 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7 | instruction | 0 | 16,969 | 12 | 33,938 |
Tags: binary search, brute force, combinatorics, constructive algorithms, dp, two pointers
Correct Solution:
```
from sys import stdin, gettrace
if gettrace():
inputi = input
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def modInt(mod):
class ModInt:
def __init__(self, value):
self.value = value % mod
def __int__(self):
return self.value
def __eq__(self, other):
return self.value == other.value
def __hash__(self):
return hash(self.value)
def __add__(self, other):
return ModInt(self.value + int(other))
def __sub__(self, other):
return ModInt(self.value - int(other))
def __mul__(self, other):
return ModInt(self.value * int(other))
def __floordiv__(self, other):
return ModInt(self.value // int(other))
def __truediv__(self, other):
return ModInt(self.value * pow(int(other), mod - 2, mod))
def __pow__(self, exp):
return pow(self.value, int(exp), mod)
def __str__(self):
return str(self.value)
return ModInt
def main():
ModInt = modInt(998244353)
n,m = map(int, inputi().split())
aa = [int(a) for a in inputi().split()]
bb = [int(a) for a in inputi().split()]
ap = n-1
bp = m-1
mn = 10000000000
prod = ModInt(1)
while ap >= 0 and bp >= 0:
b = bb[bp]
mn = min(mn, aa[ap])
while mn > b and ap >= 0:
ap -= 1
mn = min(mn, aa[ap])
if mn != b:
print(0)
return
bpc = 0
while mn == b and ap >= 0:
bpc += 1
ap -= 1
mn = min(mn, aa[ap])
if bp != 0:
prod *= bpc
bp -= 1
if ap != -1 or bp != -1:
print(0)
return
print(prod)
if __name__ == "__main__":
main()
``` | output | 1 | 16,969 | 12 | 33,939 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7
Submitted Solution:
```
from sys import stdin, stdout
import collections, heapq, bisect, math
input = stdin.readline
def rint():
return int(input())
def rstr():
return input().strip()
def rlstr():
return list(input().strip().split())
def rlint():
return list(map(int, input().split()))
def main():
n , m = rlint()
a = rlint()
b = rlint()
mod = 998244353
j = m - 1
i = n - 1
res = 1
while j > 0 :
count = 0
while i >= 0 and b[j] < a[i] :
i -= 1
while i >= 0 and b[j] <= a[i] :
count += 1
i -= 1
j -= 1
res = (res * count) % mod
if i < 0 or min(a[:i + 1]) != b[0] :
res = 0
print(res)
if __name__ == "__main__":
main()
``` | instruction | 0 | 16,970 | 12 | 33,940 |
Yes | output | 1 | 16,970 | 12 | 33,941 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7
Submitted Solution:
```
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
def solve():
M = 998244353
res = 1
i = n - 1
j = m - 1
while j >= 0 and res:
count = 0
while i >= 0 and a[i] >= b[j]:
if count > 0:
count += 1
elif a[i] == b[j]:
count = 1
i -= 1
if j == 0:
count = min(count, 1)
if i>=0:
count = 0
res *= count
res %= M
j -= 1
return res
print(solve())
``` | instruction | 0 | 16,971 | 12 | 33,942 |
Yes | output | 1 | 16,971 | 12 | 33,943 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7
Submitted Solution:
```
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 1
m = a[-1]
mod = 998244353
ml = [0]*n
for i in range(n-1,-1,-1):
m = min(m,a[i])
ml[i] = m
r = n
l = 0
for temp in b[::-1]:
while r-1>=0 and ml[r-1]>temp:
r-=1
if r == 0 or ml[r-1]!=temp:
ans = 0
break
l = r-1
while l-1>=0 and ml[l-1] == temp:
l-=1
if l:
ans = ans*(r-l)%mod
r = l
if l:
ans = 0
print(ans)
``` | instruction | 0 | 16,972 | 12 | 33,944 |
Yes | output | 1 | 16,972 | 12 | 33,945 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7
Submitted Solution:
```
######################################################
############Created by Devesh Kumar###################
#############devesh1102@gmail.com####################
##########For CodeForces(Devesh1102)#################
#####################2020#############################
######################################################
import sys
input = sys.stdin.readline
# import sys
import heapq
import copy
import math
import decimal
# import sys.stdout.flush as flush
# from decimal import *
#heapq.heapify(li)
#
#heapq.heappush(li,4)
#
#heapq.heappop(li)
#
# & Bitwise AND Operator 10 & 7 = 2
# | Bitwise OR Operator 10 | 7 = 15
# ^ Bitwise XOR Operator 10 ^ 7 = 13
# << Bitwise Left Shift operator 10<<2 = 40
# >> Bitwise Right Shift Operator
# '''############ ---- Input Functions ---- #######Start#####'''
class Node:
def _init_(self,val):
self.data = val
self.left = None
self.right = None
##to initialize wire : object_name= node(val)##to create a new node
## can also be used to create linked list
def pre_sum(arr):
#"""returns the prefix sum inclusive ie ith position in ans represent sum from 0 to ith position"""
p = [0]
for i in arr:
p.append(p[-1] + i)
p.pop(0)
return p
def pre_back(arr):
#"""returns the prefix sum inclusive ie ith position in ans represent sum from 0 to ith position"""
p = [0]
for i in arr:
p.append(p[-1] + i)
p.pop(0)
return p
def bin_search(arr,l,r,val):#strickly greater
if arr[r] <= val:
return r+1
if r-l < 2:
if arr[l]>val:
return l
else:
return r
mid = int((l+r)/2)
if arr[mid] <= val:
return bin_search(arr,mid,r,val)
else:
return bin_search(arr,l,mid,val)
def search_leftmost(arr,val):
def helper(arr,l,r,val):
# print(arr)
print(l,r)
if arr[l] == val:
return l
if r -l <=1:
if arr[r] == val:
return r
else:
print("not found")
return
mid = int((r+l)/2)
if arr[mid] >= val:
return helper(arr,l,mid,val)
else:
return helper(arr,mid,r,val)
return helper(arr,0,len(arr)-1,val)
def search_rightmost(arr,val):
def helper(arr,l,r,val):
# print(arr)
print(l,r)
if arr[r] == val:
return r
if r -l <=1:
if arr[l] == val:
return r
else:
print("not found")
return
mid = int((r+l)/2)
if arr[mid] > val:
return helper(arr,l,mid,val)
else:
return helper(arr,mid,r,val)
return helper(arr,0,len(arr)-1,val)
def preorder_postorder(root,paths,parent,left,level):
# if len(paths[node]) ==1 and node !=1 :
# return [parent,left,level]
l = 0
queue = [root]
while(queue!=[]):
node = queue[-1]
# print(queue,node)
flag = 0
for i in paths[node]:
if i!=parent[node] and level[i]== sys.maxsize:
parent[i] = node
level[i] = level[node]+1
flag = 1
queue.append(i)
if flag == 0:
left[parent[node]] +=(1+left[node])
queue.pop()
# [parent,left,level] = helper(i,paths,parent,left,level)
# l = left[i] + l +1
# left[node] = l
return [parent,left,level]
def nCr(n, r, p): #computes ncr%p
# initialize numerator
# and denominator
if r == 0:
return 1
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,p - 2, p)) % p
def f_exp(p,k0,k1,l):
#computes p^k in log k time
ans = 1
k = k0-k1
while(k != 0):
if k%2 == 0:
k = k//2
p = p * p
else:
ans = ans *p
k = (k -1)//2
p = p * p
if ans > l:
ans = -1
break
return ans
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def insr2():
s = input()
return((s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
############ ---- Input Functions ---- #######End
# #####
def pr_list(a):
print(*a, sep=" ")
def main():
# tests = inp()
tests = 1
mod = 998244353
limit = 10**18
ans = 0
for test in range(tests):
[n,m] = inlt()
a = inlt()
b = inlt()
last= {}
for i in b:
last[i] = -1
for i in range(len(a)):
if a[i] in last:
last[a[i]] = i
ans = 1
flag = 0
for i in range(1,m):
if last[b[i]] == -1 or last[b[i-1]] == -1:
flag = 1
break
if last[b[i]] < last[b[i-1]]:
flag = 1
break
dec = 0
f = 0
for j in range(last[b[i]],last[b[i-1]],-1):
if dec == 0 and a[j]< b[i]:
ans = (ans *(f) )%mod
dec = 1
if a[j]<b [i-1]:
flag = 1
break
f = f + 1
if dec == 0:
ans = (ans *(f) )%mod
if flag == 1:
break
if flag != 1:
for i in range(last[b[-1]], len(a)):
if a[i] < b[-1]:
flag = 1
break
if min(a) < b[0]:
print(0)
elif len(b) == 1:
if b[0] not in a:
print(0)
else:
print(1)
elif flag == 1:
print(0)
else:
print(ans)
if __name__== "__main__":
main()
``` | instruction | 0 | 16,973 | 12 | 33,946 |
Yes | output | 1 | 16,973 | 12 | 33,947 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7
Submitted Solution:
```
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
def bs(val):
comeco = 0
fim = len(b) - 1
while comeco <= fim:
meio = int((comeco+fim)/2)
if b[meio] > val:
fim = meio - 1
elif b[meio] < val:
comeco = meio + 1
else:
return meio
return -1
def solve():
M = 998244353
prob = [0 for x in b]
ant = 0
for i in a:
ind = bs(i)
if i < b[ant]:
if i < b[0]:
return 0
for p in range(ind, len(prob)):
prob[p] = 0
if not ind == -1:
prob[ind] += 1
if not b[ind] == b[ant]:
ant = ind
result = 1
for p in prob:
result = (result * p) % M
return result
print(solve())
``` | instruction | 0 | 16,974 | 12 | 33,948 |
No | output | 1 | 16,974 | 12 | 33,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7
Submitted Solution:
```
n,s=map(int,input().split())
r=1
mod=998244353
a=list(map(int,input().split()))
b=list(map(int,input().split()))
i=n-1
j=s-1
while r and j>=0:
count=0
while i>=0 and a[i]>=b[j]:
if count:count+=1
elif a[i]==b[j]:count=1
i-=1
if j==0:
count=min(count,1)
if i>=0:cnt=0
r=(r*count)%mod
j-=1
print(r)
``` | instruction | 0 | 16,975 | 12 | 33,950 |
No | output | 1 | 16,975 | 12 | 33,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7
Submitted Solution:
```
x,y =map(int,input().split())
lt=list(map(int,input().split()))
mt=list(map(int,input().split()))
if(min(lt)!=mt[0]):
print("0")
else:
lt.reverse()
mt.reverse()
rd=0;ans=[]
for i in range(y-1):
id=lt.index(mt[i])
if(id<rd):
rd=-4
break
k=id+1;ct=0
while(k<x):
if(lt[k]<mt[i]):
break
else:
ct+=1
k+=1
rd=0
ans.append(ct+1)
if(rd<0):
print("0")
else:
sd=1
for i in range(len(ans)):
sd*=ans[i]
print(sd)
``` | instruction | 0 | 16,976 | 12 | 33,952 |
No | output | 1 | 16,976 | 12 | 33,953 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).
You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.
For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a:
1. [12, 10, 20], [20, 25], [30];
2. [12, 10], [20, 20, 25], [30].
You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of arrays a and b respectively.
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9) — the array a.
The third line contains m integers b_1, b_2, ... , b_m (1 ≤ b_i ≤ 10^9; b_i < b_{i+1}) — the array b.
Output
In only line print one integer — the number of ways to divide the array a modulo 998244353.
Examples
Input
6 3
12 10 20 20 25 30
10 20 30
Output
2
Input
4 2
1 3 3 7
3 7
Output
0
Input
8 2
1 2 2 2 2 2 2 2
1 2
Output
7
Submitted Solution:
```
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
from math import gcd, ceil
def pre(s):
n = len(s)
pi = [0] * n
for i in range(1, n):
j = pi[i - 1]
while j and s[i] != s[j]:
j = pi[j - 1]
if s[i] == s[j]:
j += 1
pi[i] = j
return pi
def prod(a):
ans = 1
for each in a:
ans = (ans * each)
return ans
def lcm(a, b): return a * b // gcd(a, b)
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
from math import inf
for _ in range(int(input()) if not True else 1):
#n = int(input())
n, m = map(int, input().split())
#a, b = map(int, input().split())
#c, d = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if m > n:
print(0)
quit()
if m == n:
print(1 if min(a) == b[0] else 0)
quit()
mod = 998244353
i, j = n-1, m-1
ans = 1
while j+1:
if not ans:break
found = False
cover = 0
while i+1 and a[i] >= b[j]:
if not found and a[i] == b[j]:
found = True
cover += 1
elif found:
cover += 1
i -= 1
if j == 0:
cover = min(cover, 1)
if i+1:cover=0
ans = (ans * cover) % mod
j -= 1
print(ans)
``` | instruction | 0 | 16,977 | 12 | 33,954 |
No | output | 1 | 16,977 | 12 | 33,955 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers. | instruction | 0 | 17,062 | 12 | 34,124 |
Tags: bitmasks, implementation, two pointers
Correct Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
l, r = -1, -1
if k == 1:
print('1', '1')
else:
b = set([])
c = set([])
for i in range(n):
if l != -1:
b.add(a[i])
elif l == -1 and a[i] != a[0]:
l = i
b.add(a[0])
b.add(a[i])
if len(b) == k:
r = i + 1
break
if l != -1 and r != -1:
for i in range(r - 1, l - 2, -1):
c.add(a[i])
if len(c) == k:
l = i + 1
break
print(l, r)
else:
print('-1', '-1')
``` | output | 1 | 17,062 | 12 | 34,125 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers. | instruction | 0 | 17,063 | 12 | 34,126 |
Tags: bitmasks, implementation, two pointers
Correct Solution:
```
from collections import Counter
n, k = map(int, input().split())
if k == 1:
print(1, 1)
exit()
a, l = list(map(int, input().split())), 0
while l < n - 1 and a[l] == a[l + 1]:
l += 1
c, r = Counter({a[l]: 1}), l + 1
while r < n:
c[a[r]] += 1
if len(c) == k:
while len(c) == k:
c[a[l]] -= 1
c += Counter()
l += 1
print(l, r + 1)
exit()
r += 1
print(-1, -1)
``` | output | 1 | 17,063 | 12 | 34,127 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers. | instruction | 0 | 17,064 | 12 | 34,128 |
Tags: bitmasks, implementation, two pointers
Correct Solution:
```
num,k = map(int,input().split())
listnum = list(map(int,input().split()))
cnt = [0]*100001
count = 0
for i in range(num):
cnt[listnum[i]] = cnt[listnum[i]] + 1
if cnt[listnum[i]] == 1:
count += 1
if count == k:
right = i
break
if count != k:
print("-1 -1")
else:
for i in range(right+1):
if cnt[listnum[i]] > 1:
cnt[listnum[i]] = cnt[listnum[i]] - 1
else:
print(str(i + 1),str(right + 1))
exit()
``` | output | 1 | 17,064 | 12 | 34,129 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers. | instruction | 0 | 17,065 | 12 | 34,130 |
Tags: bitmasks, implementation, two pointers
Correct Solution:
```
from collections import defaultdict
n, k = map(int,input().split())
a = list(map(int,input().split()))
d = defaultdict(int)
uni = 0
res = [0, 0]
for i in range(n):
if d[a[i]] == 0:
uni+= 1
d[a[i]] = 1
if uni == k:
res[1] = i + 1
for j in range(n):
if d[a[j]] - 1 == 0:
res[0] = j + 1
print(*res)
exit()
else:
d[a[j]]-=1
else:
d[a[i]] += 1
print("-1 -1")
``` | output | 1 | 17,065 | 12 | 34,131 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers. | instruction | 0 | 17,066 | 12 | 34,132 |
Tags: bitmasks, implementation, two pointers
Correct Solution:
```
n,k=map(int,input().split())
l1=list(map(int,input().split()))
s=set()
l=-1
r=-1
ans=n
if n==1:
if k==1:
print(1,1)
else:
print(-1,-1)
else:
for i in range(n):
s.add(l1[i])
if len(s)==k:
l=1
r=i+1
break
if l==-1:
print(l,r)
else:
s=set()
for i in range(r-1,max(-1,l-2),-1):
s.add(l1[i])
if len(s)==k:
l=i+1
break
print(l,r)
``` | output | 1 | 17,066 | 12 | 34,133 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers. | instruction | 0 | 17,067 | 12 | 34,134 |
Tags: bitmasks, implementation, two pointers
Correct Solution:
```
n, k = map(int, input().split())
if k > n: print('-1 -1')
else:
t = list(map(int, input().split()))
i, p = 1, {t[0] : 0}
k -= 1
while i < n and k:
if not t[i] in p: k -= 1
p[t[i]] = i
i += 1
print('-1 -1' if k else str(min(p.values()) + 1) + ' ' + str(max(p.values()) + 1))
``` | output | 1 | 17,067 | 12 | 34,135 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers. | instruction | 0 | 17,068 | 12 | 34,136 |
Tags: bitmasks, implementation, two pointers
Correct Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0, 0)
c = {}
cnt = 0
j = 0
for i in range(1, n + 1):
if (a[i] in c):
c[a[i]] += 1
else:
c[a[i]] = 1
if (c[a[i]] == 1):
cnt += 1
while (cnt == k):
j += 1
if (a[j] not in c):
c[a[j]] = 0
c[a[j]] -= 1
if (c[a[j]] == 0):
print(j, i)
exit()
print("-1 -1")
``` | output | 1 | 17,068 | 12 | 34,137 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers. | instruction | 0 | 17,069 | 12 | 34,138 |
Tags: bitmasks, implementation, two pointers
Correct Solution:
```
#import numpy as np
mod = 10 ** 9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
c = {}
cnt = 0
r = 0
f = 0
for i in range(n):
if(a[i] not in c):
cnt+=1
c[a[i]] = 1
else:
c[a[i]] += 1
while(cnt == k):
if(c[a[r]] == 1):
print(r+1, i+1)
f = 1
break
if(f == 1):
break
c[a[r]]-=1
r+=1
if(f == 1):
break
if(f != 1):
print("-1 -1")
``` | output | 1 | 17,069 | 12 | 34,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers.
Submitted Solution:
```
n, k = list(map(int, input().split()))
arr = list(map(int, input().split()))
count = [0]*(int(1e5+1))
for i in arr:
count[i] += 1
s = sum([1 if i>0 else 0 for i in count])
if s < k:
print('-1 -1')
exit()
r = n-1
while True:
if count[arr[r]] == 1:
s -= 1
if s < k:
s += 1
break
count[arr[r]] -= 1
r -= 1
l=0
while True:
if count[arr[l]] == 1:
s -= 1
if s < k:
s += 1
break
count[arr[l]] -= 1
l += 1
print(l+1, r+1)
``` | instruction | 0 | 17,070 | 12 | 34,140 |
Yes | output | 1 | 17,070 | 12 | 34,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers.
Submitted Solution:
```
#####################
#1: Array,
#R -> ++Count[]
#L -> --Count[]
line1 = input().split(" ")
arr = list(map(int,input().split()))
n = int(line1[0])
k = int(line1[1])
counts = {}
l = 0
r = 0
count = 0
for i in range(len(arr)):
if arr[i] in counts:
counts[arr[i]] += 1
else:
count += 1
counts[arr[i]] = 1
if count >= k:
r = i
break
for i in range(len(arr)):
l = i
counts[arr[i]] -= 1
if counts[arr[i]] == 0:
l = i
break
if count < k:
print("-1 -1")
else:
print(l + 1, r + 1)
``` | instruction | 0 | 17,071 | 12 | 34,142 |
Yes | output | 1 | 17,071 | 12 | 34,143 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers.
Submitted Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = 0
r = n-1
if (k==1):
print(1,1)
exit()
if (n<k):
print(-1,-1)
exit()
distinct_element = []
for i in range(0, max(a)+1):
distinct_element.append(0)
cnt_distinct = 0
for i in range(0, n-1):
check = False
cnt_distinct = 0
for j in range(0, max(a)+1):
distinct_element[j] = 0
distinct_element[a[i]] += 1
cnt_distinct += 1
for j in range(i+1, n):
if (distinct_element[a[j]] == 0):
cnt_distinct += 1
distinct_element[a[j]] += 1
if (cnt_distinct == k):
check = True
l = i
r = j
break
if check == False and i == 0:
print(-1, -1)
exit()
if check == True:
break
res_r = r
res_l = l
# print(res_l,res_r)
# print(distinct_element)
for i in range(l, r+1):
distinct_element[a[i]] -= 1
if (distinct_element[a[i]] > 0):
res_l += 1
else:
break
print(res_l+1, res_r+1)
``` | instruction | 0 | 17,072 | 12 | 34,144 |
Yes | output | 1 | 17,072 | 12 | 34,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers.
Submitted Solution:
```
from collections import defaultdict
n,k = map(int,input().split())
hash = defaultdict(int)
l = list(map(int,input().split()))
n = len(l)
start = 0
end = -1
seti = set()
for i in range(n):
seti.add(l[i])
hash[l[i]]+=1
if len(seti) == k:
end = i
break
if end == -1:
print(-1,-1)
exit()
else:
new_start = start
new_end = end
for i in range(start,end):
hash[l[i]]-=1
if hash[l[i]] == 0:
new_start = i
hash[l[i]]+=1
break
for i in range(end,new_start-1,-1):
hash[l[i]]-=1
if hash[l[i]] == 0:
new_end = i
hash[l[i]]+=1
break
print(new_start+1,new_end+1)
``` | instruction | 0 | 17,073 | 12 | 34,146 |
Yes | output | 1 | 17,073 | 12 | 34,147 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers.
Submitted Solution:
```
def checkF(a, b):
l = a[0]
max = 100
maxi = -1
maxk = -1
for k in range(l):
res = 0
s = set()
for i in range(k,l):
if (b[i] not in s):
s.add(b[i])
res += 1
if res == a[1] and i - k <= a[1]:
if i - k <max:
max = i - k
maxi = i+1
maxk = k+1
if max == a[1]-1:return print(str(k+1)+" "+str(i+1))
break
if (i==l-a[1]+2): break
return print(str(maxk)+" "+str(maxi))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
checkF(a, b)
``` | instruction | 0 | 17,074 | 12 | 34,148 |
No | output | 1 | 17,074 | 12 | 34,149 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers.
Submitted Solution:
```
num,k = map(int,input().split())
listnum = list(map(int,input().split()))
count = 1
for i in range(1,num):
linhcanh = 1
for j in range(i):
if listnum[i] == listnum[j]:
linhcanh = 0
break
if linhcanh == 1:
count += 1
if count == k:
right = i
break
leftTrue = 0
if count != k:
print("-1 -1")
else:
for i in range(right +1):
lc1 = 1
for j in range(i + 1,right + 1):
if listnum[leftTrue] == listnum[j]:
lc1 = 0
break
if lc1 == 1:
print(str(leftTrue + 1),str(right + 1))
exit()
else:
leftTrue += 1
``` | instruction | 0 | 17,075 | 12 | 34,150 |
No | output | 1 | 17,075 | 12 | 34,151 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers.
Submitted Solution:
```
def main():
list_len , segment_len = [int(i) for i in input().split()]
segment = [int(i) for i in input().split()]
segment_min = -1
segment_max = -1
sub_segment_member = []
if segment_len == 1:
print( '1 1' )
return
for i in range(0, list_len -1):
sub_segment_member = []
for j in range(i, list_len -1):
if len(sub_segment_member) > 2:
sub_segment_member.pop()
#print(sub_segment_member)
#print('i:%d j:%d segment_min:%d segment_max:%d' %(segment[i],segment[j], segment_min, segment_max))
if (segment[j] in sub_segment_member) & ( len(sub_segment_member) == 1):
segment_min = i
break
if not segment[j] in sub_segment_member:
sub_segment_member.append(segment[j])
if len(sub_segment_member) == segment_len :
segment_min = i
segment_max = j
break
if len(sub_segment_member) == segment_len:
break
if segment_max == -1:
print( '-1 -1' )
else:
print( str(segment_min +1) + ' '+ str(segment_max+1) )
main()
``` | instruction | 0 | 17,076 | 12 | 34,152 |
No | output | 1 | 17,076 | 12 | 34,153 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers.
Submitted Solution:
```
n,k = map(int, input().split())
a = list(map(int, input().split()))
b = list()
b.extend([0]*100003)
cnt=0
j=0
if (k==1):
print("1 1")
for i in range(n):
if (b[a[i]] == 0):
cnt += 1
b[a[i]]+=1
if (cnt == k):
j = i
break
if (cnt != k):
print("-1 -1")
exit()
res = 0
for i in range(j):
if ((a[i] != a[i+1])&(b[a[i]] == 1)):
print(i+1,j+1)
exit()
else:
b[a[i]] -= 1
``` | instruction | 0 | 17,077 | 12 | 34,154 |
No | output | 1 | 17,077 | 12 | 34,155 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.
Examples
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers.
Submitted Solution:
```
from __future__ import print_function
from collections import defaultdict
# python 3 default
def main():
n, k = [int(_) for _ in raw_input().split()]
a = [int(_) for _ in raw_input().split()]
l = 0; r = 0
dicta = defaultdict(int)
dicta[a[0]] = 1
while len(dicta) < k:
r += 1
if r == n:
print("-1 -1")
return
if a[r] == a[l]:
l += 1
else:
dicta[a[r]] += 1
while dicta[a[l]] > 1:
l += 1
print(l + 1, r + 1)
if __name__ == '__main__':
main()
``` | instruction | 0 | 17,078 | 12 | 34,156 |
No | output | 1 | 17,078 | 12 | 34,157 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.