message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given the following concurrent program. There are N processes and the i-th process has the following pseudocode:
repeat ni times
yi := y
y := yi + 1
end repeat
Here y is a shared variable. Everything else is local for the process. All actions on a given row are atomic, i.e. when the process starts executing a row it is never interrupted. Beyond that all interleavings are possible, i.e. every process that has yet work to do can be granted the rights to execute its next row. In the beginning y = 0. You will be given an integer W and ni, for i = 1, ... , N. Determine if it is possible that after all processes terminate, y = W, and if it is possible output an arbitrary schedule that will produce this final value.
Input
In the first line of the input you will be given two space separated integers N (1 ≤ N ≤ 100) and W ( - 109 ≤ W ≤ 109). In the second line there are N space separated integers ni (1 ≤ ni ≤ 1000).
Output
On the first line of the output write Yes if it is possible that at the end y = W, or No otherwise. If the answer is No then there is no second line, but if the answer is Yes, then on the second line output a space separated list of integers representing some schedule that leads to the desired result. For more information see note.
Examples
Input
1 10
11
Output
No
Input
2 3
4 4
Output
Yes
1 1 2 1 2 2 2 2 2 1 2 1 1 1 1 2
Input
3 6
1 2 3
Output
Yes
1 1 2 2 2 2 3 3 3 3 3 3
Note
For simplicity, assume that there is no repeat statement in the code of the processes, but the code from the loop is written the correct amount of times. The processes are numbered starting from 1. The list of integers represent which process works on its next instruction at a given step. For example, consider the schedule 1 2 2 1 3. First process 1 executes its first instruction, then process 2 executes its first two instructions, after that process 1 executes its second instruction, and finally process 3 executes its first instruction. The list must consists of exactly 2·Σ i = 1...N ni numbers. | instruction | 0 | 91,234 | 14 | 182,468 |
Tags: constructive algorithms
Correct Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n, w = map(int, input().split())
a = [0] + list(map(int, input().split()))
total = sum(a)
def ng():
print('No')
exit()
def ok(a):
print('Yes')
print(*a)
exit()
if w < 1 or total < w:
ng()
if n == 1:
if w == a[1]:
ok([1] * (a[1] * 2))
else:
ng()
if w == 1:
if min(a[1:]) > 1:
ng()
min_i = a.index(1)
ans = []
for i in range(1, n + 1):
if i == min_i:
continue
ans += [i] * (a[i] * 2)
ok([min_i] + ans + [min_i])
ans1, ans2, ans3 = [], [], []
w -= 2
if w:
for i in range(1, 3):
x = min(a[i] - 1, w)
w -= x
a[i] -= x
ans3 += [i] * (2 * x)
for i in range(3, n + 1):
x = min(a[i], w)
w -= x
a[i] -= x
ans3 += [i] * (2 * x)
ans1 = [2] * ((a[2] - 1) * 2)
for i in range(3, n + 1):
ans1 += [i] * (a[i] * 2)
ans1 = [1] + ans1 + [1]
a[1] -= 1
ans2 = [2] + [1] * (a[1] * 2) + [2]
if w == 0:
ok(ans1 + ans2 + ans3)
else:
ng()
``` | output | 1 | 91,234 | 14 | 182,469 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney wants to send her a cheesy text message and wants to make her as happy as possible.
<image>
Initially, happiness level of Nora is 0. Nora loves some pickup lines like "I'm falling for you" and stuff. Totally, she knows n pickup lines, each consisting only of lowercase English letters, also some of them may be equal (in writing, but different in pronouncing or meaning though). Every time Nora sees i-th pickup line as a consecutive subsequence of Barney's text message her happiness level increases by ai. These substrings may overlap, for example, Nora will see the pickup line aa twice and the pickup line ab once in text message aaab.
Due to texting app limits, Barney's text may have up to l characters.
Barney asked you to help him make Nora as much happy as possible, it's gonna be legen...
Input
The first line of input contains two integers n and l (1 ≤ n ≤ 200, 1 ≤ l ≤ 1014) — the number of pickup lines and the maximum length of Barney's text.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100), meaning that Nora's happiness level increases by ai after every time seeing i-th pickup line.
The next n lines contain the pickup lines. i-th of them contains a single string si consisting of only English lowercase letter. Summary length of all pickup lines does not exceed 200.
All strings are not empty.
Output
Print the only integer — the maximum possible value of Nora's happiness level after reading Barney's text.
Examples
Input
3 6
3 2 1
heart
earth
art
Output
6
Input
3 6
3 2 8
heart
earth
art
Output
16
Note
An optimal answer for the first sample case is hearth containing each pickup line exactly once.
An optimal answer for the second sample case is artart.
Submitted Solution:
```
import math
import sys
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
numberofpkl_n = list(invr())
numberofpkl = numberofpkl_n[0]
n = numberofpkl_n[1]
hits = invr()
hits = list(hits)
pickup_lines = list()
for times in range(numberofpkl):
thing = insr()
string = ""
for ch in thing:
string +=ch
pickup_lines.append(string)
# pickup_lines = ['heart', 'earth','art']
# hits = [3 ,2,1]
# print("pickup_lines", pickup_lines)
# len < n
# len(heart) < 6
def fitsInside(line, n):
if len(line) <= n:
return True
else:
return False
# short = list(filter(lambda line: fitsInside(line, n), pickup_lines))
pickline_hit = dict(zip(pickup_lines, hits))
# print("pickline_hit", pickline_hit)
# she is fond of pickup lines
# pickupline -> happy hits
def beginning_of(given):
length = len(given)
result = list()
for indx in range(1, length + 1):
result.append(given[:indx])
return result
def evaluate_hits(given,n):
result = pickline_hit[given]
# print(result, given,1)
# ends with beginning of another pick-up-line(complete another is not counted here)
for pickline in pickup_lines:
if pickline == given:
continue
beginnings = beginning_of(pickline)[:-1]
for string in beginnings:
if string == pickline:
continue
if given.endswith(string):
remaining_character_count = len(pickline) - len(string)
if remaining_character_count + len(given) <= n :
result += pickline_hit[pickline]
pickup_line_modified = given+pickline[len(string):]
# print(pickup_line_modified)
# complete another pick-up-line counted here
for pickline in pickup_lines:
if pickline != given and given.find(pickline) > -1:
result += pickline_hit[pickline]
# print(result, pickline,3)
#multiple of given
if n > len(given):
howmanyfits = int(math.trunc(n / len(given)))
result += (howmanyfits-1)*pickline_hit[given]
# print(result,given,4)
return result
n = 6
criteria = list()
for l in range(1,n+1)[::-1]:
for item in pickup_lines:
criteria.append((evaluate_hits(item,l),l,item) )
print( sorted(criteria,reverse=True)[0][0] )
``` | instruction | 0 | 91,328 | 14 | 182,656 |
No | output | 1 | 91,328 | 14 | 182,657 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney wants to send her a cheesy text message and wants to make her as happy as possible.
<image>
Initially, happiness level of Nora is 0. Nora loves some pickup lines like "I'm falling for you" and stuff. Totally, she knows n pickup lines, each consisting only of lowercase English letters, also some of them may be equal (in writing, but different in pronouncing or meaning though). Every time Nora sees i-th pickup line as a consecutive subsequence of Barney's text message her happiness level increases by ai. These substrings may overlap, for example, Nora will see the pickup line aa twice and the pickup line ab once in text message aaab.
Due to texting app limits, Barney's text may have up to l characters.
Barney asked you to help him make Nora as much happy as possible, it's gonna be legen...
Input
The first line of input contains two integers n and l (1 ≤ n ≤ 200, 1 ≤ l ≤ 1014) — the number of pickup lines and the maximum length of Barney's text.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100), meaning that Nora's happiness level increases by ai after every time seeing i-th pickup line.
The next n lines contain the pickup lines. i-th of them contains a single string si consisting of only English lowercase letter. Summary length of all pickup lines does not exceed 200.
All strings are not empty.
Output
Print the only integer — the maximum possible value of Nora's happiness level after reading Barney's text.
Examples
Input
3 6
3 2 1
heart
earth
art
Output
6
Input
3 6
3 2 8
heart
earth
art
Output
16
Note
An optimal answer for the first sample case is hearth containing each pickup line exactly once.
An optimal answer for the second sample case is artart.
Submitted Solution:
```
import math
import sys
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
numberofpkl_n = list(invr())
numberofpkl = numberofpkl_n[0]
n = numberofpkl_n[1]
hits = invr()
hits = list(hits)
pickup_lines = list()
for times in range(numberofpkl):
thing = insr()
string = ""
for ch in thing:
string +=ch
pickup_lines.append(string)
# pickup_lines = ['heart', 'earth','art']
# hits = [3 ,2,1]
# print("pickup_lines", pickup_lines)
# len < n
# len(heart) < 6
def fitsInside(line, n):
if len(line) <= n:
return True
else:
return False
# short = list(filter(lambda line: fitsInside(line, n), pickup_lines))
pickline_hit = dict(zip(pickup_lines, [0 for i in range(len(hits))]))
sorted_pickline_hit = sorted(list(zip(pickup_lines, hits)),reverse=True)
for pkline in list(set(pickup_lines)):
for pk_hit in sorted_pickline_hit:
if pk_hit[0] == pkline:
pickline_hit[pkline] += pk_hit[1]
print(pickline_hit)
# for pl in pickup_lines:
# print("pickline_hit", pickline_hit)
# she is fond of pickup lines
# pickupline -> happy hits
def beginning_of(given):
length = len(given)
result = list()
for indx in range(1, length + 1):
result.append(given[:indx])
return result
def evaluate_hits(given,n):
result = pickline_hit[given]
# print(result, given,1)
# ends with beginning of another pick-up-line(complete another is not counted here)
for pickline in pickup_lines:
if pickline == given:
continue
beginnings = beginning_of(pickline)[:-1]
for string in beginnings:
if string == pickline:
continue
if given.endswith(string):
remaining_character_count = len(pickline) - len(string)
if remaining_character_count + len(given) <= n :
result += pickline_hit[pickline]
pickup_line_modified = given+pickline[len(string):]
# print(pickup_line_modified)
# complete another pick-up-line counted here
for pickline in pickup_lines:
if pickline != given and given.find(pickline) > -1:
result += pickline_hit[pickline]
# print(result, pickline,3)
#multiple of given
if n > len(given):
howmanyfits = int(math.trunc(n / len(given)))
result += (howmanyfits-1)*pickline_hit[given]
# print(result,given,4)
return result
criteria = list()
for l in range(1,n+1)[::-1]:
for item in pickup_lines:
criteria.append((evaluate_hits(item,l),l,item) )
print( sorted(criteria,reverse=True) )
``` | instruction | 0 | 91,329 | 14 | 182,658 |
No | output | 1 | 91,329 | 14 | 182,659 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney wants to send her a cheesy text message and wants to make her as happy as possible.
<image>
Initially, happiness level of Nora is 0. Nora loves some pickup lines like "I'm falling for you" and stuff. Totally, she knows n pickup lines, each consisting only of lowercase English letters, also some of them may be equal (in writing, but different in pronouncing or meaning though). Every time Nora sees i-th pickup line as a consecutive subsequence of Barney's text message her happiness level increases by ai. These substrings may overlap, for example, Nora will see the pickup line aa twice and the pickup line ab once in text message aaab.
Due to texting app limits, Barney's text may have up to l characters.
Barney asked you to help him make Nora as much happy as possible, it's gonna be legen...
Input
The first line of input contains two integers n and l (1 ≤ n ≤ 200, 1 ≤ l ≤ 1014) — the number of pickup lines and the maximum length of Barney's text.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100), meaning that Nora's happiness level increases by ai after every time seeing i-th pickup line.
The next n lines contain the pickup lines. i-th of them contains a single string si consisting of only English lowercase letter. Summary length of all pickup lines does not exceed 200.
All strings are not empty.
Output
Print the only integer — the maximum possible value of Nora's happiness level after reading Barney's text.
Examples
Input
3 6
3 2 1
heart
earth
art
Output
6
Input
3 6
3 2 8
heart
earth
art
Output
16
Note
An optimal answer for the first sample case is hearth containing each pickup line exactly once.
An optimal answer for the second sample case is artart.
Submitted Solution:
```
import math
import sys
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
numberofpkl_n = list(invr())
numberofpkl = numberofpkl_n[0]
n = numberofpkl_n[1]
hits = invr()
hits = list(hits)
pickup_lines = list()
for times in range(numberofpkl):
thing = insr()
string = ""
for ch in thing:
string +=ch
pickup_lines.append(string)
# pickup_lines = ['heart', 'earth','art']
# hits = [3 ,2,1]
# print("pickup_lines", pickup_lines)
# len < n
# len(heart) < 6
def fitsInside(line, n):
if len(line) <= n:
return True
else:
return False
# short = list(filter(lambda line: fitsInside(line, n), pickup_lines))
pickline_hit = dict(zip(pickup_lines, hits))
# print("pickline_hit", pickline_hit)
# she is fond of pickup lines
# pickupline -> happy hits
def beginning_of(given):
length = len(given)
result = list()
for indx in range(1, length + 1):
result.append(given[:indx])
return result
def evaluate_hits(given,n):
result = pickline_hit[given]
# print(result, given,1)
# ends with beginning of another pick-up-line(complete another is not counted here)
for pickline in pickup_lines:
if pickline == given:
continue
beginnings = beginning_of(pickline)[:-1]
for string in beginnings:
if string == pickline:
continue
if given.endswith(string):
remaining_character_count = len(pickline) - len(string)
if remaining_character_count + len(given) <= n :
result += pickline_hit[pickline]
pickup_line_modified = given+pickline[len(string):]
# print(pickup_line_modified)
# complete another pick-up-line counted here
for pickline in pickup_lines:
if pickline != given and given.find(pickline) > -1:
result += pickline_hit[pickline]
# print(result, pickline,3)
#multiple of given
if n > len(given):
howmanyfits = int(math.trunc(n / len(given)))
result += (howmanyfits-1)*pickline_hit[given]
# print(result,given,4)
return result
criteria = list()
for l in range(1,n+1)[::-1]:
for item in pickup_lines:
criteria.append((evaluate_hits(item,l),l,item) )
print( sorted(criteria,reverse=True)[0][0] )
``` | instruction | 0 | 91,330 | 14 | 182,660 |
No | output | 1 | 91,330 | 14 | 182,661 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney wants to send her a cheesy text message and wants to make her as happy as possible.
<image>
Initially, happiness level of Nora is 0. Nora loves some pickup lines like "I'm falling for you" and stuff. Totally, she knows n pickup lines, each consisting only of lowercase English letters, also some of them may be equal (in writing, but different in pronouncing or meaning though). Every time Nora sees i-th pickup line as a consecutive subsequence of Barney's text message her happiness level increases by ai. These substrings may overlap, for example, Nora will see the pickup line aa twice and the pickup line ab once in text message aaab.
Due to texting app limits, Barney's text may have up to l characters.
Barney asked you to help him make Nora as much happy as possible, it's gonna be legen...
Input
The first line of input contains two integers n and l (1 ≤ n ≤ 200, 1 ≤ l ≤ 1014) — the number of pickup lines and the maximum length of Barney's text.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100), meaning that Nora's happiness level increases by ai after every time seeing i-th pickup line.
The next n lines contain the pickup lines. i-th of them contains a single string si consisting of only English lowercase letter. Summary length of all pickup lines does not exceed 200.
All strings are not empty.
Output
Print the only integer — the maximum possible value of Nora's happiness level after reading Barney's text.
Examples
Input
3 6
3 2 1
heart
earth
art
Output
6
Input
3 6
3 2 8
heart
earth
art
Output
16
Note
An optimal answer for the first sample case is hearth containing each pickup line exactly once.
An optimal answer for the second sample case is artart.
Submitted Solution:
```
import math
pickup_lines = ['heart', 'earth','art']
hits = [3 ,2,1]
# print("pickup_lines", pickup_lines)
# len < n
# len(heart) < 6
def fitsInside(line, n):
if len(line) <= n:
return True
else:
return False
# short = list(filter(lambda line: fitsInside(line, n), pickup_lines))
pickline_hit = dict(zip(pickup_lines, hits))
# print("pickline_hit", pickline_hit)
# she is fond of pickup lines
# pickupline -> happy hits
def beginning_of(given):
length = len(given)
result = list()
for indx in range(1, length + 1):
result.append(given[:indx])
return result
def evaluate_hits(given,n):
result = pickline_hit[given]
# print(result, given,1)
# ends with beginning of another pick-up-line(complete another is not counted here)
for pickline in pickup_lines:
if pickline == given:
continue
beginnings = beginning_of(pickline)[:-1]
for string in beginnings:
if string == pickline:
continue
if given.endswith(string):
remaining_character_count = len(pickline) - len(string)
if remaining_character_count + len(given) <= n :
result += pickline_hit[pickline]
pickup_line_modified = given+pickline[len(string):]
# print(pickup_line_modified)
# complete another pick-up-line counted here
for pickline in pickup_lines:
if pickline != given and given.find(pickline) > -1:
result += pickline_hit[pickline]
# print(result, pickline,3)
#multiple of given
if n > len(given):
howmanyfits = int(math.trunc(n / len(given)))
result += (howmanyfits-1)*pickline_hit[given]
# print(result,given,4)
return result
n = 6
criteria = list()
for l in range(1,n+1)[::-1]:
for item in pickup_lines:
criteria.append((evaluate_hits(item,l),l,item) )
print( sorted(criteria,reverse=True)[0][0] )
``` | instruction | 0 | 91,331 | 14 | 182,662 |
No | output | 1 | 91,331 | 14 | 182,663 |
Provide a correct Python 3 solution for this coding contest problem.
Problem statement
AOR Ika-chan is in a bad mood lately. Apparently, I don't like the ratio of the number of followers to the number of followers of "Ikatta". Currently, AOR Ika-chan has $ A $ followers, $ B $ followers, and a ratio of $ A: B $.
Therefore, AOR Ika decided to increase or decrease the number of followers so that the ratio of the number of followers to the number of followers would be an integer ratio that she liked. The integer ratio I like is a ratio that can be expressed so that both values included in the ratio are integers of $ 1 $ or more and $ N $ or less.
However, AOR Ika doesn't want to change the number of followers as much as possible, so I want to make the absolute value of the difference from before the change as small as possible. Create a program that asks at least how many followers you need to change to make AOR Ika feel good.
Input constraints
$ 1 \ le A, \ B \ le 10 ^ {12} $
$ 1 \ leq N \ leq 100 $
sample
Sample input 1
19 30 3
Sample output 1
1
Sample input 2
3 7 7
Sample output 2
0
Sample input 3
3 7 1
Sample output 3
Four
Sample input 4
102 30 3
Sample output 4
12
By reducing the number of followers by $ 12 $, it becomes $ 90: 30 \ (= 3: 1) $, and the higher ratio number is $ 3 $ or less.
At this time, the amount of change is $ 12 $.
Sample input 5
3 4 2
Sample output 5
1
If you unfollow one person, it will be $ 2: 4 \ (= 1: 2) $, and if you follow it, it will be $ 4: 4 \ (= 1: 1) $. In both cases, the absolute value of increase / decrease is $ 1 $, which is the answer.
Sample input 6
1 100 2
Sample output 6
49
Please note that at least $ 1 $ people must be following.
input
$ A \ B \ N $
output
Output the minimum absolute value of the amount of change in $ A $, which can be an integer ratio you like.
Example
Input
19 30 3
Output
1 | instruction | 0 | 91,704 | 14 | 183,408 |
"Correct Solution:
```
a, b, n = map(int, input().split())
score = abs(a - b)
for x in range(1, n + 1):
for y in range(1, n + 1):
if b * x % y == 0:
new_a = b * x // y
score = min(score, abs(a - new_a))
print(score)
``` | output | 1 | 91,704 | 14 | 183,409 |
Provide a correct Python 3 solution for this coding contest problem.
Problem statement
AOR Ika-chan is in a bad mood lately. Apparently, I don't like the ratio of the number of followers to the number of followers of "Ikatta". Currently, AOR Ika-chan has $ A $ followers, $ B $ followers, and a ratio of $ A: B $.
Therefore, AOR Ika decided to increase or decrease the number of followers so that the ratio of the number of followers to the number of followers would be an integer ratio that she liked. The integer ratio I like is a ratio that can be expressed so that both values included in the ratio are integers of $ 1 $ or more and $ N $ or less.
However, AOR Ika doesn't want to change the number of followers as much as possible, so I want to make the absolute value of the difference from before the change as small as possible. Create a program that asks at least how many followers you need to change to make AOR Ika feel good.
Input constraints
$ 1 \ le A, \ B \ le 10 ^ {12} $
$ 1 \ leq N \ leq 100 $
sample
Sample input 1
19 30 3
Sample output 1
1
Sample input 2
3 7 7
Sample output 2
0
Sample input 3
3 7 1
Sample output 3
Four
Sample input 4
102 30 3
Sample output 4
12
By reducing the number of followers by $ 12 $, it becomes $ 90: 30 \ (= 3: 1) $, and the higher ratio number is $ 3 $ or less.
At this time, the amount of change is $ 12 $.
Sample input 5
3 4 2
Sample output 5
1
If you unfollow one person, it will be $ 2: 4 \ (= 1: 2) $, and if you follow it, it will be $ 4: 4 \ (= 1: 1) $. In both cases, the absolute value of increase / decrease is $ 1 $, which is the answer.
Sample input 6
1 100 2
Sample output 6
49
Please note that at least $ 1 $ people must be following.
input
$ A \ B \ N $
output
Output the minimum absolute value of the amount of change in $ A $, which can be an integer ratio you like.
Example
Input
19 30 3
Output
1 | instruction | 0 | 91,705 | 14 | 183,410 |
"Correct Solution:
```
#!/usr/bin/env python3
INF = 2 ** 63 - 1
def solve(a, b, n):
# a + x = k * d, b = l * g
# 1 <= k <= n, 1 <= l <= n
x = INF
for l in range(1, n + 1):
d, m = divmod(b, l)
if not m:
for k in range(1, n + 1):
x = min(x, abs(k * d - a))
return x
def main():
a, b, n = map(int, input().split())
print(solve(a, b, n))
if __name__ == '__main__':
main()
``` | output | 1 | 91,705 | 14 | 183,411 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Euler is a little, cute squirrel. When the autumn comes, he collects some reserves for winter. The interesting fact is that Euler likes to collect acorns in a specific way. A tree can be described as n acorns connected by n - 1 branches, such that there is exactly one way between each pair of acorns. Let's enumerate the acorns from 1 to n.
The squirrel chooses one acorn (not necessary with number 1) as a start, and visits them in a way called "Euler tour" (see notes), collecting each acorn when he visits it for the last time.
Today morning Kate was observing Euler. She took a sheet of paper and wrote down consecutive indices of acorns on his path. Unfortunately, during her way to home it started raining and some of numbers became illegible. Now the girl is very sad, because she has to present the observations to her teacher.
"Maybe if I guess the lacking numbers, I'll be able to do it!" she thought. Help her and restore any valid Euler tour of some tree or tell that she must have made a mistake.
Input
The first line contains a single integer n (1 ≤ n ≤ 5 ⋅ 10^5), denoting the number of acorns in the tree.
The second line contains 2n - 1 integers a_1, a_2, …, a_{2n-1} (0 ≤ a_i ≤ n) — the Euler tour of the tree that Kate wrote down. 0 means an illegible number, which has to be guessed.
Output
If there is no Euler tour satisfying the given description, output "no" in the first line.
Otherwise, on the first line output "yes", and then in the second line print the Euler tour which satisfy the given description.
Any valid Euler tour will be accepted, since the teacher doesn't know how exactly the initial tree looks.
Examples
Input
2
1 0 0
Output
yes
1 2 1
Input
4
1 0 3 2 0 0 0
Output
yes
1 4 3 2 3 4 1
Input
5
0 1 2 3 4 1 0 0 0
Output
no
Note
An Euler tour of a tree with n acorns is a sequence of 2n - 1 indices of acorns. such that each acorn occurs at least once, the first and the last acorns are same and each two consecutive acorns are directly connected with a branch.
Submitted Solution:
```
st = list(input())
posl = ''
def findOp(s,b):
p = ''
for i in s:
if((i == '/')|(i == '*')):
if(b):
p = '( ' + p +' '+ i +' )'
else:
p += ' ' + i
for i in s:
if((i == '+')|(i == '-')):
if(b):
p = '(' + p +' '+ i +' )'
else:
p += ' ' + i
return p
while (st.count(')') != 0):
inR = st.index(')')
for i in range(len(st)-1-inR , 0, -1):
if(st[i] == '('):
inL = i
posl += findOp(st[inL:inR],True)
st = st[:inL] + st[inR+1:]
posl += findOp(st,False)
print(posl)
``` | instruction | 0 | 91,738 | 14 | 183,476 |
No | output | 1 | 91,738 | 14 | 183,477 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Euler is a little, cute squirrel. When the autumn comes, he collects some reserves for winter. The interesting fact is that Euler likes to collect acorns in a specific way. A tree can be described as n acorns connected by n - 1 branches, such that there is exactly one way between each pair of acorns. Let's enumerate the acorns from 1 to n.
The squirrel chooses one acorn (not necessary with number 1) as a start, and visits them in a way called "Euler tour" (see notes), collecting each acorn when he visits it for the last time.
Today morning Kate was observing Euler. She took a sheet of paper and wrote down consecutive indices of acorns on his path. Unfortunately, during her way to home it started raining and some of numbers became illegible. Now the girl is very sad, because she has to present the observations to her teacher.
"Maybe if I guess the lacking numbers, I'll be able to do it!" she thought. Help her and restore any valid Euler tour of some tree or tell that she must have made a mistake.
Input
The first line contains a single integer n (1 ≤ n ≤ 5 ⋅ 10^5), denoting the number of acorns in the tree.
The second line contains 2n - 1 integers a_1, a_2, …, a_{2n-1} (0 ≤ a_i ≤ n) — the Euler tour of the tree that Kate wrote down. 0 means an illegible number, which has to be guessed.
Output
If there is no Euler tour satisfying the given description, output "no" in the first line.
Otherwise, on the first line output "yes", and then in the second line print the Euler tour which satisfy the given description.
Any valid Euler tour will be accepted, since the teacher doesn't know how exactly the initial tree looks.
Examples
Input
2
1 0 0
Output
yes
1 2 1
Input
4
1 0 3 2 0 0 0
Output
yes
1 4 3 2 3 4 1
Input
5
0 1 2 3 4 1 0 0 0
Output
no
Note
An Euler tour of a tree with n acorns is a sequence of 2n - 1 indices of acorns. such that each acorn occurs at least once, the first and the last acorns are same and each two consecutive acorns are directly connected with a branch.
Submitted Solution:
```
import itertools as itt
n = int(input())
s = input().replace(' ', '')
s = list(map(int, s))
l = len(s)
res = ''
for i in itt.permutations(range(1, n + 1), n):
t = list(i)
t = t[::-1][:-1] + t + t[::-1][1::]
p = 0
res = ''
for j in t:
if s[p] in (0, j) and p <= l-1:
p += 1
res += str(j)
if p == l:
print('yes')
print(res)
break
if p < l:
print('no')
``` | instruction | 0 | 91,739 | 14 | 183,478 |
No | output | 1 | 91,739 | 14 | 183,479 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2. | instruction | 0 | 91,828 | 14 | 183,656 |
Tags: data structures, probabilities
Correct Solution:
```
MOD = 998244353
def power(x,y):
res=1
while(y>0):
if(y&1):
res = x*res%MOD
y = y >>1
x = x*x%MOD
return res
n = int(input())
prob = input().split()
p = power(100,n)
q = 1
for i in range(0,n-1):
q = q*(int(prob[i]))%MOD
p = (p + q*(power(100,n-i-1))%MOD)%MOD
q = q*(int(prob[n-1]))%MOD
print(p*(power(q,MOD-2))%MOD)
``` | output | 1 | 91,828 | 14 | 183,657 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2. | instruction | 0 | 91,829 | 14 | 183,658 |
Tags: data structures, probabilities
Correct Solution:
```
#q = int(input())
#x, y = map(int,input().split(' '))
#print (' '.join(list(map(str, s))))
def extended_gcd(aa, bb):
lastremainder, remainder = abs(aa), abs(bb)
x, lastx, y, lasty = 0, 1, 1, 0
while remainder:
lastremainder, (quotient, remainder) = remainder, divmod(lastremainder, remainder)
x, lastx = lastx - quotient*x, x
y, lasty = lasty - quotient*y, y
return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1)
def modinv(a, m):
g, x, y = extended_gcd(a, m)
if g != 1:
raise ValueError
return x % m
m = 998244353
n = int(input())
p = list(map(int,input().split(' ')))
up = 0
low = 1
for i in range(n):
up = up + low
up = up * 100 % m
low = low * p[i] % m
print (up*modinv(low,m)%m)
``` | output | 1 | 91,829 | 14 | 183,659 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2. | instruction | 0 | 91,830 | 14 | 183,660 |
Tags: data structures, probabilities
Correct Solution:
```
def main():
m = 998244353
n = int(input())
pp = map(int,input().split())
probb = 100
num = 0
for i,p in enumerate(pp,1):
probu = ((100-p) * probb) % m
probb = (p * probb) % m
num = (num*100 + i*probu) % m
num = (num + n*probb) % m
print(( num * pow(probb, m-2, m) ) % m )
if __name__ == "__main__":
main()
``` | output | 1 | 91,830 | 14 | 183,661 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2. | instruction | 0 | 91,831 | 14 | 183,662 |
Tags: data structures, probabilities
Correct Solution:
```
input();p,f=998244353,0
for i in map(int,input().split()):f=100*(f+1)*pow(i,p-2,p)%p
print(f)
``` | output | 1 | 91,831 | 14 | 183,663 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2. | instruction | 0 | 91,832 | 14 | 183,664 |
Tags: data structures, probabilities
Correct Solution:
```
n=int(input())
x=list(map(int,input().split()))
s,dp,mod=0,1,998244353
for i in range(n):
if i>0:
s=(s*100)%mod
s=(s+(100-x[i])*dp*(i+1))%mod
if i==n-1:
s=(s+x[i]*dp*(i+1))%mod
dp=(dp*x[i])%mod
ans=(s*pow(dp,mod-2,mod))%mod
print(ans)
``` | output | 1 | 91,832 | 14 | 183,665 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2. | instruction | 0 | 91,833 | 14 | 183,666 |
Tags: data structures, probabilities
Correct Solution:
```
N = int(input())
P = list(map(int, input().split()))
mod = 998244353
p = 0
q = 1
for i in range(N):
p, q = (100 * (p+q)) % mod, (P[i] * q) % mod
print((p * pow(q, mod-2, mod))%mod)
``` | output | 1 | 91,833 | 14 | 183,667 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2. | instruction | 0 | 91,834 | 14 | 183,668 |
Tags: data structures, probabilities
Correct Solution:
```
M = 998244353
def gcd(a, b):
if (a == 0):
return 0, 1
x, y = gcd(b % a, a)
return y - (b // a) * x, x
k = input()
probs = list(map(int, input().split(' ')))
num, denum = 0, 1
for p in probs:
num = (num + denum) * 100 % M
denum = denum * p % M
inv, _ = gcd(denum, M)
print(num * inv % M)
``` | output | 1 | 91,834 | 14 | 183,669 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2. | instruction | 0 | 91,835 | 14 | 183,670 |
Tags: data structures, probabilities
Correct Solution:
```
import sys
readline = sys.stdin.readline
from itertools import accumulate
from collections import Counter
from bisect import bisect as br, bisect_left as bl
class PMS:
#1-indexed
def __init__(self, A, B, issum = False):
#Aに初期状態の要素をすべて入れる,Bは値域のリスト
self.X, self.comp = self.compress(B)
self.size = len(self.X)
self.tree = [0] * (self.size + 1)
self.p = 2**(self.size.bit_length() - 1)
self.dep = self.size.bit_length()
CA = Counter(A)
S = [0] + list(accumulate([CA[self.X[i]] for i in range(self.size)]))
for i in range(1, 1+self.size):
self.tree[i] = S[i] - S[i - (i&-i)]
if issum:
self.sumtree = [0] * (self.size + 1)
Ssum = [0] + list(accumulate([CA[self.X[i]]*self.X[i] for i in range(self.size)]))
for i in range(1, 1+self.size):
self.sumtree[i] = Ssum[i] - Ssum[i - (i&-i)]
def compress(self, L):
#座圧
L2 = list(set(L))
L2.sort()
C = {v : k for k, v in enumerate(L2, 1)}
# 1-indexed
return L2, C
def leng(self):
#今入っている個数を取得
return self.count(self.X[-1])
def count(self, v):
#v(Bの元)以下の個数を取得
i = self.comp[v]
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def less(self, v):
#v(Bの元である必要はない)未満の個数を取得
i = bl(self.X, v)
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def leq(self, v):
#v(Bの元である必要はない)以下の個数を取得
i = br(self.X, v)
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, v, x):
#vをx個入れる,負のxで取り出す,iの個数以上取り出すとエラーを出さずにバグる
i = self.comp[v]
while i <= self.size:
self.tree[i] += x
i += i & -i
def get(self, i):
# i番目の値を取得
if i <= 0:
return -1
s = 0
k = self.p
for _ in range(self.dep):
if s + k <= self.size and self.tree[s+k] < i:
s += k
i -= self.tree[s]
k //= 2
return self.X[s]
def gets(self, v):
#累積和がv以下となる最大のindexを返す
v1 = v
s = 0
k = self.p
for _ in range(self.dep):
if s + k <= self.size and self.sumtree[s+k] < v:
s += k
v -= self.sumtree[s]
k //= 2
if s == self.size:
return self.leng()
return self.count(self.X[s]) + (v1 - self.countsum(self.X[s]))//self.X[s]
def addsum(self, i, x):
#sumを扱いたいときにaddの代わりに使う
self.add(i, x)
x *= i
i = self.comp[i]
while i <= self.size:
self.sumtree[i] += x
i += i & -i
def countsum(self, v):
#v(Bの元)以下のsumを取得
i = self.comp[v]
s = 0
while i > 0:
s += self.sumtree[i]
i -= i & -i
return s
def getsum(self, i):
#i番目までのsumを取得
x = self.get(i)
return self.countsum(x) - x*(self.count(x) - i)
N, Q = map(int, readline().split())
P = list(map(int, readline().split()))
MOD = 998244353
T = [100*pow(pi, MOD-2, MOD)%MOD for pi in P]
AT = [None]*N
AT[0] = T[0]
for i in range(1, N):
AT[i] = (AT[i-1]+1)*T[i]%MOD
AM = [None]*N
AMi = [None]*N
AM[0] = T[0]
for i in range(1, N):
AM[i] = AM[i-1]*T[i]%MOD
AMi[N-1] = pow(AM[N-1], MOD-2, MOD)
for i in range(N-2, -1, -1):
AMi[i] = AMi[i+1]*T[i+1]%MOD
AT += [0]
AM += [1]
AMi += [1]
Ans = [None]*Q
kk = set([0, N])
PM = PMS([0, N], list(range(N+1)))
ans = AT[N-1]
for qu in range(Q):
f = int(readline()) - 1
if f not in kk:
kk.add(f)
PM.add(f, 1)
fidx = PM.count(f)
fm = PM.get(fidx-1)
fp = PM.get(fidx+1)
am = (AT[f-1] - AM[f-1]*AMi[fm-1]*AT[fm-1])%MOD
ap = (AT[fp-1] - AM[fp-1]*AMi[f-1]*AT[f-1])%MOD
aa = (AT[fp-1] - AM[fp-1]*AMi[fm-1]*AT[fm-1])%MOD
ans = (ans - aa + am + ap)%MOD
else:
kk.remove(f)
fidx = PM.count(f)
fm = PM.get(fidx-1)
fp = PM.get(fidx+1)
PM.add(f, -1)
am = (AT[f-1] - AM[f-1]*AMi[fm-1]*AT[fm-1])%MOD
ap = (AT[fp-1] - AM[fp-1]*AMi[f-1]*AT[f-1])%MOD
aa = (AT[fp-1] - AM[fp-1]*AMi[fm-1]*AT[fm-1])%MOD
ans = (ans + aa - am - ap)%MOD
Ans[qu] = ans
print('\n'.join(map(str, Ans)))
``` | output | 1 | 91,835 | 14 | 183,671 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2.
Submitted Solution:
```
import sys
import math
from collections import defaultdict
from collections import deque
from itertools import combinations
from itertools import permutations
input = lambda : sys.stdin.readline().rstrip()
read = lambda : list(map(int, input().split()))
def write(*args, sep="\n"):
for i in args:
sys.stdout.write("{}{}".format(i, sep))
INF = float('inf')
MOD = 998244353
YES = "YES"
NO = "NO"
n = int(input())
p = [0] + read()
p = list(map(lambda x : x*pow(100, MOD-2, MOD)%MOD, p))
A = [0] * (n+2)
for i in range(2, n+2):
A[i] = (A[i-1] - 1) * pow(p[i-1], MOD-2, MOD) % MOD
print((-A[-1] + MOD) % MOD)
``` | instruction | 0 | 91,836 | 14 | 183,672 |
Yes | output | 1 | 91,836 | 14 | 183,673 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2.
Submitted Solution:
```
mod = 998244353
def pow_(x, p, mod):
if p == 1: return x % mod
tmp = pow_(x, p // 2, mod)
if p % 2 == 0:
return (tmp * tmp) % mod
else:
return (tmp * tmp * x) % mod
def reverse(x, mod):
return pow_(x, mod-2, mod)
n = int(input()) + 1
p = [0] + list(map(int, input().split()))
dp = [0] * n
rev = [0] * 101
for i in range(1, 101):
rev[i] = reverse(i, mod)
for i in range(1, n):
dp[i]=(((dp[i-1]+1)*100 * rev[p[i]])) % mod
print(dp[-1])
``` | instruction | 0 | 91,837 | 14 | 183,674 |
Yes | output | 1 | 91,837 | 14 | 183,675 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2.
Submitted Solution:
```
# 解説AC
mod = 998244353
n = int(input())
p = list(map(int, input().split()))
i100 = pow(100, mod-2, mod)
p = [i*i100%mod for i in p]
top = 1
for i in range(n-2, -1, -1):
top *= p[i]
top += 1
top %= mod
bot = 1
for i in range(n):
bot *= p[i]
bot %= mod
ans = top*pow(bot, mod-2, mod)%mod
print(ans)
``` | instruction | 0 | 91,838 | 14 | 183,676 |
Yes | output | 1 | 91,838 | 14 | 183,677 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2.
Submitted Solution:
```
import sys
MOD = 998244353
def powmod(a, b):
if b == 0: return 1
res = powmod(a, b // 2)
res *= res
if b & 1:
res *= a
return res%MOD
def inv(n):
return powmod(n, MOD - 2)
_100 = inv(100)
n = int(input())
p = list(map(lambda x: int(x) * _100 % MOD, input().split()))
if n == 1:
print(inv(p[0]))
sys.exit(0)
pp = [0] * n
pp[0] = p[0]
for i in range(1, n):
pp[i] = pp[i - 1] * p[i] % MOD
k = 0
q = inv(pp[n - 2])
for i in range(0, n - 1):
c = q
if i:
c *= pp[i - 1]
c %= MOD
k += c
if k>MOD:k -= MOD
res = (k + 1) * inv(p[-1])
res %= MOD
print(res)
``` | instruction | 0 | 91,839 | 14 | 183,678 |
Yes | output | 1 | 91,839 | 14 | 183,679 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2.
Submitted Solution:
```
n=int(input())
numbers = [100/int(num) for num in input().split()]
result=0
for num in numbers:
if num==1:
result+=1
break
result+=result*(num-1)+num
print(int(result)%998244353)
``` | instruction | 0 | 91,840 | 14 | 183,680 |
No | output | 1 | 91,840 | 14 | 183,681 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2.
Submitted Solution:
```
n = int(input())
ret = 0
inputs = input().split()
for i in inputs:
b = int(i)
b = 100/b
ret*=b
ret+=b
print(int(ret)%998244353)
``` | instruction | 0 | 91,841 | 14 | 183,682 |
No | output | 1 | 91,841 | 14 | 183,683 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2.
Submitted Solution:
```
def phiEuler(n):
return n-1
def powerMo(a,n,mo):
ans = 1
posi = a
while n > 0:
if n&1:
ans *= posi
ans %= mo
n >>= 1
posi = posi*posi%mo
return ans
def modInv(a,mo):
# return a's mod inv
phi = phiEuler(mo)
return(powerMo(a,phi-1,mo))
n = int(input())
P = [int(s) for s in input().split()]
mo = 998244353
y = 1
for p in P:
y *= p
y %= mo
yInv = modInv(y, mo)
stop = [0]*n
rem = 1
ele = modInv(100, mo)
for i in range(n):
stop[i] = rem*(100-P[i])
stop[i] %= mo
rem = rem*P[i]*ele
rem %= mo
sum = 0
for i,days in enumerate(stop):
sum += days*(i+1)
sum += n
print(sum)
``` | instruction | 0 | 91,842 | 14 | 183,684 |
No | output | 1 | 91,842 | 14 | 183,685 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability (p_i)/(100) for all 1 ≤ i ≤ n.
Some mirrors are called checkpoints. Initially, only the 1st mirror is a checkpoint. It remains a checkpoint all the time.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
* The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
* In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to i.
There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given q queries, each query is represented by an integer u: If the u-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the u-th mirror is no longer a checkpoint.
After each query, you need to calculate [the expected number](https://en.wikipedia.org/wiki/Expected_value) of days until Creatnx becomes happy.
Each of this numbers should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
Input
The first line contains two integers n, q (2 ≤ n, q ≤ 2 ⋅ 10^5) — the number of mirrors and queries.
The second line contains n integers: p_1, p_2, …, p_n (1 ≤ p_i ≤ 100).
Each of q following lines contains a single integer u (2 ≤ u ≤ n) — next query.
Output
Print q numbers – the answers after each query by modulo 998244353.
Examples
Input
2 2
50 50
2
2
Output
4
6
Input
5 5
10 20 30 40 50
2
3
4
5
3
Output
117
665496274
332748143
831870317
499122211
Note
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to 1/2. So, the expected number of days, until one mirror will say, that he is beautiful is equal to 2 and the answer will be equal to 4 = 2 + 2.
Submitted Solution:
```
n = int(input())
ret = 0
inputs = input().split()
for i in inputs:
b = int(i)
b = 100/b
ret*=b
ret+=b
print(int(ret))
``` | instruction | 0 | 91,843 | 14 | 183,686 |
No | output | 1 | 91,843 | 14 | 183,687 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1 | instruction | 0 | 92,005 | 14 | 184,010 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n = int(input())
before = list(map(int, input().split()))
after = list(map(int, input().split()))
permutation = n * [ None ]
for i in range(n):
permutation[before[i] - 1] = after[i]
print(' '.join(map(str, permutation)))
``` | output | 1 | 92,005 | 14 | 184,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1 | instruction | 0 | 92,006 | 14 | 184,012 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
x = int(input())
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
bestFriends = {}
for i,j in zip(a,b):
bestFriends[i] = j
for i in range(x):
print(bestFriends[i+1],end=" ")
``` | output | 1 | 92,006 | 14 | 184,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1 | instruction | 0 | 92,007 | 14 | 184,014 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
l=[0]*(n+1)
for i in range(n):
l[a[i]]=b[i]
#print(l)
print(*(l[1:]))
``` | output | 1 | 92,007 | 14 | 184,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1 | instruction | 0 | 92,008 | 14 | 184,016 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
from sys import stdin, stdout
n = int(stdin.readline())
ans = [0 for i in range(n + 1)]
a, b = list(map(int, stdin.readline().split())), list(map(int, stdin.readline().split()))
for i in range(n):
ans[a[i]] = b[i]
stdout.write(' '.join(list(map(str, ans[1:]))))
``` | output | 1 | 92,008 | 14 | 184,017 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1 | instruction | 0 | 92,009 | 14 | 184,018 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
import sys
def solve():
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
indexof = [0] * (n + 1)
for i in range(n): indexof[a[i]] = i
res = [0] * n
for i in range(n):
res[i] = indexof[b[i]] + 1
return ' '.join(map(str, res))
if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
print(solve())
``` | output | 1 | 92,009 | 14 | 184,019 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1 | instruction | 0 | 92,010 | 14 | 184,020 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n, a, b = int(input()), list(map(int, input().split())), list(map(int, input().split()))
p = [0] * (n+1)
for i in range(n):
p[a[i]] = b[i]
print(" ".join(map(str, p[1:])))
``` | output | 1 | 92,010 | 14 | 184,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1 | instruction | 0 | 92,011 | 14 | 184,022 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
def main():
n = int(input())
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
p = [i for i in range(0, n + 1)]
for i in range(n):
p[a[i]] = b[i]
print(' '.join(list(map(str, p[1:]))))
return 0
if __name__ == "__main__":
main()
``` | output | 1 | 92,011 | 14 | 184,023 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1 | instruction | 0 | 92,012 | 14 | 184,024 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
p = [0]*n
for i,a1 in enumerate(a):
p[a1-1] = b[i]
for x in p:
print(x," " ,end='',)
print()
``` | output | 1 | 92,012 | 14 | 184,025 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1
Submitted Solution:
```
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
dic = {}
for i in range(0,len(a)):
dic[a[i]] = i
pic = []
for i in b:
pic.append(dic[i]+1)
for i in pic:
print(i,end=" ")
``` | instruction | 0 | 92,013 | 14 | 184,026 |
Yes | output | 1 | 92,013 | 14 | 184,027 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1
Submitted Solution:
```
x = input()
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
bestFriends = {}
for i,j in zip(a,b):
bestFriends[i] = j
for i in range(len(a)):
print(bestFriends[i+1],end=" ")
``` | instruction | 0 | 92,014 | 14 | 184,028 |
Yes | output | 1 | 92,014 | 14 | 184,029 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1
Submitted Solution:
```
n = int(input())
a, b = list(map(int, input().split())), list(map(int, input().split()))
p, q = [0] * (n + 1), [0] * n
for i, j in enumerate(a, 1): p[j] = i
for i, j in enumerate(b): q[i] = str(p[j])
print(' '.join(q))
``` | instruction | 0 | 92,015 | 14 | 184,030 |
Yes | output | 1 | 92,015 | 14 | 184,031 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really "special individuals" for who i = p[i].
Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm:
* on the first day of revising each student studies his own Mathematical Analysis notes,
* in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend.
Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.
You are given two sequences that describe the situation on the third and fourth days of revising:
* a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising;
* b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising.
You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).
Output
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.
Examples
Input
4
2 1 4 3
3 4 2 1
Output
4 3 1 2
Input
5
5 2 3 1 4
1 3 2 4 5
Output
4 3 2 5 1
Input
2
1 2
2 1
Output
2 1
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(zip(a,b))
c=sorted(c,key=lambda x:(x[0],x[1]))
for i in range(n):
print(c[i][1],end=" ")
``` | instruction | 0 | 92,016 | 14 | 184,032 |
Yes | output | 1 | 92,016 | 14 | 184,033 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names.
There are n students in a summer school. Teachers chose exactly n pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym b to a student with name a as the length of the largest common prefix a and b. We will represent such value as <image>. Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students.
Find the matching between students and pseudonyms with the maximum quality.
Input
The first line contains number n (1 ≤ n ≤ 100 000) — the number of students in the summer school.
Next n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.
The last n lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating.
The total length of all the names and pseudonyms doesn't exceed 800 000 characters.
Output
In the first line print the maximum possible quality of matching pseudonyms to students.
In the next n lines describe the optimal matching. Each line must have the form a b (1 ≤ a, b ≤ n), that means that the student who was number a in the input, must match to the pseudonym number b in the input.
The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.
Examples
Input
5
gennady
galya
boris
bill
toshik
bilbo
torin
gendalf
smaug
galadriel
Output
11
4 1
2 5
1 3
5 2
3 4
Note
The first test from the statement the match looks as follows:
* bill → bilbo (lcp = 3)
* galya → galadriel (lcp = 3)
* gennady → gendalf (lcp = 3)
* toshik → torin (lcp = 2)
* boris → smaug (lcp = 0) | instruction | 0 | 92,147 | 14 | 184,294 |
Tags: dfs and similar, strings, trees
Correct Solution:
```
import sys
class Node:
def __init__(self, d):
global nodes
self.ch = {}
self.a = [[], []]
self.d = d
nodes += [self]
nodes = []
pairs = []
res = 0
N = int(sys.stdin.readline())
_input = sys.stdin.readlines()
_input = [s[:-1] for s in _input]
A = [_input[:N], _input[N:]]
T = Node(0)
for i, l in enumerate(A):
for j, s in enumerate(l):
t = T
for c in s:
v = ord(c) - ord('a')
if not v in t.ch:
t.ch[v] = Node(t.d + 1)
t = t.ch[v]
t.a[i] += [j + 1]
for n in reversed(nodes):
for i in n.ch:
n.a[0] += n.ch[i].a[0]
n.a[1] += n.ch[i].a[1]
k = min(len(n.a[0]), len(n.a[1]))
for i in range(k):
pairs += [str(n.a[0][-1]) + ' ' + str(n.a[1][-1])]
n.a[0].pop()
n.a[1].pop()
res += n.d
print(res)
print('\n'.join(pairs))
``` | output | 1 | 92,147 | 14 | 184,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names.
There are n students in a summer school. Teachers chose exactly n pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym b to a student with name a as the length of the largest common prefix a and b. We will represent such value as <image>. Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students.
Find the matching between students and pseudonyms with the maximum quality.
Input
The first line contains number n (1 ≤ n ≤ 100 000) — the number of students in the summer school.
Next n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.
The last n lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating.
The total length of all the names and pseudonyms doesn't exceed 800 000 characters.
Output
In the first line print the maximum possible quality of matching pseudonyms to students.
In the next n lines describe the optimal matching. Each line must have the form a b (1 ≤ a, b ≤ n), that means that the student who was number a in the input, must match to the pseudonym number b in the input.
The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.
Examples
Input
5
gennady
galya
boris
bill
toshik
bilbo
torin
gendalf
smaug
galadriel
Output
11
4 1
2 5
1 3
5 2
3 4
Note
The first test from the statement the match looks as follows:
* bill → bilbo (lcp = 3)
* galya → galadriel (lcp = 3)
* gennady → gendalf (lcp = 3)
* toshik → torin (lcp = 2)
* boris → smaug (lcp = 0) | instruction | 0 | 92,148 | 14 | 184,296 |
Tags: dfs and similar, strings, trees
Correct Solution:
```
import sys
SIGMA = 26
nodes = []
pairs = []
res = 0
class Node:
def __init__(self):
self.ch = {}
self.a = []
self.b = []
self.d = 0
def add(self, s, i):
t = self
for c in s:
v = ord(c) - ord('a')
if not v in t.ch:
t.ch[v] = Node()
t.ch[v].d = t.d + 1
nodes.append(t.ch[v])
t = t.ch[v]
t.a.append(i)
def inc(self, s, i):
t = self
for c in s:
v = ord(c) - ord('a')
if not v in t.ch:
break
t = t.ch[v]
t.b.append(i)
def solve(self):
global pairs
global res
for i in range(SIGMA):
if i in self.ch:
self.a.extend(self.ch[i].a)
self.b.extend(self.ch[i].b)
k = min(len(self.a), len(self.b))
for i in range(k):
pairs.append(str(self.a[-1]) + ' ' + str(self.b[-1]))
self.a.pop()
self.b.pop()
res += self.d
return res
sys.setrecursionlimit(2000000)
_input = sys.stdin.readlines()
_input = [s[:-1] for s in _input]
N = int(_input[0])
A = _input[1 : N + 1]
B = _input[N + 1 :]
T = Node()
nodes.append(T)
for i, s in enumerate(A):
T.add(s, i + 1)
for i, s in enumerate(B):
T.inc(s, i + 1)
for n in reversed(nodes):
n.solve()
print(res)
print('\n'.join(pairs))
``` | output | 1 | 92,148 | 14 | 184,297 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names.
There are n students in a summer school. Teachers chose exactly n pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym b to a student with name a as the length of the largest common prefix a and b. We will represent such value as <image>. Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students.
Find the matching between students and pseudonyms with the maximum quality.
Input
The first line contains number n (1 ≤ n ≤ 100 000) — the number of students in the summer school.
Next n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.
The last n lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating.
The total length of all the names and pseudonyms doesn't exceed 800 000 characters.
Output
In the first line print the maximum possible quality of matching pseudonyms to students.
In the next n lines describe the optimal matching. Each line must have the form a b (1 ≤ a, b ≤ n), that means that the student who was number a in the input, must match to the pseudonym number b in the input.
The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.
Examples
Input
5
gennady
galya
boris
bill
toshik
bilbo
torin
gendalf
smaug
galadriel
Output
11
4 1
2 5
1 3
5 2
3 4
Note
The first test from the statement the match looks as follows:
* bill → bilbo (lcp = 3)
* galya → galadriel (lcp = 3)
* gennady → gendalf (lcp = 3)
* toshik → torin (lcp = 2)
* boris → smaug (lcp = 0) | instruction | 0 | 92,149 | 14 | 184,298 |
Tags: dfs and similar, strings, trees
Correct Solution:
```
import sys
SIGMA = 26
nodes = []
pairs = []
res = 0
class Node:
def __init__(self):
self.ch = {}
self.a = []
self.b = []
self.d = 0
def add(self, s, i):
t = self
for c in s:
v = ord(c) - ord('a')
if not v in t.ch:
t.ch[v] = Node()
t.ch[v].d = t.d + 1
nodes.append(t.ch[v])
t = t.ch[v]
t.a.append(i)
def inc(self, s, i):
t = self
for c in s:
v = ord(c) - ord('a')
if not v in t.ch:
break
t = t.ch[v]
t.b.append(i)
def solve(self):
global pairs
global res
for i in range(SIGMA):
if i in self.ch:
self.a.extend(self.ch[i].a)
self.b.extend(self.ch[i].b)
k = min(len(self.a), len(self.b))
for i in range(k):
pairs.append(str(self.a[-1]) + ' ' + str(self.b[-1]))
self.a.pop()
self.b.pop()
res += self.d
return res
_input = sys.stdin.readlines()
_input = [s[:-1] for s in _input]
N = int(_input[0])
A = _input[1 : N + 1]
B = _input[N + 1 :]
T = Node()
nodes.append(T)
for i, s in enumerate(A):
T.add(s, i + 1)
for i, s in enumerate(B):
T.inc(s, i + 1)
for n in reversed(nodes):
n.solve()
print(res)
print('\n'.join(pairs))
``` | output | 1 | 92,149 | 14 | 184,299 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names.
There are n students in a summer school. Teachers chose exactly n pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym b to a student with name a as the length of the largest common prefix a and b. We will represent such value as <image>. Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students.
Find the matching between students and pseudonyms with the maximum quality.
Input
The first line contains number n (1 ≤ n ≤ 100 000) — the number of students in the summer school.
Next n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.
The last n lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating.
The total length of all the names and pseudonyms doesn't exceed 800 000 characters.
Output
In the first line print the maximum possible quality of matching pseudonyms to students.
In the next n lines describe the optimal matching. Each line must have the form a b (1 ≤ a, b ≤ n), that means that the student who was number a in the input, must match to the pseudonym number b in the input.
The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.
Examples
Input
5
gennady
galya
boris
bill
toshik
bilbo
torin
gendalf
smaug
galadriel
Output
11
4 1
2 5
1 3
5 2
3 4
Note
The first test from the statement the match looks as follows:
* bill → bilbo (lcp = 3)
* galya → galadriel (lcp = 3)
* gennady → gendalf (lcp = 3)
* toshik → torin (lcp = 2)
* boris → smaug (lcp = 0)
Submitted Solution:
```
s = int(input())
n = str(input())
nol = n.count("0")
satu = n.count("1")
while True:
if satu == 0 or nol ==0:
break
nol -= 1
satu -=1
if satu == 0 or nol == 0:
break
if nol == 0:
print(satu)
elif satu ==0 :
print(nol)
print (nol)
else :
print("0")
``` | instruction | 0 | 92,150 | 14 | 184,300 |
No | output | 1 | 92,150 | 14 | 184,301 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names.
There are n students in a summer school. Teachers chose exactly n pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym b to a student with name a as the length of the largest common prefix a and b. We will represent such value as <image>. Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students.
Find the matching between students and pseudonyms with the maximum quality.
Input
The first line contains number n (1 ≤ n ≤ 100 000) — the number of students in the summer school.
Next n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.
The last n lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating.
The total length of all the names and pseudonyms doesn't exceed 800 000 characters.
Output
In the first line print the maximum possible quality of matching pseudonyms to students.
In the next n lines describe the optimal matching. Each line must have the form a b (1 ≤ a, b ≤ n), that means that the student who was number a in the input, must match to the pseudonym number b in the input.
The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.
Examples
Input
5
gennady
galya
boris
bill
toshik
bilbo
torin
gendalf
smaug
galadriel
Output
11
4 1
2 5
1 3
5 2
3 4
Note
The first test from the statement the match looks as follows:
* bill → bilbo (lcp = 3)
* galya → galadriel (lcp = 3)
* gennady → gendalf (lcp = 3)
* toshik → torin (lcp = 2)
* boris → smaug (lcp = 0)
Submitted Solution:
```
n=int(input())
s=input()
one=s.count('1')
zero=s.count('0')
k=min(one,zero)
k*=2
print(n-k)
``` | instruction | 0 | 92,151 | 14 | 184,302 |
No | output | 1 | 92,151 | 14 | 184,303 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names.
There are n students in a summer school. Teachers chose exactly n pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym b to a student with name a as the length of the largest common prefix a and b. We will represent such value as <image>. Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students.
Find the matching between students and pseudonyms with the maximum quality.
Input
The first line contains number n (1 ≤ n ≤ 100 000) — the number of students in the summer school.
Next n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.
The last n lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating.
The total length of all the names and pseudonyms doesn't exceed 800 000 characters.
Output
In the first line print the maximum possible quality of matching pseudonyms to students.
In the next n lines describe the optimal matching. Each line must have the form a b (1 ≤ a, b ≤ n), that means that the student who was number a in the input, must match to the pseudonym number b in the input.
The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.
Examples
Input
5
gennady
galya
boris
bill
toshik
bilbo
torin
gendalf
smaug
galadriel
Output
11
4 1
2 5
1 3
5 2
3 4
Note
The first test from the statement the match looks as follows:
* bill → bilbo (lcp = 3)
* galya → galadriel (lcp = 3)
* gennady → gendalf (lcp = 3)
* toshik → torin (lcp = 2)
* boris → smaug (lcp = 0)
Submitted Solution:
```
class Tree():
def __init__(self, value, index):
self.value = value
self.left = None
self.right = None
self.count = 1
self.index = index
def insert(self, value, index):
if value < self.value:
if self.left is None:
self.left = Tree(value, index)
return
self.left.insert(value, index)
if value >= self.value:
if self.right is None:
self.right = Tree(value, index)
return
self.right.insert(value, index)
@staticmethod
def largest_common_prefix(str1, str2):
common = 0
for i in range(len(str1)):
if str1[i] == str2[i]:
common += 1
else: return common
return len(str1)
def lnr(self):
if self.left is not None:
self.left.lnr()
print (self.value, self.index)
if self.right is not None:
self.right.lnr()
def search(self, value):
root_prefix = self.largest_common_prefix(self.value, value)
if value == self.value:
if self.count == 1:
self.count -= 1
return len(value), self.index
else:
return self.right.search(value)
if value > self.value: #kiem tra ben phai
if self.right: #coi co node phai khong
right_prefix = self.largest_common_prefix(self.right.value, value)
if root_prefix > right_prefix:
if self.count > 0:
self.count -= 1
return root_prefix, self.index
else:
return 0, 0
else:
return self.right.search(value)
else: #khong co thi thang similar nhat la thang root
if self.count > 0:
self.count -= 1
return root_prefix, self.index
else:
return 0, 0
if value < self.value: #kiem tra ben trai
if self.left: #coi co node trai khong
left_prefix = self.largest_common_prefix(self.left.value, value)
if root_prefix > left_prefix:
if self.count > 0:
self.count -= 1
return root_prefix, self.index
else:
return 0, 0
else:
return self.left.search(value)
else:
if self.count > 0:
self.count -= 1
return root_prefix, self.index
else:
return 0, 0
# @staticmethod
def count_nhanh(self):
if self.count == 1:
return True
if self.left:
return self.left.count_nhanh()
if self.right:
return self.right.count_nhanh()
return False
def search_zero(self, value):
if self.right:
check_right = self.right.count_nhanh()
else:
check_right = False
if self.left:
check_left = self.left.count_nhanh()
else:
check_left = False
if check_left == check_right == False and self.count == 1:
return self.index
if value > self.value:
if check_right == True:
return self.right.search_zero(value)
else:
return self.left.search_zero(value)
if value < self.value:
if check_left == True:
return self.left.search_zero(value)
else:
return self.right.search_zero(value)
if __name__ == "__main__":
n = int(input())
tree = Tree(input(), 1)
for i in range(n-1):
tree.insert(input(), i+2)
save = {}
queue = []
for i in range(n):
pseudonyms = input()
value, index = tree.search(pseudonyms)
if value == 0:
queue.append([pseudonyms, i+1])
else:
save["{} {}".format(index, i+1)] = value
for remain in queue:
index = tree.search_zero(remain[0])
save["{} {}".format(index, remain[1])] = 0
#return output
print (sum(save.values()))
for i,v in enumerate(save):
if i % 2 == 0:
print (v)
else:
print (v[::-1])
``` | instruction | 0 | 92,152 | 14 | 184,304 |
No | output | 1 | 92,152 | 14 | 184,305 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names.
There are n students in a summer school. Teachers chose exactly n pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym b to a student with name a as the length of the largest common prefix a and b. We will represent such value as <image>. Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students.
Find the matching between students and pseudonyms with the maximum quality.
Input
The first line contains number n (1 ≤ n ≤ 100 000) — the number of students in the summer school.
Next n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.
The last n lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating.
The total length of all the names and pseudonyms doesn't exceed 800 000 characters.
Output
In the first line print the maximum possible quality of matching pseudonyms to students.
In the next n lines describe the optimal matching. Each line must have the form a b (1 ≤ a, b ≤ n), that means that the student who was number a in the input, must match to the pseudonym number b in the input.
The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.
Examples
Input
5
gennady
galya
boris
bill
toshik
bilbo
torin
gendalf
smaug
galadriel
Output
11
4 1
2 5
1 3
5 2
3 4
Note
The first test from the statement the match looks as follows:
* bill → bilbo (lcp = 3)
* galya → galadriel (lcp = 3)
* gennady → gendalf (lcp = 3)
* toshik → torin (lcp = 2)
* boris → smaug (lcp = 0)
Submitted Solution:
```
n = int(input())
def lcp(fStr, sStr):
maxx = 0
string = fStr[0]
if string == fStr[0]:
maxx += 1
for i in range(1, len(fStr)):
string += fStr[i]
if string in sStr:
maxx += 1
else:
return 0
return maxx
names = []
psevd = []
ans = []
ansNum = 0
for _ in range(n):
names.append(str(input()))
for _ in range(n):
psevd.append(str(input()))
for psev in psevd:
maxLen = 0
nameIndex = 0
psevdInvex = psevd.index(psev)
num = 0
for name in names:
if name != "":
num = lcp(name, psev)
print(name, psev, num)
if num >= maxLen:
maxLen = num
nameIndex = names.index(name)
ans.append([nameIndex + 1, psevdInvex + 1])
names[nameIndex] = ""
ansNum += maxLen
print(ansNum - 1)
for elem in ans:
print(elem[0], elem[1])
"""
5
gennady
galya
boris
bill
toshik
bilbo
torin
gendalf
smaug
galadriel
"""
``` | instruction | 0 | 92,153 | 14 | 184,306 |
No | output | 1 | 92,153 | 14 | 184,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A simple recommendation system would recommend a user things liked by a certain number of their friends. In this problem you will implement part of such a system.
You are given user's friends' opinions about a list of items. You are also given a threshold T — the minimal number of "likes" necessary for an item to be recommended to the user.
Output the number of items in the list liked by at least T of user's friends.
Input
The first line of the input will contain three space-separated integers: the number of friends F (1 ≤ F ≤ 10), the number of items I (1 ≤ I ≤ 10) and the threshold T (1 ≤ T ≤ F).
The following F lines of input contain user's friends' opinions. j-th character of i-th line is 'Y' if i-th friend likes j-th item, and 'N' otherwise.
Output
Output an integer — the number of items liked by at least T of user's friends.
Examples
Input
3 3 2
YYY
NNN
YNY
Output
2
Input
4 4 1
NNNY
NNYN
NYNN
YNNN
Output
4 | instruction | 0 | 92,187 | 14 | 184,374 |
Tags: *special
Correct Solution:
```
'kitten'
f,l,t=map(int,input().split())
s=[input() for _ in range(f)]
a=0
for j in range(l):
c=0
for i in range(f):
c+=s[i][j]=='Y';
a+=c>=t
print(a)
``` | output | 1 | 92,187 | 14 | 184,375 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A simple recommendation system would recommend a user things liked by a certain number of their friends. In this problem you will implement part of such a system.
You are given user's friends' opinions about a list of items. You are also given a threshold T — the minimal number of "likes" necessary for an item to be recommended to the user.
Output the number of items in the list liked by at least T of user's friends.
Input
The first line of the input will contain three space-separated integers: the number of friends F (1 ≤ F ≤ 10), the number of items I (1 ≤ I ≤ 10) and the threshold T (1 ≤ T ≤ F).
The following F lines of input contain user's friends' opinions. j-th character of i-th line is 'Y' if i-th friend likes j-th item, and 'N' otherwise.
Output
Output an integer — the number of items liked by at least T of user's friends.
Examples
Input
3 3 2
YYY
NNN
YNY
Output
2
Input
4 4 1
NNNY
NNYN
NYNN
YNNN
Output
4 | instruction | 0 | 92,188 | 14 | 184,376 |
Tags: *special
Correct Solution:
```
#kitten
F, I, T = list(map(int, input().split()))
s = [""] * F
for i in range(F):
s[i] = input()
a = 0
for j in range(I):
c = 0
for i in range(F):
c += (s[i][j] == 'Y')
a += (c >= T)
print(a)
``` | output | 1 | 92,188 | 14 | 184,377 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A simple recommendation system would recommend a user things liked by a certain number of their friends. In this problem you will implement part of such a system.
You are given user's friends' opinions about a list of items. You are also given a threshold T — the minimal number of "likes" necessary for an item to be recommended to the user.
Output the number of items in the list liked by at least T of user's friends.
Input
The first line of the input will contain three space-separated integers: the number of friends F (1 ≤ F ≤ 10), the number of items I (1 ≤ I ≤ 10) and the threshold T (1 ≤ T ≤ F).
The following F lines of input contain user's friends' opinions. j-th character of i-th line is 'Y' if i-th friend likes j-th item, and 'N' otherwise.
Output
Output an integer — the number of items liked by at least T of user's friends.
Examples
Input
3 3 2
YYY
NNN
YNY
Output
2
Input
4 4 1
NNNY
NNYN
NYNN
YNNN
Output
4 | instruction | 0 | 92,189 | 14 | 184,378 |
Tags: *special
Correct Solution:
```
import sys
n,m,k=map(int,sys.stdin.readline().split ())
c=[0]*m
for i in range(n):
T=sys.stdin.readline()
for j in range(m) :
if T[j]=='Y' :
c[j]+=1
kitten=0
for i in range(m):
if c[i]>=k:
kitten+=1
print(kitten)
``` | output | 1 | 92,189 | 14 | 184,379 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A simple recommendation system would recommend a user things liked by a certain number of their friends. In this problem you will implement part of such a system.
You are given user's friends' opinions about a list of items. You are also given a threshold T — the minimal number of "likes" necessary for an item to be recommended to the user.
Output the number of items in the list liked by at least T of user's friends.
Input
The first line of the input will contain three space-separated integers: the number of friends F (1 ≤ F ≤ 10), the number of items I (1 ≤ I ≤ 10) and the threshold T (1 ≤ T ≤ F).
The following F lines of input contain user's friends' opinions. j-th character of i-th line is 'Y' if i-th friend likes j-th item, and 'N' otherwise.
Output
Output an integer — the number of items liked by at least T of user's friends.
Examples
Input
3 3 2
YYY
NNN
YNY
Output
2
Input
4 4 1
NNNY
NNYN
NYNN
YNNN
Output
4 | instruction | 0 | 92,190 | 14 | 184,380 |
Tags: *special
Correct Solution:
```
f,i,t = map(int,input().split())
a = [0] * i
for z in range(f):
u = input().strip()
for q in range(i):
if u[q] == 'Y': a[q] += 1
print(len([_ for _ in a if _ >= t]))
# kitten
# meow~~
``` | output | 1 | 92,190 | 14 | 184,381 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A simple recommendation system would recommend a user things liked by a certain number of their friends. In this problem you will implement part of such a system.
You are given user's friends' opinions about a list of items. You are also given a threshold T — the minimal number of "likes" necessary for an item to be recommended to the user.
Output the number of items in the list liked by at least T of user's friends.
Input
The first line of the input will contain three space-separated integers: the number of friends F (1 ≤ F ≤ 10), the number of items I (1 ≤ I ≤ 10) and the threshold T (1 ≤ T ≤ F).
The following F lines of input contain user's friends' opinions. j-th character of i-th line is 'Y' if i-th friend likes j-th item, and 'N' otherwise.
Output
Output an integer — the number of items liked by at least T of user's friends.
Examples
Input
3 3 2
YYY
NNN
YNY
Output
2
Input
4 4 1
NNNY
NNYN
NYNN
YNNN
Output
4 | instruction | 0 | 92,191 | 14 | 184,382 |
Tags: *special
Correct Solution:
```
f,l,t = map(int,input().split())
s = []
for i in range(f):
s.append(input())
ans = 0
for i in range(l):
cnt = 0
for j in range(f):
cnt += (s[j][i]=='Y')
ans += (cnt >= t)
# kitten
print( ans )
``` | output | 1 | 92,191 | 14 | 184,383 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A simple recommendation system would recommend a user things liked by a certain number of their friends. In this problem you will implement part of such a system.
You are given user's friends' opinions about a list of items. You are also given a threshold T — the minimal number of "likes" necessary for an item to be recommended to the user.
Output the number of items in the list liked by at least T of user's friends.
Input
The first line of the input will contain three space-separated integers: the number of friends F (1 ≤ F ≤ 10), the number of items I (1 ≤ I ≤ 10) and the threshold T (1 ≤ T ≤ F).
The following F lines of input contain user's friends' opinions. j-th character of i-th line is 'Y' if i-th friend likes j-th item, and 'N' otherwise.
Output
Output an integer — the number of items liked by at least T of user's friends.
Examples
Input
3 3 2
YYY
NNN
YNY
Output
2
Input
4 4 1
NNNY
NNYN
NYNN
YNNN
Output
4 | instruction | 0 | 92,192 | 14 | 184,384 |
Tags: *special
Correct Solution:
```
c = []
F, I, T = map(int, input().split())
for i in range(F):
s = input()
for j in range(I):
c.append(0)
c[j] += s[j] == 'Y'
r = 0
for i in range(I):
r += c[i] >= T
print(r)
kitten = 0
``` | output | 1 | 92,192 | 14 | 184,385 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.