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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Initially, you have the array a consisting of one element 1 (a = [1]).
In one move, you can do one of the following things:
* Increase some (single) element of a by 1 (choose some i from 1 to the current length of a and increase a_i by one);
* Append the copy of some (single) element of a to the end of the array (choose some i from 1 to the current length of a and append a_i to the end of the array).
For example, consider the sequence of five moves:
1. You take the first element a_1, append its copy to the end of the array and get a = [1, 1].
2. You take the first element a_1, increase it by 1 and get a = [2, 1].
3. You take the second element a_2, append its copy to the end of the array and get a = [2, 1, 1].
4. You take the first element a_1, append its copy to the end of the array and get a = [2, 1, 1, 2].
5. You take the fourth element a_4, increase it by 1 and get a = [2, 1, 1, 3].
Your task is to find the minimum number of moves required to obtain the array with the sum at least n.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (1 ≤ n ≤ 10^9) — the lower bound on the sum of the array.
Output
For each test case, print the answer: the minimum number of moves required to obtain the array with the sum at least n.
Example
Input
5
1
5
42
1337
1000000000
Output
0
3
11
72
63244
Submitted Solution:
```
import math
for _ in range(int(input())):
n = int(input())
b = []
if n<4:
print(n-1)
continue
for i in range(1,int(math.sqrt(n))+2):
k = (i-1)+n//i#+(n%i)
if (i+1)*(k-i)==n:
b.append(k-1)
else:
b.append(k)
print(min(b))
#print(b)
``` | instruction | 0 | 30,280 | 12 | 60,560 |
No | output | 1 | 30,280 | 12 | 60,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant has two permutations a and b of length n, consisting of numbers from 1 to n, inclusive. Let's denote the i-th (1 ≤ i ≤ n) element of the permutation a as ai, the j-th (1 ≤ j ≤ n) element of the permutation b — as bj.
The distance between permutations a and b is the minimum absolute value of the difference between the positions of the occurrences of some number in a and in b. More formally, it's such minimum |i - j|, that ai = bj.
A cyclic shift number i (1 ≤ i ≤ n) of permutation b consisting from n elements is a permutation bibi + 1... bnb1b2... bi - 1. Overall a permutation has n cyclic shifts.
The Little Elephant wonders, for all cyclic shifts of permutation b, what is the distance between the cyclic shift and permutation a?
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the size of the permutations. The second line contains permutation a as n distinct numbers from 1 to n, inclusive. The numbers are separated with single spaces. The third line contains permutation b in the same format.
Output
In n lines print n integers — the answers for cyclic shifts. Print the answers to the shifts in the order of the shifts' numeration in permutation b, that is, first for the 1-st cyclic shift, then for the 2-nd, and so on.
Examples
Input
2
1 2
2 1
Output
1
0
Input
4
2 1 3 4
3 4 2 1
Output
2
1
0
1
Submitted Solution:
```
import time,math,bisect,sys
sys.setrecursionlimit(100000)
from sys import stdin,stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi=3.14159265358979323846264338327950
def II(): # to take integer input
return int(stdin.readline())
def IO(): # to take string input
return stdin.readline()
def IP(): # to take tuple as input
return map(int,stdin.readline().split())
def L(): # to take list as input
return list(map(int,stdin.readline().split()))
def P(x): # to print integer,list,string etc..
return stdout.write(str(x)+"\n")
def PI(x,y): # to print tuple separatedly
return stdout.write(str(x)+" "+str(y)+"\n")
def lcm(a,b): # to calculate lcm
return (a*b)//gcd(a,b)
def gcd(a,b): # to calculate gcd
if a==0:
return b
elif b==0:
return a
if a>b:
return gcd(a%b,b)
else:
return gcd(a,b%a)
def readTree(): # to read tree
v=int(input())
adj=[set() for i in range(v+1)]
for i in range(v-1):
u1,u2=In()
adj[u1].add(u2)
adj[u2].add(u1)
return adj,v
def bfs(adj,v): # a schema of bfs
visited=[False]*(v+1)
q=deque()
while q:
pass
def sieve():
li=[True]*1000001
li[0],li[1]=False,False
for i in range(2,len(li),1):
if li[i]==True:
for j in range(i*i,len(li),i):
li[j]=False
prime=[]
for i in range(1000001):
if li[i]==True:
prime.append(i)
return prime
def setBit(n):
count=0
while n!=0:
n=n&(n-1)
count+=1
return count
#####################################################################################
mx=10**9+7
def solve():
n=II()
a=L()
li=list(a)
li.sort()
c=0
for i in range(n):
if li[i]!=a[i]:
c+=1
if c==2 or c==0:
print('YES')
else:
print('NO')
#t=II()
#for i in range(t):
solve()
#######
#
#
####### # # # #### # # #
# # # # # # # # # # #
# #### # # #### #### # #
###### # # #### # # # # #
``` | instruction | 0 | 30,369 | 12 | 60,738 |
No | output | 1 | 30,369 | 12 | 60,739 |
Provide tags and a correct Python 3 solution for this coding contest problem.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large. | instruction | 0 | 30,414 | 12 | 60,828 |
Tags: greedy, implementation
Correct Solution:
```
a = input()
i = 0; ans = 1
while i < len(a):
i += 1
c = i-1
while i < len(a) and a[i] == '0':
i += 1
if (c > i-c) or (c == i-c and a[:c] >= a[c:i]):
ans += 1
else:
ans = 1
print(ans)
``` | output | 1 | 30,414 | 12 | 60,829 |
Provide tags and a correct Python 3 solution for this coding contest problem.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large. | instruction | 0 | 30,415 | 12 | 60,830 |
Tags: greedy, implementation
Correct Solution:
```
a = input()
i = 0
ans = 1
c = 0
while i < len(a):
i += 1
c = i - 1
while i < len(a) and a[i]=='0':
i += 1
if c > i - c:
ans += 1
elif c == i - c:
if a[:c] >= a[c:i]:
ans += 1
else:
ans = 1
else:
ans = 1
print(ans)
``` | output | 1 | 30,415 | 12 | 60,831 |
Provide tags and a correct Python 3 solution for this coding contest problem.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large. | instruction | 0 | 30,416 | 12 | 60,832 |
Tags: greedy, implementation
Correct Solution:
```
def gte(x, y):
if len(x)>len(y): return True
if len(x)==len(y): return x>=y
return False
x = input()
r = 0
l = len(x)-1
while True:
if l==0 or not gte(x[:l], x[l:]): break
if x[l] == '0':
l-=1
continue
x = x[:l]
r+=1
l-=1
r+=1
print(r)
``` | output | 1 | 30,416 | 12 | 60,833 |
Provide tags and a correct Python 3 solution for this coding contest problem.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large. | instruction | 0 | 30,417 | 12 | 60,834 |
Tags: greedy, implementation
Correct Solution:
```
def compare(a,b):
fl = True
if len(a) > len(b):
return True
elif len(a) < len(b):
return False
elif len(a) == len(b):
fl = None
for i in range(len(a)):
if int(b[i]) > int(a[i]) and fl == None:
fl = False
break
elif int(a[i]) > int(b[i]) and fl == None:
fl = True
break
if fl == None:
fl = True
return fl
n = input()
l = []
a = ""
i = 0
while i < len(n):
a += n[i]
fl = False
while i < len(n) and n[i] == '0':
l[-1] += '0'
i += 1
fl = True
if fl:
a = ""
continue
l.append(a)
i += 1
a = ""
s = 1
for i in (range(1,len(l))):
if compare(l[i-1],l[i]):
s = s + 1
else:
s = 1
l[i] = l[i-1] + l[i]
l[i-1] = ''
print(s)
``` | output | 1 | 30,417 | 12 | 60,835 |
Provide tags and a correct Python 3 solution for this coding contest problem.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large. | instruction | 0 | 30,418 | 12 | 60,836 |
Tags: greedy, implementation
Correct Solution:
```
s = input()
r = len(s)
c = 1
for i in range(len(s)-1, 0, -1):
if s[i] != '0':
if i > r-i:
c += 1
r = i
elif i == r-i:
if s[:i] >= s[i:r]:
c += 1
r = i
else:
break
print(c)
``` | output | 1 | 30,418 | 12 | 60,837 |
Provide tags and a correct Python 3 solution for this coding contest problem.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large. | instruction | 0 | 30,419 | 12 | 60,838 |
Tags: greedy, implementation
Correct Solution:
```
s = input()+'#'
if len(s)==2:
print(1)
exit()
elif '0' not in s:
if int(s[0])<int(s[1]):
print(len(s)-2)
else:
print(len(s)-1)
else:
c=0;ind=-1;ans=0
zer = s.count('0')
for i in range(len(s)):
if s[i]=='0':
c+=1
else:
if i-c-1<c+1:
ans=i
elif c+1==i-c-1 and s[:i-c-1]<s[i-c-1:i]:
ans=i
c=0
a=s[:ans].count('0')
zer-=a
print(len(s)-ans-zer)
``` | output | 1 | 30,419 | 12 | 60,839 |
Provide tags and a correct Python 3 solution for this coding contest problem.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large. | instruction | 0 | 30,420 | 12 | 60,840 |
Tags: greedy, implementation
Correct Solution:
```
import sys
from math import *
from collections import *
readints=lambda:map(int, input().strip('\n').split())
s = input()
n = len(s)
arr = deque()
i=n-1
while i>=0:
j=i
while j>=0 and s[j]=='0': j-=1
arr.appendleft(((j,i), 1))
i=j-1
#print(arr)
def gte(x,y):
if x[1]-x[0]>y[1]-y[0]: return True
if x[1]-x[0]<y[1]-y[0]: return False
n = x[1]-x[0]+1
for i in range(n):
if s[i+x[0]]>s[i+y[0]]: return True
if s[i+x[0]]<s[i+y[0]]: return False
return True
while len(arr) > 1:
x = arr.popleft()
y = arr.popleft()
if gte(x[0],y[0]):
arr.appendleft(((x[0][0],y[0][1]),x[1]+y[1]))
else:
arr.appendleft(((x[0][0],y[0][1]),1))
#print(arr)
print(arr[0][1])
``` | output | 1 | 30,420 | 12 | 60,841 |
Provide tags and a correct Python 3 solution for this coding contest problem.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large. | instruction | 0 | 30,421 | 12 | 60,842 |
Tags: greedy, implementation
Correct Solution:
```
p = input().strip()
n = len(p)
j = n - 1
ans = 0
while(j >= 0):
k = j
while p[k] == '0':
k -= 1
ln = j - k + 1
if ln > k:
ans += 1
j = -1
elif ln == k:
if (p[0] >= p[k]):
j = k - 1
else:
j = -1
ans += 1
else:
ans += 1
j = k - 1
print(ans)
``` | output | 1 | 30,421 | 12 | 60,843 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large.
Submitted Solution:
```
#!/usr/bin/python3
def main():
s = input()
n = len(s)
if n == 1:
return 1
fst = 1
if int(s[0]) < int(s[1]):
fst = 2
ans = 1
j = 1
while j < n and s[j] == '0':
j += 1
if j == n:
return 1
cur = 1
for i in range(j + 1, n + 1):
if i == n or s[i] != '0':
if (i - cur < cur) or (i - cur == cur and int(s[0]) < int(s[i - cur])):
ans = 1
else:
ans += 1
cur = 1
else:
cur += 1
return ans
print(main())
``` | instruction | 0 | 30,422 | 12 | 60,844 |
Yes | output | 1 | 30,422 | 12 | 60,845 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large.
Submitted Solution:
```
a = input()
i = 0
ans = 1
while i < len(a):
i += 1
c = i - 1
while i < len(a) and a[i]=='0':
i += 1
if (c > i - c) or (c == i - c and a[:c] >= a[c:i]):
ans += 1
else:
ans = 1
print(ans)
``` | instruction | 0 | 30,423 | 12 | 60,846 |
Yes | output | 1 | 30,423 | 12 | 60,847 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large.
Submitted Solution:
```
s = input().strip()
n = len(s)
i = j = k = 0
while i < n:
j = i
i += 1
while i < n and s[i] == '0': i += 1
if j > i - j: k += 1
elif j == i - j:
if s[: j] >= s[j: i]: k += 1
else: k = 0
else: k = 0
print(k + 1)
``` | instruction | 0 | 30,424 | 12 | 60,848 |
Yes | output | 1 | 30,424 | 12 | 60,849 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large.
Submitted Solution:
```
a = input()
i = 0
ans = 1
c = 0
while i < len(a):
i += 1
c = i - 1
while i < len(a) and a[i]=='0':
i += 1
if (c > i - c) or (c == i - c and a[:c] >= a[c:i]):
ans += 1
else:
ans = 1
print(ans)
``` | instruction | 0 | 30,425 | 12 | 60,850 |
Yes | output | 1 | 30,425 | 12 | 60,851 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large.
Submitted Solution:
```
n = input()
k = len(n)-1
t = len(n)
s = 0
while k >= 0:
while k > 0 and n[k] == '0':
k -= 1
if k == 0:
s += 1
break
if int(n)//(int("1" + "0"*(len(n)-k-1))) >= (int(n) - (int(n)//(int("1" + "0"*(len(n)-k-1))))*(int("1" + "0"*(len(n)-k-1))))//(int("1" + "0"*(len(n)-k-2))):
s += 1
t = k
k -= 1
print(s)
``` | instruction | 0 | 30,426 | 12 | 60,852 |
No | output | 1 | 30,426 | 12 | 60,853 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large.
Submitted Solution:
```
if __name__=='__main__':
n=input()
j=len(n)-1
k=j-1
count=0
arr=0
if n=='1000000000000001223300003342220044555':
print(17)
else:
if int(n[j])==0:
while int(n[k:j+1])==0:
k-=1
arr=int(n[k:j+1])
while k>=0:
while int(n[k:j])==0:
k-=1
if int(n[k:j])<int(n[j]) or arr>int(n[k:j]):
k-=1
elif int(n[k:j])>=int(n[j]) or arr<=int(n[k:j]):
arr=int(n[k:j])
j=k
k=j-1
count+=1
print(count+1)
``` | instruction | 0 | 30,427 | 12 | 60,854 |
No | output | 1 | 30,427 | 12 | 60,855 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large.
Submitted Solution:
```
p=str(input())
n=len(p)
def v(i,k,j):
if k-i>j-k-1:
return(True)
if k-i<j-k-1:return(False)
if int(p[i:k+1])>=int(p[k+1:j+1]):return(True)
return(False)
def dp(i,j):
k=j-1
if i==j:return(1)
while k>=i:
if v(i,j,k) and p[k+1]!='0':
return(dp(i,k)+dp(k+1,j))
k-=1
return(1)
print(dp(0,n-1))
``` | instruction | 0 | 30,428 | 12 | 60,856 |
No | output | 1 | 30,428 | 12 | 60,857 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:
* Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj.
* Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22.
* Add number v to the end of the array. The length of the array will increase by one.
* Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
Input
The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.
Output
Print an integer — the maximum number of elements array b could contain originally.
Examples
Input
9555
Output
4
Input
10000000005
Output
2
Input
800101
Output
3
Input
45
Output
1
Input
1000000000000001223300003342220044555
Output
17
Input
19992000
Output
1
Input
310200
Output
2
Note
Let's consider the test examples:
* Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}.
* Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros.
* Originally array b could be equal to {800, 10, 1}.
* Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large.
Submitted Solution:
```
def integer(n):
if n=="" :
return 0;
return int(n)
def fun(s):
length=len(s)
found=0
for i in range(1,length):
n1=s[:i]
n2=s[i:]
num1=integer(n1)
num2=integer(n2)
if num1>=num2:
found=found+1
#print(num2)
break
if found==0:
#print("by1")
return 1
else:
if int(n2)==0:
return 1;
fun_n2=fun(n2)
if fun_n2==1:
#print("by2")
return 2
else:
#print("byadd")
return fun_n2+fun(n1)
s=input()
print(fun(s))
``` | instruction | 0 | 30,429 | 12 | 60,858 |
No | output | 1 | 30,429 | 12 | 60,859 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>. | instruction | 0 | 30,478 | 12 | 60,956 |
Tags: data structures
Correct Solution:
```
import sys
class SegmTree():
def __init__(self, array=None):
size = len(array)
N = 1
while N < size:
N <<= 1
self.N = N
self.tree = [0] * (2*self.N)
for i in range(size):
self.tree[i+self.N] = array[i]
self.build()
def build(self):
for i in range(self.N - 1, 0, -1):
self.tree[i] = self.tree[i<<1] + self.tree[i<<1|1]
def add(self, i, value=1):
i += self.N
while i > 0:
self.tree[i] += value
i >>= 1
def get_sum(self, l, r):
N = self.N
l += N
r += N
result = 0
while l < r:
if l & 1:
result += self.tree[l]
l += 1
if r & 1:
r -= 1
result += self.tree[r]
l >>= 1
r >>= 1
return result
def find_kth_nonzero(self, k):
i = 1
if k < 1 or k > self.tree[1]:
return -1
while i < self.N:
i <<= 1
if self.tree[i] < k:
k -= self.tree[i]
i |= 1
return i - self.N
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ord_p = [0] * n
ord_q = [0] * n
st = SegmTree([1] * n)
for i, val in enumerate(p):
ord_p[i] = st.get_sum(0, val)
st.add(val, -1)
st = SegmTree([1] * n)
for i, val in enumerate(q):
ord_q[i] = st.get_sum(0, val)
st.add(val, -1)
transfer = 0
for i in range(n-1, -1, -1):
radix = n-i
ord_p[i] = ord_p[i] + ord_q[i] + transfer
if ord_p[i] < radix:
transfer = 0
else:
transfer = 1
ord_p[i] -= radix
st = SegmTree([1] * n)
for i in range(n):
k = ord_p[i] + 1
ord_q[i] = st.find_kth_nonzero(k)
st.add(ord_q[i], -1)
print(*ord_q)
``` | output | 1 | 30,478 | 12 | 60,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>. | instruction | 0 | 30,479 | 12 | 60,958 |
Tags: data structures
Correct Solution:
```
def sum(BIT, i):
s = 0
while i > 0:
s += BIT[i]
i -= i & (-i)
return s
def update(BIT, i, v):
while i < len(BIT):
BIT[i] += v
i += i & (-i)
def find(fen, k):
curr = 0
ans = 0
prevsum = 0
for i in range(19, -1, -1):
if ((curr + (1 << i) < n) and fen[curr + (1 << i)] + prevsum < k):
ans = curr + (1 << i)
curr = ans
prevsum += fen[curr]
return ans + 1
def Rank(x,BIT) :
return sum(BIT,x)
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
factp = []
factq = []
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
for val in p:
factp.append(Rank(val+1,BIT)-1)
update(BIT,val+1,-1)
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
for val in q:
factq.append(Rank(val+1,BIT)-1)
update(BIT,val+1,-1)
carry = 0
for i in range(n - 1, -1, -1):
radix = n - i
factp[i] = factp[i] + factq[i] + carry
if factp[i] < radix:
carry = 0
else:
carry = 1
factp[i] -= radix
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
res=[]
for i in range(n):
k = factp[i]+1
res.append(find(BIT,k)-1)
update(BIT,res[-1]+1,-1)
print(*res)
``` | output | 1 | 30,479 | 12 | 60,959 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>. | instruction | 0 | 30,480 | 12 | 60,960 |
Tags: data structures
Correct Solution:
```
import sys
class SegmTree():
def __init__(self, array=None):
size = len(array)
N = 1
while N < size:
N <<= 1
self.N = N
self.tree = [0] * (2*self.N)
for i in range(size):
self.tree[i+self.N] = array[i]
self.build()
def build(self):
for i in range(self.N - 1, 0, -1):
self.tree[i] = self.tree[i<<1] + self.tree[i<<1|1]
def add(self, i, value=1):
i += self.N
while i > 0:
self.tree[i] += value
i >>= 1
def get_sum(self, l, r):
N = self.N
l += N
r += N
result = 0
while l < r:
if l & 1:
result += self.tree[l]
l += 1
if r & 1:
r -= 1
result += self.tree[r]
l >>= 1
r >>= 1
return result
def find_kth_nonzero(self, k):
i = 1
if k < 1 or k > self.tree[1]:
return -1
while i < self.N:
i <<= 1
if self.tree[i] < k:
k -= self.tree[i]
i |= 1
return i - self.N
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ord_p = []
ord_q = []
st = SegmTree([1] * n)
for i, val in enumerate(p):
ord_p.append(st.get_sum(0, val))
st.add(val, -1)
st = SegmTree([1] * n)
for i, val in enumerate(q):
ord_q.append(st.get_sum(0, val))
st.add(val, -1)
transfer = 0
for i in range(n-1, -1, -1):
radix = n-i
ord_p[i] = ord_p[i] + ord_q[i] + transfer
if ord_p[i] < radix:
transfer = 0
else:
transfer = 1
ord_p[i] -= radix
st = SegmTree([1] * n)
for i in range(n):
k = ord_p[i] + 1
ord_q[i] = st.find_kth_nonzero(k)
st.add(ord_q[i], -1)
print(*ord_q)
``` | output | 1 | 30,480 | 12 | 60,961 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>.
Submitted Solution:
```
def main():
n = int(input())
pp = list(map(int, input().split()))
qq = input().split()
for q, i in zip(qq, sorted(range(n), key=pp.__getitem__)):
pp[i] = q
print(' '.join(pp))
if __name__ == '__main__':
main()
``` | instruction | 0 | 30,481 | 12 | 60,962 |
No | output | 1 | 30,481 | 12 | 60,963 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>.
Submitted Solution:
```
from math import factorial
import sys
def kthPermutation(n, k):
nums = [0 for i in range(n)]
factorial = [0 for i in range(n+1)]
factorial[0] = 1
factorial[1] = 1
nums[0] = 1
for i in range(2,n+1):
nums[i-1] = i;
factorial[i] = i*factorial[i - 1]
if(k <= 1):
return nums
if(k >= factorial[n]):
nums = nums[::-1]
return nums
k -= 1
for i in range(n-1):
fact = factorial[n-i-1]
index = int(k/fact)
nums = shiftRight(nums, i, i+index)
k = k - fact*index;
return nums
def shiftRight(a, s, e):
temp = a[e]
i = e
while (i>s):
a[i] = a[i-1]
i-=1
a[s] = temp
return a
def lehmer(nums):
factoradic = []
while True:
if len(nums) == 0:
break
current_sum = 0
i = min(nums)
index = nums.index(i)
for x in range(index):
if nums[x]>i:
current_sum +=1
factoradic.append(current_sum)
nums.pop(index)
final = 0
for index,num in enumerate(factoradic[::-1]):
final += num*factorial(index)
return final
lines = sys.stdin.read().split("\n")
lines.pop(-1)
n = int(lines.pop(0))
nums = []
for num in lines[0].split(" "):
nums.append(int(num))
p = lehmer(nums)
nums = []
for num in lines[1].split(" "):
nums.append(int(num))
q = lehmer(nums)
final = kthPermutation(n, (p+q)%factorial(n))
for num in final:
print(num-1,end=" ")
#print(lehmer([3,4,1,2]))
#print(kthPermutation(4,8))
``` | instruction | 0 | 30,482 | 12 | 60,964 |
No | output | 1 | 30,482 | 12 | 60,965 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>.
Submitted Solution:
```
from array import array
n = int(input())
def sum(a, i):
s = 0
while i >= 0:
s += a[i]
i = (i&(i+1))-1
return s
def change(a, i, s):
while i < len(a):
a[i] += s
i |= i + 1
def get(p):
a = array('l',[0]*n)
fen = array('l',[0]*n)
for i in range(n):
change(fen, i, 1)
for i in range(n):
a[i] = sum(fen, p[i]) - 1
change(fen, p[i], - 1)
return a
def rget(a):
p = array('l',[0]*n)
fen = array('l',[0]*n)
for i in range(n):
change(fen, i, 1)
l,r = -1, n-1
while r - l > 1:
m = (l+r) // 2
if sum(fen, m)-1 < a[i]:
l = m
else:
r = m
p[i] = r
change(fen, p[i], -1)
return p
a = array('l',list(map(int,input().split())))
b = array('l',list(map(int,input().split())))
pa = get(a)
pb = get(b)
pc = array('d',[pa[i] + pb[i] for i in range(n)])
for i in range(n-1,-1,-1):
if pc[i] >= n - i:
if i > 0:
pc[i-1] += 1
pc[i] -= n - i
c = rget(pc)
print(' '.join(map(str,c)))
``` | instruction | 0 | 30,483 | 12 | 60,966 |
No | output | 1 | 30,483 | 12 | 60,967 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>.
Submitted Solution:
```
def sum(BIT, i):
s = 0
while i > 0:
s += BIT[i]
i -= i & (-i)
return s
def update(BIT, i, v):
while i < len(BIT):
BIT[i] += v
i += i & (-i)
def find(fen, k):
curr = 0
ans = 0
prevsum = 0
for i in range(19, -1, -1):
if ((curr + (1 << i) < n) and fen[curr + (1 << i)] + prevsum < k):
ans = curr + (1 << i)
curr = ans
prevsum += fen[curr]
return ans + 1
def Rank(x,BIT) :
return sum(BIT,x)
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
factp = []
factq = []
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
for val in p:
factp.append(Rank(val+1,BIT)-1)
update(BIT,val+1,-1)
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
for val in q:
factq.append(Rank(val+1,BIT)-1)
update(BIT,val+1,-1)
carry = 0
for i in range(n - 1, -1, -1):
radix = n - i
factp[i] = factp[i] + factq[i] + carry
if factp[i] < radix:
carru = 0
else:
carry = 1
factp[i] -= radix
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
res=[]
for i in range(n):
k = factp[i]+1
res.append(find(BIT,k)-1)
update(BIT,res[-1]+1,-1)
print(*res)
``` | instruction | 0 | 30,484 | 12 | 60,968 |
No | output | 1 | 30,484 | 12 | 60,969 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:
<image>
You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes.
Input
The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
Output
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
Examples
Input
4 3
2
3
4
Output
3
2
4
Input
13 4
10
5
4
8
Output
13
3
8
9
Note
The first example is shown in the picture.
In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. | instruction | 0 | 30,665 | 12 | 61,330 |
Tags: constructive algorithms, math
Correct Solution:
```
import sys
[n, q] = map(int, sys.stdin.readline().strip().split())
qis = [int(sys.stdin.readline().strip()) for _ in range(q)]
def query(n, q):
d = 2 * n - q
while d % 2 == 0:
d //= 2
return (n - d // 2)
for qi in qis:
print (query(n, qi))
``` | output | 1 | 30,665 | 12 | 61,331 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:
<image>
You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes.
Input
The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
Output
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
Examples
Input
4 3
2
3
4
Output
3
2
4
Input
13 4
10
5
4
8
Output
13
3
8
9
Note
The first example is shown in the picture.
In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. | instruction | 0 | 30,666 | 12 | 61,332 |
Tags: constructive algorithms, math
Correct Solution:
```
#!/usr/bin/env python3
import sys
[n, q] = map(int, sys.stdin.readline().strip().split())
qis = [int(sys.stdin.readline().strip()) for _ in range(q)]
def query(n, q):
d = 2 * n - q
while d % 2 == 0:
d //= 2
return (n - d // 2)
for qi in qis:
print (query(n, qi))
``` | output | 1 | 30,666 | 12 | 61,333 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:
<image>
You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes.
Input
The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
Output
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
Examples
Input
4 3
2
3
4
Output
3
2
4
Input
13 4
10
5
4
8
Output
13
3
8
9
Note
The first example is shown in the picture.
In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. | instruction | 0 | 30,667 | 12 | 61,334 |
Tags: constructive algorithms, math
Correct Solution:
```
import math
from decimal import Decimal
def na():
n = int(input())
b = [int(x) for x in input().split()]
return n,b
def nab():
n = int(input())
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
return n,b,c
def dv():
n, m = map(int, input().split())
return n,m
def dva():
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
return n,m,b
def eratosthenes(n):
sieve = list(range(n + 1))
for i in sieve:
if i > 1:
for j in range(i + i, len(sieve), i):
sieve[j] = 0
return sorted(set(sieve))
def nm():
n = int(input())
b = [int(x) for x in input().split()]
m = int(input())
c = [int(x) for x in input().split()]
return n,b,m,c
def dvs():
n = int(input())
m = int(input())
return n, m
n, q = map(int, input().split())
ans = []
for i in range(q):
x = int(input())
ans.append(x)
for i in ans:
d = 2 * n - i
while d % 2 == 0:
d //= 2
print(n - d // 2)
``` | output | 1 | 30,667 | 12 | 61,335 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:
<image>
You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes.
Input
The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
Output
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
Examples
Input
4 3
2
3
4
Output
3
2
4
Input
13 4
10
5
4
8
Output
13
3
8
9
Note
The first example is shown in the picture.
In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. | instruction | 0 | 30,668 | 12 | 61,336 |
Tags: constructive algorithms, math
Correct Solution:
```
from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
# python3 15.py<in>op\
n, m = map(int, input().split())
for i in range(m):
x = int(input())
while(x%2 == 0):
x= (n+(x//2))
ans = x//2
print(ans+1)
``` | output | 1 | 30,668 | 12 | 61,337 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:
<image>
You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes.
Input
The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
Output
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
Examples
Input
4 3
2
3
4
Output
3
2
4
Input
13 4
10
5
4
8
Output
13
3
8
9
Note
The first example is shown in the picture.
In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. | instruction | 0 | 30,669 | 12 | 61,338 |
Tags: constructive algorithms, math
Correct Solution:
```
from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
# python3 15.py<in>op\
n, m = map(int, input().split())
for i in range(m):
x = int(input())
while(x%2 == 0):
x+= (n-(x//2))
ans = x//2
print(ans+1)
``` | output | 1 | 30,669 | 12 | 61,339 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:
<image>
You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes.
Input
The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
Output
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
Examples
Input
4 3
2
3
4
Output
3
2
4
Input
13 4
10
5
4
8
Output
13
3
8
9
Note
The first example is shown in the picture.
In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. | instruction | 0 | 30,670 | 12 | 61,340 |
Tags: constructive algorithms, math
Correct Solution:
```
import sys
import bisect
input=sys.stdin.readline
#t=int(input())
t=1
mod=10**9+7
for _ in range(t):
#n=int(input())
n,q=map(int,input().split())
#s=input()
#l=list(map(int,input().split()))
#pref=[[0 for j in range(3001)] for i in range(n+2)]
for i in range(q):
x=int(input())
if x%2!=0:
print((x+1)//2)
else:
loop=(n-x//2)
#print(111,x,loop)
while 1:
x+=loop
if (x)%2!=0:
print((x-1)//2+1)
break
#print(loop)
loop//=2
``` | output | 1 | 30,670 | 12 | 61,341 |
Provide a correct Python 3 solution for this coding contest problem.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5 | instruction | 0 | 30,783 | 12 | 61,566 |
"Correct Solution:
```
def root(i):
if par[i] == i:
return i
par[i] = root(par[i])
return par[i]
def union(x, y):
rx = root(x)
ry = root(y)
if rx != ry:
par[ry] = rx
def same(x, y):
return par[x] == par[y]
n,m = map(int, input().split(" "))
p = [0] + list(map(int, input().split(" ")))
a = [(list(map(int, input().split(" ")))) for i in range(m)]
par = list(range(n + 1))
count = 0
for i, j in a:
union(i, j)
#print(par)
#print(p)
for i in range(1, n + 1):
if same(root(par[p[i]]), root(par[i])):
count += 1
print(count)
``` | output | 1 | 30,783 | 12 | 61,567 |
Provide a correct Python 3 solution for this coding contest problem.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5 | instruction | 0 | 30,784 | 12 | 61,568 |
"Correct Solution:
```
def find(x):
while union[x] != x:
x = union[x]
return x
N, M = map(int, input().split())
p = list(map(int, input().split()))
union = [i for i in range(N+1)]
for i in range(M):
x, y = map(int, input().split())
x = find(x)
while union[y] != y:
union[y], y = x, union[y]
union[y] = x
ans = 0
for idx, n in enumerate(p):
if idx+1 == n:
ans += 1
else:
x, y = find(idx+1), find(n)
if x == y:
ans += 1
print(ans)
``` | output | 1 | 30,784 | 12 | 61,569 |
Provide a correct Python 3 solution for this coding contest problem.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5 | instruction | 0 | 30,785 | 12 | 61,570 |
"Correct Solution:
```
N,M=map(int,input().split())
p = list(map(int,input().split()))
for i in range(N): p[i] -= 1
par = [ i for i in range(N)]
def uf_root(a):
if par[a] == a: return a
par[a] = uf_root( par[a] )
return par[a]
def uf_same(a,b):
if (uf_root(a) ==uf_root(b)): return True
return False
def uf_union(a,b):
if (uf_same(a,b)): return
a = uf_root(a)
b = uf_root(b)
par[a] = b
return
for i in range(M):
x,y = map(int, input().split())
x -= 1
y -= 1
uf_union(x,y)
ans = 0
for i in range(N):
if ( uf_same(i,p[i]) ):
ans += 1
print(ans)
``` | output | 1 | 30,785 | 12 | 61,571 |
Provide a correct Python 3 solution for this coding contest problem.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5 | instruction | 0 | 30,786 | 12 | 61,572 |
"Correct Solution:
```
n,m = map(int,input().split())
f = lambda x : int(x) - 1
p = list(map(f,input().split()))
par = [i for i in range(n)]
def root(x):
if par[x] == x:
return x
else:
par[x] = root(par[x])
return par[x]
def unite(x,y):
if root(x) == root(y):
return
else:
par[root(x)] = root(y)
for i in range(m):
x,y = map(f,input().split())
unite(x,y)
ans = 0
for i in range(n):
if root(i) == root(p[i]): ans += 1
print(ans)
``` | output | 1 | 30,786 | 12 | 61,573 |
Provide a correct Python 3 solution for this coding contest problem.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5 | instruction | 0 | 30,787 | 12 | 61,574 |
"Correct Solution:
```
def main():
cnt = 0
for _ in range(m):
x, y = map(int, input().split())
unite(x, y)
for i, pi in enumerate(p, 1):
if same(i, pi):
cnt += 1
print(cnt)
def root(x):
if parent[x] == x:
return x
parent[x] = root(parent[x])
return parent[x]
def same(x, y):
return root(x) == root(y)
def unite(x, y):
x = root(x)
y = root(y)
if x != y:
if rank[x] < rank[y]:
parent[x] = y
elif rank[x] > rank[y]:
parent[y] = x
else:
parent[y] = x
rank[x] += 1
if __name__ == "__main__":
n, m = map(int, input().split())
p = list(map(int, input().split()))
parent = list(range(n+1))
rank = [0]*(n+1)
main()
``` | output | 1 | 30,787 | 12 | 61,575 |
Provide a correct Python 3 solution for this coding contest problem.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5 | instruction | 0 | 30,788 | 12 | 61,576 |
"Correct Solution:
```
N, M = map(int, input().split())
p = [int(i) for i in input().split()]
# graph = [[]]*(N+1)
graph = [[] for _ in range(N+1)]
group = [0]*(N+1)
ans = 0
for _ in range(M):
x, y = map(int, input().split())
graph[x].append(y)
graph[y].append(x)
for i, num in enumerate(p, 1):
space = [i]
for point in space:
if group[point]:
continue
group[point] = i
space.extend(graph[point])
ans += (group[num] == group[i])
print(ans)
``` | output | 1 | 30,788 | 12 | 61,577 |
Provide a correct Python 3 solution for this coding contest problem.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5 | instruction | 0 | 30,789 | 12 | 61,578 |
"Correct Solution:
```
class UnionFind():
def __init__(self,n):
self.par=[i for i in range(n)]
def root(self, x):
if self.par[x]==x:
return x
else:
self.par[x]=self.root(self.par[x])
return self.par[x]
def same(self,x,y):
return self.root(x)==self.root(y)
def unite(self,x,y):
x=self.root(x)
y=self.root(y)
if x!=y:
self.par[x]=y
n, m=map(int, input().split())
p=[int(s)-1 for s in input().split()]
pair=[[int(s)-1 for s in input().split()] for _ in range(m)]
uf=UnionFind(n)
for pi in pair:
uf.unite(pi[0],pi[1])
cnt=0
for i, pi in enumerate(p):
if uf.same(i,pi):
cnt+=1
print(cnt)
``` | output | 1 | 30,789 | 12 | 61,579 |
Provide a correct Python 3 solution for this coding contest problem.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5 | instruction | 0 | 30,790 | 12 | 61,580 |
"Correct Solution:
```
a,b=map(int,input().split())
l=list(map(int,input().split()))
root=[i for i in range(a)]
height=[0]*a
group=[set() for i in range(a)]
def find(a):
f=a
if a==root[a]:
return a
while a!=root[a]:
a=root[a]
root[f]=a
return a
def union(a,b):
A=find(a)
B=find(b)
if A==B:
return
if height[A]>height[B]:
root[B]=root[A]
else:
root[A]=root[B]
if height[A]==height[B]:
height[B]+=1
for i in range(b):
x,y=map(int,input().split())
x-=1;y-=1
union(x,y)
for i in range(a):
group[root[find(i)]].add(l[i])
ans=0
for i in range(a):
if i+1 in group[find(i)]:
ans+=1
print(ans)
``` | output | 1 | 30,790 | 12 | 61,581 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5
Submitted Solution:
```
N, M = map(int, input().split())
# p = [int(i) for i in input().split()]
p = map(int,input().split())
# graph = [[]]*(N+1)
graph = [[] for _ in range(N+1)]
group = [0]*(N+1)
ans = 0
for _ in range(M):
x, y = map(int, input().split())
graph[x].append(y)
graph[y].append(x)
for i, num in enumerate(p, 1):
space = [i]
for point in space:
if group[point]:
continue
group[point] = i
space.extend(graph[point])
ans += (group[num] == group[i])
print(ans)
``` | instruction | 0 | 30,791 | 12 | 61,582 |
Yes | output | 1 | 30,791 | 12 | 61,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5
Submitted Solution:
```
from collections import deque
n, m = map(int, input().split())
p = list(map(int, input().split()))
par = list(range(n+1))
def find(x):
p_ = par[x]
if p_ == x:
return x
a = find(p_)
par[x] = a
return a
for _ in range(m):
x, y = map(int, input().split())
bx, by = find(x), find(y)
par[y] = bx
par[by] = bx
ans = 0
for i in range(1, n+1):
if find(i) == find(p[i-1]):
ans += 1
print(ans)
``` | instruction | 0 | 30,792 | 12 | 61,584 |
Yes | output | 1 | 30,792 | 12 | 61,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5
Submitted Solution:
```
# Union Find
# xの根を求める
def find(x):
if par[x] < 0:
return x
else:
par[x] = find(par[x])
return par[x]
# xとyの属する集合の併合
def unite(x, y):
x = find(x)
y = find(y)
if x == y:
return False
else:
# x < y にする
if par[x] > par[y]:
x, y = y, x
par[x] += par[y]
par[y] = x
return True
# xとyが同じ集合に属するかの判定
def same(x, y):
return find(x) == find(y)
n, m = map(int, input().split())
par = [-1] * n
l = list(map(int, input().split()))
for _ in range(m):
x, y = map(int, input().split())
unite(x - 1, y - 1)
print(sum(same(i, l[i] - 1) for i in range(n)))
``` | instruction | 0 | 30,793 | 12 | 61,586 |
Yes | output | 1 | 30,793 | 12 | 61,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5
Submitted Solution:
```
n, m=map(int, input().split())
p=[0]+list(map(int, input().split()))
par=[i for i in range(n+1)]
cnt=0
def root(i):
if par[i]==i:
return i
else:
par[i]=root(par[i])
return par[i]
for i in range(m):
x, y=map(int, input().split())
if root(x)==root(y):
0
#print(par[1:])
else:
par[root(y)]=root(x)
#print(par[1:])
ans=0
for i in range(n):
n=1+i
if root(n)==root(p[n]):
ans=ans+1
print(ans)
'''
print(par[1:])
root_ls=[0]+[root(i+1) for i in range(n)]
print(par[1:])
print(root_ls[1:])
'''
``` | instruction | 0 | 30,794 | 12 | 61,588 |
Yes | output | 1 | 30,794 | 12 | 61,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5
Submitted Solution:
```
import numpy as np
n,m = map(int, input().split())
p = np.array(list(map(int, input().split())))
p = p-1
q = np.zeros((m,2), dtype='uint32')
for i in range(m):
q[i] = list(map(int, input().split()))
q=q-1
par = np.arange(n)
def root(x):
if par[x] == x:
return x
else:
return root(par[x])
def merge(x, y):
x = root(x)
y = root(y)
if x != y:
par[x] = y
pass
for x,y in q:
merge(x,y)
tree = dict()
tree2 = dict()
for i in range(n):
tree.setdefault(root(i), set())
tree2.setdefault(root(i), set())
tree[root(i)].add(i)
tree2[root(i)].add(p[i])
ans = 0
for value, value2 in zip(tree.values(), tree2.values()):
ans += len(value & value2)
print(ans)
``` | instruction | 0 | 30,795 | 12 | 61,590 |
No | output | 1 | 30,795 | 12 | 61,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5
Submitted Solution:
```
from collections import deque
import sys
sys.setrecursionlimit(10**9)
def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))
INF=10**20
def main():
N,M=mi()
P = []
for p in mi():
P.append(p-1)
X = [[] for _ in range(N)]
for i in range(M):
x,y = mi()
X[x-1].append(y-1)
X[y-1].append(x-1)
def search(x,y): # x → y が存在するか探す
stack = deque([(x,[x],set([x]))])
while stack:
current, history, seen = stack.popleft()
if current == y: break
for next in X[current]:
if next in seen: continue
stack.append((next,history+[next], seen|set([next])))
if current == y: return history
else: return []
def swap(i,j):
p = P[i]
P[i] = P[j]
P[j] = p
ans = 0
for i in range(N):
while P[i] != i:
path = search(i,P[i])
if len(path) <= 1: break
for j in range(len(path)-1):
swap(path[j], path[j+1])
for i in range(N):
if P[i] == i:
ans += 1
print(ans)
if __name__ == "__main__":
main()
``` | instruction | 0 | 30,796 | 12 | 61,592 |
No | output | 1 | 30,796 | 12 | 61,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5
Submitted Solution:
```
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
n, m = map(int, input().split())
p = list( map(int, input().split()))
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def root(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.root(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.root(x)
y = self.root(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.root(x)]
def is_same(self, x, y):
return self.root(x) == self.root(y)
def members(self, x):
root = self.root(x)
return [i for i in range(self.n) if self.root(i) == root]
def all_roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.all_roots())
def all_group_members(self):
return {r: self.members(r) for r in self.all_roots()}
def __str__(self):
return ''.join('{}: {}'.format(r, self.members(r)) for r in self.all_roots())
uf = UnionFind(n)
ab = [tuple(map(int,input().split())) for i in range(m)]
for i, (a,b) in enumerate(ab):
a,b = a-1,b-1
uf.union(a,b)
ans = 0
visited = [0]*n
for i in range(len(p)):
if visited[p[i]-1] == 1:
continue
if uf.size(p[i]-1) == 1:
if p[i]-1 == i: ans += 1
visited[i]==1
else:
loop = uf.members(p[i]-1)
visited[i]=1
for l in loop:
if p[l]-1 in loop:ans += 1
visited[l] = 1
print(ans)
``` | instruction | 0 | 30,797 | 12 | 61,594 |
No | output | 1 | 30,797 | 12 | 61,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:
* Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.
Find the maximum possible number of i such that p_i = i after operations.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* p is a permutation of integers from 1 through N.
* 1 ≤ x_j,y_j ≤ N
* x_j ≠ y_j
* If i ≠ j, \\{x_i,y_i\\} ≠ \\{x_j,y_j\\}.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the maximum possible number of i such that p_i = i after operations.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Input
3 2
3 2 1
1 2
2 3
Output
3
Input
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
Output
8
Input
5 1
1 2 3 4 5
1 5
Output
5
Submitted Solution:
```
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
class UnionFind():
def __init__(self, n):
self.n = n
# parents[i]: 要素iの親要素の番号
# 要素iが根の場合、parents[i] = -(そのグループの要素数)
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
# 要素xが属するグループの要素数を返す
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
# 要素xが属するグループに属する要素をリストで返す
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
# 全ての根の要素をリストで返す
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
# グループの数を返す
def group_count(self):
return len(self.roots())
# 辞書{根の要素: [そのグループに含まれる要素のリスト], ...}を返す
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
# print()での表示用
# all_group_members()をprintする
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
N, M = MAP()
P = LIST()
L = [LIST() for i in range(M)]
dic = defaultdict(list)
tree = UnionFind(N+1)
for a, b in L:
tree.union(a, b)
a = tree.all_group_members()
# print(a)
for key, l in a.items():
for y in l:
dic[key].append(P[y-1])
youso_set = set()
graph_set = set()
ans = 0
for key, l in a.items():
if key == 0:
continue
youso_set = set(dic[key])
graph_set = set(l)
intersec = len(youso_set & graph_set)
ans += intersec
print(ans)
``` | instruction | 0 | 30,798 | 12 | 61,596 |
No | output | 1 | 30,798 | 12 | 61,597 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of integers a_1, a_2, ..., a_k is called a good array if a_1 = k - 1 and a_1 > 0. For example, the sequences [3, -1, 44, 0], [1, -99] are good arrays, and the sequences [3, 7, 8], [2, 5, 4, 1], [0] — are not.
A sequence of integers is called good if it can be divided into a positive number of good arrays. Each good array should be a subsegment of sequence and each element of the sequence should belong to exactly one array. For example, the sequences [2, -3, 0, 1, 4], [1, 2, 3, -3, -9, 4] are good, and the sequences [2, -3, 0, 1], [1, 2, 3, -3 -9, 4, 1] — are not.
For a given sequence of numbers, count the number of its subsequences that are good sequences, and print the number of such subsequences modulo 998244353.
Input
The first line contains the number n~(1 ≤ n ≤ 10^3) — the length of the initial sequence. The following line contains n integers a_1, a_2, ..., a_n~(-10^9 ≤ a_i ≤ 10^9) — the sequence itself.
Output
In the single line output one integer — the number of subsequences of the original sequence that are good sequences, taken modulo 998244353.
Examples
Input
3
2 1 1
Output
2
Input
4
1 1 1 1
Output
7
Note
In the first test case, two good subsequences — [a_1, a_2, a_3] and [a_2, a_3].
In the second test case, seven good subsequences — [a_1, a_2, a_3, a_4], [a_1, a_2], [a_1, a_3], [a_1, a_4], [a_2, a_3], [a_2, a_4] and [a_3, a_4]. | instruction | 0 | 30,961 | 12 | 61,922 |
Tags: combinatorics, dp
Correct Solution:
```
mod = 998244353
n = int(input())
a = list(map(int, input().split()))
c = [[1]*n for _ in range(n)]
for i in range(n):
for j in range(1, i//2 + 1):
c[i][i-j] = c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod
d = [0] * n + [1]
for i in range(n-1,-1,-1):
if a[i] <= 0:
continue
for j in range(i+a[i]+1,n+1):
d[i] += (d[j] * c[j-i-1][a[i]])
d[i] %= mod
print((sum(d)-1)%mod)
``` | output | 1 | 30,961 | 12 | 61,923 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of integers a_1, a_2, ..., a_k is called a good array if a_1 = k - 1 and a_1 > 0. For example, the sequences [3, -1, 44, 0], [1, -99] are good arrays, and the sequences [3, 7, 8], [2, 5, 4, 1], [0] — are not.
A sequence of integers is called good if it can be divided into a positive number of good arrays. Each good array should be a subsegment of sequence and each element of the sequence should belong to exactly one array. For example, the sequences [2, -3, 0, 1, 4], [1, 2, 3, -3, -9, 4] are good, and the sequences [2, -3, 0, 1], [1, 2, 3, -3 -9, 4, 1] — are not.
For a given sequence of numbers, count the number of its subsequences that are good sequences, and print the number of such subsequences modulo 998244353.
Input
The first line contains the number n~(1 ≤ n ≤ 10^3) — the length of the initial sequence. The following line contains n integers a_1, a_2, ..., a_n~(-10^9 ≤ a_i ≤ 10^9) — the sequence itself.
Output
In the single line output one integer — the number of subsequences of the original sequence that are good sequences, taken modulo 998244353.
Examples
Input
3
2 1 1
Output
2
Input
4
1 1 1 1
Output
7
Note
In the first test case, two good subsequences — [a_1, a_2, a_3] and [a_2, a_3].
In the second test case, seven good subsequences — [a_1, a_2, a_3, a_4], [a_1, a_2], [a_1, a_3], [a_1, a_4], [a_2, a_3], [a_2, a_4] and [a_3, a_4]. | instruction | 0 | 30,962 | 12 | 61,924 |
Tags: combinatorics, dp
Correct Solution:
```
n = int(input())
m = 998244353
dl = [1] + [0] * n;
for i in map(int, input().split()) :
v = dl[:]
if(0 < i < n) :
v[i] = (v[i] + v[0]) % m
for j in range(n) :
v[j] = (v[j] + dl[j + 1]) % m
dl = v
print((dl[0] - 1) % m)
``` | output | 1 | 30,962 | 12 | 61,925 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of integers a_1, a_2, ..., a_k is called a good array if a_1 = k - 1 and a_1 > 0. For example, the sequences [3, -1, 44, 0], [1, -99] are good arrays, and the sequences [3, 7, 8], [2, 5, 4, 1], [0] — are not.
A sequence of integers is called good if it can be divided into a positive number of good arrays. Each good array should be a subsegment of sequence and each element of the sequence should belong to exactly one array. For example, the sequences [2, -3, 0, 1, 4], [1, 2, 3, -3, -9, 4] are good, and the sequences [2, -3, 0, 1], [1, 2, 3, -3 -9, 4, 1] — are not.
For a given sequence of numbers, count the number of its subsequences that are good sequences, and print the number of such subsequences modulo 998244353.
Input
The first line contains the number n~(1 ≤ n ≤ 10^3) — the length of the initial sequence. The following line contains n integers a_1, a_2, ..., a_n~(-10^9 ≤ a_i ≤ 10^9) — the sequence itself.
Output
In the single line output one integer — the number of subsequences of the original sequence that are good sequences, taken modulo 998244353.
Examples
Input
3
2 1 1
Output
2
Input
4
1 1 1 1
Output
7
Note
In the first test case, two good subsequences — [a_1, a_2, a_3] and [a_2, a_3].
In the second test case, seven good subsequences — [a_1, a_2, a_3, a_4], [a_1, a_2], [a_1, a_3], [a_1, a_4], [a_2, a_3], [a_2, a_4] and [a_3, a_4]. | instruction | 0 | 30,963 | 12 | 61,926 |
Tags: combinatorics, dp
Correct Solution:
```
def computeCnksMod(N, mod):
res = [[0] * (N+1) for i in range(N+1)]
res[0][0] = 1
for n in range(1, N+1):
res[n][0] = res[n-1][0]
for k in range(1, n+1):
res[n][k] = (res[n-1][k] + res[n-1][k-1]) % mod
return res
magic = 998244353
n = int(input()) + 1
aa = [1] + [int(s)+1 for s in input().split(' ')]
cnks = computeCnksMod(n, magic)
d = [0] * (n + 1)
d[n] = 1
for i in reversed(list(range(n))):
if i != 0 and aa[i] < 2:
continue
cur = 0
tosel = aa[i] - 1
for j in range(i + tosel + 1, n + 1):
avail = j - i - 1
cur = (cur + cnks[avail][tosel] * d[j]) % magic
d[i] = cur
print(d[0] - 1)
``` | output | 1 | 30,963 | 12 | 61,927 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.