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.
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence a consisting of n (1 β€ n β€ 105) elements a1, a2, ..., an (1 β€ ai β€ 105). A subsequence ai1, ai2, ..., aik where 1 β€ i1 < i2 < ... < ik β€ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 β€ i β€ n), into three groups:
1. group of all i such that ai belongs to no longest increasing subsequences.
2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
3. group of all i such that ai belongs to every longest increasing subsequence.
Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.
Input
The first line contains the single integer n (1 β€ n β€ 105) denoting the number of elements of sequence a.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 105).
Output
Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.
Examples
Input
1
4
Output
3
Input
4
1 3 2 5
Output
3223
Input
4
1 5 2 3
Output
3133
Note
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.
In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}. | instruction | 0 | 52,588 | 12 | 105,176 |
Tags: data structures, dp, greedy, hashing, math
Correct Solution:
```
n = int(input())
secuencia = [None] * n
ma = 0
for num, e in enumerate(input().strip().split()):
en = int(e)
secuencia[num] = [en, 0, num]
ma = max(ma, en)
escritura = ["1"] * len(secuencia)
# ma = max((x[0] for x in secuencia))
bit = [0] * (ma + 1)
def max_x(x, l):
suma = 0
while x != 0:
suma = max(suma, l[x])
x -= (x & -x)
return suma
def update_x(x, l, max_n, val):
while x <= max_n:
if val > l[x]:
l[x] = val
else:
return
x += (x & -x)
# def index_list(item, alist, first=0, last=-1):
# pos = first
# while first <= last:
# midpoint = (first + last) // 2
# pos = midpoint
# if alist[midpoint][1] == item:
# return midpoint
# else:
# if item > alist[midpoint][1]:
# last = midpoint - 1
# else:
# first = midpoint + 1
# pos += 1
# return pos
def new_get_secuence(e):
num = secuencia[e][0]
maximo = max_x(num - 1, bit) + 1
update_x(num, bit, ma, maximo)
return maximo
for e in range(n):
secuencia[e][1] = new_get_secuence(e)
secuencia.sort(key=lambda x: (-x[1], -x[2]))
ultimos = [(ma + 1, 0, n)]
partir = 0
moment_max = secuencia[0][1]
# while moment_max > 0:
# terminar = n
# usados = []
# for e in range(partir, n):
# if secuencia[e][1] < moment_max:
# terminar = e
# break
# for element in ultimos:
# if secuencia[e][2] < element[2]:
# if secuencia[e][0] < element[0]:
# usados.append(secuencia[e])
# break
# if len(usados) == 1:
# escritura[usados[0][2]] = "3"
# else:
# for e in usados:
# escritura[e[2]] = "2"
# ultimos = usados
# partir = terminar
# moment_max -= 1
usados = []
for e in secuencia:
if e[1] < moment_max:
if len(usados) == 1:
escritura[usados[0][2]] = "3"
else:
for y in usados:
escritura[y[2]] = "2"
ultimos = usados
usados = []
moment_max -= 1
for element in ultimos:
if e[2] < element[2]:
if e[0] < element[0]:
usados.append(e)
break
else:
break
if len(usados) == 1:
escritura[usados[0][2]] = "3"
else:
for y in usados:
escritura[y[2]] = "2"
print("".join(escritura))
``` | output | 1 | 52,588 | 12 | 105,177 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence a consisting of n (1 β€ n β€ 105) elements a1, a2, ..., an (1 β€ ai β€ 105). A subsequence ai1, ai2, ..., aik where 1 β€ i1 < i2 < ... < ik β€ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 β€ i β€ n), into three groups:
1. group of all i such that ai belongs to no longest increasing subsequences.
2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
3. group of all i such that ai belongs to every longest increasing subsequence.
Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.
Input
The first line contains the single integer n (1 β€ n β€ 105) denoting the number of elements of sequence a.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 105).
Output
Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.
Examples
Input
1
4
Output
3
Input
4
1 3 2 5
Output
3223
Input
4
1 5 2 3
Output
3133
Note
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.
In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}. | instruction | 0 | 52,589 | 12 | 105,178 |
Tags: data structures, dp, greedy, hashing, math
Correct Solution:
```
# http://codeforces.com/problemset/problem/486/E
import sys
import re
import collections
import threading
import bisect # bisect.bisect(list, value)
N = 0
Last = []
AA = [0 for _ in range(100005)]
LL = [0 for _ in range(100005)]
BB = [0 for _ in range(100005)] # reverse
SS = [0 for _ in range(100005)] # reverse
TT = [0 for _ in range(100005)] # reverse
ANS = [1 for _ in range(100005)]
def LIS(A, L):
global N
Last.append(-1) # dummy
for i in range(N):
if Last[-1] < A[i]:
Last.append(A[i])
it = bisect.bisect_left(Last, A[i]) # lower_bound() in C++
Last[it] = A[i]
L[i] = it
N = int(sys.stdin.readline())
AA = list(map(int, sys.stdin.readline().strip().split()))
LIS(AA, LL)
length = max(LL)
for i in range(N):
BB[N-i-1] = 1000000 - AA[i]
Last = []
LIS(BB, SS)
for i in range(N):
TT[N-i-1] = SS[i]
for i in range(N):
if LL[i] + TT[i] == length + 1 :
ANS[i] = 3
maxi = 0;
mini = 1000000;
for i in range(N):
if ANS[i] != 1:
if AA[i] <= maxi: ANS[i] = 2
maxi = max(maxi, AA[i])
for i in range(N-1, -1, -1):
if ANS[i] != 1:
if AA[i] >= mini: ANS[i] = 2
mini = min(mini, AA[i])
for i in range(N):
print(ANS[i], end="", flush=True)
print()
``` | output | 1 | 52,589 | 12 | 105,179 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence a consisting of n (1 β€ n β€ 105) elements a1, a2, ..., an (1 β€ ai β€ 105). A subsequence ai1, ai2, ..., aik where 1 β€ i1 < i2 < ... < ik β€ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 β€ i β€ n), into three groups:
1. group of all i such that ai belongs to no longest increasing subsequences.
2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
3. group of all i such that ai belongs to every longest increasing subsequence.
Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.
Input
The first line contains the single integer n (1 β€ n β€ 105) denoting the number of elements of sequence a.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 105).
Output
Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.
Examples
Input
1
4
Output
3
Input
4
1 3 2 5
Output
3223
Input
4
1 5 2 3
Output
3133
Note
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.
In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}. | instruction | 0 | 52,590 | 12 | 105,180 |
Tags: data structures, dp, greedy, hashing, math
Correct Solution:
```
import bisect
import sys
MAX_N = 1000000
from bisect import*
def LIS(arr):
d = [0]*n
last = []
for i in range(n):
if not last or last[-1] < arr[i]:
last.append(arr[i])
idx = bisect_left(last,arr[i])
last[idx] = arr[i]
d[i] = idx +1
return d
n = int(input())
a = list(map(int,input().split()))
Order_d = LIS(a)
b = [-v for v in a]
b.reverse()
Reverse_d = LIS(b)
Reverse_d.reverse()
Lis = max(Order_d)
ans = [1]*n
for i in range(n):
if Order_d[i] + Reverse_d[i] == Lis+1:
ans[i] = 3
Lmax = -1e10
Rmin = 1e10
for i in range(n):
if ans[i]==1:continue
if a[i]<=Lmax:
ans[i]=2
Lmax = max(Lmax,a[i])
for i in range(n-1,-1,-1):
if ans[i]==1:continue
if a[i]>=Rmin:
ans[i] = 2
Rmin = min(Rmin,a[i])
first_Lis_idx = 0
second_Lis_idx = 0
for i in range(n):
if ans[i]==3:
first_Lis_idx = i
break
for i in range(n-1,-1,-1):
if ans[i]==3:
second_Lis_idx = i
break
for v in ans:
print(v,end='')
``` | output | 1 | 52,590 | 12 | 105,181 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence a consisting of n (1 β€ n β€ 105) elements a1, a2, ..., an (1 β€ ai β€ 105). A subsequence ai1, ai2, ..., aik where 1 β€ i1 < i2 < ... < ik β€ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 β€ i β€ n), into three groups:
1. group of all i such that ai belongs to no longest increasing subsequences.
2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
3. group of all i such that ai belongs to every longest increasing subsequence.
Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.
Input
The first line contains the single integer n (1 β€ n β€ 105) denoting the number of elements of sequence a.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 105).
Output
Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.
Examples
Input
1
4
Output
3
Input
4
1 3 2 5
Output
3223
Input
4
1 5 2 3
Output
3133
Note
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.
In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}. | instruction | 0 | 52,591 | 12 | 105,182 |
Tags: data structures, dp, greedy, hashing, math
Correct Solution:
```
# a simple parser for python. use get_number() and get_word() to read
def main():
def parser():
while 1:
data = list(input().split(' '))
for number in data:
if len(number) > 0:
yield(number)
input_parser = parser()
gets = lambda: next(input_parser)
def getNum():
data = gets()
try:
return int(data)
except ValueError:
return float(data)
# ---------program---------
from bisect import bisect_left
binslb = bisect_left
MAXA = int(9e9)
n = getNum()
RANGN = range(n)
a = [ getNum() for _ in RANGN ]
revlis = []
g = [MAXA]*n
for i in reversed(RANGN):
x = -a[i]
pt = binslb( g, x )
revlis.append(pt+1)
if( x < g[pt] ):
g[pt] = x
hlis = max( revlis )
lis, inlis = [], []
d = [0]*n
for i in RANGN: g[i] = MAXA
for i in RANGN:
pt = binslb( g, a[i] )
lis.append( pt+1 )
inlis.append( lis[i] + revlis[n-i-1] > hlis )
d[pt] += inlis[-1]
if( a[i] < g[pt] ):
g[pt] = a[i]
print( ''.join(
[ '32'[d[lis[i]-1] > 1] if inlis[i] else '1'
for i in RANGN ]
) )
if __name__ == "__main__":
main()
``` | output | 1 | 52,591 | 12 | 105,183 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence a consisting of n (1 β€ n β€ 105) elements a1, a2, ..., an (1 β€ ai β€ 105). A subsequence ai1, ai2, ..., aik where 1 β€ i1 < i2 < ... < ik β€ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 β€ i β€ n), into three groups:
1. group of all i such that ai belongs to no longest increasing subsequences.
2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
3. group of all i such that ai belongs to every longest increasing subsequence.
Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.
Input
The first line contains the single integer n (1 β€ n β€ 105) denoting the number of elements of sequence a.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 105).
Output
Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.
Examples
Input
1
4
Output
3
Input
4
1 3 2 5
Output
3223
Input
4
1 5 2 3
Output
3133
Note
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.
In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}. | instruction | 0 | 52,592 | 12 | 105,184 |
Tags: data structures, dp, greedy, hashing, math
Correct Solution:
```
# a simple parser for python. use get_number() and get_word() to read
def main():
def parser():
while 1:
data = list(input().split(' '))
for number in data:
if len(number) > 0:
yield(number)
input_parser = parser()
gets = lambda: next(input_parser)
def getNum():
data = gets()
try:
return int(data)
except ValueError:
return float(data)
# ---------program---------
from bisect import bisect_left # as bisect_left
MAXA = int(9e9)
n = getNum()
RANGN = range(n)
a = [ getNum() for _ in RANGN ]
revlis = []
g = [MAXA]*n
for i in reversed(RANGN):
x = -a[i]
pt = bisect_left( g, x )
revlis.append(pt+1)
if( x < g[pt] ):
g[pt] = x
hlis = max( revlis )
lis, inlis = [], []
d = [0]*n
for i in RANGN: g[i] = MAXA
for i in RANGN:
pt = bisect_left( g, a[i] )
lis.append( pt+1 )
inlis.append( lis[i] + revlis[n-i-1] > hlis )
d[pt] += inlis[-1]
if( a[i] < g[pt] ):
g[pt] = a[i]
print( ''.join(
[ '32'[d[lis[i]-1] > 1] if inlis[i] else '1'
for i in RANGN ]
) )
if __name__ == "__main__":
main()
``` | output | 1 | 52,592 | 12 | 105,185 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence a consisting of n (1 β€ n β€ 105) elements a1, a2, ..., an (1 β€ ai β€ 105). A subsequence ai1, ai2, ..., aik where 1 β€ i1 < i2 < ... < ik β€ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 β€ i β€ n), into three groups:
1. group of all i such that ai belongs to no longest increasing subsequences.
2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
3. group of all i such that ai belongs to every longest increasing subsequence.
Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.
Input
The first line contains the single integer n (1 β€ n β€ 105) denoting the number of elements of sequence a.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 105).
Output
Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.
Examples
Input
1
4
Output
3
Input
4
1 3 2 5
Output
3223
Input
4
1 5 2 3
Output
3133
Note
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.
In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}.
Submitted Solution:
```
n = int(input())
secuencia = [None] * n
ma = 0
for num, e in enumerate(input().strip().split()):
en = int(e)
secuencia[num] = [en, 0, num]
ma = max(ma, en)
escritura = ["1"] * len(secuencia)
# ma = max((x[0] for x in secuencia))
bit = [0] * (ma + 1)
def max_x(x, l):
suma = 0
while x != 0:
suma = max(suma, l[x])
x -= (x & -x)
return suma
def update_x(x, l, max_n, val):
while x <= max_n:
if val > l[x]:
l[x] = val
else:
return
x += (x & -x)
# def index_list(item, alist, first=0, last=-1):
# pos = first
# while first <= last:
# midpoint = (first + last) // 2
# pos = midpoint
# if alist[midpoint][1] == item:
# return midpoint
# else:
# if item > alist[midpoint][1]:
# last = midpoint - 1
# else:
# first = midpoint + 1
# pos += 1
# return pos
def new_get_secuence(e):
num = secuencia[e][0]
maximo = max_x(num - 1, bit) + 1
update_x(num, bit, ma, maximo)
return maximo
for e in range(n):
secuencia[e][1] = new_get_secuence(e)
secuencia.sort(key=lambda x: (-x[1], x[2]))
ultimos = [(ma + 1, 0, n)]
partir = 0
moment_max = secuencia[0][1]
# while moment_max > 0:
# terminar = n
# usados = []
# for e in range(partir, n):
# if secuencia[e][1] < moment_max:
# terminar = e
# break
# for element in ultimos:
# if secuencia[e][2] < element[2]:
# if secuencia[e][0] < element[0]:
# usados.append(secuencia[e])
# break
# if len(usados) == 1:
# escritura[usados[0][2]] = "3"
# else:
# for e in usados:
# escritura[e[2]] = "2"
# ultimos = usados
# partir = terminar
# moment_max -= 1
usados = []
for e in secuencia:
if e[1] < moment_max:
if len(usados) == 1:
escritura[usados[0][2]] = "3"
else:
for y in usados:
escritura[y[2]] = "2"
ultimos = usados
usados = []
moment_max -= 1
for element in ultimos:
if e[2] < element[2]:
if e[0] < element[0]:
usados.append(e)
break
else:
break
if len(usados) == 1:
escritura[usados[0][2]] = "3"
else:
for y in usados:
escritura[y[2]] = "2"
print("".join(escritura))
``` | instruction | 0 | 52,593 | 12 | 105,186 |
No | output | 1 | 52,593 | 12 | 105,187 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence a consisting of n (1 β€ n β€ 105) elements a1, a2, ..., an (1 β€ ai β€ 105). A subsequence ai1, ai2, ..., aik where 1 β€ i1 < i2 < ... < ik β€ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 β€ i β€ n), into three groups:
1. group of all i such that ai belongs to no longest increasing subsequences.
2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
3. group of all i such that ai belongs to every longest increasing subsequence.
Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.
Input
The first line contains the single integer n (1 β€ n β€ 105) denoting the number of elements of sequence a.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 105).
Output
Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.
Examples
Input
1
4
Output
3
Input
4
1 3 2 5
Output
3223
Input
4
1 5 2 3
Output
3133
Note
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.
In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}.
Submitted Solution:
```
n = int(input())
secuencia = [[int(x), 0, y] for y, x in enumerate(input().strip().split())]
escritura = ["1"] * len(secuencia)
# moment_max = 1
ma = max((x[0] for x in secuencia))
bit = [0] * (ma + 1)
def max_x(x, l):
suma = 0
while x != 0:
suma = max(suma, l[x])
x -= (x & -x)
return suma
def update_x(x, l, max_n, val):
while x <= max_n:
if val > l[x]:
l[x] = val
else:
return
x += (x & -x)
def index_list(item, alist, first=0):
last = len(alist) - 1
pos = first
while first <= last:
midpoint = (first + last) // 2
pos = midpoint
if alist[midpoint][1] == item:
return midpoint
else:
if item > alist[midpoint][1]:
last = midpoint - 1
else:
first = midpoint + 1
pos += 1
return pos
def get_secuence(e):
global moment_max
num = secuencia[e][0]
maximo = 1
for elem in secuencia[e + 1:]:
if elem[1] + 1 <= maximo:
return maximo
if elem[0] > num and elem[1] + 1 > maximo:
maximo = elem[1] + 1
if maximo > moment_max:
moment_max = maximo
return maximo
return maximo
def new_get_secuence(e):
num = secuencia[e][0]
maximo = max_x(num - 1, bit) + 1
update_x(maximo, bit, ma, maximo)
return maximo
# for e in range(len(secuencia) - 1, -1, -1):
# secuencia[e][1] = get_secuence(e)
# q = secuencia.pop(e)
# secuencia.insert(index_list(q[1], secuencia, e), q)
for e in range(n):
secuencia[e][1] = new_get_secuence(e)
#print(bit)
#print(secuencia)
secuencia.sort(key=lambda x: -x[1])
#print(secuencia)
ultimos = [(ma + 1, 0, n)]
partir = 0
moment_max = secuencia[0][1]
# while moment_max > 0:
# terminar = len(secuencia)
# for e in range(partir, len(secuencia)):
# if secuencia[e][1] < moment_max:
# terminar = e
# break
# usados = []
# for e in range(partir, terminar):
# for element in ultimos:
# if secuencia[e][2] > element[2] and secuencia[e][0] > element[0]:
# usados.append(secuencia[e])
# break
# if len(usados) == 1:
# for e in usados:
# escritura[e[2]] = "3"
# else:
# for e in usados:
# escritura[e[2]] = "2"
# ultimos = usados
# partir = terminar
# moment_max -= 1
while moment_max > 0:
terminar = n
for e in range(partir, n):
if secuencia[e][1] < moment_max:
terminar = e
break
usados = []
#print(':', partir, terminar)
for e in range(partir, terminar):
for element in ultimos:
if secuencia[e][2] < element[2] and secuencia[e][0] < element[0]:
usados.append(secuencia[e])
break
if len(usados) == 1:
escritura[usados[0][2]] = "3"
else:
for e in usados:
escritura[e[2]] = "2"
#print(ultimos,usados)
ultimos = usados
partir = terminar
moment_max -= 1
print("".join(escritura))
``` | instruction | 0 | 52,594 | 12 | 105,188 |
No | output | 1 | 52,594 | 12 | 105,189 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence a consisting of n (1 β€ n β€ 105) elements a1, a2, ..., an (1 β€ ai β€ 105). A subsequence ai1, ai2, ..., aik where 1 β€ i1 < i2 < ... < ik β€ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 β€ i β€ n), into three groups:
1. group of all i such that ai belongs to no longest increasing subsequences.
2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
3. group of all i such that ai belongs to every longest increasing subsequence.
Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.
Input
The first line contains the single integer n (1 β€ n β€ 105) denoting the number of elements of sequence a.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 105).
Output
Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.
Examples
Input
1
4
Output
3
Input
4
1 3 2 5
Output
3223
Input
4
1 5 2 3
Output
3133
Note
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.
In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}.
Submitted Solution:
```
n = int(input())
secuencia = [[int(x), 0, y] for y, x in enumerate(input().strip().split())]
escritura = ["1"] * len(secuencia)
ma = max((x[0] for x in secuencia))
bit = [0] * (ma + 1)
def max_x(x, l):
suma = 0
while x != 0:
suma = max(suma, l[x])
x -= (x & -x)
return suma
def update_x(x, l, max_n, val):
while x <= max_n:
if val > l[x]:
l[x] = val
else:
return
x += (x & -x)
def index_list(item, alist, first=0, last=-1):
pos = first
while first <= last:
midpoint = (first + last) // 2
pos = midpoint
if alist[midpoint][1] == item:
return midpoint
else:
if item > alist[midpoint][1]:
last = midpoint - 1
else:
first = midpoint + 1
pos += 1
return pos
def new_get_secuence(e):
num = secuencia[e][0]
maximo = max_x(num - 1, bit) + 1
update_x(num, bit, ma, maximo)
return maximo
for e in range(n):
secuencia[e][1] = new_get_secuence(e)
secuencia.sort(key=lambda x: (-x[1], -x[0]))
print(secuencia)
ultimos = [(ma + 1, 0, n)]
partir = 0
moment_max = secuencia[0][1]
while moment_max > 0:
terminar = n
usados = []
for e in range(partir, n):
if secuencia[e][1] < moment_max:
terminar = e
break
for element in ultimos:
if secuencia[e][2] < element[2]:
if secuencia[e][0] < element[0]:
usados.append(secuencia[e])
break
else:
break
if len(usados) == 1:
escritura[usados[0][2]] = "3"
else:
for e in usados:
escritura[e[2]] = "2"
ultimos = usados
partir = terminar
moment_max -= 1
print("".join(escritura))
``` | instruction | 0 | 52,595 | 12 | 105,190 |
No | output | 1 | 52,595 | 12 | 105,191 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence a consisting of n (1 β€ n β€ 105) elements a1, a2, ..., an (1 β€ ai β€ 105). A subsequence ai1, ai2, ..., aik where 1 β€ i1 < i2 < ... < ik β€ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 β€ i β€ n), into three groups:
1. group of all i such that ai belongs to no longest increasing subsequences.
2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
3. group of all i such that ai belongs to every longest increasing subsequence.
Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.
Input
The first line contains the single integer n (1 β€ n β€ 105) denoting the number of elements of sequence a.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 105).
Output
Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.
Examples
Input
1
4
Output
3
Input
4
1 3 2 5
Output
3223
Input
4
1 5 2 3
Output
3133
Note
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.
In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}.
Submitted Solution:
```
n = int(input())
secuencia = [[int(x), 0, y] for y, x in enumerate(input().strip().split())]
escritura = ["1"] * len(secuencia)
ma = max((x[0] for x in secuencia))
bit = [0] * (ma + 1)
def max_x(x, l):
suma = 0
while x != 0:
suma = max(suma, l[x])
x -= (x & -x)
return suma
def update_x(x, l, max_n, val):
while x <= max_n:
if val > l[x]:
l[x] = val
else:
return
x += (x & -x)
def index_list(item, alist, first=0, last=-1):
pos = first
while first <= last:
midpoint = (first + last) // 2
pos = midpoint
if alist[midpoint][1] == item:
return midpoint
else:
if item > alist[midpoint][1]:
last = midpoint - 1
else:
first = midpoint + 1
pos += 1
return pos
def new_get_secuence(e):
num = secuencia[e][0]
maximo = max_x(num - 1, bit) + 1
update_x(num, bit, ma, maximo)
return maximo
for e in range(n):
secuencia[e][1] = new_get_secuence(e)
secuencia.sort(key=lambda x: (-x[1], -x[0]))
ultimos = [(ma + 1, 0, n)]
partir = 0
moment_max = secuencia[0][1]
while moment_max > 0:
terminar = n
usados = []
for e in range(partir, n):
if secuencia[e][1] < moment_max:
terminar = e
break
for element in ultimos:
if secuencia[e][2] < element[2]:
if secuencia[e][0] < element[0]:
usados.append(secuencia[e])
break
else:
break
if len(usados) == 1:
escritura[usados[0][2]] = "3"
else:
for e in usados:
escritura[e[2]] = "2"
ultimos = usados
partir = terminar
moment_max -= 1
print("".join(escritura))
``` | instruction | 0 | 52,596 | 12 | 105,192 |
No | output | 1 | 52,596 | 12 | 105,193 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers k, pa and pb.
You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.
You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and <image>. Print the value of <image>.
Input
The first line will contain three integers integer k, pa, pb (1 β€ k β€ 1 000, 1 β€ pa, pb β€ 1 000 000).
Output
Print a single integer, the answer to the problem.
Examples
Input
1 1 1
Output
2
Input
3 1 4
Output
370000006
Note
The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.
The expected amount of times that 'ab' will occur across all valid sequences is 2.
For the second sample, the answer is equal to <image>. | instruction | 0 | 52,789 | 12 | 105,578 |
Tags: dp, math, probabilities
Correct Solution:
```
k, pa, pb = map(int, input().split())
MOD = 10**9 + 7
INF = ((pa + pb) * pow(pb, MOD-2, MOD)) % MOD
rAB = pow(pa+pb, MOD-2, MOD)
rB = pow(pb, MOD-2, MOD)
memo = {}
def dfs(a, ab):
if ab >= k:
return ab
if a + ab >= k:
#return INF
#return (pa + pb) / pb
return ((a + MOD-1) + (pa + pb) * rB + ab) % MOD
return a - 1 + (pa + pb) / pb + ab
if (a, ab) in memo:
return memo[a, ab]
#res = (((dfs(a+1, ab)+1) * pa * rAB) + ((dfs(a, ab+a)+1) * pb * rAB)) % MOD
#res = (dfs(a+1, ab)) * pa / (pa + pb) + (dfs(a, ab+a)) * pb / (pa + pb)
res = (dfs(a+1, ab) * pa * rAB) + (dfs(a, ab+a) * pb * rAB)
#print(a, ab, res)
memo[a, ab] = res = res % MOD
return res
#print((dfs(1, 0) * pa * rAB + 1) % MOD)
#print((pb + dfs(1, 0)*pa) / pa)
print(dfs(1, 0))
``` | output | 1 | 52,789 | 12 | 105,579 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers k, pa and pb.
You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.
You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and <image>. Print the value of <image>.
Input
The first line will contain three integers integer k, pa, pb (1 β€ k β€ 1 000, 1 β€ pa, pb β€ 1 000 000).
Output
Print a single integer, the answer to the problem.
Examples
Input
1 1 1
Output
2
Input
3 1 4
Output
370000006
Note
The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.
The expected amount of times that 'ab' will occur across all valid sequences is 2.
For the second sample, the answer is equal to <image>. | instruction | 0 | 52,791 | 12 | 105,582 |
Tags: dp, math, probabilities
Correct Solution:
```
mod = 1000000007
N = 1005
#input start
k,pa,pb = map(int,input().split())
dp = [[0 for x in range(N)] for y in range(N)]
#end of input
def fast_expo(a,b):
ret = 1
while b:
if b%2==1:
ret = ret*a%mod
b //= 2
a = a*a%mod
return ret
def inv(x):
return fast_expo(x,mod-2)%mod
def dp_val(a,b):
if b>=k:
return b
return dp[a][b]
for i in range(k):
dp[k][i] = (i+k+pa*inv(pb))%mod
den = inv(pa+pb)
for i in range(k-1,0,-1):
for j in range(k-1,-1,-1):
dp[i][j] = pa*dp_val(i+1,j)*den%mod+pb*dp_val(i,i+j)*den%mod
dp[i][j]%=mod
print(dp[1][0])
``` | output | 1 | 52,791 | 12 | 105,583 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers k, pa and pb.
You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.
You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and <image>. Print the value of <image>.
Input
The first line will contain three integers integer k, pa, pb (1 β€ k β€ 1 000, 1 β€ pa, pb β€ 1 000 000).
Output
Print a single integer, the answer to the problem.
Examples
Input
1 1 1
Output
2
Input
3 1 4
Output
370000006
Note
The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.
The expected amount of times that 'ab' will occur across all valid sequences is 2.
For the second sample, the answer is equal to <image>. | instruction | 0 | 52,792 | 12 | 105,584 |
Tags: dp, math, probabilities
Correct Solution:
```
import math
import sys
k, pa, pb = list( map( int, input().split() ) )
memo = {}
sys.setrecursionlimit(1500*1500*2)
MOD = (10**9 + 7 )
def pow( a, b ):
ret = 1
while b > 0:
if b & 1:
ret = (ret*a)%MOD
a = (a*a)%MOD
b //= 2
return ( ret )
def inv(a):
return pow( a, MOD-2 )
Pa = pa * inv( pa + pb )
Pb = pb * inv( pa + pb )
pa_inv_pb = pa * inv(pb)
def f( total_a, total_ab ):
if total_a+total_ab >= k:
return total_a+total_ab + pa_inv_pb
if (total_a,total_ab) in memo:
return memo[ (total_a,total_ab) ]
Best = 0
Best += Pa * f( total_a+1, total_ab )
Best %= MOD
Best += Pb * f( total_a, total_a+total_ab )
Best %= MOD
memo[ (total_a,total_ab) ] = Best
return ( Best )
#print( k, pa, pb )
print( ( f(1,0) ) % MOD )
``` | output | 1 | 52,792 | 12 | 105,585 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers k, pa and pb.
You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.
You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and <image>. Print the value of <image>.
Input
The first line will contain three integers integer k, pa, pb (1 β€ k β€ 1 000, 1 β€ pa, pb β€ 1 000 000).
Output
Print a single integer, the answer to the problem.
Examples
Input
1 1 1
Output
2
Input
3 1 4
Output
370000006
Note
The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.
The expected amount of times that 'ab' will occur across all valid sequences is 2.
For the second sample, the answer is equal to <image>. | instruction | 0 | 52,793 | 12 | 105,586 |
Tags: dp, math, probabilities
Correct Solution:
```
mod = 1000000007
N = 1005
#input start
k,pa,pb = map(int,input().split())
dp = [[0 for x in range(k)] for y in range(k+1)]
#end of input
def fast_expo(a,b):
ret = 1
while b:
if b%2==1:
ret = ret*a%mod
b //= 2
a = a*a%mod
return ret
def inv(x):
return fast_expo(x,mod-2)%mod
def dp_val(a,b):
if b>=k:
return b
return dp[a][b]
for i in range(k):
dp[k][i] = (i+k+pa*inv(pb))%mod
den = inv(pa+pb)
for i in range(k-1,0,-1):
for j in range(k-1,-1,-1):
dp[i][j] = pa*dp_val(i+1,j)*den%mod+pb*dp_val(i,i+j)*den%mod
dp[i][j]%=mod
print(dp[1][0])
``` | output | 1 | 52,793 | 12 | 105,587 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers k, pa and pb.
You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.
You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and <image>. Print the value of <image>.
Input
The first line will contain three integers integer k, pa, pb (1 β€ k β€ 1 000, 1 β€ pa, pb β€ 1 000 000).
Output
Print a single integer, the answer to the problem.
Examples
Input
1 1 1
Output
2
Input
3 1 4
Output
370000006
Note
The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.
The expected amount of times that 'ab' will occur across all valid sequences is 2.
For the second sample, the answer is equal to <image>. | instruction | 0 | 52,794 | 12 | 105,588 |
Tags: dp, math, probabilities
Correct Solution:
```
k, a, b = map(int, input().split())
m = 1000000007
d = a * pow(b, m - 2, m) % m
c = pow(a + b, m - 2, m)
u, v = [0] * k, [0] * k
for s in range(k, 0, -1):
v[s - 1] = s + d
for i in range(s, k):
j = max(i - s, s - 1)
v[i] = c * (a * u[i] + b * (s + v[j])) % m
u, v = v, u
print(u[k - 1])
``` | output | 1 | 52,794 | 12 | 105,589 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers k, pa and pb.
You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.
You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and <image>. Print the value of <image>.
Input
The first line will contain three integers integer k, pa, pb (1 β€ k β€ 1 000, 1 β€ pa, pb β€ 1 000 000).
Output
Print a single integer, the answer to the problem.
Examples
Input
1 1 1
Output
2
Input
3 1 4
Output
370000006
Note
The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.
The expected amount of times that 'ab' will occur across all valid sequences is 2.
For the second sample, the answer is equal to <image>. | instruction | 0 | 52,795 | 12 | 105,590 |
Tags: dp, math, probabilities
Correct Solution:
```
import sys
input=sys.stdin.readline
dp={}
mod=int(1000000007)
sys.setrecursionlimit(100000)
def bigmod(n,p):
p=int(p)
if p==0:
return 1
x=bigmod(n,p/2)
x=(x*x)%mod
if p%2==1:
x=(x*n)%mod
return x
k,pa,pb=map(int,input().split())
r=bigmod(pa+pb,mod-2)
r1=bigmod(pb,mod-2)
r1=(pa*r1)%mod
p=(pa*r)%mod
q=(pb*r)%mod
def cal(k1,a1):
if k1+a1>=k:
return (k1+a1+r1)%mod
if (k1,a1) in dp:
return dp[(k1,a1)]
dp[(k1,a1)]=((cal(k1+a1,a1)*q)%mod+(cal(k1,a1+1)*p)%mod)%mod
return dp[(k1,a1)]
print(cal(0,1))
``` | output | 1 | 52,795 | 12 | 105,591 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers k, pa and pb.
You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.
You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and <image>. Print the value of <image>.
Input
The first line will contain three integers integer k, pa, pb (1 β€ k β€ 1 000, 1 β€ pa, pb β€ 1 000 000).
Output
Print a single integer, the answer to the problem.
Examples
Input
1 1 1
Output
2
Input
3 1 4
Output
370000006
Note
The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.
The expected amount of times that 'ab' will occur across all valid sequences is 2.
For the second sample, the answer is equal to <image>. | instruction | 0 | 52,796 | 12 | 105,592 |
Tags: dp, math, probabilities
Correct Solution:
```
import sys
#input=sys.stdin.readline
dp={}
mod=int(1000000007)
sys.setrecursionlimit(100000)
def bigmod(n,p):
p=int(p)
if p==0:
return 1
x=bigmod(n,p/2)
x=(x*x)%mod
if p%2==1:
x=(x*n)%mod
return x
k,pa,pb=map(int,input().split())
r=bigmod(pa+pb,mod-2)
r1=bigmod(pb,mod-2)
r1=(pa*r1)%mod
p=(pa*r)%mod
q=(pb*r)%mod
def cal(k1,a1):
if k1+a1>=k:
return (k1+a1+r1)%mod
if (k1,a1) in dp:
return dp[(k1,a1)]
dp[(k1,a1)]=((cal(k1+a1,a1)*q)%mod+(cal(k1,a1+1)*p)%mod)%mod
return dp[(k1,a1)]
print(cal(0,1))
``` | output | 1 | 52,796 | 12 | 105,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers k, pa and pb.
You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.
You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and <image>. Print the value of <image>.
Input
The first line will contain three integers integer k, pa, pb (1 β€ k β€ 1 000, 1 β€ pa, pb β€ 1 000 000).
Output
Print a single integer, the answer to the problem.
Examples
Input
1 1 1
Output
2
Input
3 1 4
Output
370000006
Note
The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.
The expected amount of times that 'ab' will occur across all valid sequences is 2.
For the second sample, the answer is equal to <image>.
Submitted Solution:
```
# Question: D - 1
# Assignment 11
# Daniel Perez, bd2255
# modulus to divide into
m = 10**9 + 7
def rainy(K, PA, PB):
# calculating the values of Q and P provided for by the problem
P = PA * pow(PB, m - 2, m) % m
Q = pow(PA + PB, m - 2, m)
# calculate the sequences to initialize the array
FINAL, CURR = [0] * K, [0] * K
# sequence for getting the values of the sequence generation
for s in range(K, 0, -1):
CURR[s - 1] = s + P
for i in range(s, K):
# getting the max value from current sequence for the value to
# use in the formula
j = max(i - s, s - 1)
# this calculates the value of Q inverse? not sure yet
CURR[i] = Q * (PA * FINAL[i] + PB * (s + CURR[j])) % m
# swap the value of final and current sequences to continue the calculation
FINAL, CURR = CURR, FINAL
# return the last value of the final sequence
return FINAL[K - 1]
k, a, b = map(int, input().split())
print(rainy(k, a, b))
``` | instruction | 0 | 52,797 | 12 | 105,594 |
Yes | output | 1 | 52,797 | 12 | 105,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers k, pa and pb.
You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.
You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and <image>. Print the value of <image>.
Input
The first line will contain three integers integer k, pa, pb (1 β€ k β€ 1 000, 1 β€ pa, pb β€ 1 000 000).
Output
Print a single integer, the answer to the problem.
Examples
Input
1 1 1
Output
2
Input
3 1 4
Output
370000006
Note
The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.
The expected amount of times that 'ab' will occur across all valid sequences is 2.
For the second sample, the answer is equal to <image>.
Submitted Solution:
```
import sys
input=sys.stdin.readline
dp={}
mod=int(1000000007)
sys.setrecursionlimit(100000)
def bigmod(n,p):
n,p=int(n),int(p)
if p==0:
return 1
x=bigmod(n,p/2)
x=(x*x)%mod
if p%2==1:
x=(x*n)%mod
return x
k,pa,pb=map(int,input().split())
r=bigmod(pa+pb,mod-2)
r1=bigmod(pb,mod-2)
r1=(pa*r1)%mod
p=(pa*r)%mod
q=(pb*r)%mod
def cal(k1,a1):
if k1+a1>=k:
return (k1+a1+r1)%mod
if (k1,a1) in dp:
return dp[(k1,a1)]
dp[(k1,a1)]=((cal(k1+a1,a1)*q)%mod+(cal(k1,a1+1)*p)%mod)%mod
return dp[(k1,a1)]
print(cal(0,1))
``` | instruction | 0 | 52,798 | 12 | 105,596 |
Yes | output | 1 | 52,798 | 12 | 105,597 |
Provide a correct Python 3 solution for this coding contest problem.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3 | instruction | 0 | 52,876 | 12 | 105,752 |
"Correct Solution:
```
N,K=map(int,input().split())
p=[int(i)-1 for i in input().split()]
q=[int(i)-1 for i in input().split()]
def inv(seq):
res=[0 for i in range(N)]
for i in range(N):
res[seq[i]]=i
return res
def times(seq1,seq2):
res=[0 for i in range(N)]
for i in range(N):
res[i]=seq1[seq2[i]]
return res
md=[[0 for i in range(N)] for i in range(6)]
for i in range(N):
md[0][i]=p[i]
md[1][i]=q[i]
for i in range(2,6):
md[i]=times(md[i-1],inv(md[i-2]))
A=times(times(times(q,inv(p)),inv(q)),p)
T=(K-1)//6
def expseq(seq,k):
if k==0:
return [i for i in range(N)]
elif k==1:
return seq
elif k%2==0:
return expseq(times(seq,seq),k//2)
else:
return times(expseq(times(seq,seq),k//2),seq)
tmp=expseq(A,T)
ans=times(times(tmp,md[(K-1)%6]),inv(tmp))
print(" ".join([str(i+1) for i in ans]))
``` | output | 1 | 52,876 | 12 | 105,753 |
Provide a correct Python 3 solution for this coding contest problem.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3 | instruction | 0 | 52,877 | 12 | 105,754 |
"Correct Solution:
```
N,K=map(int,input().split())
p=[int(i)-1 for i in input().split()]
q=[int(i)-1 for i in input().split()]
e=[i for i in range(N)]
def cir(x,y):
z=[0]*N
for i in range(N):
z[i]=x[y[i]]
return z
def inv(x):
z=[0]*N
for i in range(N):
z[x[i]]=i
return z
def f(x,y):
return cir(y,inv(x))
def double(x,k):
a=k
t=x
num=e
while a!=0:
if a%2==1:
num=cir(num,t)
a=a//2
t=cir(t,t)
return num
P=inv(p)
Q=inv(q)
#print(p,q,P,Q)
quo=(K-1)//6
r=K-quo*6-1
X=cir(q,cir(P,cir(Q,p)))
#Y=cir(P,cir(q,cir(p,Q)))
#Z=cir(q,double(Y,quo))
Z=double(X,quo)
A=cir(Z,cir(p,inv(Z)))
B=cir(Z,cir(q,inv(Z)))
#print(A,B,quo,r)
def g(n):
if n==0:
return A
if n==1:
return B
return f(g(n-2),g(n-1))
L=g(r)
for i in range(N):
L[i]+=1
print(' '.join(map(str,L)))
``` | output | 1 | 52,877 | 12 | 105,755 |
Provide a correct Python 3 solution for this coding contest problem.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3 | instruction | 0 | 52,878 | 12 | 105,756 |
"Correct Solution:
```
from operator import itemgetter
def get_identity():
return list(range(1, n + 1))
def composition(ppp, qqq):
return [ppp[q - 1] for q in qqq]
def reverse_composition(ppp, qqq):
return [ppp[i] for i, q in sorted(enumerate(qqq), key=itemgetter(1))]
def solve(k, ppp, qqq):
qp = reverse_composition(qqq, ppp)
qpq = reverse_composition(qp, qqq)
qpqp = composition(qpq, ppp)
l, m = divmod(k - 1, 6)
res = get_identity()
tmp = qpqp
while l:
if l % 2 == 1:
res = composition(res, tmp)
tmp = composition(tmp, tmp)
l >>= 1
m = (k - 1) % 6
if m == 0:
base = ppp
elif m == 1:
base = qqq
elif m == 2:
base = reverse_composition(qqq, ppp)
elif m == 3:
res = composition(res, qqq)
base = reverse_composition(get_identity(), ppp)
elif m == 4:
res = composition(res, qqq)
res = reverse_composition(res, ppp)
base = reverse_composition(get_identity(), qqq)
elif m == 5:
res = composition(res, qqq)
res = reverse_composition(res, ppp)
base = reverse_composition(get_identity(), qqq)
base = composition(base, ppp)
else:
raise NotImplementedError
ans = composition(res, base)
ans = reverse_composition(ans, res)
return ans
n, k = map(int, input().split())
ppp = list(map(int, input().split()))
qqq = list(map(int, input().split()))
print(*solve(k, ppp, qqq))
``` | output | 1 | 52,878 | 12 | 105,757 |
Provide a correct Python 3 solution for this coding contest problem.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3 | instruction | 0 | 52,879 | 12 | 105,758 |
"Correct Solution:
```
N,K=map(int,input().split())
p=[int(i)-1for i in input().split()]
q=[int(i)-1for i in input().split()]
T=lambda s,t:[s[t[i]]for i in range(N)]
m=[[0for i in[0]*N]for i in[0]*6];m[0]=p;m[1]=q
def I(s):
r=[0]*N
for i in range(N):r[s[i]]=i
return r
for i in range(4):m[i+2]=T(m[i+1],I(m[i]))
E=lambda s,k:T(E(T(s,s),k//2),s)if k%2 else (E(T(s,s),k//2)if k else list(range(N)))
t=E(T(T(T(q,I(p)),I(q)),p),~-K//6);print(" ".join([str(i+1)for i in T(T(t,m[~-K%6]),I(t))]))
``` | output | 1 | 52,879 | 12 | 105,759 |
Provide a correct Python 3 solution for this coding contest problem.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3 | instruction | 0 | 52,880 | 12 | 105,760 |
"Correct Solution:
```
N,K=map(int,input().split());p=[int(i)-1for i in input().split()];q=[int(i)-1for i in input().split()];T=lambda s,t:[s[t[i]]for i in range(N)];m=[[0for i in[0]*N]for i in[0]*6];m[0]=p;m[1]=q
def I(s):
r=[0]*N
for i in range(N):r[s[i]]=i
return r
for i in range(4):m[i+2]=T(m[i+1],I(m[i]))
E=lambda s,k:T(E(T(s,s),k//2),s)if k%2 else (E(T(s,s),k//2)if k else list(range(N)));t=E(T(T(T(q,I(p)),I(q)),p),~-K//6);print(" ".join([str(i+1)for i in T(T(t,m[~-K%6]),I(t))]))
``` | output | 1 | 52,880 | 12 | 105,761 |
Provide a correct Python 3 solution for this coding contest problem.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3 | instruction | 0 | 52,881 | 12 | 105,762 |
"Correct Solution:
```
N,K=map(int,input().split())
p=[int(i)-1 for i in input().split()]
q=[int(i)-1 for i in input().split()]
def inv(seq):
res=[0 for i in range(N)]
for i in range(N):
res[seq[i]]=i
return res
def times(seq1,seq2):
res=[0 for i in range(N)]
for i in range(N):
res[i]=seq1[seq2[i]]
return res
md=[[0 for i in range(N)] for i in range(6)]
for i in range(N):
md[0][i]=p[i]
md[1][i]=q[i]
for i in range(2,6):
md[i]=times(md[i-1],inv(md[i-2]))
A=times(times(q,inv(p)),times(inv(q),p))
T=(K-1)//6
def expseq(seq,k):
if k==0:
return [i for i in range(N)]
elif k==1:
return seq
elif k%2==0:
return expseq(times(seq,seq),k//2)
else:
return times(expseq(times(seq,seq),k//2),seq)
tmp=expseq(A,T)
ans=times(times(tmp,md[(K-1)%6]),inv(tmp))
print(" ".join([str(i+1) for i in ans]))
``` | output | 1 | 52,881 | 12 | 105,763 |
Provide a correct Python 3 solution for this coding contest problem.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3 | instruction | 0 | 52,882 | 12 | 105,764 |
"Correct Solution:
```
N,K=map(int,input().split());p=[int(i)-1for i in input().split()];q=[int(i)-1for i in input().split()];T=lambda s,t:[s[t[i]]for i in range(N)];m=[[0for i in[0]*N]for i in[0]*6];m[0]=p;m[1]=q
def I(s):
r=[0]*N
for i in range(N):r[s[i]]=i
return r
for i in range(4):m[i+2]=T(m[i+1],I(m[i]))
E=lambda s,k:T(E(T(s,s),k//2),s)if k%2 else (E(T(s,s),k//2)if k else list(range(N)));t=E(T(T(T(q,I(p)),I(q)),p),~-K//6);print(*[str(i+1)for i in T(T(t,m[~-K%6]),I(t))])
``` | output | 1 | 52,882 | 12 | 105,765 |
Provide a correct Python 3 solution for this coding contest problem.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3 | instruction | 0 | 52,883 | 12 | 105,766 |
"Correct Solution:
```
def perpro(q,p):
res=[-1]*len(p)
for i in range(len(p)):
res[i]=q[p[i]]
return res
def inverse(p):
res=[-1]*len(p)
for i in range(len(p)):
res[p[i]]=i
return res
N,K=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
ide_ele=[i for i in range(N)]
for i in range(N):
p[i]-=1
q[i]-=1
invp=[-1]*N
for i in range(N):
invp[p[i]]=i
invq=[-1]*N
for i in range(N):
invq[q[i]]=i
A=perpro(q,perpro(invp,perpro(invq,p)))
kthA=[A]
for i in range(30):
kthA.append(perpro(kthA[-1],kthA[-1]))
def _nthA(n):
res=ide_ele
for i in range(30):
if n>>i&1==1:
res=perpro(res,kthA[i])
return res
def ntha(n):
qqq=n//4
r=n%4
if r==0:
return _nthA(qqq)
elif r==1:
return perpro(_nthA(qqq),q)
elif r==2:
return perpro(_nthA(qqq),perpro(q,invp))
else:
return perpro(_nthA(qqq),perpro(q,perpro(invp,invq)))
Q=(K-1)//3
r=(K-1)%3
ans=[]
if Q==0:
if K==1:
ans=p
elif K==2:
ans=q
else:
ans=perpro(q,invp)
else:
if Q%2==0:
if r==0:
mid=p
a=ntha(2*Q-1)
ans=perpro(a,perpro(mid,inverse(a)))
elif r==1:
mid=q
a=ntha(2*Q)
ans=perpro(a,perpro(mid,inverse(a)))
else:
mid=perpro(q,invp)
a=ntha(2*Q)
ans=perpro(a,perpro(mid,inverse(a)))
else:
if r==0:
mid=invp
a=ntha(2*Q-1)
ans=perpro(a,perpro(mid,inverse(a)))
elif r==1:
mid=invq
a=ntha(2*Q)
ans=perpro(a,perpro(mid,inverse(a)))
else:
mid=perpro(invq,p)
a=ntha(2*Q)
ans=perpro(a,perpro(mid,inverse(a)))
for i in range(N):
ans[i]+=1
print(*ans)
``` | output | 1 | 52,883 | 12 | 105,767 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3
Submitted Solution:
```
N,K=map(int,input().split());p=[int(i)-1 for i in input().split()];q=[int(i)-1 for i in input().split()];T=lambda s,t:[s[t[i]] for i in range(N)];m=[[0 for i in [0]*N] for i in [0]*6];m[0]=p;m[1]=q
def I(s):
r=[0]*N
for i in range(N):r[s[i]]=i
return r
for i in range(4):m[i+2]=T(m[i+1],I(m[i]))
E=lambda s,k:(E(T(s,s),k//2) if k!=0 else list(range(N)))if k%2==0 else T(E(T(s,s),k//2),s);t=E(T(T(T(q,I(p)),I(q)),p),~-K//6);print(" ".join([str(i+1) for i in T(T(t,m[~-K%6]),I(t))]))
``` | instruction | 0 | 52,884 | 12 | 105,768 |
Yes | output | 1 | 52,884 | 12 | 105,769 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3
Submitted Solution:
```
n,k = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
def perprod(x,y): #xy
nn = len(x)
ret = [0 for i in range(nn)]
for i in range(nn):
ret[i] = x[y[i]-1]
return ret
def perinv(x): #inverse
nn = len(x)
ret = [0 for i in range(nn)]
for i in range(nn):
ret[x[i]-1] =i+1
return ret
amari = (k-1) % 6
e = (k-1) //6
a =[[] for i in range(6)]
a[0] = p
a[1] = q
a[2] = perprod(a[1],perinv(a[0]))
a[3] = perprod(a[2],perinv(a[1]))
a[4] = perprod(a[3],perinv(a[2]))
a[5] = perprod(a[4],perinv(a[3]))
pp = perprod(perprod(a[1],perinv(a[0])),perprod(perinv(a[1]),a[0]))
ans1 = [i+1 for i in range(n)]
while e>0:
if(e & 1): ans1 = perprod(ans1,pp)
pp = perprod(pp,pp)
e >>= 1
ans2 = perinv(ans1)
ans = perprod(ans1,a[amari])
ans = perprod(ans,ans2)
print(*ans)
``` | instruction | 0 | 52,885 | 12 | 105,770 |
Yes | output | 1 | 52,885 | 12 | 105,771 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3
Submitted Solution:
```
N,K=map(int,input().split());p=[int(i)-1 for i in input().split()];q=[int(i)-1 for i in input().split()]
def I(s):
r=[0]*N
for i in range(N):r[s[i]]=i
return r
def T(s,t):return [s[t[i]] for i in range(N)]
m=[[0 for i in [0]*N] for i in [0]*6]
for i in range(N):m[0][i]=p[i];m[1][i]=q[i]
for i in range(4):m[i+2]=T(m[i+1],I(m[i]))
def E(s,k):
if k%2==0:
return E(T(s,s),k//2) if k!=0 else list(range(N))
else:
return T(E(T(s,s),k//2),s)
t=E(T(T(T(q,I(p)),I(q)),p),~-K//6);print(" ".join([str(i+1) for i in T(T(t,m[~-K%6]),I(t))]))
``` | instruction | 0 | 52,886 | 12 | 105,772 |
Yes | output | 1 | 52,886 | 12 | 105,773 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3
Submitted Solution:
```
N,K=map(int,input().split());p=[int(i)-1 for i in input().split()];q=[int(i)-1 for i in input().split()]
def I(s):
r=[0 for i in range(N)]
for i in range(N):r[s[i]]=i
return r
def T(s,t):
r=[0 for i in range(N)]
for i in range(N):r[i]=s[t[i]]
return r
m=[[0 for i in range(N)] for i in range(6)];A=T(T(T(q,I(p)),I(q)),p);X=(K-1)//6
for i in range(N):m[0][i]=p[i];m[1][i]=q[i]
for i in range(2,6):m[i]=T(m[i-1],I(m[i-2]))
def E(s,k):
if k==0:
return [i for i in range(N)]
elif k==1:
return s
elif k%2==0:
return E(T(s,s),k//2)
else:
return T(E(T(s,s),k//2),s)
t=E(A,X);a=T(T(t,m[(K-1)%6]),I(t));print(" ".join([str(i+1) for i in a]))
``` | instruction | 0 | 52,887 | 12 | 105,774 |
Yes | output | 1 | 52,887 | 12 | 105,775 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3
Submitted Solution:
```
#D
n, k = map(int, input().split())
before = input().split()
after = input().split()
if k == 1:
result = before
elif k == 2:
result = after
else:
for x in range(k - 2):
sort_dict = {k: v for k, v in zip(before, after)}
sorted_items = sorted(sort_dict.items())
next_seq = [v[1] for v in sorted_items]
before = after
after = next_seq
result = next_seq
print(' '.join(result))
``` | instruction | 0 | 52,888 | 12 | 105,776 |
No | output | 1 | 52,888 | 12 | 105,777 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3
Submitted Solution:
```
N, K = list(map(int,input().split()))
p = input().split()
q = input().split()
a=[p,q]
memo = {}
def perm(p,q):
pq = ','.join(p) + ',' + ','.join(q)
if pq in memo:
return memo[pq]
else:
ans = [0] * len(p)
for i in range(len(p)):
ans[int(p[i])-1] = q[i]
memo[pq] = ans
return ans
if K>=3:
for k in range(2,K):
a.append(perm(a[k-2],a[k-1]))
print(a[-1])
``` | instruction | 0 | 52,889 | 12 | 105,778 |
No | output | 1 | 52,889 | 12 | 105,779 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3
Submitted Solution:
```
n,k = map(int,input().split())
p,q = list(map(int,input().split()))
def prog(k):
if(k==1):
return p
elif(k==2):
return q
else:
r=[0]*n
for i in range(1,n+1)
r[prog(k-2)[i]]=prog(k-1)[i]
return r
''.join(prog(k))
``` | instruction | 0 | 52,890 | 12 | 105,780 |
No | output | 1 | 52,890 | 12 | 105,781 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:
* The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q.
You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:
* a_1=p, a_2=q
* a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )
Given a positive integer K, find a_K.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
* p and q are permutations of the integers from 1 through N.
Input
Input is given from Standard Input in the following format:
N K
p_1 ... p_N
q_1 ... q_N
Output
Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.
Examples
Input
3 3
1 2 3
3 2 1
Output
3 2 1
Input
5 5
4 5 1 2 3
3 2 1 5 4
Output
4 3 2 1 5
Input
10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5
Output
7 9 4 8 2 5 1 6 10 3
Submitted Solution:
```
n, k = map(int, input().split())
ppp = list(map(int, input().split()))
qqq = list(map(int, input().split()))
ini_p = ppp.copy()
ini_q = qqq.copy()
i = 0
for i in range(k - 2):
rrr = [q for p, q in sorted(zip(ppp, qqq))]
if rrr == ini_p:
break
ppp = qqq
qqq = rrr
else:
print(*qqq)
exit()
k = k % (i + 2)
ppp = ini_p
qqq = ini_q
for i in range(k - 2):
rrr = [q for p, q in sorted(zip(ppp, qqq))]
ppp = qqq
qqq = rrr
print(*qqq)
``` | instruction | 0 | 52,891 | 12 | 105,782 |
No | output | 1 | 52,891 | 12 | 105,783 |
Provide a correct Python 3 solution for this coding contest problem.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\\{1, 7, 7, 4, 8, 1, 6\\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\\{1, 7, 7\\}$, $\\{7, 7, 4\\}$, $\\{7, 4, 8\\}$, $\\{4, 8, 1\\}$, $\\{8, 1, 6\\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively.
Constraints
* $1 \leq N \leq 10^6$
* $1 \leq L \leq 10^6$
* $1 \leq a_i \leq 10^9$
* $L \leq N$
Input
The input is given in the following format.
$N$ $L$
$a_1$ $a_2$ ... $a_N$
Output
Print a sequence of the minimum in a line. Print a space character between adjacent elements.
Example
Input
7 3
1 7 7 4 8 1 6
Output
1 4 4 1 1 | instruction | 0 | 53,047 | 12 | 106,094 |
"Correct Solution:
```
from collections import deque
N, L = map(int, input().split())
box = list(map(int, input().split()))
que = deque()
ans = []
for i, a in enumerate(box):
while que and a <= que[-1][1]:
que.pop()
que.append((i,a))
ans.append(que[0][1])
if que and que[0][0] <= i-L+1:
que.popleft()
print(*ans[L-1:])
``` | output | 1 | 53,047 | 12 | 106,095 |
Provide a correct Python 3 solution for this coding contest problem.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\\{1, 7, 7, 4, 8, 1, 6\\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\\{1, 7, 7\\}$, $\\{7, 7, 4\\}$, $\\{7, 4, 8\\}$, $\\{4, 8, 1\\}$, $\\{8, 1, 6\\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively.
Constraints
* $1 \leq N \leq 10^6$
* $1 \leq L \leq 10^6$
* $1 \leq a_i \leq 10^9$
* $L \leq N$
Input
The input is given in the following format.
$N$ $L$
$a_1$ $a_2$ ... $a_N$
Output
Print a sequence of the minimum in a line. Print a space character between adjacent elements.
Example
Input
7 3
1 7 7 4 8 1 6
Output
1 4 4 1 1 | instruction | 0 | 53,049 | 12 | 106,098 |
"Correct Solution:
```
import collections, numbers
import random
def compute(array, window, maximize):
if not isinstance(window, numbers.Integral):
raise TypeError()
if not isinstance(maximize, bool):
raise TypeError()
if window <= 0:
raise ValueError("Window size must be positive")
result = []
deque = collections.deque()
for i, val in enumerate(array):
val = array[i]
while len(deque) > 0 and ((not maximize and val < deque[-1]) or (maximize and val > deque[-1])):
deque.pop()
deque.append(val)
j = i + 1 - window
if j >= 0:
result.append(deque[0])
if array[j] == deque[0]:
deque.popleft()
return result
class SlidingWindowMinMax(object):
def __init__(self):
self.mindeque = collections.deque()
self.maxdeque = collections.deque()
def get_minimum(self):
return self.mindeque[0]
def get_maximum(self):
return self.maxdeque[0]
def add_tail(self, val):
while len(self.mindeque) > 0 and val < self.mindeque[-1]:
self.mindeque.pop()
self.mindeque.append(val)
while len(self.maxdeque) > 0 and val > self.maxdeque[-1]:
self.maxdeque.pop()
self.maxdeque.append(val)
def remove_head(self, val):
if val < self.mindeque[0]:
raise ValueError("Wrong value")
elif val == self.mindeque[0]:
self.mindeque.popleft()
if val > self.maxdeque[0]:
raise ValueError("Wrong value")
elif val == self.maxdeque[0]:
self.maxdeque.popleft()
if __name__ == "__main__":
n, l = (int(x) for x in input().split())
array = list((int(x) for x in input().split()))
answer = compute(array, l, False)
print(*answer)
``` | output | 1 | 53,049 | 12 | 106,099 |
Provide a correct Python 3 solution for this coding contest problem.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\\{1, 7, 7, 4, 8, 1, 6\\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\\{1, 7, 7\\}$, $\\{7, 7, 4\\}$, $\\{7, 4, 8\\}$, $\\{4, 8, 1\\}$, $\\{8, 1, 6\\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively.
Constraints
* $1 \leq N \leq 10^6$
* $1 \leq L \leq 10^6$
* $1 \leq a_i \leq 10^9$
* $L \leq N$
Input
The input is given in the following format.
$N$ $L$
$a_1$ $a_2$ ... $a_N$
Output
Print a sequence of the minimum in a line. Print a space character between adjacent elements.
Example
Input
7 3
1 7 7 4 8 1 6
Output
1 4 4 1 1 | instruction | 0 | 53,050 | 12 | 106,100 |
"Correct Solution:
```
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
res = list(sys.stdin.readline())
if res[-1] == "\n":
return res[:-1]
return res
def IR(n):
return [I() for i in range(n)]
def LIR(n):
return [LI() for i in range(n)]
def SR(n):
return [S() for i in range(n)]
def LSR(n):
return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007
class SlidingWindowAggretgation:
def __init__(self, default = float("inf"), f = min):
self.default = default
self.f = f
self.front_stack = deque()
self.back_stack = deque()
def get(self):
res = self.default
if self.front_stack:
res = self.f(res, self.front_stack[-1][1])
if self.back_stack:
res = self.f(res, self.back_stack[-1][1])
return res
def append(self, x):
fx = x
if self.back_stack:
fx = self.f(self.back_stack[-1][1], x)
self.back_stack.append((x, fx))
def popleft(self):
if not self.front_stack:
x, fx = self.back_stack.pop()
self.front_stack.append((x, x))
while self.back_stack:
x, fx = self.back_stack.pop()
fx = self.f(x, self.front_stack[-1][1])
self.front_stack.append((x, fx))
self.front_stack.pop()
def solve():
n,l = LI()
a = LI()
swag = SlidingWindowAggretgation()
ans = []
for i in range(n):
swag.append(a[i])
if i+1 >= l:
ans.append(swag.get())
swag.popleft()
print(*ans)
return
#Solve
if __name__ == "__main__":
solve()
``` | output | 1 | 53,050 | 12 | 106,101 |
Provide a correct Python 3 solution for this coding contest problem.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\\{1, 7, 7, 4, 8, 1, 6\\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\\{1, 7, 7\\}$, $\\{7, 7, 4\\}$, $\\{7, 4, 8\\}$, $\\{4, 8, 1\\}$, $\\{8, 1, 6\\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively.
Constraints
* $1 \leq N \leq 10^6$
* $1 \leq L \leq 10^6$
* $1 \leq a_i \leq 10^9$
* $L \leq N$
Input
The input is given in the following format.
$N$ $L$
$a_1$ $a_2$ ... $a_N$
Output
Print a sequence of the minimum in a line. Print a space character between adjacent elements.
Example
Input
7 3
1 7 7 4 8 1 6
Output
1 4 4 1 1 | instruction | 0 | 53,051 | 12 | 106,102 |
"Correct Solution:
```
from collections import deque
N, L = map(int, input().split())
*A, = map(int, input().split())
ans = []
que = deque()
for i, a in enumerate(A):
while que and a <= que[-1][1]:
que.pop()
que.append((i, a))
ans.append(que[0][1])
if que and que[0][0] <= i+1-L:
que.popleft()
print(*ans[L-1:])
``` | output | 1 | 53,051 | 12 | 106,103 |
Provide a correct Python 3 solution for this coding contest problem.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\\{1, 7, 7, 4, 8, 1, 6\\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\\{1, 7, 7\\}$, $\\{7, 7, 4\\}$, $\\{7, 4, 8\\}$, $\\{4, 8, 1\\}$, $\\{8, 1, 6\\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively.
Constraints
* $1 \leq N \leq 10^6$
* $1 \leq L \leq 10^6$
* $1 \leq a_i \leq 10^9$
* $L \leq N$
Input
The input is given in the following format.
$N$ $L$
$a_1$ $a_2$ ... $a_N$
Output
Print a sequence of the minimum in a line. Print a space character between adjacent elements.
Example
Input
7 3
1 7 7 4 8 1 6
Output
1 4 4 1 1 | instruction | 0 | 53,052 | 12 | 106,104 |
"Correct Solution:
```
import collections, numbers
import random
def compute(array, window, maximize):
if not isinstance(window, numbers.Integral):
raise TypeError()
if not isinstance(maximize, bool):
raise TypeError()
if window <= 0:
raise ValueError("Window size must be positive")
result = []
deque = collections.deque()
for i, val in enumerate(array):
val = array[i]
while len(deque) > 0 and ((not maximize and val < deque[-1]) or (maximize and val > deque[-1])):
deque.pop()
deque.append(val)
j = i + 1 - window
if j >= 0:
result.append(deque[0])
if array[j] == deque[0]:
deque.popleft()
return result
if __name__ == "__main__":
n, l = (int(x) for x in input().split())
array = list((int(x) for x in input().split()))
answer = compute(array, l, False)
print(*answer)
``` | output | 1 | 53,052 | 12 | 106,105 |
Provide a correct Python 3 solution for this coding contest problem.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\\{1, 7, 7, 4, 8, 1, 6\\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\\{1, 7, 7\\}$, $\\{7, 7, 4\\}$, $\\{7, 4, 8\\}$, $\\{4, 8, 1\\}$, $\\{8, 1, 6\\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively.
Constraints
* $1 \leq N \leq 10^6$
* $1 \leq L \leq 10^6$
* $1 \leq a_i \leq 10^9$
* $L \leq N$
Input
The input is given in the following format.
$N$ $L$
$a_1$ $a_2$ ... $a_N$
Output
Print a sequence of the minimum in a line. Print a space character between adjacent elements.
Example
Input
7 3
1 7 7 4 8 1 6
Output
1 4 4 1 1 | instruction | 0 | 53,054 | 12 | 106,108 |
"Correct Solution:
```
from collections import deque
N, L = map(int, input().split())
*list, = map(int, input().split())
ans = []
que = deque()
for i, num in enumerate(list):
while que and num <= que[-1][1]:
que.pop()
que.append((i, num))
ans.append(que[0][1])
if que and que[0][0] <= i+1-L:
que.popleft()
print(*ans[L-1:])
``` | output | 1 | 53,054 | 12 | 106,109 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\\{1, 7, 7, 4, 8, 1, 6\\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\\{1, 7, 7\\}$, $\\{7, 7, 4\\}$, $\\{7, 4, 8\\}$, $\\{4, 8, 1\\}$, $\\{8, 1, 6\\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively.
Constraints
* $1 \leq N \leq 10^6$
* $1 \leq L \leq 10^6$
* $1 \leq a_i \leq 10^9$
* $L \leq N$
Input
The input is given in the following format.
$N$ $L$
$a_1$ $a_2$ ... $a_N$
Output
Print a sequence of the minimum in a line. Print a space character between adjacent elements.
Example
Input
7 3
1 7 7 4 8 1 6
Output
1 4 4 1 1
Submitted Solution:
```
from itertools import accumulate
from collections import deque
class SWAG:
def __init__(self, operator_M, e_M):
self.op_M = operator_M
self.e_M = e_M
self.q = deque([])
self.accL = []
self.accR = e_M
self.L = self.R = 0
def build(self,lst):
self.q = deque(lst)
self.L = len(lst)
self.accL = list(accumulate(reversed(self.q),self.op_M))
def __len__(self):
return self.L + self.R
def fold_all(self):
if self.L: return self.op_M(self.accL[-1],self.accR)
else: return self.accR
def append(self,x):
self.q.append(x)
self.accR = self.op_M(self.accR,x)
self.R += 1
def popleft(self):
if self.L:
self.accL.pop()
self.L -= 1
return self.q.popleft()
elif self.R:
self.L,self.R = self.R-1,0
self.accL = list(accumulate(reversed(self.q),self.op_M))
self.accR = self.e_M
self.accL.pop()
return self.q.popleft()
else:
assert 0
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read
n,l,*a = [int(i) for i in read().split()]
q = SWAG(min,10**9)
q.build(a[:l])
ans = [q.fold_all()]
for i in a[l:]:
q.popleft()
q.append(i)
ans.append(q.fold_all())
print(*ans)
``` | instruction | 0 | 53,056 | 12 | 106,112 |
Yes | output | 1 | 53,056 | 12 | 106,113 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\\{1, 7, 7, 4, 8, 1, 6\\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\\{1, 7, 7\\}$, $\\{7, 7, 4\\}$, $\\{7, 4, 8\\}$, $\\{4, 8, 1\\}$, $\\{8, 1, 6\\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively.
Constraints
* $1 \leq N \leq 10^6$
* $1 \leq L \leq 10^6$
* $1 \leq a_i \leq 10^9$
* $L \leq N$
Input
The input is given in the following format.
$N$ $L$
$a_1$ $a_2$ ... $a_N$
Output
Print a sequence of the minimum in a line. Print a space character between adjacent elements.
Example
Input
7 3
1 7 7 4 8 1 6
Output
1 4 4 1 1
Submitted Solution:
```
import heapq
def SME(start,end):
if end > N : end = N
count = 0
for i,x in enumerate(Whole[start:end]):
heapq.heappush(A,[x,i])
for j in range(start,end):
a = heapq.heappop(A)
if ANS[a[1]+L-1] == None :
ANS[idx] = a[0]
count += 1
if None not in ANS[a[1]:a[1]+L] : continue
for i in range(L):
idx = a[1] + i
try :
if ANS[idx] : continue
else :
ANS[idx] = a[0]
count += 1
except IndexError : continue
if count > N * parameter : break
for i in range(L):
if ANS[i] : continue
else : ANS[i] = min(Whole[:i+1])
for i in range(L,N):
if ANS[i] : continue
else : ANS[i] = min(Whole[i+1-L:i+1])
return ANS
[N,L] = [int(x) for x in input().split()]
Whole = [int(x) for x in input().split()]
A = []
parameter = 0.997
ANS = [None]*N
ANS = SME(0, N)
for i in ANS[L-1:-1]:print(i, end=" ")
print(ANS[-1])
``` | instruction | 0 | 53,059 | 12 | 106,118 |
No | output | 1 | 53,059 | 12 | 106,119 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\\{1, 7, 7, 4, 8, 1, 6\\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\\{1, 7, 7\\}$, $\\{7, 7, 4\\}$, $\\{7, 4, 8\\}$, $\\{4, 8, 1\\}$, $\\{8, 1, 6\\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively.
Constraints
* $1 \leq N \leq 10^6$
* $1 \leq L \leq 10^6$
* $1 \leq a_i \leq 10^9$
* $L \leq N$
Input
The input is given in the following format.
$N$ $L$
$a_1$ $a_2$ ... $a_N$
Output
Print a sequence of the minimum in a line. Print a space character between adjacent elements.
Example
Input
7 3
1 7 7 4 8 1 6
Output
1 4 4 1 1
Submitted Solution:
```
import heapq
def SME(start,end):
A = []
if end > N : end = N
count = 0
count2 = 0
for i,x in enumerate(Whole[start:end]):
heapq.heappush(A,[x,i])
for j in range(start,end):
a = heapq.heappop(A)
tmp = count
for i in range(L):
idx = a[1] + i
try :
if ANS[idx] : continue
else :
ANS[idx] = a[0]
count += 1
except IndexError : continue
if count == tmp : count2 += 1
if count2 > 300 : break
for i in range(L):
if ANS[i] : continue
else : ANS[i] = min(Whole[:i+1])
for i in range(L,N):
if ANS[i] : continue
else : ANS[i] = min(Whole[i+1-L:i+1])
return ANS
[N,L] = [int(x) for x in input().split()]
Whole = [int(x) for x in input().split()]
parameter = 0.97
ANS = [None]*N
ANS = SME(0, N)
for i in ANS[L-1:-1]:print(i, end=" ")
print(ANS[-1])
``` | instruction | 0 | 53,060 | 12 | 106,120 |
No | output | 1 | 53,060 | 12 | 106,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n β₯ 1
* 1 β€ a_1 < a_2 < ... < a_n β€ d
* Define an array b of length n as follows: b_1 = a_1, β i > 1, b_i = b_{i - 1} β a_i, where β is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 β€ t β€ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 β€ d, m β€ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 53,218 | 12 | 106,436 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
t=int(input())
for q in range(t):
n,m=map(int,input().split())
st=1
ans=1
while st<=n:
ans*=min(st*2-st+1,n-st+2)
st*=2
print((ans-1)%m)
#########
``` | output | 1 | 53,218 | 12 | 106,437 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n β₯ 1
* 1 β€ a_1 < a_2 < ... < a_n β€ d
* Define an array b of length n as follows: b_1 = a_1, β i > 1, b_i = b_{i - 1} β a_i, where β is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 β€ t β€ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 β€ d, m β€ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 53,219 | 12 | 106,438 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def func1(arr,n,m):
for i in range(n-1,0,-1):
arr[i-1]+=arr[i]
arr[i-1]%=m
def func2(arr,n,m,lis):
for i in range(1,n):
arr[i-1]=(lis[i-1]*arr[i])%m
def func3(lis,n,m):
arr=[]
ans=0
for i in range(n):
arr.append(lis[i])
ans+=lis[i]
ans%=m
for i in range(1,n):
func1(arr,n-i+1,m)
for j in range(1,n-i+1):
temp=(lis[j-1]*arr[j])%m
ans+=temp
ans%=m
func2(arr,n,m,lis)
return ans
t=int(input())
for i in range(t):
d,m=list(map(int,input().split()))
a=len(bin(d)[2:])
lis=[]
for j in range(a):
if j!=a-1:
lis.append((2**j)%m)
else:
lis.append((d-(2**(j))+1)%m)
print(func3(lis,a,m))
``` | output | 1 | 53,219 | 12 | 106,439 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n β₯ 1
* 1 β€ a_1 < a_2 < ... < a_n β€ d
* Define an array b of length n as follows: b_1 = a_1, β i > 1, b_i = b_{i - 1} β a_i, where β is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 β€ t β€ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 β€ d, m β€ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 53,220 | 12 | 106,440 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
def main():
import sys
input=sys.stdin.readline
t=int(input())
for _ in range(t):
d,m=map(int,input().split())
k=0
d_=d
while d_:
d_//=2
k+=1
mod=[1]*(k+1)
for i in range(k):
mod[i+1]=mod[i]*2%m
a=1
a_=1
for i in range(k-1):
a=((a+1)*mod[i]+a)%m
a_=a_*(mod[i]+1)
ans=(a+a_*(d-2**(k-1)))%m
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 53,220 | 12 | 106,441 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n β₯ 1
* 1 β€ a_1 < a_2 < ... < a_n β€ d
* Define an array b of length n as follows: b_1 = a_1, β i > 1, b_i = b_{i - 1} β a_i, where β is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 β€ t β€ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 β€ d, m β€ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 53,221 | 12 | 106,442 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
def main():
import sys
input = sys.stdin.readline
for _ in range(int(input())):
d, mod = map(int, input().split())
LV = d.bit_length()
ans = 0
for lv in range(1, LV):
new = ((ans+1) * (1<<(lv-1)))%mod
ans += new
ans %= mod
n = d - (1 << (LV-1)) + 1
new = ((ans + 1) * n) % mod
ans += new
ans %= mod
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 53,221 | 12 | 106,443 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n β₯ 1
* 1 β€ a_1 < a_2 < ... < a_n β€ d
* Define an array b of length n as follows: b_1 = a_1, β i > 1, b_i = b_{i - 1} β a_i, where β is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 β€ t β€ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 β€ d, m β€ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 53,222 | 12 | 106,444 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
t = int(input())
maxd = 32
for case_num in range(t):
d, m = map(int, input().split(' '))
cnt = [0] * maxd
num = 2
i = 1
while num - 1 <= d:
cnt[i] = num // 2
num *= 2
i += 1
cnt[i] = d - num // 2 + 1
ans = 1
for l in range(1, maxd):
ans = ans * (cnt[l] + 1) % m
ans = (ans - 1 + m) % m
print(ans)
``` | output | 1 | 53,222 | 12 | 106,445 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n β₯ 1
* 1 β€ a_1 < a_2 < ... < a_n β€ d
* Define an array b of length n as follows: b_1 = a_1, β i > 1, b_i = b_{i - 1} β a_i, where β is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 β€ t β€ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 β€ d, m β€ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 53,223 | 12 | 106,446 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
# -*- coding: utf-8 -*-
"""poopy.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1AEzRNn-ty-g84EHrLuQaNVu_8nH6oFoL
"""
import math
def power(a, b, m):
if b == 0:
return a % m
return power(2*(a % m), b-1, m)
def expo(a, m):
return power(1, a, m)
def answer(d, m):
tests = int(math.log(d, 2))
ans = 1
for i in range(tests):
ans = ans*(expo(i, m)+1) % m
ans = (ans*(d-expo(tests,m)+2) -1) % m
return ans
num = int(input(""))
prints = []
for i in range(num):
xx = input().split(" ")
prints.append(answer(int(xx[0]), int(xx[1])))
for i in prints:
print(i)
``` | output | 1 | 53,223 | 12 | 106,447 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n β₯ 1
* 1 β€ a_1 < a_2 < ... < a_n β€ d
* Define an array b of length n as follows: b_1 = a_1, β i > 1, b_i = b_{i - 1} β a_i, where β is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 β€ t β€ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 β€ d, m β€ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 53,224 | 12 | 106,448 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
for i in range(int(input())):
d,m = map(int,input().split())
k=len(bin(d))-2
c=1
for j in range(k-1):
c*=2**j+1
c*=d-2**(k-1)+2
print((c-1)%m)
``` | output | 1 | 53,224 | 12 | 106,449 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.