message stringlengths 2 45.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 254 108k | cluster float64 3 3 | __index_level_0__ int64 508 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission.
Heidi has n Jedi Knights standing in front of her, each one with a lightsaber of one of m possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly k1 knights with lightsabers of the first color, k2 knights with lightsabers of the second color, ..., km knights with lightsabers of the m-th color.
However, since the last time, she has learned that it is not always possible to select such an interval. Therefore, she decided to ask some Jedi Knights to go on an indefinite unpaid vacation leave near certain pits on Tatooine, if you know what I mean. Help Heidi decide what is the minimum number of Jedi Knights that need to be let go before she is able to select the desired interval from the subsequence of remaining knights.
Input
The first line of the input contains n (1 β€ n β€ 2Β·105) and m (1 β€ m β€ n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k1, k2, ..., km (with <image>) β the desired counts of Jedi Knights with lightsabers of each color from 1 to m.
Output
Output one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output - 1.
Example
Input
8 3
3 3 1 2 2 1 1 3
3 1 1
Output
1
Submitted Solution:
```
n, m = map(int, input().split())
c = list(map(int, input().split()))
k = list(map(int, input().split()))
p = [0] * m
c = [o - 1 for o in c]
s = sum(k)
ok = k.count(0)
i1 = 0
i2 = 0
while i2 < n and ok < m:
p[c[i2]] += 1
if p[c[i2]] == k[c[i2]]:
ok += 1
i2 += 1
if ok != m:
print(-1)
exit(0)
ans = i2 - i1
while i2 < n:
p[c[i1]] -= 1
if k[c[i1]] <= p[c[i1]]:
i1 += 1
ans = min(ans, i2 - i1)
continue
while i2 < n and p[c[i1]] < k[c[i1]]:
p[c[i2]] += 1
i2 += 1
if p[c[i1]] >= k[c[i1]]:
ans = min(ans, i2 - i1)
print(ans - s)
``` | instruction | 0 | 39,349 | 3 | 78,698 |
No | output | 1 | 39,349 | 3 | 78,699 |
Provide a correct Python 3 solution for this coding contest problem.
ACM countries have rivers that flow from east to west in the center. This river flows from the neighboring country in the west through the neighboring country in ACM to the neighboring country in the east, and the total length in ACM is K km. It is planned to install several locks on this river and use it as a canal.
A lock is a mechanism for a ship to move between two different water levels. Locks have locks on the upstream and downstream sides, respectively, and a small body of water called a lock chamber between them. After putting the ship in this lock room, water is injected or drained, and the water level in the lock room is raised or lowered to raise or lower the ship. The schematic diagram is shown below.
<image>
Figure F-1: Schematic diagram of the lock
Since the width of this river is not very wide, it has been decided that it will be a one-way canal from west to east. The person in charge of design wants to optimize the location of the lock according to the expected navigation schedule of the ship in order to operate the canal efficiently.
You are a programmer hired by a designer. Your job is to write a program that simulates the time it takes for all ships to cross the river, given the lock information and the navigation schedules for multiple ships.
Each lock is represented by the following information.
* Distance from the western end of ACM country X (km)
* Volume of water required to switch the water level L (L)
* Maximum water injection amount per unit time F (L / h)
* Maximum displacement per unit time D (L / h)
* Hierarchical relationship between the water level on the west side and the water level on the east side of the lock
For convenience, in the simulation, it is assumed that the river continues infinitely outside the ACM as well.
At the start of the simulation, the water levels in all the lock chambers are the lower of the eastern and western water levels. In addition, the ships included in the navigation schedule shall be lined up every 1 km from east to west in the order given by input, starting from the western end of the ACM country. For convenience, the initial position of the leading ship is set to the 0km point.
As soon as the simulation starts, the ship begins to sail east. At this time, no other ship should enter less than 1km before and after one ship. The maximum speed V (km / h) is set for each ship. The ship can reach any speed in an instant and even stand still in an instant. Basically, a ship sails at the maximum speed, but if the following ship has a faster maximum speed than the preceding ship and the following ship catches up 1 km before the preceding ship, the following ship will lead. It sails at the same speed as the ship. The size of the ship and locks shall be negligible.
A ship can enter the lock only when the water level on the west side of the lock is equal to the water level in the lock chamber. Similarly, you can exit the lock only when the water level on the east side of the lock is equal to the water level in the lock chamber. If there is no ship inside, the water level in each lock will rise or fall until it matches the water level on the west side of the lock. If there is a ship, it will be displaced until it matches the water level on the east side of the lock. Even if the ship is moored just 1km away from the lock, the ship can leave the lock. However, at this time, the ship must berth at the exit from the lock until the preceding ship starts.
After passing the eastern end of the ACM country, the ship sails to infinity as fast as possible. The simulation ends when all ships have passed the eastern end of the ACM country.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> NMK
> X1 L1 F1 D1 UD1
> X2 L2 F2 D2 UD2
> ...
> XN LN FN DN UDN
> V1
> V2
> ...
> VM
>
The first line consists of three integers N, M, K. N (1 β€ N β€ 100) is the number of locks, M (1 β€ M β€ 100) is the number of ships, and K (2 β€ K β€ 1000) is the total length of the river in ACM.
The following N lines represent lock information. Each line consists of 5 integers Xi, Li, Fi, Di, UDi. Xi (1 β€ Xi β€ K -1) is the position of lock i from the western end of the ACM country (km), Li (1 β€ Li β€ 1000) is the volume of water required to switch the water level of lock i (L), Fi (1 β€ Fi β€ 1000) is the maximum water injection amount per unit time of lock i (L / h), Di (1 β€ Di β€ 1000) is the maximum drainage amount per unit time of lock i (L / h), UDi ( UDi β {0, 1}) represents the hierarchical relationship between the water level on the west side and the water level on the east side of the lock i, respectively. When UDi is 0, the lock i means that the water level is higher on the east side than on the west side. On the other hand, when UDi is 1, lock i means that the water level is lower on the east side than on the west side.
The following M rows are given the integer Vi (1 β€ Vi β€ 1000), which represents the maximum speed (km / h) of the i-th vessel in each row.
Locks are given in ascending order of Xi values. In addition, multiple locks will not be installed at the same position.
The end of the input consists of three zeros separated by spaces.
Output
For each dataset, output the time from the start to the end of the simulation in one line. The output value may contain an error of 10-6 or less. The value may be displayed in any number of digits after the decimal point.
Example
Input
1 1 100
50 200 20 40 0
1
2 4 100
7 4 1 4 1
19 5 1 4 0
5
3
7
9
1 2 3
1 1 1 1 0
1
3
1 2 10
5 10 1 1 1
2
3
0 0 0
Output
110
46.6666666667
5
41.6666666667 | instruction | 0 | 39,564 | 3 | 79,128 |
"Correct Solution:
```
while True:
n, m, k = (int(s) for s in input().split())
if (n, m, k) == (0, 0, 0):
break
t_out = [0.] * (k + m + 1)
tl_east = [0.] * (k + m + 1)
tl_west = [0.] * (k + m + 1)
for i in range(n):
xi, li, fi, di, udi = (int(s) for s in input().split())
if not udi:
tl_east[xi] = li / fi
tl_west[xi] = li / di
t_out[xi] = -tl_west[xi]
else:
tl_east[xi] = li / di
tl_west[xi] = li / fi
for i in range(m):
vi = int(input())
t_out[0] = max(t_out[1] - tl_east[1], i / vi)
for j in range(1, k + m):
t_out[j] = tl_east[j] + max(t_out[j - 1] + 1 / vi,
t_out[j] + tl_west[j],
t_out[j + 1] - tl_east[j + 1])
print(t_out[k])
``` | output | 1 | 39,564 | 3 | 79,129 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
ACM countries have rivers that flow from east to west in the center. This river flows from the neighboring country in the west through the neighboring country in ACM to the neighboring country in the east, and the total length in ACM is K km. It is planned to install several locks on this river and use it as a canal.
A lock is a mechanism for a ship to move between two different water levels. Locks have locks on the upstream and downstream sides, respectively, and a small body of water called a lock chamber between them. After putting the ship in this lock room, water is injected or drained, and the water level in the lock room is raised or lowered to raise or lower the ship. The schematic diagram is shown below.
<image>
Figure F-1: Schematic diagram of the lock
Since the width of this river is not very wide, it has been decided that it will be a one-way canal from west to east. The person in charge of design wants to optimize the location of the lock according to the expected navigation schedule of the ship in order to operate the canal efficiently.
You are a programmer hired by a designer. Your job is to write a program that simulates the time it takes for all ships to cross the river, given the lock information and the navigation schedules for multiple ships.
Each lock is represented by the following information.
* Distance from the western end of ACM country X (km)
* Volume of water required to switch the water level L (L)
* Maximum water injection amount per unit time F (L / h)
* Maximum displacement per unit time D (L / h)
* Hierarchical relationship between the water level on the west side and the water level on the east side of the lock
For convenience, in the simulation, it is assumed that the river continues infinitely outside the ACM as well.
At the start of the simulation, the water levels in all the lock chambers are the lower of the eastern and western water levels. In addition, the ships included in the navigation schedule shall be lined up every 1 km from east to west in the order given by input, starting from the western end of the ACM country. For convenience, the initial position of the leading ship is set to the 0km point.
As soon as the simulation starts, the ship begins to sail east. At this time, no other ship should enter less than 1km before and after one ship. The maximum speed V (km / h) is set for each ship. The ship can reach any speed in an instant and even stand still in an instant. Basically, a ship sails at the maximum speed, but if the following ship has a faster maximum speed than the preceding ship and the following ship catches up 1 km before the preceding ship, the following ship will lead. It sails at the same speed as the ship. The size of the ship and locks shall be negligible.
A ship can enter the lock only when the water level on the west side of the lock is equal to the water level in the lock chamber. Similarly, you can exit the lock only when the water level on the east side of the lock is equal to the water level in the lock chamber. If there is no ship inside, the water level in each lock will rise or fall until it matches the water level on the west side of the lock. If there is a ship, it will be displaced until it matches the water level on the east side of the lock. Even if the ship is moored just 1km away from the lock, the ship can leave the lock. However, at this time, the ship must berth at the exit from the lock until the preceding ship starts.
After passing the eastern end of the ACM country, the ship sails to infinity as fast as possible. The simulation ends when all ships have passed the eastern end of the ACM country.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> NMK
> X1 L1 F1 D1 UD1
> X2 L2 F2 D2 UD2
> ...
> XN LN FN DN UDN
> V1
> V2
> ...
> VM
>
The first line consists of three integers N, M, K. N (1 β€ N β€ 100) is the number of locks, M (1 β€ M β€ 100) is the number of ships, and K (2 β€ K β€ 1000) is the total length of the river in ACM.
The following N lines represent lock information. Each line consists of 5 integers Xi, Li, Fi, Di, UDi. Xi (1 β€ Xi β€ K -1) is the position of lock i from the western end of the ACM country (km), Li (1 β€ Li β€ 1000) is the volume of water required to switch the water level of lock i (L), Fi (1 β€ Fi β€ 1000) is the maximum water injection amount per unit time of lock i (L / h), Di (1 β€ Di β€ 1000) is the maximum drainage amount per unit time of lock i (L / h), UDi ( UDi β {0, 1}) represents the hierarchical relationship between the water level on the west side and the water level on the east side of the lock i, respectively. When UDi is 0, the lock i means that the water level is higher on the east side than on the west side. On the other hand, when UDi is 1, lock i means that the water level is lower on the east side than on the west side.
The following M rows are given the integer Vi (1 β€ Vi β€ 1000), which represents the maximum speed (km / h) of the i-th vessel in each row.
Locks are given in ascending order of Xi values. In addition, multiple locks will not be installed at the same position.
The end of the input consists of three zeros separated by spaces.
Output
For each dataset, output the time from the start to the end of the simulation in one line. The output value may contain an error of 10-6 or less. The value may be displayed in any number of digits after the decimal point.
Example
Input
1 1 100
50 200 20 40 0
1
2 4 100
7 4 1 4 1
19 5 1 4 0
5
3
7
9
1 2 3
1 1 1 1 0
1
3
1 2 10
5 10 1 1 1
2
3
0 0 0
Output
110
46.6666666667
5
41.6666666667
Submitted Solution:
```
while True:
n, m, k = (int(s) for s in input().split())
if (n, m, k) == (0, 0, 0):
break
t_out = [0.] * (k + m + 1)
tl_east = [0.] * (k + m + 1)
tl_west = [0.] * (k + m + 1)
for i in range(n):
xi, li, fi, di, udi = (int(s) for s in input().split())
if not udi:
tl_east[xi] = li / fi
tl_west[xi] = li / di
t_out[xi] = -tl_east[xi]
else:
tl_east[xi] = li / di
tl_west[xi] = li / fi
for i in range(m):
vi = int(input())
t_out[0] = max(t_out[1] - tl_east[1], i / vi)
for j in range(1, k + m):
t_out[j] = tl_east[j] + max(t_out[j - 1] + 1 / vi,
t_out[j] + tl_west[j],
t_out[j + 1] - tl_east[j + 1])
print(t_out[k])
``` | instruction | 0 | 39,565 | 3 | 79,130 |
No | output | 1 | 39,565 | 3 | 79,131 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2. | instruction | 0 | 39,887 | 3 | 79,774 |
Tags: math
Correct Solution:
```
n, m = map(int, input().split())
ans = 1
base = 3
while n != 0:
if n & 1:
ans = (ans * base) % m
base = (base * base) % m
n >>= 1
ans = (ans + m - 1) % m
print(ans)
``` | output | 1 | 39,887 | 3 | 79,775 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2. | instruction | 0 | 39,888 | 3 | 79,776 |
Tags: math
Correct Solution:
```
import math
def powerk (a,x,y):
if(x==1):
return(a)
elif(x<=0):
return(1)
else:
if (a<=0):
return(0)
y0=math.ceil(math.log(y,a))
if(y0<=0):
return(a**x)
if (x<y0):
return((a**x)%y)
t0=(a**y0)%y
x_div_y0=x//y0
t1=powerk(t0,x_div_y0,y)
pow3_x_mod_y0=powerk(a,(x%y0),y)
t2=t1*pow3_x_mod_y0
return(t2)
def main():
st=input().split(' ')
x=int(st[0])
y=int(st[1])
t2=powerk(3,x,y)
t3=(t2-1)%y
print(t3)
main()
``` | output | 1 | 39,888 | 3 | 79,777 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2. | instruction | 0 | 39,889 | 3 | 79,778 |
Tags: math
Correct Solution:
```
n,m=map(int,input().split());print((pow(3,n,m)-1)%m)
``` | output | 1 | 39,889 | 3 | 79,779 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2. | instruction | 0 | 39,890 | 3 | 79,780 |
Tags: math
Correct Solution:
```
def fastpow(mod,times):
if times==1:
return 3
else:
if (times%2):
now=fastpow(mod,(times-1)/2)%mod
return (now*now*3)%mod
else:
now = fastpow(mod, (times ) / 2) % mod
return (now * now ) % mod
n,m=map(int, input().split())
print((fastpow(m,n)-1)%m)
``` | output | 1 | 39,890 | 3 | 79,781 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2. | instruction | 0 | 39,891 | 3 | 79,782 |
Tags: math
Correct Solution:
```
def modpow(a, b, n):
if b == 0:
return 1
if b == 1:
return a % n
tmp = modpow(a, b // 2, n) % n
tmp = (tmp * tmp) % n
if b % 2 == 1:
return (tmp * a) % n
else:
return tmp
def main():
n, m = map(int, input().split())
ans = (modpow(3, n, m) - 1) % m
print(ans)
if __name__ == "__main__":
main()
``` | output | 1 | 39,891 | 3 | 79,783 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2. | instruction | 0 | 39,892 | 3 | 79,784 |
Tags: math
Correct Solution:
```
import sys
import string
import math
import heapq
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction
def mi(s):
return map(int, s.strip().split())
def lmi(s):
return list(mi(s))
def tmi(s):
return tuple(mi(s))
def mf(f, s):
return map(f, s)
def lmf(f, s):
return list(mf(f, s))
def js(lst):
return " ".join(str(d) for d in lst)
def jsns(lst):
return "".join(str(d) for d in lst)
def line():
return sys.stdin.readline().strip()
def linesp():
return line().split()
def iline():
return int(line())
def mat(n):
matr = []
for _ in range(n):
matr.append(linesp())
return matr
def matns(n):
mat = []
for _ in range(n):
mat.append([c for c in line()])
return mat
def mati(n):
mat = []
for _ in range(n):
mat.append(lmi(line()))
return mat
def pmat(mat):
for row in mat:
print(js(row))
def fast_exp(x, n, m):
if n == 0:
return 1
elif n % 2 == 1:
return (x * fast_exp(x, (n - 1) // 2, m)**2) % m
else:
return (fast_exp(x, n // 2, m)**2) % m
def main():
n, m = mi(line())
print((fast_exp(3, n, m) - 1) % m)
main()
``` | output | 1 | 39,892 | 3 | 79,785 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2. | instruction | 0 | 39,893 | 3 | 79,786 |
Tags: math
Correct Solution:
```
def quick_algorithm(a,b,c):
a=a%c
ans=1
while b!=0:
if b&1:
ans=(ans*a)%c
b>>=1
a=(a*a)%c
return ans
n,m=map(int,input().split())
print((quick_algorithm(3,n,m)-1)%m)
``` | output | 1 | 39,893 | 3 | 79,787 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2. | instruction | 0 | 39,894 | 3 | 79,788 |
Tags: math
Correct Solution:
```
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
# from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353
# from sys import stdin
# input = stdin.readline
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
try:
# sys.setrecursionlimit(int(pow(10,6)))
sys.stdin = open("input.txt", "r")
# sys.stdout = open("../output.txt", "w")
except:
pass
def pmat(A):
for ele in A:
print(ele,end="\n")
n,m=L()
print((pow(3,n,m)-1)%m)
endtime = time.time()
# print(f"Runtime of the program is {endtime - starttime}")
``` | output | 1 | 39,894 | 3 | 79,789 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 16 19:44:48 2019
@author: asus
"""
def pows(a,b,mod):
ret = 1;
while(b):
if(b & 1):
ret = (ret * a) % mod
a = (a * a) % mod
b >>= 1
return ret;
if __name__ == '__main__':
n,m=map(int,input().split())
k=(pows(3,n,m)%m-1+m)%m
print(k)
``` | instruction | 0 | 39,895 | 3 | 79,790 |
Yes | output | 1 | 39,895 | 3 | 79,791 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.
Submitted Solution:
```
from math import fmod
def pow1(x, base):
if base == 0:
return 1
sq = pow1(x, base // 2)
sq = (sq * sq) % m
if base % 2:
sq *= x
return sq
def sum_pow(r, n):
return (1 + fmod(-pow1(r, n + 1), m)) / (1 + fmod(- r, m))
n, m = map(int, input().split())
if n == 1:
print(2 % m)
else:
print(int((2 * (sum_pow(3, n - 1))) % m))
``` | instruction | 0 | 39,896 | 3 | 79,792 |
Yes | output | 1 | 39,896 | 3 | 79,793 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.
Submitted Solution:
```
a, b = map(int, input().split())
print((pow(3, a, b) + b - 1) % b)
# qwq
# qwq
``` | instruction | 0 | 39,897 | 3 | 79,794 |
Yes | output | 1 | 39,897 | 3 | 79,795 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.
Submitted Solution:
```
def power(a, n, m):
if n==0: return 1
if n==1: return a%m
if n%2 == 0:
x = power(a, n//2, m)
return (x*x)%m
else:
x = power(a, n//2, m)
return (((x*x)%m)*(a%m))%m
n, m = [int(x) for x in input().split()]
print((power(3, n, m)-1)%m)
``` | instruction | 0 | 39,898 | 3 | 79,796 |
Yes | output | 1 | 39,898 | 3 | 79,797 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.
Submitted Solution:
```
n,m = map(int,input().split())
# a = 2
def power(x, y, p) :
res = 1 # Initialize result
# Update x if it is more
# than or equal to p
x = x % p
while (y > 0) :
# If y is odd, multiply
# x with result
if ((y & 1) == 1) :
res = (res * x) % p
# y must be even now
y = y >> 1 # y = y/2
x = (x * x) % p
return res
print(power(3,n,m)-1)
``` | instruction | 0 | 39,899 | 3 | 79,798 |
No | output | 1 | 39,899 | 3 | 79,799 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.
Submitted Solution:
```
st=input().split(' ')
s=[int(s1) for s1 in st]
moves=0
s[0]=s[1]-s[0]
for i in range (s[0]):
moves=moves+2*(3**i)
print(moves%s[1])
``` | instruction | 0 | 39,900 | 3 | 79,800 |
No | output | 1 | 39,900 | 3 | 79,801 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.
Submitted Solution:
```
import sys
def steps(n):
if n == 0:
return 0
return 2*(n) + 2*(n*(n-1))
def solve(x,mod):
k = steps(x)
return k % mod
def readinput():
n,mod = map(int, sys.stdin.readline().rstrip().split(" "))
print(solve(n,mod))
readinput()
``` | instruction | 0 | 39,901 | 3 | 79,802 |
No | output | 1 | 39,901 | 3 | 79,803 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of n aliens. Each of them is given a rank β an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.
Input
The first line contains two space-separated integers: n and m (1 β€ n, m β€ 109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.
Output
Print a single number β the answer to the problem modulo m.
Examples
Input
1 10
Output
2
Input
3 8
Output
2
Note
In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <image>, which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>, <image>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.
Submitted Solution:
```
n, m = map(int, input().split())
def mod_exp(a, b, n):
x = a
y = 1
while b > 0:
if b%2:
y = (y*x)%n
x = (x*x)%n
b = b >> 1
return y
print(mod_exp(3, n, m)-1)
``` | instruction | 0 | 39,902 | 3 | 79,804 |
No | output | 1 | 39,902 | 3 | 79,805 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine an n Γ m grid with some blocked cells. The top left cell in the grid has coordinates (1, 1) and the bottom right cell has coordinates (n, m). There are k blocked cells in the grid and others are empty. You flash a laser beam from the center of an empty cell (xs, ys) in one of the diagonal directions (i.e. north-east, north-west, south-east or south-west). If the beam hits a blocked cell or the border of the grid it will reflect. The behavior of the beam reflection in different situations is depicted in the figure below.
<image>
After a while the beam enters an infinite cycle. Count the number of empty cells that the beam goes through at least once. We consider that the beam goes through cell if it goes through its center.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ 105). Each of the next k lines contains two integers xi and yi (1 β€ xi β€ n, 1 β€ yi β€ m) indicating the position of the i-th blocked cell.
The last line contains xs, ys (1 β€ xs β€ n, 1 β€ ys β€ m) and the flash direction which is equal to "NE", "NW", "SE" or "SW". These strings denote directions ( - 1, 1), ( - 1, - 1), (1, 1), (1, - 1).
It's guaranteed that no two blocked cells have the same coordinates.
Output
In the only line of the output print the number of empty cells that the beam goes through at least once.
Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3 3 0
1 2 SW
Output
6
Input
7 5 3
3 3
4 3
5 3
2 1 SE
Output
14
Submitted Solution:
```
print("I AK IOI")
``` | instruction | 0 | 39,905 | 3 | 79,810 |
No | output | 1 | 39,905 | 3 | 79,811 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination. | instruction | 0 | 39,906 | 3 | 79,812 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t,sx,sy,ex,ey =map(int,input().split())
dire=input()
p=0
for i in range(0,t):
if dire[i]=='S'and ey<sy:
sy-=1
elif dire[i]=='N'and ey>sy:
sy+=1
elif dire[i]=='E' and ex>sx:
sx+=1
elif dire[i]=='W' and ex<sx:
sx-=1
if sx==ex and sy==ey:
p=1
print(i+1)
break
if p==0:print('-1')
``` | output | 1 | 39,906 | 3 | 79,813 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination. | instruction | 0 | 39,907 | 3 | 79,814 |
Tags: brute force, greedy, implementation
Correct Solution:
```
# python2 or 3
import sys, threading, os.path
import collections, heapq, math,bisect
import string
from platform import python_version
sys.setrecursionlimit(10**6)
threading.stack_size(2**27)
def main():
if os.path.exists('input.txt'):
input = open('input.txt', 'r')
else:
input = sys.stdin
#--------------------------------INPUT---------------------------------
t, x1,x2,y1,y2 = list(map(int, input.readline().split()))
T = t
dirs = list(str(input.readline().rstrip('\n')))
for i in range(len(dirs)):
if t==0 or (x1 == y1 and x2==y2):
break
if dirs[i] == 'E':
if y1>x1:
x1+=1
t-=1
elif dirs[i] == 'S':
if x2>y2:
x2-=1
t-=1
elif dirs[i] == 'W':
if y1<x1:
x1-=1
t-=1
elif dirs[i] == 'N':
if x2<y2:
x2+=1
t-=1
if x1 == y1 and x2==y2:
output = T-t
else:
output= -1
#-------------------------------OUTPUT----------------------------------
if os.path.exists('output.txt'):
open('output.txt', 'w').writelines(str(output))
else:
sys.stdout.write(str(output))
if __name__ == '__main__':
main()
#threading.Thread(target=main).start()
``` | output | 1 | 39,907 | 3 | 79,815 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination. | instruction | 0 | 39,908 | 3 | 79,816 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t, sx, sy, ex, ey = [int(x) for x in input().split()]
dir = input()
allow = []
if ex - sx > 0:
allow.append('E')
elif ex - sx < 0:
allow.append('W')
if ey - sy > 0:
allow.append('N')
elif ey - sy < 0:
allow.append('S')
curr = [sx, sy]
end = [ex, ey]
mov = {'E':(1, 0), 'W':(-1, 0), 'N':(0, 1), 'S':(0, -1)}
ans = 0
for t, d in enumerate(dir):
ans += 1
if d in allow:
if curr[0] != end[0]:
curr[0] += mov[d][0]
if curr[1] != end[1]:
curr[1] += mov[d][1]
if curr == end:
break
if curr == end:
print(ans)
else:
print(-1)
``` | output | 1 | 39,908 | 3 | 79,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination. | instruction | 0 | 39,909 | 3 | 79,818 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t,x,y,u,v = map(int,input().split())
dic = {}
dic['N'],dic['S'] = (v-y,0) if v>=y else (0,y-v)
dic['E'],dic['W'] = (u-x,0) if u>=x else (0,x-u)
inp = str(input())
for i in range(t):
dic[inp[i]]-=1
if dic['N']<1 and dic['S'] < 1 and dic['E'] < 1 and dic['W'] < 1:
print (i+1)
break
else:
print (-1)
``` | output | 1 | 39,909 | 3 | 79,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination. | instruction | 0 | 39,910 | 3 | 79,820 |
Tags: brute force, greedy, implementation
Correct Solution:
```
# Author Kmoussai
import sys
import math
import random
'''
i = 0
while i < n:
i += 1
map(int, input().split())
def pgcd(a, b):
if b == 0:
return a
return pgcd(b, a%b)
'''
if len(sys.argv) >= 2:
if sys.argv[1] == 'LOCAL':
sys.stdin = open('input.in', 'r')
l = list(map(int, input().split()))
#[t,1 sx,2 sy,3 ex,4 ey]
dx = l[3] - l[1]
dy = l[4] - l[2]
t = 0
yes = False
mapp = {
'N':0,
'S':0,
'E':0,
'W':0,
}
s = input()
for i in s:
mapp[i] += 1
req = {
'N':0,
'S':0,
'E':0,
'W':0,
}
if dx > 0:
req['E'] = dx
elif dx < 0:
req['W'] = -dx
if dy > 0:
req['N'] = dy
elif dy < 0:
req['S'] = -dy
t = 0
for i in s:
if req[i] > 0:
req[i] -= 1
t += 1
if req['E'] == 0 and req['S'] == 0 and req['N'] == 0 and req['W'] == 0:
yes = True
break
if yes:
print(t)
else:
print(-1)
``` | output | 1 | 39,910 | 3 | 79,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination. | instruction | 0 | 39,911 | 3 | 79,822 |
Tags: brute force, greedy, implementation
Correct Solution:
```
def main():
ind = {'E': (1, 0), 'S': (0, -1), 'W': (-1, 0), 'N': (0, 1), '#': (0, 0)}
t, sx, sy, tx, ty = map(int, input().split())
s = str(input())
if sx == tx and sy == ty:
print('0')
return
for i in range(0, t):
if sx != tx and abs(sx+ind[s[i]][0]-tx) < abs(sx-tx): sx += ind[s[i]][0]
if sy != ty and abs(sy+ind[s[i]][1]-ty) < abs(sy-ty): sy += ind[s[i]][1]
if sx == tx and sy == ty:
print(i+1)
return
print('-1')
if __name__ == '__main__':
main()
``` | output | 1 | 39,911 | 3 | 79,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination. | instruction | 0 | 39,912 | 3 | 79,824 |
Tags: brute force, greedy, implementation
Correct Solution:
```
X = list(map(int, input().split()))
XTotal, YTotal = X[-2] - X[1], X[-1] - X[2]
S, Cnt = input(), 0
for i in S:
if XTotal == 0 and YTotal == 0:
print(Cnt)
exit()
if i in 'EW':
if XTotal > 0 and i == 'E': XTotal -= 1
if XTotal < 0 and i == 'W': XTotal += 1
else:
if YTotal > 0 and i == 'N': YTotal -= 1
if YTotal < 0 and i == 'S': YTotal += 1
Cnt += 1
if XTotal == 0 and YTotal == 0:
print(Cnt)
exit()
print(-1)
# Let's see who is the best !!!
# ravenS_The_Best
``` | output | 1 | 39,912 | 3 | 79,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination. | instruction | 0 | 39,913 | 3 | 79,826 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t,sx,sy,ex,ey=map(int,input().split())
L=list(input())
time=0
for i in L:
if i=='W' and sx>ex:
sx-=1
elif i=='E' and sx<ex:
sx+=1
elif i=='N' and sy<ey:
sy+=1
elif i=='S' and sy>ey:
sy-=1
time+=1
if sy==ey and sx==ex:
break
if time<=t and (sy==ey and sx==ex):
print(time)
else:
print("-1")
``` | output | 1 | 39,913 | 3 | 79,827 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination.
Submitted Solution:
```
# http://codeforces.com/problemset/problem/298/B
d = {'E':(1,0), 'S':(0,-1), 'W':(-1,0), 'N':(0,1)}
t,sx,sy,ex,ey = map(int, input().split())
s = input()
for i in range(t):
dx,dy = d[s[i]]
if abs(sx-ex+dx)<=abs(sx-ex) and abs(sy-ey+dy)<=abs(sy-ey):
sx+=dx
sy+=dy
if sx==ex and sy==ey:
print(i+1)
exit()
print(-1)
``` | instruction | 0 | 39,914 | 3 | 79,828 |
Yes | output | 1 | 39,914 | 3 | 79,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination.
Submitted Solution:
```
t, x, y, a, b = map(int, input().split())
wind = input()
found = False
for i in range(t):
if wind[i] == 'E':
if x < a:
x += 1
elif wind[i] == 'W':
if x > a:
x -= 1
elif wind[i] == 'S':
if y > b:
y -= 1
elif wind[i] == 'N':
if y < b:
y += 1
if x == a and y == b:
found = True
print(i + 1)
break
if not found:
print(-1)
``` | instruction | 0 | 39,915 | 3 | 79,830 |
Yes | output | 1 | 39,915 | 3 | 79,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination.
Submitted Solution:
```
t,x1,y1,x2,y2 = map(int,input().split())
s = str(input())
a = x2 - x1
b = y2 - y1
from itertools import islice
def nth_index(iterable, value, n):
matches = (idx for idx, val in enumerate(iterable) if val == value)
return next(islice(matches, n-1, n), None)
if a > 0 and b > 0:
f = nth_index(s,'E',a)
l = nth_index(s,'N',b)
if s.count('E') < abs(a) or s.count('N') < abs(b):
final = -1
else:
final = max(f,l)+1
elif a > 0 and b < 0 :
f = nth_index(s,'E',abs(a))
l = nth_index(s,'S',abs(b))
if s.count('E') < abs(a) or s.count('S') < abs(b):
final = -1
else:
final = max(f,l)+1
elif a < 0 and b > 0:
f = nth_index(s,'W',abs(a))
l = nth_index(s,'N',abs(b))
if s.count('W') < abs(a) or s.count('N') < abs(b):
final = -1
else:
final = max(f,l)+1
elif a < 0 and b < 0:
f = nth_index(s,'W',abs(a))
l = nth_index(s,'S',abs(b))
if s.count('W') < abs(a) or s.count('S') < abs(b):
final = -1
else:
final = max(f,l)+1
elif a > 0 and b == 0:
f = nth_index(s,'E',abs(a))
if s.count('E') < abs (a):
final = -1
else:
final = f+1
elif a < 0 and b == 0:
f = nth_index(s,'W',abs(a))
if s.count('W') < abs (a):
final = -1
else:
final = f+1
elif a == 0 and b > 0:
f = nth_index(s,'N',abs(b))
if s.count('N') < abs (b):
final = -1
else:
final = f+1
elif a == 0 and b < 0:
f = nth_index(s,'S',abs(b))
if s.count('S') < abs (b):
final = -1
else:
final = f+1
print(final)
``` | instruction | 0 | 39,916 | 3 | 79,832 |
Yes | output | 1 | 39,916 | 3 | 79,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination.
Submitted Solution:
```
t, sx, sy, ex, ey = map(int, input().split())
s = input()
hor_dist = abs(sx - ex)
hor_dir = 'E' if sx < ex else 'W'
ver_dist = abs(sy - ey)
ver_dir = 'N' if sy < ey else 'S'
if s.count(hor_dir) < hor_dist or s.count(ver_dir) < ver_dist:
print(-1)
else:
s = s.replace(hor_dir, '_', hor_dist)
s = s.replace(ver_dir, '_', ver_dist)
print(s.rfind('_') + 1)
``` | instruction | 0 | 39,917 | 3 | 79,834 |
Yes | output | 1 | 39,917 | 3 | 79,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination.
Submitted Solution:
```
t, x, y, xe, ye = map(int, input().split())
directions = input()
i,j = x, y
t = 0
for direction in directions:
if i == xe and j == ye:
print(t)
exit()
ni, nj = i, j
if direction == "E":
ni += 1
elif direction == "S":
nj -= 1
elif direction == "N":
nj += 1
else:
ni -= 1
if abs(xe - ni) + abs(ye - nj) < abs(xe-i) + abs(ye - j):
i, j = ni, nj
t += 1
print(-1)
``` | instruction | 0 | 39,918 | 3 | 79,836 |
No | output | 1 | 39,918 | 3 | 79,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination.
Submitted Solution:
```
# import sys
import math
# sys.stdin = open('input1.txt', 'r')
# sys.stdout = open('output1.txt', 'w')
t,x_i,y_i,x_f,y_f=input().split(" ")
command=input()
t=int(t)
x_i=int(x_i)
y_i=int(y_i)
x_f=int(x_f)
y_f=int(y_f)
v=y_f-y_i
h=x_f-x_i
coor=int(0)
if v > 0 :
for i in range(0,len(command)) :
if command[i] == 'N' and v > 0:
v-=1
coor=max(i,coor)
if h > 0 :
for i in range(0,len(command )) :
if command[i] == 'E' and h > 0 :
h-=1
coor=max(i,coor)
elif h < 0 :
for i in range(0,len(command )) :
if command[i] == 'W' and h < 0 :
h+=1
coor=max(i,coor)
if h==0 and v==0 :
print(coor+1)
else :
print("-1")
else :
for i in range(0,len(command)) :
if command[i] == 'S' and v > 0:
v+=1
coor=max(i,coor)
if h > 0 :
for i in range(0,len(command )) :
if command[i] == 'E' and h > 0 :
h-=1
coor=max(i,coor)
elif h < 0 :
for i in range(0,len(command )) :
if command[i] == 'W' and h < 0 :
h+=1
coor=max(i,coor)
if h==0 and v==0 :
print(coor+1)
else :
print("-1")
``` | instruction | 0 | 39,919 | 3 | 79,838 |
No | output | 1 | 39,919 | 3 | 79,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination.
Submitted Solution:
```
n,s1,s2,e1,e2 = list(map(int,input().split()))
s = input()
x = p = e1 - s1
y = q = e2 - s2
t = len(s)
z = 0
m1 = m2 = m3 = m4 = 0
if(x<0):
for i in range(t):
if(s[i]=='W'):
x+=1
m1 = i+1
if(x==0):
break
if(x<0):
z = 1
if(y<0):
for i in range(t):
if(s[i]=='S'):
y+=1
m2 = i+1
if(y==0):
break
if(y<0):
z = 1
if(x>0):
for i in range(t):
if(s[i]=='E'):
x-=1
m3 = i+1
if(x==0):
break
if(x>0):
z = 1
if(y>0):
for i in range(t):
if(s[i]=='N'):
y-=1
m4 = i+1
if(y==0):
break
if(y>0):
z = 1
if(z==1):
print("-1")
else:
if(p<0 and q<0):
pos = max(m1,m2)
elif(p>0 and q>0):
pos = max(m3,m4)
elif(p<0 and q>0):
pos = max(m1,m4)
else:
pos = max(m2,m3)
print(pos)
``` | instruction | 0 | 39,920 | 3 | 79,840 |
No | output | 1 | 39,920 | 3 | 79,841 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).
* If the wind blows to the east, the boat will move to (x + 1, y).
* If the wind blows to the south, the boat will move to (x, y - 1).
* If the wind blows to the west, the boat will move to (x - 1, y).
* If the wind blows to the north, the boat will move to (x, y + 1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
Input
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination.
Submitted Solution:
```
t, x, y, e, u = input().split()
s = input()
t, x, y, e, u = int(t), int(x), int(y), int(e), int(u)
cnt = 0
if x == e:
if u > y:
for i in range(len(s)):
tempx = x
tempy = y
if s[i] == 'N':
y += 1
if x == e:
tempx = x
if y == u:
tempy = y
if tempx == e and tempy == u:
cnt = i + 1
if i == len(s) - 1 and (tempx != e and tempy != u):
cnt = -1
break
if cnt == 0:
cnt = -1
elif u < y:
for i in range(len(s)):
tempx = x
tempy = y
if s[i] == 'S':
y -= 1
if x == e:
tempx = x
if y == u:
tempy = y
if tempx == e and tempy == u:
cnt = i + 1
if i == len(s) - 1 and (tempx != e and tempy != u):
cnt = -1
break
if cnt == 0:
cnt = -1
elif u == y:
tempx = x
tempy = y
cnt = 0
elif e > x:
if u > y:
for i in range(len(s)):
tempx = x
tempy = y
if s[i] == 'E':
x += 1
if s[i] == 'N':
y += 1
if x == e:
tempx = x
if y == u:
tempy = y
if tempx == e and tempy == u:
cnt = i + 1
break
if i == len(s) - 1 and (tempx != e and tempy != u):
cnt = -1
break
if cnt == 0:
cnt = -1
elif u < y:
for i in range(len(s)):
tempx = x
tempy = y
if s[i] == 'E':
x += 1
if s[i] == 'S':
y -= 1
if x == e:
tempx = x
if y == u:
tempy = y
if tempx == e and tempy == u:
cnt = i + 1
if i == len(s) - 1 and (tempx != e and tempy != u):
cnt = -1
break
if cnt == 0:
cnt = -1
elif u == y:
for i in range(len(s)):
tempx = x
tempy = y
if s[i] == 'E':
x += 1
if x == e:
tempx = x
if y == u:
tempy = y
if tempx == e and tempy == u:
cnt = i + 1
if i == len(s) - 1 and (tempx != e and tempy != u):
cnt = -1
break
if cnt == 0:
cnt = -1
elif e < x:
if u > y:
for i in range(len(s)):
tempx = x
tempy = y
if s[i] == 'W':
x -= 1
if s[i] == 'N':
y += 1
if x == e:
tempx = x
if y == u:
tempy = y
if tempx == e and tempy == u:
cnt = i + 1
if i == len(s) - 1 and (tempx != e and tempy != u):
cnt = -1
break
if cnt == 0:
cnt = -1
elif u < y:
for i in range(len(s)):
tempx = x
tempy = y
if s[i] == 'W':
x -= 1
if s[i] == 'S':
y -= 1
if x == e:
tempx = x
if y == u:
tempy = y
if tempx == e and tempy == u:
cnt = i + 1
if i == len(s) - 1 and (tempx != e and tempy != u):
cnt = -1
break
if cnt == 0:
cnt = -1
elif u == y:
for i in range(len(s)):
tempx = x
tempy = y
if s[i] == 'W':
x -= 1
if x == e:
tempx = x
if y == u:
tempy = y
if tempx == e and tempy == u:
cnt = i + 1
if i == len(s) - 1 and (tempx != e and tempy != u):
cnt = -1
break
if cnt == 0:
cnt = -1
print(cnt)
``` | instruction | 0 | 39,921 | 3 | 79,842 |
No | output | 1 | 39,921 | 3 | 79,843 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience.
In that country there are n islands and m bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerful artifacts. Johnny is not interested in artifacts, but he can get some money by selling some artifact.
At the start Johnny is in the island a and the artifact-dealer is in the island b (possibly they are on the same island). Johnny wants to find some artifact, come to the dealer and sell it. The only difficulty is that bridges are too old and destroying right after passing over them. Johnnie's character can't swim, fly and teleport, so the problem became too difficult.
Note that Johnny can't pass the half of the bridge, collect the artifact and return to the same island.
Determine if Johnny can find some artifact and sell it.
Input
The first line contains two integers n and m (1 β€ n β€ 3Β·105, 0 β€ m β€ 3Β·105) β the number of islands and bridges in the game.
Each of the next m lines contains the description of the bridge β three integers xi, yi, zi (1 β€ xi, yi β€ n, xi β yi, 0 β€ zi β€ 1), where xi and yi are the islands connected by the i-th bridge, zi equals to one if that bridge contains an artifact and to zero otherwise. There are no more than one bridge between any pair of islands. It is guaranteed that it's possible to travel between any pair of islands.
The last line contains two integers a and b (1 β€ a, b β€ n) β the islands where are Johnny and the artifact-dealer respectively.
Output
If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes).
Examples
Input
6 7
1 2 0
2 3 0
3 1 0
3 4 1
4 5 0
5 6 0
6 4 0
1 6
Output
YES
Input
5 4
1 2 0
2 3 0
3 4 0
2 5 1
1 4
Output
NO
Input
5 6
1 2 0
2 3 0
3 1 0
3 4 0
4 5 1
5 3 0
1 2
Output
YES
Submitted Solution:
```
from random import *
if random() < random():
print("Yes")
else:
print("No")
``` | instruction | 0 | 40,063 | 3 | 80,126 |
No | output | 1 | 40,063 | 3 | 80,127 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience.
In that country there are n islands and m bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerful artifacts. Johnny is not interested in artifacts, but he can get some money by selling some artifact.
At the start Johnny is in the island a and the artifact-dealer is in the island b (possibly they are on the same island). Johnny wants to find some artifact, come to the dealer and sell it. The only difficulty is that bridges are too old and destroying right after passing over them. Johnnie's character can't swim, fly and teleport, so the problem became too difficult.
Note that Johnny can't pass the half of the bridge, collect the artifact and return to the same island.
Determine if Johnny can find some artifact and sell it.
Input
The first line contains two integers n and m (1 β€ n β€ 3Β·105, 0 β€ m β€ 3Β·105) β the number of islands and bridges in the game.
Each of the next m lines contains the description of the bridge β three integers xi, yi, zi (1 β€ xi, yi β€ n, xi β yi, 0 β€ zi β€ 1), where xi and yi are the islands connected by the i-th bridge, zi equals to one if that bridge contains an artifact and to zero otherwise. There are no more than one bridge between any pair of islands. It is guaranteed that it's possible to travel between any pair of islands.
The last line contains two integers a and b (1 β€ a, b β€ n) β the islands where are Johnny and the artifact-dealer respectively.
Output
If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes).
Examples
Input
6 7
1 2 0
2 3 0
3 1 0
3 4 1
4 5 0
5 6 0
6 4 0
1 6
Output
YES
Input
5 4
1 2 0
2 3 0
3 4 0
2 5 1
1 4
Output
NO
Input
5 6
1 2 0
2 3 0
3 1 0
3 4 0
4 5 1
5 3 0
1 2
Output
YES
Submitted Solution:
```
import sys
s = t = 0
pos = 1
_is = False
_ord = [0]
dp = [0]
par = [0]
Y = [False]
adj = [[]]
n, m = map(int, input().split())
for _ in range(1, n+1):
_ord.append(0)
dp.append(0)
par.append(0)
Y.append(False)
adj.append([])
for _ in range(1, m+1):
x, y, z = map(int, input().split())
adj[x].append([y, z==1])
adj[y].append([x, z==1])
def dfs(x, go):
global t, _ord, dp, pos, adj, par, Y
_ord[x] = dp[x] = pos;
pos += 1
U = False
if x==t and go:
_is = True
v = 0
for p in adj[x]:
u, z = p
if u==par[x]:
continue
if _ord[u] > 0:
par[u] = x
v = dfs(u, go or z)
dp[x] = min(v, dp[x])
if x and v<=_ord[x]:
U = True;
else:
v = _ord[u]
dp[x] = min(v, dp[x])
if x and v<=_ord[x]:
U = True;
if Y[_ord[x]]:
Y[dp[x]] = True
if U:
Y[dp[x]] = True
return dp[x]
# print(adj)
s, t = map(int, input().split())
dfs(s, 0)
while t > 0:
if Y[_ord[t]] or _is:
print('YES')
sys.exit(0)
t = par[t]
print('NO')
``` | instruction | 0 | 40,064 | 3 | 80,128 |
No | output | 1 | 40,064 | 3 | 80,129 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience.
In that country there are n islands and m bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerful artifacts. Johnny is not interested in artifacts, but he can get some money by selling some artifact.
At the start Johnny is in the island a and the artifact-dealer is in the island b (possibly they are on the same island). Johnny wants to find some artifact, come to the dealer and sell it. The only difficulty is that bridges are too old and destroying right after passing over them. Johnnie's character can't swim, fly and teleport, so the problem became too difficult.
Note that Johnny can't pass the half of the bridge, collect the artifact and return to the same island.
Determine if Johnny can find some artifact and sell it.
Input
The first line contains two integers n and m (1 β€ n β€ 3Β·105, 0 β€ m β€ 3Β·105) β the number of islands and bridges in the game.
Each of the next m lines contains the description of the bridge β three integers xi, yi, zi (1 β€ xi, yi β€ n, xi β yi, 0 β€ zi β€ 1), where xi and yi are the islands connected by the i-th bridge, zi equals to one if that bridge contains an artifact and to zero otherwise. There are no more than one bridge between any pair of islands. It is guaranteed that it's possible to travel between any pair of islands.
The last line contains two integers a and b (1 β€ a, b β€ n) β the islands where are Johnny and the artifact-dealer respectively.
Output
If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes).
Examples
Input
6 7
1 2 0
2 3 0
3 1 0
3 4 1
4 5 0
5 6 0
6 4 0
1 6
Output
YES
Input
5 4
1 2 0
2 3 0
3 4 0
2 5 1
1 4
Output
NO
Input
5 6
1 2 0
2 3 0
3 1 0
3 4 0
4 5 1
5 3 0
1 2
Output
YES
Submitted Solution:
```
from random import *
if random() > 0.5:
print("No")
else:
print("Yes")
``` | instruction | 0 | 40,065 | 3 | 80,130 |
No | output | 1 | 40,065 | 3 | 80,131 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience.
In that country there are n islands and m bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerful artifacts. Johnny is not interested in artifacts, but he can get some money by selling some artifact.
At the start Johnny is in the island a and the artifact-dealer is in the island b (possibly they are on the same island). Johnny wants to find some artifact, come to the dealer and sell it. The only difficulty is that bridges are too old and destroying right after passing over them. Johnnie's character can't swim, fly and teleport, so the problem became too difficult.
Note that Johnny can't pass the half of the bridge, collect the artifact and return to the same island.
Determine if Johnny can find some artifact and sell it.
Input
The first line contains two integers n and m (1 β€ n β€ 3Β·105, 0 β€ m β€ 3Β·105) β the number of islands and bridges in the game.
Each of the next m lines contains the description of the bridge β three integers xi, yi, zi (1 β€ xi, yi β€ n, xi β yi, 0 β€ zi β€ 1), where xi and yi are the islands connected by the i-th bridge, zi equals to one if that bridge contains an artifact and to zero otherwise. There are no more than one bridge between any pair of islands. It is guaranteed that it's possible to travel between any pair of islands.
The last line contains two integers a and b (1 β€ a, b β€ n) β the islands where are Johnny and the artifact-dealer respectively.
Output
If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes).
Examples
Input
6 7
1 2 0
2 3 0
3 1 0
3 4 1
4 5 0
5 6 0
6 4 0
1 6
Output
YES
Input
5 4
1 2 0
2 3 0
3 4 0
2 5 1
1 4
Output
NO
Input
5 6
1 2 0
2 3 0
3 1 0
3 4 0
4 5 1
5 3 0
1 2
Output
YES
Submitted Solution:
```
n,k=map(int,input().split())
arr=list(map(int,input().split()))
foe=[]
for _ in range(k):
foe.append(tuple(map(int,input().split())))
count=0
for i in arr:
t=0
for j in arr[t:]:
t+=1
x=(i,j)
if i<=j:
for p in foe:
m=0
if p[0] in arr[i-1:j] and p[1] in arr[i-1:j]:
m+=1
break
if m==0:
count+=1
print(str(count))
``` | instruction | 0 | 40,066 | 3 | 80,132 |
No | output | 1 | 40,066 | 3 | 80,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 β€ n β€ 105) β the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 β€ mi < i) β the number of marks strictly above the water on each day.
Output
Output one single integer β the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image>
Submitted Solution:
```
N = int(input())
M = list(map(int, input().split()))
# T is the total number of marks
T = [0] * N
for i in range(1, N):
# On a au moins autant de marques que la veille, mais au moins autant que le nombre vu aujourd'hui au dessus, plus celle de l'eau actuelle
T[i] = max(T[i - 1], M[i] + 1)
# Maintenant on doit vΓ©rifier que le nombre de marques peut augmenter que de 1 en 1
# Donc on vérifie en parcourant par derrière
for i in range(N - 2, -1, -1):
T[i] = max(T[i], T[i + 1] - 1, 0)
# Maintenant que l'on a le total de marques par jour ti, on sait que di = ti - (mi + 1) et on calcule leur somme
res = sum([max(0, T[i] - M[i] - 1) for i in range(N)])
# res = max(0, res)
print(res)
``` | instruction | 0 | 40,159 | 3 | 80,318 |
Yes | output | 1 | 40,159 | 3 | 80,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 β€ n β€ 105) β the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 β€ mi < i) β the number of marks strictly above the water on each day.
Output
Output one single integer β the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image>
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
ne = n*[0]
ned = 1
for i in range(n-1,-1,-1):
if a[i]+1 > ned:
ned = a[i]+1
ne[i] = ned
ned -= 1
#print(ne)
ne.append(0)
le = 1
o = 0
for i in range(n):
o += le-a[i]-1
#print(le)
if le < ne[i+1]:
le += 1
#o = le-a[i]-1
print(o)
``` | instruction | 0 | 40,160 | 3 | 80,320 |
Yes | output | 1 | 40,160 | 3 | 80,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 β€ n β€ 105) β the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 β€ mi < i) β the number of marks strictly above the water on each day.
Output
Output one single integer β the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image>
Submitted Solution:
```
import sys
n = int(input())
a = [int(x) for x in input().split(' ')]
maxm = 0
idx=0
ans=0
b=[0]*n
for i in range(n):
if a[i]>=maxm:
maxm=a[i]
idx=i
for i in range(idx,n):
b[i]=maxm+1
i=idx-1
while i>=0:
b[i]=max(a[i]+1,b[i+1]-1)
i-=1
for i in range(1,n):
if b[i]<b[i-1]:
b[i]=b[i-1]
ans+=(b[i]-1-a[i])
print(ans)
``` | instruction | 0 | 40,161 | 3 | 80,322 |
Yes | output | 1 | 40,161 | 3 | 80,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 β€ n β€ 105) β the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 β€ mi < i) β the number of marks strictly above the water on each day.
Output
Output one single integer β the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image>
Submitted Solution:
```
N = int(input())
above = list(map(int, input().split()))
if N == 1:
print(0)
quit()
required_mark = [0] * N
required_mark[N-2] = above[N-1]
for i in reversed(range(N-2)):
required_mark[i] = max(above[i+1], required_mark[i+1] - 1)
d = 0
mark = 1
for i in range(1, N):
if mark == above[i]:
mark += 1
elif mark >= required_mark[i]:
d += mark - above[i] - 1
else:
d += mark - above[i]
mark += 1
print(d)
``` | instruction | 0 | 40,162 | 3 | 80,324 |
Yes | output | 1 | 40,162 | 3 | 80,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 β€ n β€ 105) β the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 β€ mi < i) β the number of marks strictly above the water on each day.
Output
Output one single integer β the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image>
Submitted Solution:
```
import math,sys,bisect,heapq
from collections import defaultdict,Counter,deque
from itertools import groupby,accumulate
#sys.setrecursionlimit(200000000)
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda: map(int,input().split())
alele = lambda: list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
#MOD = 1000000000 + 7
def Y(c): print(["NO","YES"][c])
def y(c): print(["no","yes"][c])
def Yy(c): print(["No","Yes"][c])
N = int(input())
A = alele()
B = [-1]*N
s = deque()
for i in range(N-1,-1,-1):
if not s:
B[i] = -1
s.append((A[i],i))
else:
while s and s[-1][0] <A[i]:
s.pop()
if not s:
B[i] = -1
else:
B[i] = s[-1][1]
s.append((A[i],i))
#print(B)
Ans = 0
maxi = 0
for i in range(N):
if B[i] != -1:
x = A[B[i]]
v = x - (B[i]- i)
maxi = max(maxi,v)
maxi= max(maxi,A[i])
#print(maxi)
Ans += maxi - A[i]
print(Ans)
``` | instruction | 0 | 40,163 | 3 | 80,326 |
No | output | 1 | 40,163 | 3 | 80,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 β€ n β€ 105) β the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 β€ mi < i) β the number of marks strictly above the water on each day.
Output
Output one single integer β the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image>
Submitted Solution:
```
N = int(input())
M = list(map(int, input().split()))
s = 0
for i in range(1, N - 1):
if M[i] < M[i - 1]:
s += max(M[i - 1] - M[i], M[i + 1] - M[i])
s += max(0, M[N - 2] - M[N - 1])
print(s)
``` | instruction | 0 | 40,164 | 3 | 80,328 |
No | output | 1 | 40,164 | 3 | 80,329 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 β€ n β€ 105) β the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 β€ mi < i) β the number of marks strictly above the water on each day.
Output
Output one single integer β the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image>
Submitted Solution:
```
n = int(input())
above = list(map(int,input().strip().split()))
sum = 1
under = 0
flag = True
for i in range(1,n):
if i < n-1 and above[i] == 0 and sum < above[i+1]:
sum += 1
elif sum <= above[i]:
sum += 1
under += sum - above[i] - 1
print(under)
``` | instruction | 0 | 40,165 | 3 | 80,330 |
No | output | 1 | 40,165 | 3 | 80,331 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 β€ n β€ 105) β the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 β€ mi < i) β the number of marks strictly above the water on each day.
Output
Output one single integer β the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image>
Submitted Solution:
```
N = int(input())
M = list(map(int, input().split()))
# T is the total number of marks
T = [0] * N
for i in range(1, N):
# On a au moins autant de marques que la veille, mais au moins autant que le nombre vu aujourd'hui au dessus, plus celle de l'eau actuelle
T[i] = max(T[i - 1], M[i] + 1)
# Maintenant on doit vΓ©rifier que le nombre de marques peut augmenter que de 1 en 1
# Donc on vérifie en parcourant par derrière
for i in range(N - 2, -1, -1):
T[i] = max(T[i], T[i + 1] - 1)
# Maintenant que l'on a le total de marques par jour ti, on sait que di = ti - (mi + 1) et on calcule leur somme
res = sum([T[i] - M[i] - 1 for i in range(N)])
print(res)
``` | instruction | 0 | 40,166 | 3 | 80,332 |
No | output | 1 | 40,166 | 3 | 80,333 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations.
Max is a young biologist. For n days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer k that if the shark in some day traveled the distance strictly less than k, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to k; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least k.
The shark never returned to the same location after it has moved from it. Thus, in the sequence of n days we can find consecutive nonempty segments when the shark traveled the distance less than k in each of the days: each such segment corresponds to one location. Max wants to choose such k that the lengths of all such segments are equal.
Find such integer k, that the number of locations is as large as possible. If there are several such k, print the smallest one.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of days.
The second line contains n distinct positive integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9) β the distance traveled in each of the day.
Output
Print a single integer k, such that
1. the shark was in each location the same number of days,
2. the number of locations is maximum possible satisfying the first condition,
3. k is smallest possible satisfying the first and second conditions.
Examples
Input
8
1 2 7 3 4 8 5 6
Output
7
Input
6
25 1 2 3 14 36
Output
2
Note
In the first example the shark travels inside a location on days 1 and 2 (first location), then on 4-th and 5-th days (second location), then on 7-th and 8-th days (third location). There are three locations in total.
In the second example the shark only moves inside a location on the 2-nd day, so there is only one location. | instruction | 0 | 40,167 | 3 | 80,334 |
Tags: brute force, data structures, dsu, trees
Correct Solution:
```
# https://codeforces.com/problemset/problem/982/D
class Union:
def __init__(self, n):
self.p = {i:i for i in range(n)}
self.rank = {i:1 for i in range(n)}
def find(self, x):
if x < 0: return x
if self.p[x] != x:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, x, y):
if x < 0 or y < 0:return
x = self.find(x)
y = self.find(y)
if x != y:
if self.rank[x] < self.rank[y]:
self.p[x] = y
self.rank[y] += self.rank[x]
else:
self.p[y] = x
self.rank[x] += self.rank[y]
def add(d, key, val):
if key not in d:
d[key] = 0
d[key] += val
if d[key] == 0:
del d[key]
def update(i, used, d, u):
used[i] = True
add(d, 1, 1)
if i - 1 >= 0 and used[i-1] == True:
par_1 = u.find(i)
par_2 = u.find(i-1)
size_1 = u.rank[par_1]
size_2 = u.rank[par_2]
add(d, size_1, -1)
add(d, size_2, -1)
add(d, size_1+size_2, 1)
u.union(par_1, par_2)
if i + 1 < n and used[i+1] == True:
par_1 = u.find(i)
par_2 = u.find(i+1)
size_1 = u.rank[par_1]
size_2 = u.rank[par_2]
add(d, size_1, -1)
add(d, size_2, -1)
add(d, size_1+size_2, 1)
u.union(par_1, par_2)
n = int(input())
a_ = list(map(int, input().split()))
a = [(i, x) for i, x in enumerate(a_)]
a = sorted(a, key=lambda x:x[1])
u = Union(n)
d = {}
used = [False] * n
ans = -1
max_length = -1
for (index, val) in a:
update(index, used, d, u)
#print(d, val)
if len(d) == 1:
len_ = list(d.values())[0]
if max_length < len_:
max_length = len_
ans = val+1
print(ans)
#8
#1 12 2 11 10 3 9 4 8 5 14 6
``` | output | 1 | 40,167 | 3 | 80,335 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations.
Max is a young biologist. For n days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer k that if the shark in some day traveled the distance strictly less than k, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to k; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least k.
The shark never returned to the same location after it has moved from it. Thus, in the sequence of n days we can find consecutive nonempty segments when the shark traveled the distance less than k in each of the days: each such segment corresponds to one location. Max wants to choose such k that the lengths of all such segments are equal.
Find such integer k, that the number of locations is as large as possible. If there are several such k, print the smallest one.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of days.
The second line contains n distinct positive integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9) β the distance traveled in each of the day.
Output
Print a single integer k, such that
1. the shark was in each location the same number of days,
2. the number of locations is maximum possible satisfying the first condition,
3. k is smallest possible satisfying the first and second conditions.
Examples
Input
8
1 2 7 3 4 8 5 6
Output
7
Input
6
25 1 2 3 14 36
Output
2
Note
In the first example the shark travels inside a location on days 1 and 2 (first location), then on 4-th and 5-th days (second location), then on 7-th and 8-th days (third location). There are three locations in total.
In the second example the shark only moves inside a location on the 2-nd day, so there is only one location. | instruction | 0 | 40,168 | 3 | 80,336 |
Tags: brute force, data structures, dsu, trees
Correct Solution:
```
def union(x,y,a,each):
x1=x
y1=y
while(a[x]>0):
x=a[x]
while(a[y]>0):
y=a[y]
each[-1*a[x]]-=1
each[-1*a[y]]-=1
#print('new size ',-1*(a[x]+a[y]), ' x ',x,' y ',y)
each[-1*(a[x]+a[y])]+=1
if(a[x]>a[y]):
a[y]+=a[x]
a[x]=y
res=y
else:
a[x]+=a[y]
a[y]=x
res=x
while(x1!=x):
temp=x1
x1=a[x1]
a[temp]=res
while(y1!=y):
temp=y1
y1=a[y1]
a[temp]=res
def find(x,y,a):
x1=x
y1=y
while(a[x]>0):
x=a[x]
while(a[y]>0):
y=a[y]
ans=False
if(y==x):
ans=True
while(y1!=y):
temp=a[y1]
a[y1]=y
y1=temp
while(x1!=x):
temp=a[x1]
a[x1]=x
x1=temp
return ans
def getsize(x,par):
x1=x
while(par[x]>0):
x=par[x]
ans=-1*par[x]
while(x!=x1):
temp=par[x1]
par[x1]=x
x1=temp
return ans
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
a[i]=[a[i],i]
a.sort()
par=[-2.1 for i in range(n+1)]
max1=0
maxk=a[0][0]-1
numloc=0
each=[0 for i in range(n+1)]
i=0
while(i<n):
curk=a[i][0]
while(i<n and a[i][0]==curk):
loc=a[i][1]
if(loc==0):
par[0]=-1
numloc+=1
each[1]+=1
if(par[1]!=-2.1):
numloc-=1
union(0,1,par,each)
elif(loc==n-1):
par[n-1]=-1
numloc+=1
each[1]+=1
if(par[n-2]!=-2.1):
numloc-=1
union(n-1,n-2,par,each)
else:
numloc+=1
each[1]+=1
par[loc]=-1
if(par[loc-1]!=-2.1):
union(loc,loc-1,par,each)
numloc-=1
if(par[loc+1]!=-2.1):
union(loc,loc+1,par,each)
numloc-=1
i+=1
#print('num loc is',numloc, ' k is ',curk)
#print(*each)
#print(*par)
#print('numl ', numloc,' size ',getsize(loc,par))
if(each[getsize(loc,par)]==numloc):
if(numloc>max1):
maxk=curk
max1=numloc
print(maxk+1)
``` | output | 1 | 40,168 | 3 | 80,337 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.