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.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good. | instruction | 0 | 99,462 | 12 | 198,924 |
Tags: brute force, greedy, implementation
Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
ak=[]
i=0
while 2**i <=2000000000:
ak.append(2**i)
i+=1
n=int(input())
a=list(map(int,input().split()))
d=dict()
for i,v in enumerate(a):
d[v]=d.get(v,set())
d[v].add(i)
ans=[0]*n
for i in range(n):
for j in ak:
if j-a[i] in d:
if (j-a[i]==a[i] and len(d[a[i]])>=2) or j-a[i]!=a[i] :
ans[i]=1
break
print(ans.count(0))
``` | output | 1 | 99,462 | 12 | 198,925 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good. | instruction | 0 | 99,463 | 12 | 198,926 |
Tags: brute force, greedy, implementation
Correct Solution:
```
input("")
output = 0
array = dict()
for element in list(map(int, input('').split(" "))):
array[element] = array.get(element, 0) + 1
for element in array:
flag = False
for j in range(31):
if ((pow(2, j) - element) in array.keys()) and ((pow(2,j) - element != element) or (pow(2,j) - element == element) and (array[element] > 1)):
flag = True
break
if flag == False:
output = output + array[element]
print(output)
``` | output | 1 | 99,463 | 12 | 198,927 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good. | instruction | 0 | 99,464 | 12 | 198,928 |
Tags: brute force, greedy, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
r = set(2**i for i in range(31))
c = {}
for i in range(n):
t = c.get(a[i], 0) + 1
c[a[i]] = t
ans = 0
for i in range(n):
f = False
for k in r:
key = k - a[i]
if key in c.keys() and (c[key] > 1 or (c[key] == 1 and key != a[i])):
f = True
if not f:
ans += 1
print(ans)
``` | output | 1 | 99,464 | 12 | 198,929 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good. | instruction | 0 | 99,465 | 12 | 198,930 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
d={}
ans=0
for i in l:
d[i]=d.get(i,0)+1
for i in range(n):
flag=0
for j in range(32):
p=1<<j
x=p-l[i]
m=d.get(x,0)
if (m>=2 or ( m==1 and p-l[i]!=l[i])):
flag=1
if flag==0:
ans=ans+1
print(ans)
``` | output | 1 | 99,465 | 12 | 198,931 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good. | instruction | 0 | 99,466 | 12 | 198,932 |
Tags: brute force, greedy, implementation
Correct Solution:
```
b = []
l = 2
t = 0
while t<=30:
b.append(l)
l=l*2
t+=1
n = int(input())
a = list(map(int,input().split()))
from collections import defaultdict
c = defaultdict(int)
d = set()
for i in a:
c[i]+=1
for i in c:
if i in d:
continue
else:
for j in b:
if j>i:
k=j-i
if k in c:
if k==i:
if c[i]>1:
d.add(i)
break
else:
d.add(i)
d.add(k)
break
l = 0
for i in d:
l+=c[i]
print(n-l)
``` | output | 1 | 99,466 | 12 | 198,933 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good. | instruction | 0 | 99,467 | 12 | 198,934 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
s=set()
d={}
for i in range(n):
if a[i] in s:
d[a[i]]=1
s.add(a[i])
cnt=0
for i in range(n):
temp=1
flag=False
for j in range(32):
if temp-a[i] in s:
if 2*a[i]==temp and d.get(a[i],-1)==-1:
temp*=2
continue
else:
flag=True
break
temp*=2
if(flag==False):
cnt+=1
print(cnt)
``` | output | 1 | 99,467 | 12 | 198,935 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good.
Submitted Solution:
```
n = int(input())
a = [int(i) for i in input().split()]
d = dict()
for i in range(n):
if d.get(a[i]):
d[a[i]] += 1
else:
d[a[i]] = 1
ans = 0
for i in range(n):
flag = False
st = 1
for j in range(31):
st *= 2
num = st - a[i]
if d.get(num):
if d[num] >= 2 or (d[num] == 1 and num != a[i]):
flag = True
if not flag:
ans += 1
print(ans)
``` | instruction | 0 | 99,468 | 12 | 198,936 |
Yes | output | 1 | 99,468 | 12 | 198,937 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good.
Submitted Solution:
```
def main():
input()
two, cnt, res = [1 << i for i in range(30, -1, -1)], {}, 0
for a in map(int, input().split()):
cnt[a] = cnt.get(a, 0) + 1
for a, c in cnt.items():
for t in two:
b = t - a
if b < 1:
res += c
break
elif b in cnt:
if a != b or c != 1:
break
print(res)
if __name__ == '__main__':
main()
``` | instruction | 0 | 99,469 | 12 | 198,938 |
Yes | output | 1 | 99,469 | 12 | 198,939 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good.
Submitted Solution:
```
import sys
input = sys.stdin.readline
def solve():
n = int(input())
a = list(map(int, input().split()))
s = dict()
for i in a:
s[i] = s.get(i, 0) + 1
r = 0
for i in a:
w = False
for j in range(32):
v = (1<<j) - i
if v != i and v in s:
w = True
break
if v == i and s[v] > 1:
w = True
break
if not w:
r += 1
print(r)
solve()
``` | instruction | 0 | 99,470 | 12 | 198,940 |
Yes | output | 1 | 99,470 | 12 | 198,941 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
count = 0
z = {}
for i in range(n):
if z.get(a[i]):
z[a[i]] += 1
else:
z[a[i]] = 1
b = [2**i for i in range(1, 31)]
for i in a:
check = True
for j in b:
if z.get(j - i):
if z[j - i] > 1 or z[j - i] == 1 and j - i != i:
check = False
if check:
count += 1
print(count)
``` | instruction | 0 | 99,471 | 12 | 198,942 |
Yes | output | 1 | 99,471 | 12 | 198,943 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good.
Submitted Solution:
```
def inp():
ls=list(map(int,input().split()))
return ls
n=int(input())
ls=inp()
cnt={}
powers=[]
ans=0
for i in range(32):
powers.append(2**i)
for i in range(n):
if ls[i] in cnt:
cnt[ls[i]]+=1
else:
cnt[ls[i]]=1
ls=list(set(ls))
for i in range(len(ls)):
flag=False
for j in range(1,32):
if (powers[j] - ls[i]) in cnt and cnt.get(powers[j] - ls[i]) >= 2 or (cnt.get(powers[j] - ls[i]) == 1 and powers[j] - ls[i] != ls[i]):
flag = True
break
if not flag:
ans += 1
print(ans)
``` | instruction | 0 | 99,472 | 12 | 198,944 |
No | output | 1 | 99,472 | 12 | 198,945 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good.
Submitted Solution:
```
import bisect
power=[1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576]
n=int(input())
a=list(map(int,input().split()))
a.sort()
count=0
l=len(power)
maxx=max(a)
for i in range(n):
ans=False
num=a[i]
ind=bisect.bisect_left(power,num)
for j in range(ind,l):
num2=power[j]-a[i]
if num2>maxx:
break
ind2=bisect.bisect_left(a,num2)
if (ind2==i and a.count(a[i])==1) :
continue
if a[ind2]==num2:
ans=True
break
if ind2-1>=0:
if (ind2-1)==i and a.count(a[ind2-1]==1):
continue
if a[ind2-1]==num2:
ans=True
break
if ind2+1<n:
if ((ind2 + 1 )== i and a.count(a[ind2 + 1] == 1)):
continue
if a[ind2+1]==num2:
ans=True
break
if not ans:
count+=1
print(count)
``` | instruction | 0 | 99,473 | 12 | 198,946 |
No | output | 1 | 99,473 | 12 | 198,947 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good.
Submitted Solution:
```
def Check(k, v, A, ans):
judge = False
if (not (k & (k-1))) and v > 1:
judge = True
for a in list(A):
if not ((a+k) & (a+k-1)):
ans, A = Check2(a, A, ans)
judge = True
if not judge:
ans += 1
return ans, A
def Check2(p, A, ans):
del A[p]
for a in list(A):
if not ((a+p) & (a+p-1)):
ans, A = Check2(a, A, ans)
return ans, A
n = int(input())
A = list(map(int, input().split()))
A_dict = dict()
for a in A:
if a in A_dict:
A_dict[a] += 1
else:
A_dict[a] = 1
ans = 0
while len(A_dict) != 0:
k, v = A_dict.popitem()
ans, A_dict = Check(k, v, A_dict, ans)
print(ans)
``` | instruction | 0 | 99,474 | 12 | 198,948 |
No | output | 1 | 99,474 | 12 | 198,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3),
* [1, 1, 1, 1023],
* [7, 39, 89, 25, 89],
* [].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
* [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two),
* [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two),
* [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two).
You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1 ≤ n ≤ 120000) — the length of the given sequence.
The second line contains the sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0
Note
In the first example, it is enough to delete one element a_4=5. The remaining elements form the sequence [4, 7, 1, 4, 9], which is good.
Submitted Solution:
```
import bisect
pw=[1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576]
l=len(pw)
n=int(input())
a=list(map(int,input().split()))
a.sort()
count=0
for i in range(n):
ind=bisect.bisect_left(pw,a[i])
ans=False
for j in range(ind,l):
diff=pw[j]-a[i]
ind2=bisect.bisect_left(a,diff)
if ind2<n and ind2!=i and a[ind2]==diff:
ans=True
continue
elif (ind2-1)<n and (ind2-1)!=i and a[ind2-1]==diff:
ans=True
continue
elif (ind2+1)<n and (ind2+1)!=i and a[ind2+1]==diff:
ans=True
continue
if not ans:
count+=1
print(count)
``` | instruction | 0 | 99,475 | 12 | 198,950 |
No | output | 1 | 99,475 | 12 | 198,951 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.
There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative integer a_i to the students. It turned out, the exam was to tell the whole sequence back to the professor.
Sounds easy enough for those who attended every class, doesn't it?
Obviously Mishka didn't attend any classes. However, professor left some clues on the values of a to help out students like Mishka:
* a was sorted in non-decreasing order (a_1 ≤ a_2 ≤ ... ≤ a_n);
* n was even;
* the following sequence b, consisting of \frac n 2 elements, was formed and given out to students: b_i = a_i + a_{n - i + 1}.
Professor also mentioned that any sequence a, which produces sequence b with the presented technique, will be acceptable.
Help Mishka to pass that last exam. Restore any sorted sequence a of non-negative integers, which produces sequence b with the presented technique. It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of sequence a. n is always even.
The second line contains \frac n 2 integers b_1, b_2, ..., b_{\frac n 2} (0 ≤ b_i ≤ 10^{18}) — sequence b, where b_i = a_i + a_{n - i + 1}.
It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Output
Print n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^{18}) in a single line.
a_1 ≤ a_2 ≤ ... ≤ a_n should be satisfied.
b_i = a_i + a_{n - i + 1} should be satisfied for all valid i.
Examples
Input
4
5 6
Output
2 3 3 3
Input
6
2 1 2
Output
0 0 1 1 1 2 | instruction | 0 | 99,503 | 12 | 199,006 |
Tags: greedy
Correct Solution:
```
n = int(input())
v = list(map(int, input().split()))
new_v = [0] * (n)
new_v[-1] = v[0]
for i in range(1, len(v)):
if v[i] > v[i - 1]:
new_v[i] = new_v[i - 1] + v[i] - v[i - 1]
else:
new_v[i] = new_v[i - 1]
new_v[-i - 1] = v[i] - new_v[i]
print(*new_v)
``` | output | 1 | 99,503 | 12 | 199,007 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.
There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative integer a_i to the students. It turned out, the exam was to tell the whole sequence back to the professor.
Sounds easy enough for those who attended every class, doesn't it?
Obviously Mishka didn't attend any classes. However, professor left some clues on the values of a to help out students like Mishka:
* a was sorted in non-decreasing order (a_1 ≤ a_2 ≤ ... ≤ a_n);
* n was even;
* the following sequence b, consisting of \frac n 2 elements, was formed and given out to students: b_i = a_i + a_{n - i + 1}.
Professor also mentioned that any sequence a, which produces sequence b with the presented technique, will be acceptable.
Help Mishka to pass that last exam. Restore any sorted sequence a of non-negative integers, which produces sequence b with the presented technique. It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of sequence a. n is always even.
The second line contains \frac n 2 integers b_1, b_2, ..., b_{\frac n 2} (0 ≤ b_i ≤ 10^{18}) — sequence b, where b_i = a_i + a_{n - i + 1}.
It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Output
Print n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^{18}) in a single line.
a_1 ≤ a_2 ≤ ... ≤ a_n should be satisfied.
b_i = a_i + a_{n - i + 1} should be satisfied for all valid i.
Examples
Input
4
5 6
Output
2 3 3 3
Input
6
2 1 2
Output
0 0 1 1 1 2 | instruction | 0 | 99,504 | 12 | 199,008 |
Tags: greedy
Correct Solution:
```
n = int(input())
arr = list(map(int,input().split()))
ans=[0]*(n)
l = 0
h = float('inf')
for i in range(n//2):
ans[i] = max(l,arr[i]-h)
ans[n-1-i] = arr[i]-ans[i]
l = ans[i]
h = ans[n-1-i]
print(*ans)
``` | output | 1 | 99,504 | 12 | 199,009 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.
There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative integer a_i to the students. It turned out, the exam was to tell the whole sequence back to the professor.
Sounds easy enough for those who attended every class, doesn't it?
Obviously Mishka didn't attend any classes. However, professor left some clues on the values of a to help out students like Mishka:
* a was sorted in non-decreasing order (a_1 ≤ a_2 ≤ ... ≤ a_n);
* n was even;
* the following sequence b, consisting of \frac n 2 elements, was formed and given out to students: b_i = a_i + a_{n - i + 1}.
Professor also mentioned that any sequence a, which produces sequence b with the presented technique, will be acceptable.
Help Mishka to pass that last exam. Restore any sorted sequence a of non-negative integers, which produces sequence b with the presented technique. It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of sequence a. n is always even.
The second line contains \frac n 2 integers b_1, b_2, ..., b_{\frac n 2} (0 ≤ b_i ≤ 10^{18}) — sequence b, where b_i = a_i + a_{n - i + 1}.
It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Output
Print n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^{18}) in a single line.
a_1 ≤ a_2 ≤ ... ≤ a_n should be satisfied.
b_i = a_i + a_{n - i + 1} should be satisfied for all valid i.
Examples
Input
4
5 6
Output
2 3 3 3
Input
6
2 1 2
Output
0 0 1 1 1 2 | instruction | 0 | 99,505 | 12 | 199,010 |
Tags: greedy
Correct Solution:
```
n, b = int(input()), list(map(int, input().split()))
a = [0 for _ in range(n)]
a[n - 1], a[0] = b[0], 0
for i in range(1, n // 2):
if a[n - i] < b[i] and b[i] - a[n - i] >= a[i - 1]:
a[n - 1 - i], a[i] = a[n - i], b[i] - a[n - i]
else:
a[n - 1 - i], a[i] = b[i] - a[i - 1], a[i - 1]
print(*a)
``` | output | 1 | 99,505 | 12 | 199,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.
There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative integer a_i to the students. It turned out, the exam was to tell the whole sequence back to the professor.
Sounds easy enough for those who attended every class, doesn't it?
Obviously Mishka didn't attend any classes. However, professor left some clues on the values of a to help out students like Mishka:
* a was sorted in non-decreasing order (a_1 ≤ a_2 ≤ ... ≤ a_n);
* n was even;
* the following sequence b, consisting of \frac n 2 elements, was formed and given out to students: b_i = a_i + a_{n - i + 1}.
Professor also mentioned that any sequence a, which produces sequence b with the presented technique, will be acceptable.
Help Mishka to pass that last exam. Restore any sorted sequence a of non-negative integers, which produces sequence b with the presented technique. It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of sequence a. n is always even.
The second line contains \frac n 2 integers b_1, b_2, ..., b_{\frac n 2} (0 ≤ b_i ≤ 10^{18}) — sequence b, where b_i = a_i + a_{n - i + 1}.
It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Output
Print n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^{18}) in a single line.
a_1 ≤ a_2 ≤ ... ≤ a_n should be satisfied.
b_i = a_i + a_{n - i + 1} should be satisfied for all valid i.
Examples
Input
4
5 6
Output
2 3 3 3
Input
6
2 1 2
Output
0 0 1 1 1 2 | instruction | 0 | 99,506 | 12 | 199,012 |
Tags: greedy
Correct Solution:
```
import sys
import os
from math import*
input=sys.stdin.buffer.readline
n=int(input())
arr=list(map(int,input().split()))
ans=[0]*n
c=0
d=arr[0]
ans[0]=c
ans[n-1]=d
for i in range(1,n//2):
if arr[i]<=d:
d=arr[i]-c
else:
c=max(c,arr[i]-d)
d=arr[i]-c
ans[i]=c
ans[n-i-1]=d
for x in ans:
print(x,end=' ')
``` | output | 1 | 99,506 | 12 | 199,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.
There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative integer a_i to the students. It turned out, the exam was to tell the whole sequence back to the professor.
Sounds easy enough for those who attended every class, doesn't it?
Obviously Mishka didn't attend any classes. However, professor left some clues on the values of a to help out students like Mishka:
* a was sorted in non-decreasing order (a_1 ≤ a_2 ≤ ... ≤ a_n);
* n was even;
* the following sequence b, consisting of \frac n 2 elements, was formed and given out to students: b_i = a_i + a_{n - i + 1}.
Professor also mentioned that any sequence a, which produces sequence b with the presented technique, will be acceptable.
Help Mishka to pass that last exam. Restore any sorted sequence a of non-negative integers, which produces sequence b with the presented technique. It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of sequence a. n is always even.
The second line contains \frac n 2 integers b_1, b_2, ..., b_{\frac n 2} (0 ≤ b_i ≤ 10^{18}) — sequence b, where b_i = a_i + a_{n - i + 1}.
It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Output
Print n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^{18}) in a single line.
a_1 ≤ a_2 ≤ ... ≤ a_n should be satisfied.
b_i = a_i + a_{n - i + 1} should be satisfied for all valid i.
Examples
Input
4
5 6
Output
2 3 3 3
Input
6
2 1 2
Output
0 0 1 1 1 2 | instruction | 0 | 99,507 | 12 | 199,014 |
Tags: greedy
Correct Solution:
```
from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import defaultdict
from itertools import combinations
import sys
import math
MAX = sys.maxsize
MAXN = 10**6+10
MOD = 10**9+7
def isprime(n):
n = abs(int(n))
if n < 2:
return False
if n == 2:
return True
if not n & 1:
return False
for x in range(3, int(n**0.5) + 1, 2):
if n % x == 0:
return False
return True
def mhd(a,b,x,y):
return abs(a-x)+abs(b-y)
def numIN():
return(map(int,sys.stdin.readline().strip().split()))
def charIN():
return(sys.stdin.readline().strip().split())
t = [(-1,-1)]*1000010
def create(a):
global t,n
for i in range(n,2*n):
t[i] = (a[i-n],i-n)
for i in range(n-1,0,-1):
x = [t[2*i],t[2*i+1]]
x.sort(key = lambda x:x[0])
t[i] = x[1]
def update(idx,value):
global t,n
idx = idx+n
t[idx] = value
while(idx>1):
idx = idx//2
x = [t[2*idx],t[2*idx+1]]
x.sort(key = lambda x:x[0])
t[idx] = x[1]
n = int(input())
b = [-1]+list(numIN())
ans = [0]*(n+1)
ans[1] = 0
ans[n] = b[1]
for i in range(2,n//2+1):
if b[i]<=b[i-1]:
ans[i] = ans[i-1]
ans[n-i+1] = b[i]-ans[i]
else:
ans[n-i+1] = ans[n-i+2]
ans[i] = b[i]-ans[n-i+1]
print(' '.join([str(i) for i in ans[1:]]))
``` | output | 1 | 99,507 | 12 | 199,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.
There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative integer a_i to the students. It turned out, the exam was to tell the whole sequence back to the professor.
Sounds easy enough for those who attended every class, doesn't it?
Obviously Mishka didn't attend any classes. However, professor left some clues on the values of a to help out students like Mishka:
* a was sorted in non-decreasing order (a_1 ≤ a_2 ≤ ... ≤ a_n);
* n was even;
* the following sequence b, consisting of \frac n 2 elements, was formed and given out to students: b_i = a_i + a_{n - i + 1}.
Professor also mentioned that any sequence a, which produces sequence b with the presented technique, will be acceptable.
Help Mishka to pass that last exam. Restore any sorted sequence a of non-negative integers, which produces sequence b with the presented technique. It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of sequence a. n is always even.
The second line contains \frac n 2 integers b_1, b_2, ..., b_{\frac n 2} (0 ≤ b_i ≤ 10^{18}) — sequence b, where b_i = a_i + a_{n - i + 1}.
It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Output
Print n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^{18}) in a single line.
a_1 ≤ a_2 ≤ ... ≤ a_n should be satisfied.
b_i = a_i + a_{n - i + 1} should be satisfied for all valid i.
Examples
Input
4
5 6
Output
2 3 3 3
Input
6
2 1 2
Output
0 0 1 1 1 2 | instruction | 0 | 99,508 | 12 | 199,016 |
Tags: greedy
Correct Solution:
```
n = int(input())
arr = list(map(int, input().split()))
left = list()
right = list()
for v in arr:
if len(left) == 0:
left.append(0)
right.append(v)
else:
rvalue = min(v, right[-1])
lvalue = v - rvalue
if lvalue < left[-1]:
lvalue = left[-1]
rvalue = v - lvalue
left.append(lvalue)
right.append(rvalue)
print(*left, *reversed(right))
``` | output | 1 | 99,508 | 12 | 199,017 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.
There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative integer a_i to the students. It turned out, the exam was to tell the whole sequence back to the professor.
Sounds easy enough for those who attended every class, doesn't it?
Obviously Mishka didn't attend any classes. However, professor left some clues on the values of a to help out students like Mishka:
* a was sorted in non-decreasing order (a_1 ≤ a_2 ≤ ... ≤ a_n);
* n was even;
* the following sequence b, consisting of \frac n 2 elements, was formed and given out to students: b_i = a_i + a_{n - i + 1}.
Professor also mentioned that any sequence a, which produces sequence b with the presented technique, will be acceptable.
Help Mishka to pass that last exam. Restore any sorted sequence a of non-negative integers, which produces sequence b with the presented technique. It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of sequence a. n is always even.
The second line contains \frac n 2 integers b_1, b_2, ..., b_{\frac n 2} (0 ≤ b_i ≤ 10^{18}) — sequence b, where b_i = a_i + a_{n - i + 1}.
It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Output
Print n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^{18}) in a single line.
a_1 ≤ a_2 ≤ ... ≤ a_n should be satisfied.
b_i = a_i + a_{n - i + 1} should be satisfied for all valid i.
Examples
Input
4
5 6
Output
2 3 3 3
Input
6
2 1 2
Output
0 0 1 1 1 2 | instruction | 0 | 99,509 | 12 | 199,018 |
Tags: greedy
Correct Solution:
```
n = int(input())
b = [int(x) for x in input().split()]
lh=[0]
rh=[b[0]]
for x in b[1:]:
if x>=rh[-1]:
y=x-rh[-1]
if y>=lh[-1]:
lh.append(y)
rh.append(rh[-1])
else:
lh.append(lh[-1])
rh.append(x-lh[-1])
else:
y = x-lh[-1]
if y<=rh[-1]:
rh.append(y)
lh.append(lh[-1])
else:
rh.append(rh[-1])
lh.append(rh[-1]-x)
print(" ".join(list(map(str,lh+rh[::-1]))))
``` | output | 1 | 99,509 | 12 | 199,019 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.
There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative integer a_i to the students. It turned out, the exam was to tell the whole sequence back to the professor.
Sounds easy enough for those who attended every class, doesn't it?
Obviously Mishka didn't attend any classes. However, professor left some clues on the values of a to help out students like Mishka:
* a was sorted in non-decreasing order (a_1 ≤ a_2 ≤ ... ≤ a_n);
* n was even;
* the following sequence b, consisting of \frac n 2 elements, was formed and given out to students: b_i = a_i + a_{n - i + 1}.
Professor also mentioned that any sequence a, which produces sequence b with the presented technique, will be acceptable.
Help Mishka to pass that last exam. Restore any sorted sequence a of non-negative integers, which produces sequence b with the presented technique. It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of sequence a. n is always even.
The second line contains \frac n 2 integers b_1, b_2, ..., b_{\frac n 2} (0 ≤ b_i ≤ 10^{18}) — sequence b, where b_i = a_i + a_{n - i + 1}.
It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.
Output
Print n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^{18}) in a single line.
a_1 ≤ a_2 ≤ ... ≤ a_n should be satisfied.
b_i = a_i + a_{n - i + 1} should be satisfied for all valid i.
Examples
Input
4
5 6
Output
2 3 3 3
Input
6
2 1 2
Output
0 0 1 1 1 2 | instruction | 0 | 99,510 | 12 | 199,020 |
Tags: greedy
Correct Solution:
```
n = int(input())
A = list(map(int,input().split()))
res = [0]*n
res[0] = 0
res[n-1] = A[0]
for i in range(1,n//2):
res[i] = max(res[i-1],A[i]-A[i-1] + res[i-1])
res[n-i-1] = A[i]-res[i]
print(*res)
``` | output | 1 | 99,510 | 12 | 199,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l ≤ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l ⊕ a_{l+1} ⊕ … ⊕ a_{mid} = a_{mid + 1} ⊕ a_{mid + 2} ⊕ … ⊕ a_r, then the pair is funny. In other words, ⊕ of elements of the left half of the subarray from l to r should be equal to ⊕ of elements of the right half. Note that ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 ≤ n ≤ 3 ⋅ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i < 2^{20}) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 ⊕ 3 = 4 ⊕ 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | instruction | 0 | 99,519 | 12 | 199,038 |
Tags: dp, implementation
Correct Solution:
```
#!/usr/bin/python3.6
'''
.__ .__ _______ _______ _________
______| |__ |__| ____ ____ _______ _____ \ _ \ \ _ \\______ \
/ ___/| | \ | | / \ / _ \\_ __ \\__ \ / /_\ \ / /_\ \ / /
\___ \ | Y \| || | \( <_> )| | \/ / __ \_\ \_/ \\ \_/ \ / /
/____ >|___| /|__||___| / \____/ |__| (____ / \_____ / \_____ //____/
\/ \/ \/ \/ \/ \/
'''
# Run locally with ./codeforces.py to run in not debug mode. [ONLINE_JUDGE]
# rm ~/pipe2; mkfifo ~/pipe2 ; python3.6 -O ./codeforces.py < ~/pipe2 | ./interactive.py > ~/pipe2
# rm ~/pipe2; mkfifo ~/pipe2 ; python3.6 -O ./codeforces.py < ~/pipe2 | ./matcher.py > ~/pipe2
import sys
from functools import reduce
from math import sqrt, floor
def eprint(*args, **kwargs):
if not __debug__:
print(*args, file=sys.stderr, **kwargs)
if __name__ == "__main__":
eprint("Local\n")
for t in range(int(input()) if not __debug__ else 1):
# eprint(f'Times:{t}')
n = int(input())
arr = list(map(int, input().split()))
amax = (1<<20)+1
# c_xor = [arr[0]]
# c_xor_freq = 2*[[0 for i in range(amax)]]
c_xor_freq = [[0 for i in range(amax)] for j in range(2)]
c_xor_freq[1][0] = 1
xor = 0
res = 0
for i in range(n):
xor ^= arr[i]
res += c_xor_freq[i%2][xor]
c_xor_freq[i%2][xor]+=1
# eprint(c_xor_freq[:50])
# print(res)
# c_xor.append(c_xor[i-1] ^ arr[i])
# c_xor.append(0)
# eprint(c_xor)
# res = 0
# for l in range(n):
# for r in range(l+1, n, 2):
# if c_xor[r] == c_xor[l-1]:
# res += 1
# eprint(f't:{t}-> L:{l}, R:{r}, res:{res}')
print(res)
``` | output | 1 | 99,519 | 12 | 199,039 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l ≤ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l ⊕ a_{l+1} ⊕ … ⊕ a_{mid} = a_{mid + 1} ⊕ a_{mid + 2} ⊕ … ⊕ a_r, then the pair is funny. In other words, ⊕ of elements of the left half of the subarray from l to r should be equal to ⊕ of elements of the right half. Note that ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 ≤ n ≤ 3 ⋅ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i < 2^{20}) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 ⊕ 3 = 4 ⊕ 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | instruction | 0 | 99,520 | 12 | 199,040 |
Tags: dp, implementation
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
ans=[[0]*(2**20+5) for i in range(2)]
ans[1][0]=1
x,an=0,0
for i in range(n):
x^=a[i]
an+=ans[i%2][x]
ans[i%2][x]+=1
print(an)
``` | output | 1 | 99,520 | 12 | 199,041 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l ≤ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l ⊕ a_{l+1} ⊕ … ⊕ a_{mid} = a_{mid + 1} ⊕ a_{mid + 2} ⊕ … ⊕ a_r, then the pair is funny. In other words, ⊕ of elements of the left half of the subarray from l to r should be equal to ⊕ of elements of the right half. Note that ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 ≤ n ≤ 3 ⋅ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i < 2^{20}) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 ⊕ 3 = 4 ⊕ 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | instruction | 0 | 99,521 | 12 | 199,042 |
Tags: dp, implementation
Correct Solution:
```
list_int_input = lambda inp: list(map(int, inp.split()))
int_input = lambda inp: int(inp)
string_to_list_input = lambda inp: list(inp)
n=int(input())
a=list_int_input(input())
a=[0]+a
xl=[]
for ind,val in enumerate(a):
if ind>0:
val=xl[-1]^val
xl.append(val)
el=xl[::2]
ol=xl[1::2]
de,do={},{}
for i in el:
de.setdefault(i,0)
de[i]+=1
for i in ol:
do.setdefault(i,0)
do[i]+=1
summ=0
for k,v in de.items():
summ+=int((v*(v-1))/2)
for k,v in do.items():
summ+=int((v*(v-1))/2)
print(summ)
``` | output | 1 | 99,521 | 12 | 199,043 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l ≤ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l ⊕ a_{l+1} ⊕ … ⊕ a_{mid} = a_{mid + 1} ⊕ a_{mid + 2} ⊕ … ⊕ a_r, then the pair is funny. In other words, ⊕ of elements of the left half of the subarray from l to r should be equal to ⊕ of elements of the right half. Note that ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 ≤ n ≤ 3 ⋅ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i < 2^{20}) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 ⊕ 3 = 4 ⊕ 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | instruction | 0 | 99,522 | 12 | 199,044 |
Tags: dp, implementation
Correct Solution:
```
input()
N=1<<20
d=[1]+[0]*N,[0]*N
r=s=i=0
for x in map(int,input().split()):s^=x;i^=1;r+=d[i][s];d[i][s]+=1
print(r)
``` | output | 1 | 99,522 | 12 | 199,045 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l ≤ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l ⊕ a_{l+1} ⊕ … ⊕ a_{mid} = a_{mid + 1} ⊕ a_{mid + 2} ⊕ … ⊕ a_r, then the pair is funny. In other words, ⊕ of elements of the left half of the subarray from l to r should be equal to ⊕ of elements of the right half. Note that ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 ≤ n ≤ 3 ⋅ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i < 2^{20}) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 ⊕ 3 = 4 ⊕ 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | instruction | 0 | 99,523 | 12 | 199,046 |
Tags: dp, implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
cnt = [[0 for i in range(int(2 << 20)+10)],
[0 for i in range(int(2 << 20)+10)]]
cnt[1][0] =1
x = 0
ans = 0
for i in range(n):
x ^= a[i]
ans += cnt[i % 2][x]
cnt[i % 2][x] += 1
print(ans)
``` | output | 1 | 99,523 | 12 | 199,047 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l ≤ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l ⊕ a_{l+1} ⊕ … ⊕ a_{mid} = a_{mid + 1} ⊕ a_{mid + 2} ⊕ … ⊕ a_r, then the pair is funny. In other words, ⊕ of elements of the left half of the subarray from l to r should be equal to ⊕ of elements of the right half. Note that ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 ≤ n ≤ 3 ⋅ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i < 2^{20}) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 ⊕ 3 = 4 ⊕ 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | instruction | 0 | 99,524 | 12 | 199,048 |
Tags: dp, implementation
Correct Solution:
```
import sys
import math
def getAndParseInt(num=1):
string = (sys.stdin.readline()).strip()
if num==1:
return int(string)
else:
return [int(part) for part in string.split()]
def getAndParseString(num=1,delim=" "):
string = (sys.stdin.readline()).strip()
if num==1:
return string
else:
return [part for part in string.split(delim)]
n = getAndParseInt()
nums = getAndParseInt(2)
xors = [{},{0:1}]
total = 0
running_xor = 0
for index,num in enumerate(nums):
running_xor = running_xor^num
total+=xors[index%2].get(running_xor,0)
xors[index%2][running_xor] = xors[index%2].get(running_xor,0)+1
print(total)
``` | output | 1 | 99,524 | 12 | 199,049 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l ≤ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l ⊕ a_{l+1} ⊕ … ⊕ a_{mid} = a_{mid + 1} ⊕ a_{mid + 2} ⊕ … ⊕ a_r, then the pair is funny. In other words, ⊕ of elements of the left half of the subarray from l to r should be equal to ⊕ of elements of the right half. Note that ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 ≤ n ≤ 3 ⋅ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i < 2^{20}) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 ⊕ 3 = 4 ⊕ 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | instruction | 0 | 99,525 | 12 | 199,050 |
Tags: dp, implementation
Correct Solution:
```
def inpl(): return list(map(int, input().split()))
from operator import xor
from itertools import accumulate, chain
from collections import Counter
N = int(input())
A = list(accumulate([0] + inpl(), xor))
B = [a for i, a in enumerate(A) if i%2]
A = [a for i, a in enumerate(A) if not i%2]
H1 = Counter(A)
H2 = Counter(B)
print(sum([h*(h-1)//2 for h in chain(H1.values(), H2.values())]))
``` | output | 1 | 99,525 | 12 | 199,051 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l ≤ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l ⊕ a_{l+1} ⊕ … ⊕ a_{mid} = a_{mid + 1} ⊕ a_{mid + 2} ⊕ … ⊕ a_r, then the pair is funny. In other words, ⊕ of elements of the left half of the subarray from l to r should be equal to ⊕ of elements of the right half. Note that ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 ≤ n ≤ 3 ⋅ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i < 2^{20}) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 ⊕ 3 = 4 ⊕ 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | instruction | 0 | 99,526 | 12 | 199,052 |
Tags: dp, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))
AX=[0,A[0]]
for i in range(1,n):
AX.append(AX[i]^A[i])
LIST=sorted(set(AX))
DICT=dict()
for i in range(len(LIST)):
DICT[LIST[i]]=i
NUMLIST=[[] for i in range(len(LIST))]
for i in range(n+1):
NUMLIST[DICT[AX[i]]].append(i)
ANS=0
for lis in NUMLIST:
l1=[l for l in lis if l%2==1]
l2=[l for l in lis if l%2==0]
ANS+=len(l1)*(len(l1)-1)//2+len(l2)*(len(l2)-1)//2
print(ANS)
``` | output | 1 | 99,526 | 12 | 199,053 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3 | instruction | 0 | 99,535 | 12 | 199,070 |
Tags: greedy
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print(1)
print('1 1')
else:
d = [[]]*(n-1)
d[0] = a
for i in range(1,n-1):
d[i] = [d[i-1][j]+a[j+i] for j in range(0,n-i)]
d2 = {}
for i,d_ in enumerate(d):
for j,x in enumerate(d_):
if x in d2:
d2[x].append([j+1,j+i+1])
else:
d2[x]=[[j+1,j+i+1]]
list_keys = list(d2.keys())
#res = 0
ma = 0
for key in list_keys:
d2[key].sort(key=lambda x:x[1])
after = -1
cnt = 0
tmp = []
for y,z in d2[key]:
if y > after:
cnt += 1
after = z
tmp.append([y,z])
if cnt > ma:
ma = cnt
res = tmp
# for j in range(len(d2[key])):
# after = -1
# cnt = 0
# tmp = []
# for y,z in d2[key][j:]:
# if y > after:
# cnt += 1
# after = z
# tmp.append([y,z])
# if cnt > ma:
# ma = cnt
# res = tmp
print(len(res))
for x in res:
print(*x)
``` | output | 1 | 99,535 | 12 | 199,071 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3 | instruction | 0 | 99,536 | 12 | 199,072 |
Tags: greedy
Correct Solution:
```
from collections import defaultdict
n = int(input())
arr = [int(i) for i in input().split()]
prefix = [0 for i in range(n)]
segments = defaultdict(list)
prefix[0] = arr[0]
for i in range(1,n):
prefix[i] = prefix[i - 1] + arr[i]
long = 0
long_i = 0
for i in range(n):
for j in range(n):
if j + i > n - 1:
break
l, r = i, j + i
res = prefix[r] - ((prefix[l - 1]) if l != 0 else 0)
if res not in segments:
segments[res].append(l)
segments[res].append(r)
elif r < segments[res][-1]:
segments[res][-2] = l
segments[res][-1] = r
elif segments[res][-1] < l:
segments[res].append(l)
segments[res].append(r)
if len(segments[res]) > long:
long = len(segments[res])
long_i = res
print(len(segments[long_i])//2)
for i in range(0,len(segments[long_i]),2):
print(segments[long_i][i] + 1,segments[long_i][i + 1] + 1)
``` | output | 1 | 99,536 | 12 | 199,073 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3 | instruction | 0 | 99,537 | 12 | 199,074 |
Tags: greedy
Correct Solution:
```
n = int(input())
lst = list(map(int,input().split()))
d,res,summa = {},{},0
for i,y in enumerate(lst):
summa+=y
s=summa
for j in range(i+1):
if d.get(s)==None:
d[s]=[0,-1]
res[s]=[]
if d[s][1]<j:
d[s][0]+=1
d[s][1]=i
res[s].append([j+1,i+1])
s-=lst[j]
ans,e = 0,-1
for i,x in enumerate(d):
if d[x][0]>ans:
ans,e=d[x][0],x
print(ans)
for i,x in enumerate(res[e]):
print(*x)
``` | output | 1 | 99,537 | 12 | 199,075 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3 | instruction | 0 | 99,538 | 12 | 199,076 |
Tags: greedy
Correct Solution:
```
from collections import defaultdict
def main():
n = int(input())
values = list(map(int, input().split(' ')))
# print(values)
ans = defaultdict(list)
for i in range(n):
s = 0
# print("---------- i = {} ----------".format(i))
for j in range(i, -1, -1):
s += values[j]
# print("i = {}; j = {} s = {}".format(i, j, s))
# print("ans = {}; (i + 1) = {}".format(i + 1, ans[s][-1]))
# if (i + 1) not in ans[s][-1]:
ans[s].append((j + 1, i + 1))
# print(ans)
answer = dict()
max = 0
for key in ans:
# print("key = {} ; ans[key] = {}".format(key, ans[key], len(ans[key])))
sum_pairs = ans[key]
non_overlap_pairs = [sum_pairs[0]]
previous_pair_second_value = sum_pairs[0][1]
for each_pair in sum_pairs[1:]:
# print("each_pair = {}".format(each_pair))
if previous_pair_second_value < each_pair[0]:
# print(each_pair)
non_overlap_pairs.append(each_pair)
previous_pair_second_value = each_pair[1]
# else:
# print("Found overlapping pair for key = {}".format(each_pair))
# ans[key] = non_overlap_pairs
if len(non_overlap_pairs) > max:
max = len(non_overlap_pairs)
answer = {max: non_overlap_pairs}
# print(list(answer.keys())[0])
for key in answer.keys():
print(key)
for value in answer[key]:
print(str(value[0]) + ' ' + str(value[1]))
if __name__=="__main__":
main()
``` | output | 1 | 99,538 | 12 | 199,077 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3 | instruction | 0 | 99,539 | 12 | 199,078 |
Tags: greedy
Correct Solution:
```
from itertools import accumulate
def main():
n = int(input())
arr = list(map(int, input().split()))
arr_sums = [0] + list(accumulate(arr))
blocks = {}
for i in range(1, n+1):
for j in range(i):
total = arr_sums[i] - arr_sums[j]
if total not in blocks:
blocks[total] = [(j+1, i)]
else:
if blocks[total][-1][1] < j+1:
blocks[total].append((j+1, i))
max_block = sorted([(i, len(x)) for i, x in blocks.items()], key=lambda y: (-y[1], y[0]))
print(max_block[0][1])
for item in blocks[max_block[0][0]]:
print(item[0], item[1])
if __name__ == '__main__':
main()
``` | output | 1 | 99,539 | 12 | 199,079 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3 | instruction | 0 | 99,540 | 12 | 199,080 |
Tags: greedy
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
dic={}
for i in range(n):
sm=0
for j in range(i,n):
sm+=a[j]
if sm in dic:
dic[sm].append((i,j))
else:
dic[sm]=[(i,j)]
ans=0
anskey=-1
for key in dic:
cnt=0
last=-1
for a,b in sorted(dic[key]):
if a>last:
cnt+=1
last=b
elif b<last:
last=b
if cnt>ans:
ans=cnt
anskey=key
last=-1
tmp=[]
for a,b in sorted(dic[anskey]):
if a>last:
last=b
tmp.append(str(a+1)+" "+str(b+1))
elif b<last:
last=b
tmp.pop()
tmp.append(str(a+1)+" "+str(b+1))
print(ans,'\n'.join(tmp),sep='\n')
``` | output | 1 | 99,540 | 12 | 199,081 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3 | instruction | 0 | 99,541 | 12 | 199,082 |
Tags: greedy
Correct Solution:
```
n = int(input())
arr= list(map(int, input().split()))
sums = {0: []}
for i in range(n):
sum = 0
for j in range(i, -1, -1):
sum += arr[j]
if sum not in sums.keys():
sums[sum] = []
sums[sum].append((j,i))
#print(sums)
ans = (0, [])
for k in sums:
allblks = sums[k]
tot = 0
r = -1
ll = []
for ele in allblks:
if ele[0] > r:
tot += 1
ll.append(ele)
r = ele[1]
if tot > ans[0] :
ans = (tot, ll)
print(ans[0])
lt = ans[1]
for r in lt:
print(r[0] +1, r[1] +1)
``` | output | 1 | 99,541 | 12 | 199,083 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3 | instruction | 0 | 99,542 | 12 | 199,084 |
Tags: greedy
Correct Solution:
```
n=int(input())
arr=list(map(int,input().split()))
dict1={}
for i in range(n):
val=0
for j in range(i,n):
val+=arr[j]
try:
dict1[val].append((i,j))
except:
KeyError
dict1[val]=[(i,j)]
ans=0
ansarr=[]
for i in dict1.keys():
if(len(dict1[i])>ans):
arr2=[]
for j in range(len(dict1[i])):
arr2.append((dict1[i][j][1]-dict1[i][j][0],j))
arr2.sort()
temp=[]
for j in range(len(arr2)):
flag=0
indexi=dict1[i][arr2[j][1]][0]
indexj=dict1[i][arr2[j][1]][1]
for k in range(len(temp)):
if(temp[k][0]<=indexi<=temp[k][1] or temp[k][0]<=indexj<=temp[k][1] or indexi<=temp[k][0]<=indexj or indexi<=temp[k][1]<=indexj):
flag=1
if(flag==0):
temp.append((indexi,indexj))
if(len(temp)>ans):
ans=len(temp)
ansarr=temp
print(ans)
finalans=[]
for i in range(len(ansarr)):
finalans.append(ansarr[i][0]+1)
finalans.append(ansarr[i][1]+1)
print(*finalans)
``` | output | 1 | 99,542 | 12 | 199,085 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3
Submitted Solution:
```
#!/usr/bin/env python3
from collections import defaultdict
def main():
n = int(input())
a = [int(ai) for ai in input().split()]
pref_sums = [0] * (n + 1)
for i, ai in enumerate(a):
pref_sums[i + 1] = pref_sums[i] + ai
block_sums = defaultdict(list)
for j in range(1, n + 1):
for i in range(1, j + 1):
block_sums[pref_sums[j] - pref_sums[i - 1]].append((i, j))
ans = []
for A in block_sums.values():
res, k = [A[0]], 0
for i in range(1, len(A)):
if A[i][0] > A[k][1]:
res.append(A[i])
k = i
if len(res) > len(ans):
ans = res
print(len(ans))
print('\n'.join(map(lambda s: '%d %d' % s, ans)))
if __name__ == '__main__':
main()
``` | instruction | 0 | 99,543 | 12 | 199,086 |
Yes | output | 1 | 99,543 | 12 | 199,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3
Submitted Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @oj: codeforces
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020/12/17 17:03
# @url: https://codeforc.es/contest/1141/problem/F2
import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
# region fastio
BUFSIZE = 8192
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")
# ------------------------------
## 注意嵌套括号!!!!!!
## 先有思路,再写代码,别着急!!!
## 先有朴素解法,不要有思维定式,试着换思路解决
## 精度 print("%.10f" % ans)
## sqrt:int(math.sqrt(n))+1
## 字符串拼接不要用+操作,会超时
## 二进制转换:bin(1)[2:].rjust(32,'0')
## array copy:cur=array[::]
## oeis:example 1, 3, _, 1260, _, _, _, _, _, 12164510040883200
## sqrt:Decimal(x).sqrt()避免精度误差
## 无穷大表示:float('inf')
## py 10**6 排序+双指针 3秒可能TLE
def main():
n = int(input())
a = list(map(int, input().split()))
prefix = list(itertools.accumulate(a))
d = collections.defaultdict(list)
for i in range(n):
for j in range(i, n):
if i == 0:
d[prefix[j]].append((i, j))
else:
v = prefix[j] - prefix[i - 1]
d[v].append((i, j))
ans = []
for k in d.keys():
v = d[k]
cnt = []
## 按区间右端点排序
tmp = sorted(v,key=lambda x:(x[1],x[0]))
if len(tmp) == 1:
cnt.append((tmp[0][0] + 1, tmp[0][1] + 1))
else:
pre = tmp[0]
## 贪心求不相交区间的最大个数
cnt.append((pre[0] + 1, pre[1] + 1))
for i in range(1, len(tmp)):
cur = tmp[i]
if cur[0] > pre[1]:
cnt.append((cur[0] + 1, cur[1] + 1))
pre = cur
if len(cnt) > len(ans):
ans = cnt
############## TLE code ##############
## 按区间右端点排序
# sr = sorted(res, key=lambda x: (x[2], x[1], x[0]))
# print(time.time() - start)
# l, r = 0, 0
# while r < len(sr):
# pre = sr[l]
# cnt = [(pre[0] + 1, pre[1] + 1)]
# while r < len(sr) and sr[l][2] == sr[r][2]:
# cur=sr[r]
# if cur[0] > pre[1]:
# cnt.append((cur[0] + 1, cur[1] + 1))
# pre = cur
# r += 1
# l = r
# if len(cnt) > len(ans):
# ans = cnt
# print (time.time()-start)
print(len(ans))
for a in ans:
print(a[0], a[1])
if __name__ == "__main__":
main()
``` | instruction | 0 | 99,544 | 12 | 199,088 |
Yes | output | 1 | 99,544 | 12 | 199,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3
Submitted Solution:
```
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
rec = defaultdict(list)
for j in range(n):
for k in range(j, n):
rec[sum(a[j:k + 1])].append((j, k))
ans = []
for k in rec.keys():
tmp = []
rec[k] = sorted(rec[k], key=lambda x: x[1])
pre = -1
for a, b in rec[k]:
if pre >= a:
continue
else:
tmp.append((a + 1, b + 1))
pre = b
if len(tmp) > len(ans):
ans = tmp
print(len(ans))
for a, b in ans:
print(a, b)
``` | instruction | 0 | 99,545 | 12 | 199,090 |
Yes | output | 1 | 99,545 | 12 | 199,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3
Submitted Solution:
```
from collections import defaultdict
n = int(input())
nums = list(map(int, input().split()))
freq = defaultdict(list)
for i in range(n):
for j in range(i+1, n+1):
freq[sum(nums[i:j])].append((i+1, j))
ans = []
for k in freq:
l = freq[k]
l.sort(key=lambda x: x[1])
tmp = [l[0]]
for i, j in l:
if i <= tmp[-1][1]:
continue
tmp.append([i, j])
if len(tmp) > len(ans):
ans = tmp
print (len(ans))
for i, j in ans:
print (i, j)
``` | instruction | 0 | 99,546 | 12 | 199,092 |
Yes | output | 1 | 99,546 | 12 | 199,093 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3
Submitted Solution:
```
from collections import defaultdict
def find_best_path(pairs: list, n=1500):
#bfs?
dist = [1 for p in pairs]
#everyone is its own pred
pred = [x for x in range(len(pairs))]
#count different starts
#for every end, give a mapping where to find the correct offsets
offset = [-1 for x in range(n)]
for i, p in enumerate(pairs):
offset[p[0]] = i
last_offset=0
for i, o in enumerate(offset):
if o == -1:
offset[i] = last_offset
else:
last_offset = o
# pairs are already kind of sorted (by start and end)
#could be optimized: lookup of possible successors (not sure if worth)
for i in range(len(pairs)):
#offset[i] is the last entry
end = pairs[i][1]
for j in range(offset[end]+1, len(pairs)):
#for j in range(i+1, len(pairs)):
# valid successor
#if pairs[j][0] > pairs[i][1]:
if dist[j] < dist[i] + 1:
dist[j] += 1
pred[j] = i
last_entry = max(range(len(pairs)), key=lambda x: dist[x])
partitioning = []
while True:
entry = pairs[last_entry]
#1-indexing
partitioning.append([entry[0] + 1, entry[1] + 1])
if pred[last_entry] == last_entry:
break
last_entry = pred[last_entry]
del(pred)
del(dist)
return len(partitioning), partitioning
def find_partitioning(l: list):
ht = defaultdict(list)
for i, _ in enumerate(l):
val = 0
for j in range(i, len(l)):
val += l[j]
ht[val].append((i, j))
print("hashing done")
maximum_partitioning_size = 0
partitioning = []
for s, val in ht.items():
#print(s)
#print(len(val), maximum_partitioning_size)
if len(val) < maximum_partitioning_size:
continue
num_blocks, blocks = find_best_path(val, len(l))
if num_blocks > maximum_partitioning_size:
maximum_partitioning_size = num_blocks
partitioning = blocks
return partitioning
def test_find_partitioning():
l = [7, 1, 2, 2, 1, 5, 3]
res = find_partitioning(l)
print(res)
assert len(res) == 3
assert res[0] == [7,7]
assert res[1] == [4,5]
assert res[2] == [2,3]
def test_find_partitioning_2():
l = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
res = find_partitioning(l)
#assert len(res) == 4
assert len(res) == 2
#assert res[0] == [7,7]
#assert res[1] == [2,3]
#assert res[2] == [4,5]
def test_big_partitioning():
import numpy as np
l = np.random.randint(-1,1, 1000)
res = find_partitioning(l)
if __name__ == "__main__":
import sys
lines = sys.stdin.readlines()
entries = lines[1].split()
entries = [int(i) for i in entries]
partitioning = find_partitioning(entries)
print(len(partitioning))
for p in partitioning:
print(p[0], p[1])
``` | instruction | 0 | 99,547 | 12 | 199,094 |
No | output | 1 | 99,547 | 12 | 199,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3
Submitted Solution:
```
import time
def index(key, item, index):
if key in index:
index[key].append(item)
else:
index[key] = [item]
def is_in_order(l):
if not l:
return True
for i in range(1, len(l)):
old, new = l[i - 1], l[i]
if new[0] < old[0]:
return False
elif new[0] == old[0]:
if new[1] < old[1]:
return False
return True
def schedule2(times):
# print(times)
assert is_in_order(times)
result = []
a_min = 0
for s, e in times:
if s >= a_min:
result.append((s, e))
a_min = e
return result
def schedule(times):
index_by_b = {}
for time in times:
index(time[1], time, index_by_b)
b_keys = sorted(list(index_by_b.keys()))
result = []
a_min = 0
# Get interval with minimun end time whose start time >= a_min.
for end_time in b_keys:
start = index_by_b[end_time].pop()[0]
if start >= a_min:
result.append((start, end_time))
a_min = end_time
return result
def solve(n, a_l):
index_by_sum = {}
for i in range(n):
sum_ = 0
for j in range(i + 1, n + 1):
sum_ += a_l[j - 1]
if sum_ in index_by_sum:
index_by_sum[sum_].append((i, j))
else:
index_by_sum[sum_] = [(i, j)]
result = []
for sum_, times in index_by_sum.items():
sub_result = schedule2(times)
if len(sub_result) > len(result):
result = sub_result
return result
def test():
n = 7
a_l = [4, 1, 2, 2, 1, 5, 3]
# n = 1500
# a_l = list(range(1, n + 1))
tick = time.time()
result = solve(n, a_l)
tock = time.time()
print(len(result))
for a, b in result:
print(a + 1, b)
print("T:", round(tock - tick, 5))
def main():
n = int(input())
a_l = list(map(int, input().split()))
result = solve(n, a_l)
print(len(result))
for a, b in result:
print(a + 1, b)
if __name__ == "__main__":
main()
``` | instruction | 0 | 99,548 | 12 | 199,096 |
No | output | 1 | 99,548 | 12 | 199,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3
Submitted Solution:
```
n=int(input())
arr=list(map(int,input().split()))
dict1={}
for i in range(n):
val=0
for j in range(i,n):
val+=arr[j]
try:
dict1[val].append((i,j))
except:
KeyError
dict1[val]=[(i,j)]
ans=0
ansarr=[]
for i in dict1.keys():
if(len(dict1[i])>ans):
arr2=[]
for j in range(len(dict1[i])):
arr2.append((dict1[i][j][1]-dict1[i][j][0],j))
arr2.sort()
temp=[]
for j in range(len(arr2)):
flag=0
indexi=dict1[i][arr2[j][1]][0]
indexj=dict1[i][arr2[j][1]][1]
for k in range(len(temp)):
if(temp[k][0]<=indexi<=temp[k][1] or temp[k][0]<=indexj<=temp[k][1] or indexi<=temp[k][0]<=indexj or indexi<=temp[k][1]<=indexj):
flag=1
if(flag==1):
break
else:
temp.append((indexi,indexj))
if(len(temp)>ans):
ans=len(temp)
ansarr=temp
print(ans)
finalans=[]
for i in range(len(ansarr)):
finalans.append(ansarr[i][0]+1)
finalans.append(ansarr[i][1]+1)
print(*finalans)
``` | instruction | 0 | 99,549 | 12 | 199,098 |
No | output | 1 | 99,549 | 12 | 199,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that:
* They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i.
* For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$
* The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k.
<image> The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5).
Output
In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
Input
7
4 1 2 2 1 5 3
Output
3
7 7
2 3
4 5
Input
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Output
2
3 4
1 1
Input
4
1 1 1 1
Output
4
4 4
1 1
2 2
3 3
Submitted Solution:
```
from collections import defaultdict
n = int(input())
nums = [int(x) for x in input().split()]
mapas = [defaultdict(list) for _ in range(n)]
for i, nu in enumerate(nums):
if i:
mapas[i] = defaultdict(list, mapas[i-1])
# print(mapas[i])
mapas[i][nu].append((i+1,i+1))
su = nu
for j in reversed(range(1, i)):
su += nums[j]
if len(mapas[j-1][su])+1 > len(mapas[i][su]):
mapas[i][su] = mapas[j-1][su]+ [(j+1, i+1)]
# print(su)
if i:
su += nums[0]
# print(su, mapas[i])
if len(mapas[i][su]) == 0:
mapas[i][su] = [(1, i+1)]
# print(mapas[i])
# print()
# mapas[i][su] = max(1, mapas[i][su])
mx, my = -1,-1
# print(mapas[-1])
for x, y in mapas[-1].items():
if len(y)> my:
mx, my = x, len(y)
print(my)
# print(mapas[-1][mx])
for a, b in mapas[-1][mx]:
print(a, b)
``` | instruction | 0 | 99,550 | 12 | 199,100 |
No | output | 1 | 99,550 | 12 | 199,101 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:
* The array consists of n distinct positive (greater than 0) integers.
* The array contains two elements x and y (these elements are known for you) such that x < y.
* If you sort the array in increasing order (such that a_1 < a_2 < … < a_n), differences between all adjacent (consecutive) elements are equal (i.e. a_2 - a_1 = a_3 - a_2 = … = a_n - a_{n-1}).
It can be proven that such an array always exists under the constraints given below.
Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize max(a_1, a_2, ..., a_n).
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow.
The only line of the test case contains three integers n, x and y (2 ≤ n ≤ 50; 1 ≤ x < y ≤ 50) — the length of the array and two elements that are present in the array, respectively.
Output
For each test case, print the answer: n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter).
It can be proven that such an array always exists under the given constraints.
Example
Input
5
2 1 49
5 20 50
6 20 50
5 3 8
9 13 22
Output
1 49
20 40 30 50 10
26 32 20 38 44 50
8 23 18 13 3
1 10 13 4 19 22 25 16 7 | instruction | 0 | 99,614 | 12 | 199,228 |
Tags: brute force, math, number theory
Correct Solution:
```
#!/usr/bin/env python3
# encoding: utf-8
#----------
# Constants
#----------
#----------
# Functions
#----------
# The function that solves the task
def calc(n, x, y):
best_maximum, best_k = 1000000001, 0
for m in range(1, n):
k = (y - x) // m
if m * k != y - x:
continue
start = y - k * (n-1)
delta = 0
if start <= 0:
delta = (k - start) // k
start += delta * k
if start > x:
continue
maximum = y + delta * k
if maximum < best_maximum:
best_maximum = maximum
best_k = k
assert best_k > 0
res = []
for i in range(n):
res.append(best_maximum)
best_maximum -= best_k
return res
# Reads a string from stdin, splits it by space chars, converts each
# substring to int, adds it to a list and returns the list as a result.
def get_ints():
return [ int(n) for n in input().split() ]
# Reads a string from stdin, splits it by space chars, converts each substring
# to floating point number, adds it to a list and returns the list as a result.
def get_floats():
return [ float(n) for n in input().split() ]
# Converts a sequence to the space separated string
def seq2str(seq):
return ' '.join(str(item) for item in seq)
#----------
# Execution start point
#----------
if __name__ == "__main__":
zzz = get_ints()
assert len(zzz) == 1
t = zzz[0]
for i in range(t):
zzz = get_ints()
assert len(zzz) == 3
n, x, y = zzz[0], zzz[1], zzz[2]
res = calc(n, x, y)
print(seq2str(res))
``` | output | 1 | 99,614 | 12 | 199,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:
* The array consists of n distinct positive (greater than 0) integers.
* The array contains two elements x and y (these elements are known for you) such that x < y.
* If you sort the array in increasing order (such that a_1 < a_2 < … < a_n), differences between all adjacent (consecutive) elements are equal (i.e. a_2 - a_1 = a_3 - a_2 = … = a_n - a_{n-1}).
It can be proven that such an array always exists under the constraints given below.
Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize max(a_1, a_2, ..., a_n).
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow.
The only line of the test case contains three integers n, x and y (2 ≤ n ≤ 50; 1 ≤ x < y ≤ 50) — the length of the array and two elements that are present in the array, respectively.
Output
For each test case, print the answer: n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter).
It can be proven that such an array always exists under the given constraints.
Example
Input
5
2 1 49
5 20 50
6 20 50
5 3 8
9 13 22
Output
1 49
20 40 30 50 10
26 32 20 38 44 50
8 23 18 13 3
1 10 13 4 19 22 25 16 7 | instruction | 0 | 99,615 | 12 | 199,230 |
Tags: brute force, math, number theory
Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 3 21:10:40 2020
@author: dennis
"""
import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
for case in range(int(input())):
n, x, y = [int(x) for x in input().split()]
m = 10**9 + 1
for i in range(1, 51):
for j in range(1, 51):
r = range(i, n*j + i, j)
if x in r and y in r:
s = max(r)
if s < m:
m = s
ans = [x for x in r]
print(*ans)
``` | output | 1 | 99,615 | 12 | 199,231 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:
* The array consists of n distinct positive (greater than 0) integers.
* The array contains two elements x and y (these elements are known for you) such that x < y.
* If you sort the array in increasing order (such that a_1 < a_2 < … < a_n), differences between all adjacent (consecutive) elements are equal (i.e. a_2 - a_1 = a_3 - a_2 = … = a_n - a_{n-1}).
It can be proven that such an array always exists under the constraints given below.
Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize max(a_1, a_2, ..., a_n).
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow.
The only line of the test case contains three integers n, x and y (2 ≤ n ≤ 50; 1 ≤ x < y ≤ 50) — the length of the array and two elements that are present in the array, respectively.
Output
For each test case, print the answer: n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter).
It can be proven that such an array always exists under the given constraints.
Example
Input
5
2 1 49
5 20 50
6 20 50
5 3 8
9 13 22
Output
1 49
20 40 30 50 10
26 32 20 38 44 50
8 23 18 13 3
1 10 13 4 19 22 25 16 7 | instruction | 0 | 99,616 | 12 | 199,232 |
Tags: brute force, math, number theory
Correct Solution:
```
def answer(n,x,y):
diff=y-x
d=-1
for i in range(n-1,0,-1):
if diff%i==0:
d=diff//i
break
l=[y]
q=y-d
while len(l)<n and q>0:
l.append(q)
q=q-d
q=y+d
while len(l)<n:
l.append(q)
q+=d
return l
t=int(input())
for i in range(t):
n,x,y=map(int,input().split())
print(*answer(n,x,y))
``` | output | 1 | 99,616 | 12 | 199,233 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:
* The array consists of n distinct positive (greater than 0) integers.
* The array contains two elements x and y (these elements are known for you) such that x < y.
* If you sort the array in increasing order (such that a_1 < a_2 < … < a_n), differences between all adjacent (consecutive) elements are equal (i.e. a_2 - a_1 = a_3 - a_2 = … = a_n - a_{n-1}).
It can be proven that such an array always exists under the constraints given below.
Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize max(a_1, a_2, ..., a_n).
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow.
The only line of the test case contains three integers n, x and y (2 ≤ n ≤ 50; 1 ≤ x < y ≤ 50) — the length of the array and two elements that are present in the array, respectively.
Output
For each test case, print the answer: n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter).
It can be proven that such an array always exists under the given constraints.
Example
Input
5
2 1 49
5 20 50
6 20 50
5 3 8
9 13 22
Output
1 49
20 40 30 50 10
26 32 20 38 44 50
8 23 18 13 3
1 10 13 4 19 22 25 16 7 | instruction | 0 | 99,617 | 12 | 199,234 |
Tags: brute force, math, number theory
Correct Solution:
```
t=int(input())
w=[]
def func(t,h):
i=1
boo=True
while boo:
if t % i==0:
if t< i * h:
boo=False
return i
else:
i=i+1
else:
i=i+1
for i in range(t):
n,x,y=input().split()
n=int(n)
x=int(x)
y=int(y)
r=[]
d=func(y-x,n)
if y>=n*d:
for z in range(y-n*d+d,y+1,d):
r.append(z)
else:
i=0
c=0
while y-i*d>0:
r.append(y-i*d)
c=c+1
i=i+1
i=1
while c!=n:
r.append(y+i*d)
c=c+1
i=i+1
w.append(r)
for i in w:
print(*i)
``` | output | 1 | 99,617 | 12 | 199,235 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.