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.
You are given two arrays a and b, each consisting of n positive integers, and an integer x. Please determine if one can rearrange the elements of b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n).
Input
The first line of input contains one integer t (1 β€ t β€ 100) β the number of test cases. t blocks follow, each describing an individual test case.
The first line of each test case contains two integers n and x (1 β€ n β€ 50; 1 β€ x β€ 1000) β the length of arrays a and b, and the parameter x, described in the problem statement.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ x) β the elements of array a in non-descending order.
The third line of each test case contains n integers b_1, b_2, β¦, b_n (1 β€ b_1 β€ b_2 β€ ... β€ b_n β€ x) β the elements of array b in non-descending order.
Test cases are separated by a blank line.
Output
For each test case print Yes if one can rearrange the corresponding array b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n) or No otherwise.
Each character can be printed in any case.
Example
Input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
Output
Yes
Yes
No
No
Note
In the first test case, one can rearrange b so it'll look like [1, 2, 1]. In this case, 1 + 1 β€ 4; 2 + 2 β€ 4; 3 + 1 β€ 4.
In the second test case, one can set b to [5, 2], then 1 + 5 β€ 6; 4 + 2 β€ 6.
In the third test case, no matter how one shuffles array b, a_4 + b_4 = 4 + b_4 > 4.
In the fourth test case, there is only one rearrangement of array b and it doesn't satisfy the condition since 5 + 5 > 5. | instruction | 0 | 19,648 | 12 | 39,296 |
Tags: greedy, sortings
Correct Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 1 10:08:25 2020
@author: beto
"""
def reacomodo(x, lista_a, lista_b):
for i in range(len(lista_a)):
if lista_a[i] + lista_b[abs(i-len(lista_a)+1)] > x:
return('No')
return('Yes')
test = int(input())
for i in range(test):
n, x = map(int, input().split())
lista_a = list(map(int,input().split()))
lista_b = list(map(int, input().split()))
print(reacomodo(x,lista_a,lista_b))
if i == test-1:
pass
else:
input()
``` | output | 1 | 19,648 | 12 | 39,297 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each consisting of n positive integers, and an integer x. Please determine if one can rearrange the elements of b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n).
Input
The first line of input contains one integer t (1 β€ t β€ 100) β the number of test cases. t blocks follow, each describing an individual test case.
The first line of each test case contains two integers n and x (1 β€ n β€ 50; 1 β€ x β€ 1000) β the length of arrays a and b, and the parameter x, described in the problem statement.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ x) β the elements of array a in non-descending order.
The third line of each test case contains n integers b_1, b_2, β¦, b_n (1 β€ b_1 β€ b_2 β€ ... β€ b_n β€ x) β the elements of array b in non-descending order.
Test cases are separated by a blank line.
Output
For each test case print Yes if one can rearrange the corresponding array b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n) or No otherwise.
Each character can be printed in any case.
Example
Input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
Output
Yes
Yes
No
No
Note
In the first test case, one can rearrange b so it'll look like [1, 2, 1]. In this case, 1 + 1 β€ 4; 2 + 2 β€ 4; 3 + 1 β€ 4.
In the second test case, one can set b to [5, 2], then 1 + 5 β€ 6; 4 + 2 β€ 6.
In the third test case, no matter how one shuffles array b, a_4 + b_4 = 4 + b_4 > 4.
In the fourth test case, there is only one rearrangement of array b and it doesn't satisfy the condition since 5 + 5 > 5.
Submitted Solution:
```
t=int(input())
for k in range(t):
n,x=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a.sort()
b.sort(reverse=True)
flag=0
for i in range(n):
if a[i]+b[i]>x:
flag=1
break
if flag==1:
print("No")
else:
print("Yes")
if k!=t-1:
tp=input()
``` | instruction | 0 | 19,649 | 12 | 39,298 |
Yes | output | 1 | 19,649 | 12 | 39,299 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each consisting of n positive integers, and an integer x. Please determine if one can rearrange the elements of b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n).
Input
The first line of input contains one integer t (1 β€ t β€ 100) β the number of test cases. t blocks follow, each describing an individual test case.
The first line of each test case contains two integers n and x (1 β€ n β€ 50; 1 β€ x β€ 1000) β the length of arrays a and b, and the parameter x, described in the problem statement.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ x) β the elements of array a in non-descending order.
The third line of each test case contains n integers b_1, b_2, β¦, b_n (1 β€ b_1 β€ b_2 β€ ... β€ b_n β€ x) β the elements of array b in non-descending order.
Test cases are separated by a blank line.
Output
For each test case print Yes if one can rearrange the corresponding array b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n) or No otherwise.
Each character can be printed in any case.
Example
Input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
Output
Yes
Yes
No
No
Note
In the first test case, one can rearrange b so it'll look like [1, 2, 1]. In this case, 1 + 1 β€ 4; 2 + 2 β€ 4; 3 + 1 β€ 4.
In the second test case, one can set b to [5, 2], then 1 + 5 β€ 6; 4 + 2 β€ 6.
In the third test case, no matter how one shuffles array b, a_4 + b_4 = 4 + b_4 > 4.
In the fourth test case, there is only one rearrangement of array b and it doesn't satisfy the condition since 5 + 5 > 5.
Submitted Solution:
```
T=int(input())
for i in range(T):
n,x=list(map(int, input().split()))
a=list(map(int, input().split()))
b=list(map(int, input().split()))
if i<T-1: _=input()
a.sort()
b.sort()
b=b[::-1]
flag=0
# print(a)
# print(b)
for i,j in zip(a,b):
if i+j>x:
flag=1
break
if flag==1:
print('No')
else:
print('Yes')
``` | instruction | 0 | 19,650 | 12 | 39,300 |
Yes | output | 1 | 19,650 | 12 | 39,301 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each consisting of n positive integers, and an integer x. Please determine if one can rearrange the elements of b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n).
Input
The first line of input contains one integer t (1 β€ t β€ 100) β the number of test cases. t blocks follow, each describing an individual test case.
The first line of each test case contains two integers n and x (1 β€ n β€ 50; 1 β€ x β€ 1000) β the length of arrays a and b, and the parameter x, described in the problem statement.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ x) β the elements of array a in non-descending order.
The third line of each test case contains n integers b_1, b_2, β¦, b_n (1 β€ b_1 β€ b_2 β€ ... β€ b_n β€ x) β the elements of array b in non-descending order.
Test cases are separated by a blank line.
Output
For each test case print Yes if one can rearrange the corresponding array b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n) or No otherwise.
Each character can be printed in any case.
Example
Input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
Output
Yes
Yes
No
No
Note
In the first test case, one can rearrange b so it'll look like [1, 2, 1]. In this case, 1 + 1 β€ 4; 2 + 2 β€ 4; 3 + 1 β€ 4.
In the second test case, one can set b to [5, 2], then 1 + 5 β€ 6; 4 + 2 β€ 6.
In the third test case, no matter how one shuffles array b, a_4 + b_4 = 4 + b_4 > 4.
In the fourth test case, there is only one rearrangement of array b and it doesn't satisfy the condition since 5 + 5 > 5.
Submitted Solution:
```
cases = int(input())
for t in range(cases):
n,x = list(map(int,input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
f = 0
if t!=cases-1:
_ = input()
b = b[::-1]
for i in range(n):
if b[i]+a[i]>x:
f = 1
break
if f==0:
print("Yes")
else:
print("No")
``` | instruction | 0 | 19,651 | 12 | 39,302 |
Yes | output | 1 | 19,651 | 12 | 39,303 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each consisting of n positive integers, and an integer x. Please determine if one can rearrange the elements of b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n).
Input
The first line of input contains one integer t (1 β€ t β€ 100) β the number of test cases. t blocks follow, each describing an individual test case.
The first line of each test case contains two integers n and x (1 β€ n β€ 50; 1 β€ x β€ 1000) β the length of arrays a and b, and the parameter x, described in the problem statement.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ x) β the elements of array a in non-descending order.
The third line of each test case contains n integers b_1, b_2, β¦, b_n (1 β€ b_1 β€ b_2 β€ ... β€ b_n β€ x) β the elements of array b in non-descending order.
Test cases are separated by a blank line.
Output
For each test case print Yes if one can rearrange the corresponding array b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n) or No otherwise.
Each character can be printed in any case.
Example
Input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
Output
Yes
Yes
No
No
Note
In the first test case, one can rearrange b so it'll look like [1, 2, 1]. In this case, 1 + 1 β€ 4; 2 + 2 β€ 4; 3 + 1 β€ 4.
In the second test case, one can set b to [5, 2], then 1 + 5 β€ 6; 4 + 2 β€ 6.
In the third test case, no matter how one shuffles array b, a_4 + b_4 = 4 + b_4 > 4.
In the fourth test case, there is only one rearrangement of array b and it doesn't satisfy the condition since 5 + 5 > 5.
Submitted Solution:
```
for k in range(int(input())):
if k!=0:
input()
n,x=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a.sort()
b.sort()
b=b[::-1]
flag=0
for i in range(n):
a[i]=a[i]+b[i]
if a[i]>x:
flag=1
break
if flag==1:
print('NO')
else:
print('YES')
``` | instruction | 0 | 19,652 | 12 | 39,304 |
Yes | output | 1 | 19,652 | 12 | 39,305 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each consisting of n positive integers, and an integer x. Please determine if one can rearrange the elements of b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n).
Input
The first line of input contains one integer t (1 β€ t β€ 100) β the number of test cases. t blocks follow, each describing an individual test case.
The first line of each test case contains two integers n and x (1 β€ n β€ 50; 1 β€ x β€ 1000) β the length of arrays a and b, and the parameter x, described in the problem statement.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ x) β the elements of array a in non-descending order.
The third line of each test case contains n integers b_1, b_2, β¦, b_n (1 β€ b_1 β€ b_2 β€ ... β€ b_n β€ x) β the elements of array b in non-descending order.
Test cases are separated by a blank line.
Output
For each test case print Yes if one can rearrange the corresponding array b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n) or No otherwise.
Each character can be printed in any case.
Example
Input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
Output
Yes
Yes
No
No
Note
In the first test case, one can rearrange b so it'll look like [1, 2, 1]. In this case, 1 + 1 β€ 4; 2 + 2 β€ 4; 3 + 1 β€ 4.
In the second test case, one can set b to [5, 2], then 1 + 5 β€ 6; 4 + 2 β€ 6.
In the third test case, no matter how one shuffles array b, a_4 + b_4 = 4 + b_4 > 4.
In the fourth test case, there is only one rearrangement of array b and it doesn't satisfy the condition since 5 + 5 > 5.
Submitted Solution:
```
k = int(input())
for t in range(k):
n,x = map(int,input().split())
arr1 = list(map(int,input().split()))
arr2 = list(map(int,input().split()))
if max(arr1) + min(arr2) <= x and max(arr2) + min(arr1) <= x:
print('YES')
else:
print('NO')
if t < k-1:
s = input()
``` | instruction | 0 | 19,653 | 12 | 39,306 |
No | output | 1 | 19,653 | 12 | 39,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each consisting of n positive integers, and an integer x. Please determine if one can rearrange the elements of b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n).
Input
The first line of input contains one integer t (1 β€ t β€ 100) β the number of test cases. t blocks follow, each describing an individual test case.
The first line of each test case contains two integers n and x (1 β€ n β€ 50; 1 β€ x β€ 1000) β the length of arrays a and b, and the parameter x, described in the problem statement.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ x) β the elements of array a in non-descending order.
The third line of each test case contains n integers b_1, b_2, β¦, b_n (1 β€ b_1 β€ b_2 β€ ... β€ b_n β€ x) β the elements of array b in non-descending order.
Test cases are separated by a blank line.
Output
For each test case print Yes if one can rearrange the corresponding array b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n) or No otherwise.
Each character can be printed in any case.
Example
Input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
Output
Yes
Yes
No
No
Note
In the first test case, one can rearrange b so it'll look like [1, 2, 1]. In this case, 1 + 1 β€ 4; 2 + 2 β€ 4; 3 + 1 β€ 4.
In the second test case, one can set b to [5, 2], then 1 + 5 β€ 6; 4 + 2 β€ 6.
In the third test case, no matter how one shuffles array b, a_4 + b_4 = 4 + b_4 > 4.
In the fourth test case, there is only one rearrangement of array b and it doesn't satisfy the condition since 5 + 5 > 5.
Submitted Solution:
```
t=int(input())
n,x=input().split()
n=int(n)
x=int(x)
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a=sorted(a)
b=sorted(a,reverse=True)
boo=True
for i in range(n):
if a[i]+b[i]>x:
print("No")
boo=False
break
if boo:
print("Yes")
for i in range(t-1):
k=input()
n,x=input().split()
n=int(n)
x=int(x)
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a=sorted(a)
b=sorted(a,reverse=True)
boo=True
for i in range(n):
if a[i]+b[i]>x:
print("No")
boo=False
break
if boo:
print("Yes")
``` | instruction | 0 | 19,654 | 12 | 39,308 |
No | output | 1 | 19,654 | 12 | 39,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each consisting of n positive integers, and an integer x. Please determine if one can rearrange the elements of b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n).
Input
The first line of input contains one integer t (1 β€ t β€ 100) β the number of test cases. t blocks follow, each describing an individual test case.
The first line of each test case contains two integers n and x (1 β€ n β€ 50; 1 β€ x β€ 1000) β the length of arrays a and b, and the parameter x, described in the problem statement.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ x) β the elements of array a in non-descending order.
The third line of each test case contains n integers b_1, b_2, β¦, b_n (1 β€ b_1 β€ b_2 β€ ... β€ b_n β€ x) β the elements of array b in non-descending order.
Test cases are separated by a blank line.
Output
For each test case print Yes if one can rearrange the corresponding array b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n) or No otherwise.
Each character can be printed in any case.
Example
Input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
Output
Yes
Yes
No
No
Note
In the first test case, one can rearrange b so it'll look like [1, 2, 1]. In this case, 1 + 1 β€ 4; 2 + 2 β€ 4; 3 + 1 β€ 4.
In the second test case, one can set b to [5, 2], then 1 + 5 β€ 6; 4 + 2 β€ 6.
In the third test case, no matter how one shuffles array b, a_4 + b_4 = 4 + b_4 > 4.
In the fourth test case, there is only one rearrangement of array b and it doesn't satisfy the condition since 5 + 5 > 5.
Submitted Solution:
```
from math import *
sInt = lambda: int(input())
mInt = lambda: map(int, input().split())
lInt = lambda: list(map(int, input().split()))
t = sInt()
for i in range(t):
n,x = mInt()
a = lInt()
b = lInt()
if i<t-1:
space = input()
if max(a)+min(b)<=x or max(b)+min(a)<=x:
print("Yes")
else:
print("No")
``` | instruction | 0 | 19,655 | 12 | 39,310 |
No | output | 1 | 19,655 | 12 | 39,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each consisting of n positive integers, and an integer x. Please determine if one can rearrange the elements of b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n).
Input
The first line of input contains one integer t (1 β€ t β€ 100) β the number of test cases. t blocks follow, each describing an individual test case.
The first line of each test case contains two integers n and x (1 β€ n β€ 50; 1 β€ x β€ 1000) β the length of arrays a and b, and the parameter x, described in the problem statement.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 β€ a_2 β€ ... β€ a_n β€ x) β the elements of array a in non-descending order.
The third line of each test case contains n integers b_1, b_2, β¦, b_n (1 β€ b_1 β€ b_2 β€ ... β€ b_n β€ x) β the elements of array b in non-descending order.
Test cases are separated by a blank line.
Output
For each test case print Yes if one can rearrange the corresponding array b so that a_i + b_i β€ x holds for each i (1 β€ i β€ n) or No otherwise.
Each character can be printed in any case.
Example
Input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
Output
Yes
Yes
No
No
Note
In the first test case, one can rearrange b so it'll look like [1, 2, 1]. In this case, 1 + 1 β€ 4; 2 + 2 β€ 4; 3 + 1 β€ 4.
In the second test case, one can set b to [5, 2], then 1 + 5 β€ 6; 4 + 2 β€ 6.
In the third test case, no matter how one shuffles array b, a_4 + b_4 = 4 + b_4 > 4.
In the fourth test case, there is only one rearrangement of array b and it doesn't satisfy the condition since 5 + 5 > 5.
Submitted Solution:
```
try:
for _ in range(int(input())):
n,x=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if min(b)+max(a)>x:
print("No")
else:
print("Yes")
except:
pass
``` | instruction | 0 | 19,656 | 12 | 39,312 |
No | output | 1 | 19,656 | 12 | 39,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.
For permutation p = p0, p1, ..., pn, Polo has defined its beauty β number <image>.
Expression <image> means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal β as "xor".
Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
Input
The single line contains a positive integer n (1 β€ n β€ 106).
Output
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.
If there are several suitable permutations, you are allowed to print any of them.
Examples
Input
4
Output
20
0 2 1 4 3 | instruction | 0 | 19,721 | 12 | 39,442 |
Tags: implementation, math
Correct Solution:
```
n = int(input())
p = [i for i in range(n + 1)]
k = 1
while(2 * k <= n):
k *= 2
m = n + 1
while m > 0:
while k >= m:
k //= 2
for i in range(m - k):
if k - i - 1 >= 0:
p[k + i], p[k - i - 1] = p[k - i - 1], p[k + i]
m = k - i - 1
print(n * (n + 1))
print(' '.join(map(str, p)))
``` | output | 1 | 19,721 | 12 | 39,443 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.
For permutation p = p0, p1, ..., pn, Polo has defined its beauty β number <image>.
Expression <image> means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal β as "xor".
Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
Input
The single line contains a positive integer n (1 β€ n β€ 106).
Output
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.
If there are several suitable permutations, you are allowed to print any of them.
Examples
Input
4
Output
20
0 2 1 4 3 | instruction | 0 | 19,722 | 12 | 39,444 |
Tags: implementation, math
Correct Solution:
```
import sys
from math import gcd,sqrt,ceil
from collections import defaultdict,Counter,deque
from bisect import bisect_left,bisect_right
import math
from itertools import permutations
# input=sys.stdin.readline
# def print(x):
# sys.stdout.write(str(x)+"\n")
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# import sys
# import io, os
# input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def get_sum(bit,i):
s = 0
i+=1
while i>0:
s+=bit[i]
i-=i&(-i)
return s
def update(bit,n,i,v):
i+=1
while i<=n:
bit[i]+=v
i+=i&(-i)
def modInverse(b,m):
g = math.gcd(b, m)
if (g != 1):
return -1
else:
return pow(b, m - 2, m)
def primeFactors(n):
sa = set()
sa.add(n)
while n % 2 == 0:
sa.add(2)
n = n // 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
sa.add(i)
n = n // i
# sa.add(n)
return sa
def seive(n):
pri = [True]*(n+1)
p = 2
while p*p<=n:
if pri[p] == True:
for i in range(p*p,n+1,p):
pri[i] = False
p+=1
return pri
def debug(n):
l = [i for i in range(n+1)]
z = permutations(l)
maxi = 0
for i in z:
fin = 0
for j in range(n+1):
fin+=j^i[j]
maxi = max(maxi,fin)
return maxi
n = int(input())
hash = defaultdict(int)
ans = [0]*(n+1)
seti = set()
for i in range(n,0,-1):
if i not in seti:
z1 = i^int('1'*len((bin(i)[2:])),2)
seti.add(z1)
ans[z1] = i
ans[i] = z1
# print(ans)
fin = 0
for i in range(n+1):
fin+=i^ans[i]
print(fin)
print(*ans)
``` | output | 1 | 19,722 | 12 | 39,445 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.
For permutation p = p0, p1, ..., pn, Polo has defined its beauty β number <image>.
Expression <image> means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal β as "xor".
Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
Input
The single line contains a positive integer n (1 β€ n β€ 106).
Output
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.
If there are several suitable permutations, you are allowed to print any of them.
Examples
Input
4
Output
20
0 2 1 4 3 | instruction | 0 | 19,723 | 12 | 39,446 |
Tags: implementation, math
Correct Solution:
```
n = int(input()) + 1
print(n * n - n)
t = []
k = 1 << 20
while n:
while k >> 1 >= n: k >>= 1
t = [(k - 1) ^ i for i in range(k - n, n)] + t
n = k - n
print(' '.join(map(str, t)))
``` | output | 1 | 19,723 | 12 | 39,447 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.
For permutation p = p0, p1, ..., pn, Polo has defined its beauty β number <image>.
Expression <image> means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal β as "xor".
Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
Input
The single line contains a positive integer n (1 β€ n β€ 106).
Output
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.
If there are several suitable permutations, you are allowed to print any of them.
Examples
Input
4
Output
20
0 2 1 4 3 | instruction | 0 | 19,724 | 12 | 39,448 |
Tags: implementation, math
Correct Solution:
```
s = n = int(input()) + 1
t, r = [], list(range(n))[::-1]
k = 2 ** 20
while s:
while k >= 2 * s: k //= 2
t = r[n - s: n + s - k] + t
s = k - s
print(n * n - n)
print(' '.join(map(str, t)))
``` | output | 1 | 19,724 | 12 | 39,449 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.
For permutation p = p0, p1, ..., pn, Polo has defined its beauty β number <image>.
Expression <image> means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal β as "xor".
Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
Input
The single line contains a positive integer n (1 β€ n β€ 106).
Output
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.
If there are several suitable permutations, you are allowed to print any of them.
Examples
Input
4
Output
20
0 2 1 4 3 | instruction | 0 | 19,725 | 12 | 39,450 |
Tags: implementation, math
Correct Solution:
```
s = n = int(input()) + 1
t, r = [], list(range(n))[::-1]
k = 2 ** 20
while s:
while k >= 2 * s: k //= 2
t = r[n - s: n + s - k] + t
s = k - s
print(n*(n-1))
print(*t)
``` | output | 1 | 19,725 | 12 | 39,451 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.
For permutation p = p0, p1, ..., pn, Polo has defined its beauty β number <image>.
Expression <image> means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal β as "xor".
Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
Input
The single line contains a positive integer n (1 β€ n β€ 106).
Output
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.
If there are several suitable permutations, you are allowed to print any of them.
Examples
Input
4
Output
20
0 2 1 4 3 | instruction | 0 | 19,726 | 12 | 39,452 |
Tags: implementation, math
Correct Solution:
```
import math
def ones(n):
number_of_bits = (int)(math.floor(math.log(n) /
math.log(2))) + 1;
return [((1 << number_of_bits) - 1) ^ n,(1 << number_of_bits) - 1];
n = int(input())
has=[0]*(n+1)
ans=[0]*(n+1)
fin=0
for i in range(n,0,-1):
if has[i]==0:
com,fi = ones(i)
# print(com,fi,i)
has[com]=1
fin+=2*fi
ans[com]=i
ans[i]=com
print(fin)
print(*ans)
``` | output | 1 | 19,726 | 12 | 39,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.
For permutation p = p0, p1, ..., pn, Polo has defined its beauty β number <image>.
Expression <image> means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal β as "xor".
Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
Input
The single line contains a positive integer n (1 β€ n β€ 106).
Output
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.
If there are several suitable permutations, you are allowed to print any of them.
Examples
Input
4
Output
20
0 2 1 4 3 | instruction | 0 | 19,727 | 12 | 39,454 |
Tags: implementation, math
Correct Solution:
```
import math
def solve():
n, = map(int, input().split())
top = 1
while top <= n:
top *= 2
top -= 1
ans = [0] * (n + 1)
i = n
cur = n
while i > 0:
if top - i > cur:
cur = i
top = 1
while top <= cur:
top *= 2
top -= 1
ans[i] = top - i
ans[top - i] = i
i -= 1
score = 0
if n % 2 == 1:
ans[top] = 0
ans[0] = top
for i in range(n + 1):
score += ans[i] ^ i
print(score)
print(*ans)
if __name__ == "__main__":
t = 1
# t = int(input())
for _ in range(t):
solve()
``` | output | 1 | 19,727 | 12 | 39,455 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.
For permutation p = p0, p1, ..., pn, Polo has defined its beauty β number <image>.
Expression <image> means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal β as "xor".
Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
Input
The single line contains a positive integer n (1 β€ n β€ 106).
Output
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.
If there are several suitable permutations, you are allowed to print any of them.
Examples
Input
4
Output
20
0 2 1 4 3
Submitted Solution:
```
n = int(input())
a = [i for i in range(n + 1)]
for i in range(1, n, 2):
a[i], a[i + 1] = a[i + 1], a[i]
ans = sum(i ^ a[i] for i in range(n + 1))
print(ans)
print(*a)
``` | instruction | 0 | 19,728 | 12 | 39,456 |
No | output | 1 | 19,728 | 12 | 39,457 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.
For permutation p = p0, p1, ..., pn, Polo has defined its beauty β number <image>.
Expression <image> means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal β as "xor".
Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
Input
The single line contains a positive integer n (1 β€ n β€ 106).
Output
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.
If there are several suitable permutations, you are allowed to print any of them.
Examples
Input
4
Output
20
0 2 1 4 3
Submitted Solution:
```
from sys import stdin,stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int,stdin.readline().split()))
for _ in range(1):#nmbr()):
n=nmbr()
ans=[i for i in range(n+1)]
for i in range(1,n,2):
ans[i],ans[i+1]=ans[i+1],ans[i]
if n&1:ans[0],ans[-1]=ans[-1],ans[0]
x=0
for i in range(n+1):
x+=(i^ans[i])
print(x)
print(*ans)
``` | instruction | 0 | 19,729 | 12 | 39,458 |
No | output | 1 | 19,729 | 12 | 39,459 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sonya was unable to think of a story for this problem, so here comes the formal description.
You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.
Input
The first line of the input contains a single integer n (1 β€ n β€ 3000) β the length of the array.
Next line contains n integer ai (1 β€ ai β€ 109).
Output
Print the minimum number of operation required to make the array strictly increasing.
Examples
Input
7
2 1 5 11 5 9 11
Output
9
Input
5
5 4 3 2 1
Output
12
Note
In the first sample, the array is going to look as follows:
2 3 5 6 7 9 11
|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9
And for the second sample:
1 2 3 4 5
|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12 | instruction | 0 | 19,889 | 12 | 39,778 |
Tags: dp, sortings
Correct Solution:
```
N=int(input())
s=list(map(int,input().split()))
for i in range(N):s[i]-=i
X=sorted(s)
dp=[0]*N
for i in s:
mi = 7e77
for j in range(N):
mi = min(mi, dp[j])
dp[j] = mi + abs(i-X[j])
print(min(dp))
``` | output | 1 | 19,889 | 12 | 39,779 |
Provide a correct Python 3 solution for this coding contest problem.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2 | instruction | 0 | 20,235 | 12 | 40,470 |
"Correct Solution:
```
import heapq
pq = []
def insert(S,k):
heapq.heappush(pq, - k)
def extractMax(S):
a = - heapq.heappop(S)
print(str(a))
L = input()
while L != "end":
if L =="extract":
extractMax(pq)
else:
L = L.split()
insert(pq, int(L[1]))
L = input()
``` | output | 1 | 20,235 | 12 | 40,471 |
Provide a correct Python 3 solution for this coding contest problem.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2 | instruction | 0 | 20,236 | 12 | 40,472 |
"Correct Solution:
```
from heapq import heappush, heappop
heap = []
while True:
n = input().split()
if n[0] == 'end':
break
elif n[0] == 'insert':
heappush(heap, -int(n[1]))
else:
print(-heappop(heap))
``` | output | 1 | 20,236 | 12 | 40,473 |
Provide a correct Python 3 solution for this coding contest problem.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2 | instruction | 0 | 20,237 | 12 | 40,474 |
"Correct Solution:
```
from heapq import heapify, heappush, heappop
pq = []
heapify([])
while True:
S = input()
if S == "end":
break
elif S == "extract":
print(-1 * heappop(pq))
else:
com, k = S.split()
k = -1 * int(k)
heappush(pq, k)
``` | output | 1 | 20,237 | 12 | 40,475 |
Provide a correct Python 3 solution for this coding contest problem.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2 | instruction | 0 | 20,238 | 12 | 40,476 |
"Correct Solution:
```
import heapq
S = []
while True:
op = input()
if op[:2] == "in":
op, k = op.split()
k = int(k)*(-1)
heapq.heappush(S, k)
elif op[:2] == "ex":
print(heapq.heappop(S)*(-1))
else:
break
``` | output | 1 | 20,238 | 12 | 40,477 |
Provide a correct Python 3 solution for this coding contest problem.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2 | instruction | 0 | 20,239 | 12 | 40,478 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
from heapq import heappush, heappop
A = []
inp = [None]
while inp[0] != "end":
inp = input().split()
if inp[0] == "insert":
heappush(A, -int(inp[1]))
elif inp[0] == "extract":
print(-heappop(A))
``` | output | 1 | 20,239 | 12 | 40,479 |
Provide a correct Python 3 solution for this coding contest problem.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2 | instruction | 0 | 20,240 | 12 | 40,480 |
"Correct Solution:
```
import heapq
pq = []
while True:
c = input().split()
if c[0] == 'end':
break
if c[0] == 'extract':
print(-heapq.heappop(pq))
else:
heapq.heappush(pq, -int(c[1]))
``` | output | 1 | 20,240 | 12 | 40,481 |
Provide a correct Python 3 solution for this coding contest problem.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2 | instruction | 0 | 20,241 | 12 | 40,482 |
"Correct Solution:
```
import sys
from heapq import *
H=[]
O=[]
for e in sys.stdin:
if'end'==e:break
if'i'==e[0]:heappush(H,-int(e[7:]))
elif't'==e[2]:O+=[-heappop(H)]
print('\n'.join(map(str,O)))
``` | output | 1 | 20,241 | 12 | 40,483 |
Provide a correct Python 3 solution for this coding contest problem.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2 | instruction | 0 | 20,242 | 12 | 40,484 |
"Correct Solution:
```
import sys
from heapq import *
H=[]
for e in sys.stdin:
if'i'==e[0]:heappush(H,-int(e[7:]))
elif'x'==e[1]:print(-heappop(H))
``` | output | 1 | 20,242 | 12 | 40,485 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2
Submitted Solution:
```
import sys
from heapq import heapify, heappush, heappop
hq = []
S = []
for s in sys.stdin:
if s[2] == "s":
heappush(hq, -int(s[7:]))
elif s[2] == "t":
S.append(-heappop(hq))
else:
break
print("\n".join(map(str, S)))
``` | instruction | 0 | 20,243 | 12 | 40,486 |
Yes | output | 1 | 20,243 | 12 | 40,487 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2
Submitted Solution:
```
import sys
nodes = ['OFFSET'] + [-1] * 4000000
_num_nodes = 0
outputs = [None] * 2000000
_num_outputs = 0
def max_heapify(idx):
left_idx = idx * 2
right_idx = left_idx + 1
max_idx = idx
if nodes[left_idx] > nodes[max_idx]:
max_idx = left_idx
if nodes[right_idx] > nodes[max_idx]:
max_idx = right_idx
if max_idx != idx:
tmp = nodes[idx]
nodes[idx] = nodes[max_idx]
nodes[max_idx] = tmp
max_heapify(max_idx)
return
def insert(node_no):
global _num_nodes
nodes[_num_nodes+1] = node_no
_num_nodes += 1
idx = _num_nodes
while idx > 1:
parent_idx = idx // 2
if nodes[idx] > nodes[parent_idx]:
tmp = nodes[parent_idx]
nodes[parent_idx] = nodes[idx]
nodes[idx] = tmp
else:
break
idx = parent_idx
return
def extract():
global _num_outputs
outputs[_num_outputs] = nodes[1]
_num_outputs += 1
global _num_nodes
nodes[1] = nodes[_num_nodes]
nodes[_num_nodes] = -1
_num_nodes -= 1
max_heapify(1)
return
def main():
commands = sys.stdin.readlines()
for command in commands:
if command[0] == 'i':
insert(int(command[7:]))
elif command[1] == 'x':
extract()
elif command[1] == 'n':
break
for i in range(_num_outputs):
print(outputs[i])
return
main()
``` | instruction | 0 | 20,244 | 12 | 40,488 |
Yes | output | 1 | 20,244 | 12 | 40,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2
Submitted Solution:
```
import sys
from heapq import heappush, heappop
q = []
while True:
order = sys.stdin.readline().split()
if order[0] == 'end':
break
if order[0] == 'extract':
print(-heappop(q))
else:
heappush(q, -int(order[1]))
``` | instruction | 0 | 20,245 | 12 | 40,490 |
Yes | output | 1 | 20,245 | 12 | 40,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2
Submitted Solution:
```
import heapq
H = []
heapq.heapify(H)
while True:
tmp = input().split()
if tmp[0] == "end":
break
if tmp[0] == "insert":
heapq.heappush(H, -int(tmp[1]))
elif tmp[0] == "extract":
ret = heapq.heappop(H)
print(-ret)
``` | instruction | 0 | 20,246 | 12 | 40,492 |
Yes | output | 1 | 20,246 | 12 | 40,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2
Submitted Solution:
```
class PQueue:
def __init__(self):
self.keys = []
self.size = 0
def maxHeapify(self, i):
l = i * 2 + 1
r = i * 2 + 2
if l < self.size and self.keys[l] > self.keys[i]:
largest = l
else:
largest = i
if r < self.size and self.keys[r] > self.keys[largest]:
largest = r
if largest != i:
self.keys[i], self.keys[largest] = self.keys[largest], self.keys[i]
self.maxHeapify(largest)
def insert(self, key):
i = self.size
self.size += 1
self.keys.append(key)
parent = (i - 1) // 2
while i > 0 and self.keys[parent] < self.keys[i]:
self.keys[i], self.keys[parent] = self.keys[parent], self.keys[i]
i = parent
parent = (parent - 1) // 2
def heapExtraMax(self):
max_key = self.keys[0]
if self.size > 1:
self.keys[0] = self.keys.pop()
self.size -= 1
if self.size > 1:
self.maxHeapify(0)
else:
del self.keys[0]
self.size -= 1
return max_key
S = PQueue()
import sys
for x in sys.stdin.readlines():
if x[0] == 'i':
k = int(x[7:])
S.insert(k)
elif x[:2] == 'ex':
m_k = S.heapExtraMax()
print(m_k)
else:
pass
``` | instruction | 0 | 20,247 | 12 | 40,494 |
No | output | 1 | 20,247 | 12 | 40,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2
Submitted Solution:
```
from sys import stdin, stdout, exit
def max_heapify(a, i):
heap_size = a[0]
L = 2*i
R = 2*i + 1
if L <= heap_size and a[L] > a[i]:
largest = L
else:
largest = i
if R <= heap_size and a[R] > a[largest]:
largest = R
if largest != i:
a[i], a[largest] = a[largest], a[i]
max_heapify(a, largest)
def main():
ret = []
heap = [-1] * 2000001
heap[0] = 1
for cmd in stdin:
if cmd[0] == 'i':
heap[0] += 1
i = heap[0]
heap[i] = int(cmd[7:])
half = i // 2
while i > 1 and heap[half] < heap[i]:
heap[i], heap[half] = heap[half], heap[i]
i //= 2
half = i // 2
elif cmd[1] == 'x':
ret.append(heap[1])
heap[1] = heap[heap[0]]
heap[0] -= 1
max_heapify(heap, 1)
elif cmd[1] == 'n':
sys.stdout.writelines("\n".join([str(s) for s in ret]))
print()
exit(0)
main()
``` | instruction | 0 | 20,248 | 12 | 40,496 |
No | output | 1 | 20,248 | 12 | 40,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2
Submitted Solution:
```
import sys
class QueueEmptyError(Exception):
pass
class PriorityQueue:
def __init__(self):
self._nodes = []
def add(self, v):
self._nodes.append(v)
i = len(self._nodes) - 1
vi = self._nodes[i]
while i > 0:
p = (i-1) // 2
vp = self._nodes[p]
if vp < vi:
self._nodes[p], self._nodes[i] = vi, vp
i, vi = p, vp
else:
break
def extract(self):
size = len(self._nodes) - 1
if size < 0:
raise QueueEmptyError()
node = self._nodes[0]
last = self._nodes.pop()
if size > 0:
self._nodes[0] = last
i, vi = 0, last
while i < size // 2:
top, vt = i, vi
left = (i+1) * 2 - 1
right = (i+1) * 2
if left < size:
vl = self._nodes[left]
if vl > vt:
top, vt = left, vl
if right < size:
vr = self._nodes[right]
if vr > vt:
top, vt = right, vr
if top > i:
self._nodes[top], self._nodes[i] = vi, vt
i, vi = top, vt
else:
break
return node
def run():
q = PriorityQueue()
for line in sys.stdin:
if line.startswith("i"):
q.add(int(line[7:]))
elif line.startswith("ex"):
print(q.extract())
elif line.startswith("end"):
break
else:
ValueError("invalid command")
if __name__ == '__main__':
run()
``` | instruction | 0 | 20,249 | 12 | 40,498 |
No | output | 1 | 20,249 | 12 | 40,499 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:
* $insert(S, k)$: insert an element $k$ into the set $S$
* $extractMax(S)$: remove and return the element of $S$ with the largest key
Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$. The priority queue manages a set of integers, which are also keys for the priority.
Constraints
* The number of operations $\leq 2,000,000$
* $0 \leq k \leq 2,000,000,000$
Input
Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.
The input ends with "end" operation.
Output
For each "extract" operation, print the element extracted from the priority queue $S$ in a line.
Example
Input
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end
Output
8
10
11
2
Submitted Solution:
```
Q,s = [],[]
flag = 0
while(flag==0):
c = input()
if c == "end":
break
elif c == "extract":
print(Q.pop(0))
else:
s = c.split()
Q.append(int(s[1]))
Q.sort(reverse = True)
``` | instruction | 0 | 20,250 | 12 | 40,500 |
No | output | 1 | 20,250 | 12 | 40,501 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3]. | instruction | 0 | 20,387 | 12 | 40,774 |
Tags: combinatorics, math
Correct Solution:
```
# -*- coding: utf-8 -*-
class FactMod():
def __init__(self, n, mod):
self.mod = mod
self.f = [1]*(n+1)
for i in range(1, n+1):
self.f[i] = self.f[i-1]*i % mod
self.inv = [pow(self.f[-1], mod-2, mod)]
for i in range(1, n+1)[::-1]:
self.inv.append(self.inv[-1]*i % mod)
self.inv.reverse()
def comb(self, n, r):
ret = self.f[n] * self.inv[n-r]*self.inv[r]
ret %= self.mod
return ret
N, M = map(int, input().split())
MOD = 998244353
F = FactMod(M, MOD)
if N == 2:
print(0)
else:
ans = F.comb(M, N-1)*(N-2)*pow(2, N-3, MOD) % MOD
print(ans)
``` | output | 1 | 20,387 | 12 | 40,775 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3]. | instruction | 0 | 20,388 | 12 | 40,776 |
Tags: combinatorics, math
Correct Solution:
```
import sys
mod=998244353
fact=[1 for _ in range(200006)]
for i in range(1,200005):
fact[i]=(fact[i-1]*i)%mod
def modinv(a,mod):
return pow(a,mod-2,mod)
n,m=map(int,sys.stdin.readline().split())
if n==2:
print(0)
else:
ans=(fact[m]*modinv(fact[n-1],mod)*modinv(fact[m-n+1],mod))%mod
ans=ans*(n-2)
ans=ans%mod
ans=ans*pow(2,n-3,mod)%mod
print(ans)
``` | output | 1 | 20,388 | 12 | 40,777 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3]. | instruction | 0 | 20,389 | 12 | 40,778 |
Tags: combinatorics, math
Correct Solution:
```
import math
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
n,m=map(int,input().split())
p=998244353
ans=0
if(n==2) :
ans=0
else :
ans=((ncr(m,n-1,p)%p)*((n-2)%p)*(pow(2,n-3,p)%p))%p
print(ans)
``` | output | 1 | 20,389 | 12 | 40,779 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3]. | instruction | 0 | 20,390 | 12 | 40,780 |
Tags: combinatorics, math
Correct Solution:
```
from sys import stdin,stdout #
import math #
import heapq #
#
t = 1 #
def aint(): #
return int(input().strip()) #
def lint(): #
return list(map(int,input().split())) #
def fint(): #
return list(map(int,stdin.readline().split())) #
#
########################################################
fact=[1]
MOD=998244353
for i in range(1,200002):
fact.append((fact[-1]*i)%MOD)
def modinv(n):
return pow(n,MOD-2,MOD)
def main():
n,m=lint()
if n==2:
print(0)
else:
print((fact[m]*modinv(fact[n-1])*modinv(fact[m-n+1])*pow(2,n-3,MOD)*(n-2))%MOD)
t=1
########################################################
for i in range(t): #
main() #
``` | output | 1 | 20,390 | 12 | 40,781 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3]. | instruction | 0 | 20,391 | 12 | 40,782 |
Tags: combinatorics, math
Correct Solution:
```
MOD = 998244353
fak = []
fak.append(1)
for i in range(200005):
fak.append(fak[-1] * (i + 1) % MOD)
def brzo(x, y):
if not y: return 1
if y % 2: return brzo(x, y - 1) * x % MOD
k = brzo(x, y // 2)
return k * k % MOD
def inv(x):
return brzo(x, MOD - 2)
n, m = map(int, input().split())
if n <= 2: print(0)
else: print((fak[m]*inv(fak[n-1]*fak[m-n+1])*(n-2)*(1 << (n - 3)))%MOD)
``` | output | 1 | 20,391 | 12 | 40,783 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3]. | instruction | 0 | 20,392 | 12 | 40,784 |
Tags: combinatorics, math
Correct Solution:
```
'''
@judge CodeForces
@id 1312D
@name Count the Arrays
@tag Combinatorics, Congruence Modulus
'''
from sys import stdin
def ExtGCD(a, b):
if a % b == 0:
return (b, 0, 1)
g, x, y = ExtGCD(b, a % b)
return (g, y, x - (a // b) * y)
def inv(a, m):
_, _, y = ExtGCD(m, a)
return (y + m) % m
def modComb(a, b, m):
ans = 1
for x in range(1, min(b, a - b) + 1):
ans = (ans * (a - x + 1)) % m
ans = (ans * inv(x, m)) % m
return ans
def modPow(a, b, m):
ans = 1
aa = a
while b > 0:
if b & 1:
ans = (ans * aa) % m
aa = (aa * aa) % m
b >>= 1
return ans
def solve(n, m):
MOD = 998244353
ax = modComb(m, n - 1, MOD) * (n - 2) % MOD
ay = modPow(2, n - 3, MOD)
return ax * ay % MOD
for line in stdin:
n, m = map(int, line.split())
print(solve(n, m))
``` | output | 1 | 20,392 | 12 | 40,785 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3]. | instruction | 0 | 20,393 | 12 | 40,786 |
Tags: combinatorics, math
Correct Solution:
```
import sys
input = sys.stdin.readline
#sys.setrecursionlimit(10**6)
def I(): return input().strip()
def II(): return int(input().strip())
def LI(): return [*map(int, input().strip().split())]
import string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter, OrderedDict
from itertools import permutations, combinations, groupby
from operator import itemgetter
MOD = 998244353
def ncr(nu, r, p):
num = den = 1
for i in range(r):
num = (num * (nu - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def power(y, x, MOD):
temp = 1
for i in range(x):
temp = (temp * y) % MOD
return temp
for _ in range(1):
n, m = LI()
if n <= 2:
print(0)
continue
ans = (ncr(m, n - 1, MOD) * (n - 2)) % MOD
ans = (ans * pow(2, n - 3, MOD)) % MOD
print(ans)
``` | output | 1 | 20,393 | 12 | 40,787 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3]. | instruction | 0 | 20,394 | 12 | 40,788 |
Tags: combinatorics, math
Correct Solution:
```
n, m = map(int, input().split())
if n == 2:
print(0)
else:
C, ans = 998244353, 0
fact, pow1 = [1], pow(2, n-3, C)
for q in range(1, m+1):
fact.append(fact[-1]*q % C)
for q in range(n-1, m+1):
ans += fact[q-1]*pow(fact[n-2], C-2, C)*pow(fact[q-n+1], C-2, C)*(n-2)*pow1
print(ans % C)
``` | output | 1 | 20,394 | 12 | 40,789 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3].
Submitted Solution:
```
import math, collections, sys
input = sys.stdin.readline
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den, p - 2, p)) % p
n, m = map(int, input().split())
mod = 998244353
if n == 2:
print(0)
else:
ans = ncr(m, n-1, mod)
ans*=(n-2)
ans%=mod
ans*=pow(2, n-3, mod)
ans%=mod
print(ans)
``` | instruction | 0 | 20,395 | 12 | 40,790 |
Yes | output | 1 | 20,395 | 12 | 40,791 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3].
Submitted Solution:
```
import sys
input = sys.stdin.readline
#sys.setrecursionlimit(10**6)
def I(): return input().strip()
def II(): return int(input().strip())
def LI(): return [*map(int, input().strip().split())]
import string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter, OrderedDict
from itertools import permutations, combinations, groupby
from operator import itemgetter
MOD = 998244353
# Python3 function to
# calculate nCr % p
def ncr(nt, r, p):
# initialize numerator
# and denominator
num = den = 1
for i in range(r):
num = (num * (nt - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
for _ in range(1):
n, m = LI()
if n <= 2:
print(0)
continue
ans = (ncr(m, n - 1, MOD) * (n - 2)) % MOD
ans = (ans * pow(2, n - 3, MOD)) % MOD
print(ans)
``` | instruction | 0 | 20,396 | 12 | 40,792 |
Yes | output | 1 | 20,396 | 12 | 40,793 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3].
Submitted Solution:
```
n,m = map(int,input().split())
if n == 2:
print(0)
exit()
mod = 998244353
MAX_N = 200004
fact = [1]
fact_inv = [0]*(MAX_N+4)
for i in range(MAX_N+3):
fact.append(fact[-1]*(i+1)%mod)
fact_inv[-1] = pow(fact[-1],mod-2,mod)
for i in range(MAX_N+2,-1,-1):
fact_inv[i] = fact_inv[i+1]*(i+1)%mod
def mod_comb_k(n,k,mod):
return fact[n] * fact_inv[k] % mod * fact_inv[n-k] %mod
res = (mod_comb_k(m,n-1,mod)*(n-2)%mod)*pow(2,n-3,mod)%mod
print(res)
``` | instruction | 0 | 20,397 | 12 | 40,794 |
Yes | output | 1 | 20,397 | 12 | 40,795 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3].
Submitted Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
from fractions import gcd
import heapq
raw_input = stdin.readline
pr = stdout.write
mod=998244353
def ni():
return int(raw_input())
def li():
return list(map(int,raw_input().split()))
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return tuple(map(int,stdin.read().split()))
range = xrange # not for python 3.0+
# main code
def inv(x):
return pow(x,mod-2,mod)
n,m=li()
if n==2:
print 0
exit()
fac=[1]*(m+1)
for i in range(1,m+1):
fac[i]=(i*fac[i-1])%mod
ans=(fac[m]*inv((fac[n-1]*fac[m-n+1])%mod))%mod
ans=(ans*(n-2))%mod
ans=(ans*pow(2,n-3,mod))%mod
pn(ans)
``` | instruction | 0 | 20,399 | 12 | 40,798 |
Yes | output | 1 | 20,399 | 12 | 40,799 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3].
Submitted Solution:
```
MOD = 998244353
def C(n, r):
num = den = 1
for i in range(r):
num = (num * (n - i)) % MOD
den = (den * (i + 1)) % MOD
return (num * pow(den, MOD - 2, MOD)) % MOD
n, m = map(int, input().split())
ans = C(m, n - 1) * (n - 2) * pow(2, n - 3)
print(ans % MOD)
``` | instruction | 0 | 20,400 | 12 | 40,800 |
No | output | 1 | 20,400 | 12 | 40,801 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3].
Submitted Solution:
```
import math
def nCr(n,r):
if n-r<0:
return 0
return (math.factorial(n))//(math.factorial(r)*math.factorial(n-r))
n,m=map(int,input().split())
print(nCr(m,n-1)*(n-2)%(998244353))
``` | instruction | 0 | 20,401 | 12 | 40,802 |
No | output | 1 | 20,401 | 12 | 40,803 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your task is to calculate the number of arrays such that:
* each array contains n elements;
* each element is an integer from 1 to m;
* for each array, there is exactly one pair of equal elements;
* for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that a_j < a_{j + 1}, if j < i, and a_j > a_{j + 1}, if j β₯ i).
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5).
Output
Print one integer β the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.
Examples
Input
3 4
Output
6
Input
3 5
Output
10
Input
42 1337
Output
806066790
Input
100000 200000
Output
707899035
Note
The arrays in the first example are:
* [1, 2, 1];
* [1, 3, 1];
* [1, 4, 1];
* [2, 3, 2];
* [2, 4, 2];
* [3, 4, 3].
Submitted Solution:
```
n, m = map(int, input().split())
def C(n, m):
mm = 1
nn = 1
for t in range(1, min(n, m -n) + 1):
mm *= m
nn *= t
m -= 1
return mm // nn
def power(a, n):
if n == 0:
return 1
elif n == 1:
return a
elif n % 2 != 0:
return a * power(a, n - 1)
elif n % 2 == 0:
return power(a * a, n / 2)
print(C(n, m))
print(power(2, n - 3))
print(C(n - 1, m) * (n - 2) * power(2, n - 3))
``` | instruction | 0 | 20,402 | 12 | 40,804 |
No | output | 1 | 20,402 | 12 | 40,805 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.