message stringlengths 2 67k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 463 109k | cluster float64 19 19 | __index_level_0__ int64 926 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As we all know Barney's job is "PLEASE" and he has not much to do at work. That's why he started playing "cups and key". In this game there are three identical cups arranged in a line from left to right. Initially key to Barney's heart is under the middle cup.
<image>
Then at one turn Barney swaps the cup in the middle with any of other two cups randomly (he choses each with equal probability), so the chosen cup becomes the middle one. Game lasts n turns and Barney independently choses a cup to swap with the middle one within each turn, and the key always remains in the cup it was at the start.
After n-th turn Barney asks a girl to guess which cup contains the key. The girl points to the middle one but Barney was distracted while making turns and doesn't know if the key is under the middle cup. That's why he asked you to tell him the probability that girl guessed right.
Number n of game turns can be extremely large, that's why Barney did not give it to you. Instead he gave you an array a1, a2, ..., ak such that
<image>
in other words, n is multiplication of all elements of the given array.
Because of precision difficulties, Barney asked you to tell him the answer as an irreducible fraction. In other words you need to find it as a fraction p / q such that <image>, where <image> is the greatest common divisor. Since p and q can be extremely large, you only need to find the remainders of dividing each of them by 109 + 7.
Please note that we want <image> of p and q to be 1, not <image> of their remainders after dividing by 109 + 7.
Input
The first line of input contains a single integer k (1 ≤ k ≤ 105) — the number of elements in array Barney gave you.
The second line contains k integers a1, a2, ..., ak (1 ≤ ai ≤ 1018) — the elements of the array.
Output
In the only line of output print a single string x / y where x is the remainder of dividing p by 109 + 7 and y is the remainder of dividing q by 109 + 7.
Examples
Input
1
2
Output
1/2
Input
3
1 1 1
Output
0/1
Submitted Solution:
```
mod = 10**9 + 7
k = int(input())
arr = [int(j) for j in input().split()]
findeven = 0
for i in range(k):
if arr[i]%2 == 0:
findeven = 1
cur = pow(2, arr[0], mod)
for i in range(1, k):
cur = pow(cur, arr[i], mod)
# print(cur)
inv2 = pow(2, mod-2, mod)
if findeven == 1:
lr = (cur-1)//3
m = (cur-1)//3 + 1
else:
lr = (cur-2)//3 + 1
m = (cur-2)//3
lr = lr*inv2 % mod
m = m*inv2 % mod
cur = cur*inv2 % mod
# print(lr, m, lr)
print(m, '/', cur, sep='')
``` | instruction | 0 | 71,255 | 19 | 142,510 |
No | output | 1 | 71,255 | 19 | 142,511 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim.
Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game.
Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
Input
The first line of the input contains two integers n (1 ≤ n ≤ 109) and x (1 ≤ x ≤ 100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1.
Output
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10 - 6.
Example
Input
2 2
0.500000 0.250000 0.250000
Output
0.62500000 | instruction | 0 | 71,256 | 19 | 142,512 |
Tags: games, matrices, probabilities
Correct Solution:
```
import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
n,x=map(int,input().split())
def mult(a,b):
# compute a*b
c=[0]*128
for i in range(128):
for j in range(128):
c[i^j]+=a[i]*b[j]
return c
def quickpow(a,b):
# compute a**b
if b==1:
return a
if b&1:
return mult(quickpow(mult(a,a),b//2),a)
return quickpow(mult(a,a),b//2)
prob=list(map(float,input().split()))
prob+=[0.0]*(128-len(prob))
print("%.9f"%(1-quickpow(prob,n)[0]))
``` | output | 1 | 71,256 | 19 | 142,513 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim.
Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game.
Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
Input
The first line of the input contains two integers n (1 ≤ n ≤ 109) and x (1 ≤ x ≤ 100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1.
Output
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10 - 6.
Example
Input
2 2
0.500000 0.250000 0.250000
Output
0.62500000 | instruction | 0 | 71,257 | 19 | 142,514 |
Tags: games, matrices, probabilities
Correct Solution:
```
import sys
readline = sys.stdin.readline
n, x = map(int,readline().split())
tmp = list(map(float,readline().split()))
for i in range(128 - x - 1):
tmp.append(0)
def MatM(a,b):
c = [0 for i in range(128)]
for i in range(128):
for j in range(128):
c[i ^ j] += a[i] * b[j]
return c
def MatPow(mat,p):
if p == 1:
return tmp.copy()
res = MatPow(mat, p // 2)
newMat = MatM(res,res)
if p & 1:
return MatM(newMat, mat)
return newMat
print( ('%.16f'%(1 - MatPow(tmp,n)[0])) )
``` | output | 1 | 71,257 | 19 | 142,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim.
Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game.
Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
Input
The first line of the input contains two integers n (1 ≤ n ≤ 109) and x (1 ≤ x ≤ 100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1.
Output
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10 - 6.
Example
Input
2 2
0.500000 0.250000 0.250000
Output
0.62500000 | instruction | 0 | 71,258 | 19 | 142,516 |
Tags: games, matrices, probabilities
Correct Solution:
```
import os
from io import BytesIO
#input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
def main():
def mult(m1,m2):
ans = [0] * 128
for i in range(128):
for j in range(128):
ans[i^j] += m1[i] * m2[j]
return ans
def power(number, n):
res = number
while(n):
if n & 1:
res = mult(res,number)
number = mult(number,number)
n >>= 1
return res
n,x = map(int,input().split())
ai = list(map(float,input().split()))
ai += [0]*(128-len(ai))
if n == 1:
print(1 - ai[0])
return
ansm = power(ai,n-1)
print(1 - ansm[0])
main()
``` | output | 1 | 71,258 | 19 | 142,517 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim.
Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game.
Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
Input
The first line of the input contains two integers n (1 ≤ n ≤ 109) and x (1 ≤ x ≤ 100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1.
Output
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10 - 6.
Example
Input
2 2
0.500000 0.250000 0.250000
Output
0.62500000 | instruction | 0 | 71,259 | 19 | 142,518 |
Tags: games, matrices, probabilities
Correct Solution:
```
from sys import stdin,stdout
def mult(x,y):
z = [0]*128
for i in range(128):
for j in range(128):
z[i^j] += x[i]*y[j]
return z
n,x = map(int,stdin.readline().split())
a = list(map(float,stdin.readline().split()))
for _ in range(x,128):
a.append(0)
ans = [0]*128
ans[0] = 1
while n > 0:
if n % 2 == 1:
ans = mult(ans,a)
a = mult(a,a)
n //= 2
stdout.write(str(1-ans[0]))
``` | output | 1 | 71,259 | 19 | 142,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim.
Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game.
Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
Input
The first line of the input contains two integers n (1 ≤ n ≤ 109) and x (1 ≤ x ≤ 100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1.
Output
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10 - 6.
Example
Input
2 2
0.500000 0.250000 0.250000
Output
0.62500000
Submitted Solution:
```
from sys import stdin,stdout
from decimal import Decimal
from functools import reduce
n,x = map(int,stdin.readline().split())
print(1 - reduce(lambda x,y: x +y*y,map(Decimal,stdin.readline().split()),Decimal(0)))
``` | instruction | 0 | 71,260 | 19 | 142,520 |
No | output | 1 | 71,260 | 19 | 142,521 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim.
Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game.
Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
Input
The first line of the input contains two integers n (1 ≤ n ≤ 109) and x (1 ≤ x ≤ 100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1.
Output
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10 - 6.
Example
Input
2 2
0.500000 0.250000 0.250000
Output
0.62500000
Submitted Solution:
```
import sys
readline = sys.stdin.readline
n, x = map(int,readline().split())
tmp = list(map(float,readline().split()))
for i in range(128 - x - 1):
tmp.append(0)
def MatM(a,b):
c = [0 for i in range(128)]
for i in range(127):
for j in range(127):
c[i ^ j] += a[i] * b[j]
return c
def MatPow(mat,p):
if p == 1:
return tmp.copy()
res = MatPow(mat, p // 2)
newMat = MatM(res,res)
if p & 1:
return MatM(newMat, mat)
return newMat
print( ('%.16f'%(1 - MatPow(tmp,n)[0])) )
``` | instruction | 0 | 71,261 | 19 | 142,522 |
No | output | 1 | 71,261 | 19 | 142,523 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim.
Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game.
Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
Input
The first line of the input contains two integers n (1 ≤ n ≤ 109) and x (1 ≤ x ≤ 100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1.
Output
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10 - 6.
Example
Input
2 2
0.500000 0.250000 0.250000
Output
0.62500000
Submitted Solution:
```
import sys
readline = sys.stdin.readline
n, x = map(int,readline().split())
tmp = list(map(float,readline().split()))
for i in range(128 - x - 1):
tmp.append(0)
def MatM(a,b):
c = [0 for i in range(128)]
for i in range(127):
for j in range(127):
c[i ^ j] += a[i] * b[j]
return c
def MatPow(mat,p):
if p == 1:
return tmp.copy()
res = MatPow(mat, p // 2)
newMat = MatM(res,res)
if p & 1:
return MatM(newMat, mat)
return newMat
print(1 - MatPow(tmp,n)[0])
``` | instruction | 0 | 71,262 | 19 | 142,524 |
No | output | 1 | 71,262 | 19 | 142,525 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim.
Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game.
Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
Input
The first line of the input contains two integers n (1 ≤ n ≤ 109) and x (1 ≤ x ≤ 100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1.
Output
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10 - 6.
Example
Input
2 2
0.500000 0.250000 0.250000
Output
0.62500000
Submitted Solution:
```
import os
from io import BytesIO
#input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
def main():
n = 0
r = 0
r2 = 0
class Matrix():
def __init__(self,ar):
self.ar = ar
def __mul__(self,other):
ans = [0 for i in r]
for i in r2:
i2 = i * n
for j in r2:
for k in r2:
ans[i2+j] += self.ar[i2+k] * other.ar[k*n+j]
return Matrix(ans)
def __mod__(self,other):
for i in r:
self.ar[i] %= other
return self
mod = 10**9 + 7
def power(number, n):
res = number
while(n):
if n & 1:
res *= number
number *= number
n >>= 1
return res
n,x = map(int,input().split())
n2= n
x += 1
n = x
r = range(x*x)
r2 = range(x)
ai = list(map(float,input().split()))
ar = [1 for i in r]
for i in r2:
for j in range(i,n):
temp = ((i ^ j) != 0) * ai[i] * ai[j]
ar[i*n+j] = temp
ar[j*n+i] = temp
m1 = Matrix(ar)
if n2 == 1:
print(1 - ai[0])
elif n2 == 2:
ans = 0
for i in r:
ans += ar[i]
print(ans)
else:
print(ar)
ansm = power(m1,n2-2).ar
print(ansm)
ans = 0
for i in r:
ans += ansm[i]
print(ans)
main()
``` | instruction | 0 | 71,263 | 19 | 142,526 |
No | output | 1 | 71,263 | 19 | 142,527 |
Provide a correct Python 3 solution for this coding contest problem.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1 | instruction | 0 | 71,531 | 19 | 143,062 |
"Correct Solution:
```
from collections import deque
def main():
grid = []
h,w = 0,0
y = [1,0,-1,0]
x = [0,1,0,-1]
inf = 11
gy,gx = 0,0
ans = inf
def inside(i,j):
return 0<=i and i<h and 0<=j and j<w
def dfs(cy,cx,count):
nonlocal ans
if 10<count:return inf
if ans<=count:return inf
res = inf
for i in range(4):
ny = cy+y[i]
nx = cx+x[i]
if not inside(ny,nx):continue
if grid[ny][nx] == 1:continue
while True:
if ny==gy and nx==gx:
ans = min(ans,count+1)
return count+1
if not inside(ny+y[i],nx+x[i]):break
if grid[ny+y[i]][nx+x[i]] == 1:break
ny += y[i]
nx += x[i]
if not inside(ny+y[i],nx+x[i]):continue
grid[ny+y[i]][nx+x[i]] = 0
res = min(res,dfs(ny,nx,count+1))
grid[ny+y[i]][nx+x[i]] = 1
return res
while True:
w,h = map(int,input().split())
if h == 0:break
grid = []
ans = inf
for _ in range(h):
grid.append(list(map(int,input().split())))
sy,sx = 0,0
for i in range(h):
for j in range(w):
if grid[i][j] == 2:
grid[i][j] = 0
sy,sx = i,j
if grid[i][j] == 3:
grid[i][j] = 0
gy,gx = i,j
res = dfs(sy,sx,0)
if res == inf:res = -1
print(res)
if __name__ == '__main__':
main()
``` | output | 1 | 71,531 | 19 | 143,063 |
Provide a correct Python 3 solution for this coding contest problem.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1 | instruction | 0 | 71,532 | 19 | 143,064 |
"Correct Solution:
```
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
N_MOVE = 4
EMPTY = 0
ROCK = 1
START = 2
GOAL = 3
INF = 100000
def in_field(field, x, y):
return y >=0 and y < len(field) and x >= 0 and x< len(field[0])
def move_to_rock(field, x, y, direction):
while(True):
x += dx[direction]
y += dy[direction]
if not in_field(field, x, y):
return None
elif field[y][x] == ROCK:
x -= dx[direction]
y -= dy[direction]
break
elif field[y][x] == GOAL:
break
return x, y
def dfs(depth, field, x, y):
if depth > 10:
return None
cost = INF
for r in range(N_MOVE):
nx, ny = x + dx[r], y + dy[r]
if not in_field(field, nx, ny):
continue
if field[ny][nx] == ROCK:
continue
next_pos = move_to_rock(field, x, y, r)
if next_pos is None:
continue
nx, ny = next_pos
if field[ny][nx] == GOAL:
return depth
rock_pos_x, rock_pos_y = nx+dx[r], ny+dy[r]
assert field[rock_pos_y][rock_pos_x] == ROCK
field[rock_pos_y][rock_pos_x] = EMPTY
result = dfs(depth+1, field, nx, ny)
if result is not None:
cost = min(cost, result)
field[rock_pos_y][rock_pos_x] = ROCK
return cost
def find_start_pos(field):
h = len(field)
w = len(field[0])
for y in range(h):
for x in range(w):
if field[y][x] == START:
return x, y
return None # ?
def solve():
w, h = map(int, input().split())
if w == 0 or h == 0:
return None
field = []
for y in range(h):
field.append(list(map(int, input().split())))
x, y = find_start_pos(field)
res = dfs(1, field, x, y)
return res
if __name__ == '__main__':
while(True):
answer = solve()
if answer is None:
break
elif answer == INF:
print(-1)
else:
print(answer)
``` | output | 1 | 71,532 | 19 | 143,065 |
Provide a correct Python 3 solution for this coding contest problem.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1 | instruction | 0 | 71,533 | 19 | 143,066 |
"Correct Solution:
```
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
N_MOVE = 4
EMPTY = 0
ROCK = 1
START = 2
GOAL = 3
INF = 100000
def in_field(field, x, y):
return y >=0 and y < len(field) and x >= 0 and x< len(field[0])
def move_to_rock(field, x, y, direction):
while(True):
x += dx[direction]
y += dy[direction]
if not in_field(field, x, y):
return None
elif field[y][x] == ROCK:
x -= dx[direction]
y -= dy[direction]
break
elif field[y][x] == GOAL:
break
return x, y
def dfs(depth, field, x, y):
if depth > 10:
return None
cost = INF
for r in range(N_MOVE):
if cost <= depth:
return cost
nx, ny = x + dx[r], y + dy[r]
if not in_field(field, nx, ny):
continue
if field[ny][nx] == ROCK:
continue
next_pos = move_to_rock(field, x, y, r)
if next_pos is None:
continue
nx, ny = next_pos
if field[ny][nx] == GOAL:
return depth
rock_pos_x, rock_pos_y = nx+dx[r], ny+dy[r]
assert field[rock_pos_y][rock_pos_x] == ROCK
field[rock_pos_y][rock_pos_x] = EMPTY
result = dfs(depth+1, field, nx, ny)
if result is not None:
cost = min(cost, result)
field[rock_pos_y][rock_pos_x] = ROCK
return cost
def find_start_pos(field):
h = len(field)
w = len(field[0])
for y in range(h):
for x in range(w):
if field[y][x] == START:
return x, y
return None # ?
def solve():
w, h = map(int, input().split())
if w == 0 or h == 0:
return None
field = []
for y in range(h):
field.append(list(map(int, input().split())))
x, y = find_start_pos(field)
res = dfs(1, field, x, y)
return res
if __name__ == '__main__':
while(True):
answer = solve()
if answer is None:
break
elif answer == INF:
print(-1)
else:
print(answer)
``` | output | 1 | 71,533 | 19 | 143,067 |
Provide a correct Python 3 solution for this coding contest problem.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1 | instruction | 0 | 71,534 | 19 | 143,068 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
from math import sqrt
from collections import deque
def inpl(): return list(map(int, input().split()))
W, H = inpl()
while H:
G = []
for h in range(H):
tmp = []
for w, x in enumerate(inpl()):
tmp.append(x==1)
if x == 2:
s = (h, w)
if x == 3:
g = (h, w)
G.append(tmp)
Q = deque()
Q.append([s, 0, []])
found = False
while Q:
s, turn, deleated = Q.popleft()
for h, w in deleated:
G[h][w] = False
for dh, dw in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
if found:
break
h, w = s
for step in range(30):
nh, nw = h+dh, w+dw
if not ((0 <= nh < H) & (0 <= nw < W)):
break
if (nh, nw) == g:
print(turn+1)
found = True
break
elif G[nh][nw] == True:
if turn != 9 and step > 0:
Q.append([(h, w), turn+1, deleated + [(nh, nw)]])
break
h, w = nh, nw
for h, w in deleated:
G[h][w] = True
if found:
break
else:
print(-1)
W, H = inpl()
``` | output | 1 | 71,534 | 19 | 143,069 |
Provide a correct Python 3 solution for this coding contest problem.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1 | instruction | 0 | 71,535 | 19 | 143,070 |
"Correct Solution:
```
def getNewBoard(board, blockRow, blockCol):
newBoard = []
for row in board:
newBoard.append(row[:])
newBoard[blockRow][blockCol] = 0
return newBoard
def isValidCoord(row, col, ROWS, COLS):
return row >= 0 and row < ROWS and col >= 0 and col < COLS
def throw(direction, board, ROWS, COLS, sRow, sCol, eRow, eCol):
if direction == 'U':
rAdj = -1
cAdj = 0
if not isValidCoord(sRow + rAdj, sCol + cAdj, ROWS, COLS) \
or board[sRow + rAdj][sCol + cAdj] == 1:
return None, None, None
if direction == 'D':
rAdj = 1
cAdj = 0
if not isValidCoord(sRow + rAdj, sCol + cAdj, ROWS, COLS) \
or board[sRow + rAdj][sCol + cAdj] == 1:
return None, None, None
if direction == 'L':
rAdj = 0
cAdj = -1
if not isValidCoord(sRow + rAdj, sCol + cAdj, ROWS, COLS) \
or board[sRow + rAdj][sCol + cAdj] == 1:
return None, None, None
if direction == 'R':
rAdj = 0
cAdj = 1
if not isValidCoord(sRow + rAdj, sCol + cAdj, ROWS, COLS) \
or board[sRow + rAdj][sCol + cAdj] == 1:
return None, None, None
while True:
sRow += rAdj
sCol += cAdj
if not isValidCoord(sRow, sCol, ROWS, COLS):
return None, None, None
if sRow == eRow and sCol == eCol:
return None, sRow, sCol
if isValidCoord(sRow + rAdj, sCol + cAdj, ROWS, COLS) and board[sRow + rAdj][sCol + cAdj] == 1:
return getNewBoard(board, sRow + rAdj, sCol + cAdj), sRow, sCol
def play(board, ROWS, COLS, sRow, sCol, eRow, eCol, numThrows):
global minThrows
if board == None and sRow == None and sCol == None:
return
if minThrows != None and numThrows >= minThrows:
return
if numThrows > 10:
return
if sRow == eRow and sCol == eCol:
minThrows = numThrows if minThrows == None else min(minThrows, numThrows)
return
directions = ['U', 'D', 'L', 'R']
for d in directions:
if numThrows < 10:
newBoard, newRow, newCol = throw(d, board, ROWS, COLS, sRow, sCol, eRow, eCol)
play(newBoard, ROWS, COLS, newRow, newCol, eRow, eCol, numThrows + 1)
if __name__ == '__main__':
while True:
COLS, ROWS = [ int(x) for x in list(filter(lambda x: x != '', \
input().strip().split(' '))) ]
if ROWS == 0 and COLS == 0:
break
sRow = None
sCol = None
eRow = None
eCol = None
board = []
for r in range(ROWS):
row = [ int(x) for x in list(filter(lambda x: x != '', \
input().strip().split(' '))) ]
board.append(row)
if sRow == None and sCol == None and 2 in row:
sRow = r
sCol = row.index(2)
board[sRow][sCol] = 0
if eRow == None and eCol == None and 3 in row:
eRow = r
eCol = row.index(3)
minThrows = None
play(board, ROWS, COLS, sRow, sCol, eRow, eCol, 0)
print(minThrows if minThrows != None else -1)
``` | output | 1 | 71,535 | 19 | 143,071 |
Provide a correct Python 3 solution for this coding contest problem.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1 | instruction | 0 | 71,536 | 19 | 143,072 |
"Correct Solution:
```
from collections import deque
import sys
input = sys.stdin.readline
def main():
while True:
w, h = map(int, input().split())
if w == 0:
break
# 読み込み
board = [list(map(int, input().split())) for i in range(h)]
# 地図を整形
# wall : ID( 1~ )
# air: 0
s_x, s_y, g_x, g_y = 0, 0, 0, 0; id = 1;
for y in range(h):
for x in range(w):
if board[y][x] == 1:
board[y][x] = id
id += 1
elif board[y][x] == 2:
s_x = x; s_y = y;
board[y][x] = 0
elif board[y][x] == 3:
g_x = x; g_y = y;
board[y][x] = 0
# スタートから幅優先探索
search_list = deque()
search_list.append([s_x, s_y, 0, 0, 0, [0], 1])
ans = -1
check = []
while len(search_list) > 0:
# print(search_list)
x, y, step, dx, dy, through, count = search_list.popleft()
if x == g_x and y == g_y:
ans = step
break
if dx == 0 and dy == 0:# 動き始めの処理
if step+1 < 11:
for next_x, next_y in [[x-1, y], [x+1, y], [x, y+1], [x, y-1]]:
if 0 <= next_x and next_x < w and 0 <= next_y and next_y < h:
tmp = board[next_y][next_x]
if tmp in through[:count]:
search_list.append([next_x, next_y, step+1, next_x-x, next_y-y, through[:count], count])
else:# 壁にぶつかるまで滑る
t_x = x; t_y = y;
drop = False
goal = False
while True:
n_x = t_x + dx; n_y = t_y + dy;
if n_x == g_x and n_y == g_y:
ans = step
goal = True
break
if 0 <= n_x and n_x < w and 0 <= n_y and n_y < h:
if board[n_y][n_x] not in through[:count]:
break
else:
drop = True
break
t_x += dx; t_y += dy;
if goal:
break
if not drop:
tmp = board[t_y+dy][t_x+dx]
through = through[:count] + [tmp]
search_list.append([t_x, t_y, step, 0, 0, through, count+1])
print(ans)
if __name__ == "__main__":
main()
``` | output | 1 | 71,536 | 19 | 143,073 |
Provide a correct Python 3 solution for this coding contest problem.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1 | instruction | 0 | 71,537 | 19 | 143,074 |
"Correct Solution:
```
#提出が大幅に遅れてしまい、本当に申し訳ありません。
#着席位置は左*奥
#問題は「Curling 2.0」(http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1144&lang=jp)
#要は全探索しろという話なのですが、DFSで5日ほど頑張って結局バグが取れなかったのと、
#枝刈りができなかったのとが原因でQueueを使ったBFSに切り替えました
#無限にバグを出したので実装が相当気持ち悪い事になっています
from queue import Queue as q
#以下、しばらく上下左右それぞれの移動の関数です
#移動はとにかく一方向に見ていって、ゴールなら終わり、壁なら次の状態を返す、端までいったらNoneを返すものです
#ただし、試行回数が10を超えてしまったら強制的にNoneを返します
def l_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if j == 0 or field[i][j-1] == 1 or n == 0:
return(None)
while(j):
if field[i][j-1] == 0:
j -= 1
elif field[i][j-1] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i] + [field[i][:j-1] + [0] + field[i][j:]] + field[i+1:]
n -= 1
return([nfield,n,i,j])
return(None)
def u_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if i == 0 or field[i-1][j] == 1 or n == 0:
return(None)
while(i):
if field[i-1][j] == 0:
i -= 1
elif field[i-1][j] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i-1] + [field[i-1][:j] + [0] + field[i-1][j+1:]] + field[i:]
n -= 1
return([nfield,n,i,j])
return(None)
def r_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if j == y-1 or field[i][j+1] == 1 or n == 0:
return(None)
while(j < y-1):
if field[i][j+1] == 0:
j += 1
elif field[i][j+1] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i] + [field[i][:j+1] + [0] + field[i][j+2:]] + field[i+1:]
n -= 1
return([nfield,n,i,j])
return(None)
def d_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if i == x-1 or field[i+1][j] == 1 or n == 0:
return(None)
while(i < x-1):
if field[i+1][j] == 0:
i += 1
elif field[i+1][j] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i+1] + [field[i+1][:j] + [0] + field[i+1][j+1:]] + field[i+2:]
n -= 1
return([nfield,n,i,j])
return(None)
while(True):
flag = False #これがTrueになったら出力して次のデータセット
y,x = map(int,input().split()) #入力部
lis = q() #BFS用のQueue
if x == 0:
break
field = [] #これはカーリングの盤面
for i in range(x):
field.append(list(map(int, input().split()))) #盤面の入力部
for i in range(x):
for j in range(y):
if field[i][j] == 2:
start_x, start_y = i,j #開始位置を探す
field[i][j] = 0
data = [field,10,start_x,start_y] #これから扱うデータは[盤面、残り試行回数、石のx座標、y座標]とします
lis.put(data) #とりあえずQueueに初期状態をぶっこむ
while(lis.qsize()):
#後はひたすらQueueから状態を持ってきて上下左右に動かしてQueueに入れ直すことを繰り返します
#ゴールについた瞬間に出力して次のデータセットに移ります
#一番苦労したのはPythonだとシャローコピーのせいで盤面を直接いじれない点でした
#かといって二次元配列のディープコピーはめちゃくちゃ遅くTLEが取れませんでした
#仕方ないので上の実装にあるように無理やり盤面を作る方針にしました
d = lis.get()
tmp = l_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = u_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = d_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = r_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
#最後までたどり着かなかったら-1を出力して終わり
if not flag:
print(-1)
``` | output | 1 | 71,537 | 19 | 143,075 |
Provide a correct Python 3 solution for this coding contest problem.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1 | instruction | 0 | 71,538 | 19 | 143,076 |
"Correct Solution:
```
from queue import Queue as q
from copy import deepcopy as cp
def l_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if j == 0 or field[i][j-1] == 1 or n == 0:
return(None)
while(j):
if field[i][j-1] == 0:
j -= 1
elif field[i][j-1] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i] + [field[i][:j-1] + [0] + field[i][j:]] + field[i+1:]
n -= 1
return([nfield,n,i,j])
return(None)
def u_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if i == 0 or field[i-1][j] == 1 or n == 0:
return(None)
while(i):
if field[i-1][j] == 0:
i -= 1
elif field[i-1][j] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i-1] + [field[i-1][:j] + [0] + field[i-1][j+1:]] + field[i:]
n -= 1
return([nfield,n,i,j])
return(None)
def r_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if j == y-1 or field[i][j+1] == 1 or n == 0:
return(None)
while(j < y-1):
if field[i][j+1] == 0:
j += 1
elif field[i][j+1] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i] + [field[i][:j+1] + [0] + field[i][j+2:]] + field[i+1:]
n -= 1
return([nfield,n,i,j])
return(None)
def d_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if i == x-1 or field[i+1][j] == 1 or n == 0:
return(None)
while(i < x-1):
if field[i+1][j] == 0:
i += 1
elif field[i+1][j] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i+1] + [field[i+1][:j] + [0] + field[i+1][j+1:]] + field[i+2:]
n -= 1
return([nfield,n,i,j])
return(None)
while(True):
flag = False
y,x = map(int,input().split())
lis = q()
if x == 0:
break
field = []
for i in range(x):
field.append(list(map(int, input().split())))
for i in range(x):
for j in range(y):
if field[i][j] == 2:
start_x, start_y = i,j
field[i][j] = 0
data = [field,10,start_x,start_y]
lis.put(data)
while(lis.qsize()):
d = lis.get()
tmp = l_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = u_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = d_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = r_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
if not flag:
print(-1)
``` | output | 1 | 71,538 | 19 | 143,077 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1
Submitted Solution:
```
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15
Dy = (-1,0,1,0)
Dx = (0,1,0,-1)
MAXH = 25
MAXW = 25
dist = [[100]*MAXW for _ in range(MAXH)]
grid = [[-1]*MAXH for _ in range(MAXH)]
def dfs(ny,nx,t,H,W):
if t == 11:
return
for dy,dx in zip(Dy,Dx):
y,x = ny,nx
before_y,before_x = ny,nx
while True:
y += dy
x += dx
if y < 0 or y >= H:
break
if x < 0 or x >= W:
break
if grid[y][x] == 1:
if (before_y,before_x) != (ny,nx):
grid[y][x] = 0
if dist[before_y][before_x] > t:
dist[before_y][before_x] = t
dfs(before_y,before_x,t + 1,H,W)
grid[y][x] = 1
break
if grid[y][x] == 3:
dist[y][x] = min(t,dist[y][x])
return
before_y = y
before_x = x
def solve(H,W):
sy,sx,gy,gx = -1,-1,-1,-1
for i in range(H):
A = list(map(int,input().split()))
for j in range(W):
if A[j] == 2:
sy,sx = i,j
if A[j] == 3:
gy,gx = i,j
grid[i][j] = A[j]
dist[i][j] = 100
dist[sy][sx] = 0
dfs(sy,sx,1,H,W)
ans = dist[gy][gx] if dist[gy][gx] <= 10 else -1
print(ans)
def main():
while True:
W,H = map(int,input().split())
if H == 0 and W == 0:
return
solve(H,W)
if __name__ == '__main__':
main()
``` | instruction | 0 | 71,539 | 19 | 143,078 |
Yes | output | 1 | 71,539 | 19 | 143,079 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1
Submitted Solution:
```
ans_list = []
def main():
while True:
ans = solve()
if ans == "end":
break
ans_list.append(ans)
for ans in ans_list:
print(ans)
def solve():
W,H = map(int,input().split())
if (H,W) == (0,0):
return "end"
grid = [list(map(int,input().split())) for _ in range(H)]
for i,line in enumerate(grid):
for j,p in enumerate(line):
if p == 2:
si,sj = i,j
around = [(0,1),(1,0),(0,-1),(-1,0)]
res = [11]
def dfs(i=si,j=sj,d=0):
if d == 10:
return
for di,dj in around:
ni = i; nj = j; nd = d
# すぐ次のマスに障害物がある場合は進めない
if not(0 <= ni+di < H and 0 <= nj+dj < W):
continue
if grid[ni+di][nj+dj] == 1:
continue
ni += di; nj += dj; nd += 1
while True:
if grid[ni][nj] == 3:
res[0] = min(res[0],nd)
break
# はみ出たら終わり
if not(0 <= ni+di < H and 0 <= nj+dj < W):
break
# 壁に当たったらそこを支点に探索する
if grid[ni+di][nj+dj] == 1:
grid[ni+di][nj+dj] = 0
dfs(ni,nj,nd)
grid[ni+di][nj+dj] = 1
break
ni += di; nj += dj
dfs()
if res[0] == 11:
res[0] = -1
return res[0]
main()
``` | instruction | 0 | 71,540 | 19 | 143,080 |
Yes | output | 1 | 71,540 | 19 | 143,081 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1
Submitted Solution:
```
ans = 1145141919
def dfs(board,y,x,dis,count):
global ans
if count <= 10 and count < ans:
if (board[y][x] == '3'):
ans = min(ans,count)
else:
if (0 <= y+dis[0] < h) and (0 <= x+dis[1] < w):
if board[y+dis[0]][x+dis[1]] != '1':
dfs(board,y+dis[0],x+dis[1],dis,count)
else:
board[y+dis[0]][x+dis[1]] = '0'
for ddd in dydx:
ppy = y + ddd[0]
ppx = x + ddd[1]
if (0 <= ppy < h) and (0 <= ppx < w):
if (board[ppy][ppx] != '1'):
dfs(board,ppy,ppx,(ddd[0],ddd[1]),count+1)
board[y+dis[0]][x+dis[1]] = '1'
while True:
try:
w,h = map(int , input().split())
if ((not w) and (not h)):
break
board = [list(input().split()) for i in range(h)]
for y,row in enumerate(board):
try:
spos = (y,row.index("2"))
break
except ValueError:
pass
dydx = [[-1,0],[1,0],[0,-1],[0,1]]
for dd in dydx:
py = spos[0]+dd[0]
px = spos[1]+dd[1]
if (0 <= py < h) and (0 <= px < w):
if (board[py][px] != '1'):
dfs(board,py,px,(dd[0],dd[1]),1)
if ans == 1145141919 :
print('-1')
else:
print(ans)
ans = 1145141919
except:
break
``` | instruction | 0 | 71,541 | 19 | 143,082 |
Yes | output | 1 | 71,541 | 19 | 143,083 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1
Submitted Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
rr = []
while True:
w,h = LI()
if h == 0:
break
a = [LI() for _ in range(h)]
s = t = None
for i in range(h):
for j in range(w):
if a[i][j] == 2:
s = (i,j)
a[i][j] = 0
elif a[i][j] == 3:
t = (i,j)
q = set([(s,tuple())])
tr = -1
for i in range(1,11):
nq = set()
for c,l in q:
l = list(l)
for di,dj in dd:
ni = c[0]
nj = c[1]
while 0 <= ni < h and 0 <= nj < w and (a[ni][nj] == 0 or (ni,nj) in l):
ni += di
nj += dj
if (ni,nj) == t:
tr = i
break
if ni < 0 or ni >= h or nj < 0 or nj >= w:
continue
ni -= di
nj -= dj
if ni != c[0] or nj != c[1]:
nq.add(((ni,nj), tuple(sorted(l + [(ni+di,nj+dj)]))))
if tr > 0:
break
if tr > 0:
break
q = nq
rr.append(tr)
return '\n'.join(map(str, rr))
print(main())
``` | instruction | 0 | 71,542 | 19 | 143,084 |
Yes | output | 1 | 71,542 | 19 | 143,085 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1
Submitted Solution:
```
from queue import Queue as q
from copy import deepcopy as cp
def l_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if j == 0 or field[i][j-1] == 1 or n == 0:
return(None)
field[i] = cp(field[i])
while(j):
if field[i][j-1] == 0:
field[i][j] = 0
j -= 1
field[i][j] = 2
elif field[i][j-1] == 3:
flag = True
print(11-n)
return(None)
else:
field[i][j-1] = 0
n -= 1
return([field,n,i,j])
return(None)
def u_move(data):
global x
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if i == 0 or field[i-1][j] == 1 or n == 0:
return(None)
for _ in range(x):
field[i][_] = cp(field[i][_])
while(i):
if field[i-1][j] == 0:
field[i][j] = 0
i -= 1
field[i][j] = 2
elif field[i-1][j] == 3:
flag = True
print(11-n)
return(None)
else:
field[i-1][j] = 0
n -= 1
return([field,n,i,j])
return(None)
def r_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
global y
if j == y-1 or field[i][j+1] == 1 or n == 0:
return(None)
field[i] = cp(field[i])
while(j < y-1):
if field[i][j+1] == 0:
field[i][j] = 0
j += 1
field[i][j] = 2
elif field[i][j+1] == 3:
flag = True
print(11-n)
return(None)
else:
field[i][j+1] = 0
n -= 1
return([field,n,i,j])
return(None)
def d_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
global x
if i == x-1 or field[i+1][j] == 1 or n == 0:
return(None)
for _ in range(x):
field[i][_] = cp(field[i][_])
while(i < x-1):
if field[i+1][j] == 0:
field[i][j] = 0
i += 1
field[i][j] = 2
elif field[i+1][j] == 3:
flag = True
print(11-n)
return(None)
else:
field[i+1][j] = 0
n -= 1
return([field,n,i,j])
return(None)
while(True):
flag = False
y,x = map(int,input().split())
lis = q()
if x == 0:
break
field = []
for i in range(x):
field.append(list(map(int, input().split())))
for i in range(x):
for j in range(y):
if field[i][j] == 2:
start_x, start_y = i,j
data = [field,10,start_x,start_y]
lis.put(data)
while(lis.qsize()):
d = lis.get()
tmp = l_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = u_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = d_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = r_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
if not flag:
print(-1)
``` | instruction | 0 | 71,543 | 19 | 143,086 |
No | output | 1 | 71,543 | 19 | 143,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1
Submitted Solution:
```
print(1)
``` | instruction | 0 | 71,544 | 19 | 143,088 |
No | output | 1 | 71,544 | 19 | 143,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1
Submitted Solution:
```
from queue import Queue as q
from copy import deepcopy as cp
def l_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if j == 0 or field[i][j-1] == 1 or n == 0:
return(None)
field[i][j] = 0
while(j):
if field[i][j-1] == 0:
j -= 1
elif field[i][j-1] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i] + [field[i][:j-1] + [0] + field[i][j:]] + field[i+1:]
n -= 1
return([nfield,n,i,j])
return(None)
def u_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if i == 0 or field[i-1][j] == 1 or n == 0:
return(None)
field[i][j] = 0
while(i):
if field[i-1][j] == 0:
i -= 1
elif field[i-1][j] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i-1] + [field[i-1][:j] + [0] + field[i-1][j+1:]] + field[i:]
n -= 1
return([nfield,n,i,j])
return(None)
def r_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if j == y-1 or field[i][j+1] == 1 or n == 0:
return(None)
field[i][j] = 0
while(j < y-1):
if field[i][j+1] == 0:
j += 1
elif field[i][j+1] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i] + [field[i][:j+1] + [0] + field[i][j+2:]] + field[i+1:]
n -= 1
return([nfield,n,i,j])
return(None)
def d_move(data):
global flag
field,n,i,j = data[0],data[1],data[2],data[3]
if i == x-1 or field[i+1][j] == 1 or n == 0:
return(None)
field[i][j] = 0
while(i < x-1):
if field[i+1][j] == 0:
i += 1
elif field[i+1][j] == 3:
flag = True
print(11-n)
return(None)
else:
nfield = field[:i+1] + [field[i+1][:j] + [0] + field[i+1][j+1:]] + field[i:]
n -= 1
return([nfield,n,i,j])
return(None)
while(True):
flag = False
y,x = map(int,input().split())
lis = q()
if x == 0:
break
field = []
for i in range(x):
field.append(list(map(int, input().split())))
for i in range(x):
for j in range(y):
if field[i][j] == 2:
start_x, start_y = i,j
field[i][j] = 0
data = [field,10,start_x,start_y]
lis.put(data)
while(lis.qsize()):
d = lis.get()
tmp = l_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = u_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = d_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
tmp = r_move(d)
if flag:
break
if tmp != None:
lis.put(tmp)
if not flag:
print(-1)
``` | instruction | 0 | 71,545 | 19 | 143,090 |
No | output | 1 | 71,545 | 19 | 143,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
<image>
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
* At the beginning, the stone stands still at the start square.
* The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
* When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. D-2(a)).
* Once thrown, the stone keeps moving to the same direction until one of the following occurs:
* The stone hits a block (Fig. D-2(b), (c)).
* The stone stops at the square next to the block it hit.
* The block disappears.
* The stone gets out of the board.
* The game ends in failure.
* The stone reaches the goal square.
* The stone stops there and the game ends in success.
* You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
<image>
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. D-3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. D-3(b).
<image>
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.
Each dataset is formatted as follows.
> the width(=w) and the height(=h) of the board
> First row of the board
> ...
> h-th row of the board
>
The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.
Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.
> 0 | vacant square
> ---|---
> 1 | block
> 2 | start position
> 3 | goal position
The dataset for Fig. D-1 is as follows:
> 6 6
> 1 0 0 2 1 0
> 1 1 0 0 0 0
> 0 0 0 0 0 3
> 0 0 0 0 0 0
> 1 0 0 0 0 1
> 0 1 1 1 1 1
>
Output
For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.
Example
Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Output
1
4
-1
4
10
-1
Submitted Solution:
```
import collections
import copy
import sys
if sys.version[0] == '2':
range, input = xrange, raw_input
drc = [(0, -1), (1, 0), (0, 1), (-1, 0)]
def in_board(r, c):
return 0 <= r < H and 0 <= c < W
def bfs(origin_board):
sr, sc, gr, gc = 0, 0, 0, 0
for r in range(H):
for c in range(W):
if origin_board[r][c] == 2:
sr, sc = r, c
elif origin_board[r][c] == 3:
gr, gc = r, c
origin_board[sr][sc] = origin_board[gr][gc] = 0
que = collections.deque([(sr, sc, origin_board, 0)])
while que:
r, c, board, cnt = que.popleft()
if cnt == 10:
continue
for dr, dc in drc:
nr, nc = r + dr, c + dc
if in_board(nr, nc) and board[nr][nc] != 1:
next_board = copy.deepcopy(board)
while in_board(nr, nc) and board[nr][nc] == 0:
if (nr, nc) == (gr, gc):
return cnt + 1
nr += dr
nc += dc
if in_board(nr, nc) and cnt < 9:
next_board[nr][nc] = 0
que.append((nr - dr, nc - dc, next_board, cnt + 1))
return -1
while True:
W, H = map(int, input().split())
if not (W | H):
break
origin_board = [[int(x) for x in input().split()] for _ in range(H)]
print(bfs(origin_board))
``` | instruction | 0 | 71,546 | 19 | 143,092 |
No | output | 1 | 71,546 | 19 | 143,093 |
Provide a correct Python 3 solution for this coding contest problem.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1 | instruction | 0 | 71,559 | 19 | 143,118 |
"Correct Solution:
```
while True:
data = input().split()
n,a,b,c,x = int(data[0]),int(data[1]),int(data[2]),int(data[3]),int(data[4])
if n == 0 and a == 0 and b == 0 and c == 0 and x == 0: break
yList = input().split()
frames = []
bRand = x
for i in range(0,10001):
frames.append(bRand)
bRand = (a * bRand + b)%c
yCount = 0
frameCount = -1
for i,rand in enumerate(frames):
if int(yList[yCount]) == rand:
yCount += 1
if yCount == len(yList):
frameCount = i
break
print(frameCount)
``` | output | 1 | 71,559 | 19 | 143,119 |
Provide a correct Python 3 solution for this coding contest problem.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1 | instruction | 0 | 71,560 | 19 | 143,120 |
"Correct Solution:
```
while True:
N, A, B, C, X = map(int, input().split())
if N == 0 and A == 0 and B == 0 and C == 0 and X == 0:
break
Y = list(map(int, input().split()))
done_frame = -1
cur_reel = 0
cur_X = X
for i in range(10001):
if cur_X == Y[cur_reel]:
if cur_reel == N - 1:
done_frame = i
break
else:
cur_reel += 1
cur_X = (A * cur_X + B) % C
if done_frame >= 0:
print(done_frame)
else:
print(-1)
``` | output | 1 | 71,560 | 19 | 143,121 |
Provide a correct Python 3 solution for this coding contest problem.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1 | instruction | 0 | 71,561 | 19 | 143,122 |
"Correct Solution:
```
# coding: utf-8
def nextX(x,a,b,c):
return (a*x+b)%c
while 1:
n,a,b,c,x=map(int,input().split())
if n==0:
break
data=list(map(int,input().split()))
for i in range(0,10001):
if data[0]==x:
del data[0]
if(len(data)==0):
print(i)
break
x=nextX(x,a,b,c)
else:
print(-1)
``` | output | 1 | 71,561 | 19 | 143,123 |
Provide a correct Python 3 solution for this coding contest problem.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1 | instruction | 0 | 71,562 | 19 | 143,124 |
"Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
while True:
N,A,B,C,X = map(int,input().split(" "))
if N == 0 and A == 0 and B == 0 and C == 0 and X == 0:
break
Y = list(map(int,input().split(" ")))
pointer = 0
for i in range(10001):
X = (A*X+B)%C if i != 0 else X
if X == Y[pointer]:
pointer += 1
if pointer == len(Y):
break
if pointer != len(Y):
print(-1)
else:
print(i)
``` | output | 1 | 71,562 | 19 | 143,125 |
Provide a correct Python 3 solution for this coding contest problem.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1 | instruction | 0 | 71,563 | 19 | 143,126 |
"Correct Solution:
```
while 1:
n, a, b, c, x = map(int, input().split())
if n == 0:
break
target = list(map(int, input().split()))
if x == target[0]:
target.pop(0)
if target == []:
print(0)
continue
for i in range(10000):
x = (a * x + b) % c
if x == target[0]:
target.pop(0)
if target == []:
print(i+1)
break
else:
print(-1)
``` | output | 1 | 71,563 | 19 | 143,127 |
Provide a correct Python 3 solution for this coding contest problem.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1 | instruction | 0 | 71,564 | 19 | 143,128 |
"Correct Solution:
```
for e in iter(input, '0 0 0 0 0'):
N, A, B, C, X = map(int, e.split())
Y = [*map(int, input().split())]
lenY = len(Y)
tgt = Y[0] == X
cnt = 0
while tgt < lenY and cnt < 10000:
cnt += 1
X = (A * X + B) % C
tgt += Y[tgt] == X
print(cnt if tgt == lenY else -1)
``` | output | 1 | 71,564 | 19 | 143,129 |
Provide a correct Python 3 solution for this coding contest problem.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1 | instruction | 0 | 71,565 | 19 | 143,130 |
"Correct Solution:
```
while 1:
n,a,b,c,x=map(int,input().split())
if n==0:break
y=list(map(int,input().split()))
d=[0,1][x==y[0]]
for i in range(10001):
if d==n:print(i);break
x=(a*x+b)%c
if x==y[d]:d+=1
else:print(-1)
``` | output | 1 | 71,565 | 19 | 143,131 |
Provide a correct Python 3 solution for this coding contest problem.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1 | instruction | 0 | 71,566 | 19 | 143,132 |
"Correct Solution:
```
while True:
n, a, b, c, x = map(int, input().split(" "))
if n == 0 and a == 0 and b == 0 and c == 0 and x == 0:
break
y = list(map(int, input().split(" ")))
ans = 0
i = 0
while ans <= 10000:
if x == y[i]:
i += 1
if i == n:
break
x = (a * x + b) % c
ans += 1
if ans >= 10001:
ans = -1
print(ans)
``` | output | 1 | 71,566 | 19 | 143,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1
Submitted Solution:
```
def Next(x) :
return (A * x + B) % C
while True :
N, A, B, C, X = map(int, input().split())
if(C == 0) :
break
else :
Y = list(map(int,input().split()))
for i in range(10002) :
if(i == 10001) :
print(-1)
else :
if(X == Y[0]) :
del Y[0]
if(len(Y) == 0) :
print(i)
break
else :
X = Next(X)
``` | instruction | 0 | 71,567 | 19 | 143,134 |
Yes | output | 1 | 71,567 | 19 | 143,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1
Submitted Solution:
```
while True:
n,a,b,c,x = map(int,input().split())
if n==0: break
l = list(map(int,input().split()))
for i in range(0,10001):
if l[0]==x:
del l[0]
if not(l):
print(i)
break
x = (a*x+b)%c
else: print(-1)
``` | instruction | 0 | 71,568 | 19 | 143,136 |
Yes | output | 1 | 71,568 | 19 | 143,137 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1
Submitted Solution:
```
# x' = ( A * x + B) mod C
def random(a,b,c,x):
li = [x]
for i in range(100000):
x = (a * x + b) % c
li.append(x)
return li
while True:
n,a,b,c,x = [int(i) for i in input().split()]
if n == a == b == c == x == 0:
break
y_lis = [int(y) for y in input().split()]
x_lis = random(a,b,c,x)
f = 0
r = 0
if set(y_lis) & set(x_lis) == set():
print(-1)
else:
while f <= 10000:
if x_lis[f] == y_lis[r]:
r += 1
if r == n:
break
f += 1
if r == n:
print(f)
else:
print(-1)
``` | instruction | 0 | 71,569 | 19 | 143,138 |
Yes | output | 1 | 71,569 | 19 | 143,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1
Submitted Solution:
```
while True:
n, a, b, c, x = map(int, input().split())
if (n, a, b, c, x) == (0, 0, 0, 0, 0):
break
y = list(map(int, input().split()))
count = -1
i = 0
while i < len(y):
if y[i] == x:
i += 1
x = (a * x + b) % c
count += 1
if (10000 < count):
count = -1
break
print(count)
``` | instruction | 0 | 71,570 | 19 | 143,140 |
Yes | output | 1 | 71,570 | 19 | 143,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1
Submitted Solution:
```
f=lambda A,x,B,C:(A*x+B)%C
while 1:
N,A,B,C,X=map(int,raw_input().split())
if not N and not A and not B and not C and not X:break
Y=list(map(int,raw_input().split()))
T=X
c=[[] for _ in range(N)]
sl=0
for i in range(10005):
if T==Y[sl]:
sl+=1
if sl==N:break
T=f(A,T,B,C)
if i>10000:i=-1
print(i)
``` | instruction | 0 | 71,571 | 19 | 143,142 |
No | output | 1 | 71,571 | 19 | 143,143 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1
Submitted Solution:
```
class LCG:
def __init__(self, a, b, c, x):
self.a = a
self.b = b
self.c = c
self.x = x
self.count = 0
def get_next(self):
self.x = (self.a * self.x + self.b) % self.c
self.count += 1
return self.x
def get_count(self):
return self.count
if __name__ == "__main__":
while True:
n, a, b, c, x = map(int, input().split())
if n == 0: break
ran = LCG(a, b, c, x)
reel = map(int, input().split())
for r in reel:
while x != r:
x = ran.get_next()
if ran.get_count() > 10000:
print(-1)
break
else:
continue
break
else:
print(ran.get_count())
``` | instruction | 0 | 71,572 | 19 | 143,144 |
No | output | 1 | 71,572 | 19 | 143,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1
Submitted Solution:
```
while True:
a = input()
a = a.split(" ")
b = []
for i in a:
b.append(int(i))
if b[0] == 0 and b[1] == 0 and b[2] == 0 and b[3] == 0 and b[4] == 0:
break
c = input()
c = c.split(" ")
d = []
for i in c:
d.append(int(i))
if b[4] == d[0]:
print(0)
continue
ans = 2
for i in d:
for e in range(10000):
x = (b[1] * b[4] + b[2]) % b[3]
if x == i:
b[4] = x
break
b[4] = x
ans = ans + 1
if ans >= 10001:
ans = -1
print(ans)
``` | instruction | 0 | 71,573 | 19 | 143,146 |
No | output | 1 | 71,573 | 19 | 143,147 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nathan O. Davis is trying to capture a game and struggling to get a very rare item. This rare item can be obtained by arranging special patterns in a row on a casino slot machine. The slot machine has N reels, and pressing the button once stops the currently rotating leftmost reel. Therefore, you need to press the button N times to get this rare item. Also, in order to stop with a special pattern, it is necessary to press the button at a certain timing. However, the timing of pressing this button was very severe, so I was in trouble because it didn't go well. Therefore, Nathan decided to analyze the game while checking the memory value during the game using the memory viewer.
As a result of Nathan's analysis, in order to stop the reel with that special pattern, the "random number" written at address 007E0D1F in the memory must have a specific value when the button is pressed. I found out. It was also found that the value of the random number changes every frame by the linear congruential method, and whether or not the button is pressed is judged once every frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following formula.
x'= (A x x + B) mod C
Here, x is the value of the current random number, x'is the value of the next random number, and A, B, and C are some constants. Also, y mod z represents the remainder when y is divided by z.
For example, suppose a slot machine with two reels has A = 5, B = 7, C = 11 and the first "random number" value is 10. And, the condition for stopping the reel with a special pattern is that the reel on the left side is 2 and the reel on the right side is 4. At this time, the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, so if you press the button in the first frame, the reel on the left side will stop with a special pattern. Since the value of the random number in the second frame that follows is (5 x 2 + 7) mod 11 = 6, the reel on the right side does not stop with a special pattern even if the button is pressed in the second frame. In the following 3rd frame, the random number value becomes (5 x 6 + 7) mod 11 = 4, so if you press the button in the 3rd frame, the reel on the right side will stop with a special pattern. Therefore, all reels can be stopped with a special pattern in a minimum of 3 frames, and rare items can be obtained.
Your job is to help Nathan get a rare item by writing a program that asks how many frames in the shortest frame all reels can be stopped with a special pattern. ..
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> N A B C X
> Y1 Y2 ... YN
The first row contains five integers N (1 ≤ N ≤ 100), A, B (0 ≤ A, B ≤ 10,000), C (1 ≤ C ≤ 10,000), and X (0 ≤ X <C), respectively. It is given separated by two whitespace characters. Here, X represents the value of the first random number. On the following line, N integers Y1, Y2, ..., YN (0 ≤ Yi ≤ 10,000) are given, separated by a single space character. These represent the conditions for stopping the reel with a specific pattern, and the i-th number Yi is the random number value Yi to stop the i-th reel from the left with a special pattern. It means that there must be.
The end of the input is indicated by a single line containing five zeros separated by blanks.
Output
For each dataset, output the shortest number of frames required to stop at a special pattern on one line. If it cannot be stopped within 10,000 frames, output -1 instead of the number of frames. The output must not contain extra blanks or line breaks.
Sample Input
1 5 7 11 10
Ten
2 5 7 11 10
twenty four
2 1 1 256 0
128 255
2 0 0 1 0
1 2 3 4 5 6 7 8
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
Ten
2 1 1 10000 0
twenty one
0 0 0 0 0
Output for the Sample Input
0
3
255
-1
198
199
10000
-1
Example
Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0
Output
0
3
255
-1
198
199
10000
-1
Submitted Solution:
```
while True:
n,a,b,c,x = map(int,input().split())
if n==0: break
l = list(map(int,input().split()))
for i in range(1,10001):
x = (a*x+b)%c
if l[0]==x:
del l[0]
if not(l):
print(i)
break
else: print(-1)
``` | instruction | 0 | 71,574 | 19 | 143,148 |
No | output | 1 | 71,574 | 19 | 143,149 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The i-th emote increases the opponent's happiness by a_i units (we all know that emotes in this game are used to make opponents happy).
You have time to use some emotes only m times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than k times in a row (otherwise the opponent will think that you're trolling him).
Note that two emotes i and j (i ≠ j) such that a_i = a_j are considered different.
You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.
Input
The first line of the input contains three integers n, m and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ k ≤ m ≤ 2 ⋅ 10^9) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is value of the happiness of the i-th emote.
Output
Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.
Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence: 4, 4, 5, 4, 4, 5, 4, 4, 5. | instruction | 0 | 71,680 | 19 | 143,360 |
Tags: greedy, math, sortings
Correct Solution:
```
n,m,k = map(int, input().split())
A = list(map(int, input().split()))
Max = 0
SMax = 0
ind = 0
for i in range(n):
if A[i] > Max:
Max = A[i]
ind = i
if k >= m:
print(Max*m)
else:
for j in range(n):
if A[j] > SMax and j != ind:
SMax = A[j]
c = m//(k+1)
d = m%(k+1)
print(int(Max*((c*k)+d))+int(SMax*c))
``` | output | 1 | 71,680 | 19 | 143,361 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The i-th emote increases the opponent's happiness by a_i units (we all know that emotes in this game are used to make opponents happy).
You have time to use some emotes only m times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than k times in a row (otherwise the opponent will think that you're trolling him).
Note that two emotes i and j (i ≠ j) such that a_i = a_j are considered different.
You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.
Input
The first line of the input contains three integers n, m and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ k ≤ m ≤ 2 ⋅ 10^9) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is value of the happiness of the i-th emote.
Output
Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.
Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence: 4, 4, 5, 4, 4, 5, 4, 4, 5. | instruction | 0 | 71,681 | 19 | 143,362 |
Tags: greedy, math, sortings
Correct Solution:
```
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse = True)
ans = a[0] * k + a[1]
ans *= m // (k + 1)
ans += (m % (k + 1)) * a[0]
print(ans)
``` | output | 1 | 71,681 | 19 | 143,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The i-th emote increases the opponent's happiness by a_i units (we all know that emotes in this game are used to make opponents happy).
You have time to use some emotes only m times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than k times in a row (otherwise the opponent will think that you're trolling him).
Note that two emotes i and j (i ≠ j) such that a_i = a_j are considered different.
You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.
Input
The first line of the input contains three integers n, m and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ k ≤ m ≤ 2 ⋅ 10^9) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is value of the happiness of the i-th emote.
Output
Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.
Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence: 4, 4, 5, 4, 4, 5, 4, 4, 5. | instruction | 0 | 71,682 | 19 | 143,364 |
Tags: greedy, math, sortings
Correct Solution:
```
N,M,K=map(int, input().split())
l=list(map(int, input().split()))
_Max=0
for i in l:
_Max=max(_Max, i);
c=0
for i in l:
if i==_Max:
c+=1
if c>=2:
print(_Max*M)
else:
sec_M=max(list(filter(lambda x: x<_Max, l)))
print((M//(K+1))*sec_M+(M-M//(K+1))*_Max)
``` | output | 1 | 71,682 | 19 | 143,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The i-th emote increases the opponent's happiness by a_i units (we all know that emotes in this game are used to make opponents happy).
You have time to use some emotes only m times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than k times in a row (otherwise the opponent will think that you're trolling him).
Note that two emotes i and j (i ≠ j) such that a_i = a_j are considered different.
You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.
Input
The first line of the input contains three integers n, m and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ k ≤ m ≤ 2 ⋅ 10^9) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is value of the happiness of the i-th emote.
Output
Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.
Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence: 4, 4, 5, 4, 4, 5, 4, 4, 5. | instruction | 0 | 71,683 | 19 | 143,366 |
Tags: greedy, math, sortings
Correct Solution:
```
n, m, k = [int(p) for p in input().split()]
arr = [int(p) for p in input().split()]
arr.sort(reverse=True)
a1, a2 = arr[0], arr[1]
if m % (k+1) == 0:
b = m//(k+1)
print(b*(k*a1 + a2))
else:
b = m//(k+1)
print(b*(k*a1+a2) + a1*(m%(k+1)))
``` | output | 1 | 71,683 | 19 | 143,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The i-th emote increases the opponent's happiness by a_i units (we all know that emotes in this game are used to make opponents happy).
You have time to use some emotes only m times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than k times in a row (otherwise the opponent will think that you're trolling him).
Note that two emotes i and j (i ≠ j) such that a_i = a_j are considered different.
You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.
Input
The first line of the input contains three integers n, m and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ k ≤ m ≤ 2 ⋅ 10^9) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is value of the happiness of the i-th emote.
Output
Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.
Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence: 4, 4, 5, 4, 4, 5, 4, 4, 5. | instruction | 0 | 71,684 | 19 | 143,368 |
Tags: greedy, math, sortings
Correct Solution:
```
n,m,k=input().split()
n=int(n)
m=int(m)
k=int(k)
ar=list(map(int,input().split()))
mx=max(ar)
count=0
mx2=0
for i in range(0,n):
if(ar[i]==mx):
count=count+1
if(ar[i]>mx2 and mx!=ar[i]):
mx2=ar[i]
if(count>=2 and ar[i]>mx2):
mx2=ar[i]
g=m//(k+1)
print(((g*mx*k)+(g*mx2)+(mx*(m%(k+1)))))
# print(g*mx*k,g*mx2,mx*(m%(k+1)))
``` | output | 1 | 71,684 | 19 | 143,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The i-th emote increases the opponent's happiness by a_i units (we all know that emotes in this game are used to make opponents happy).
You have time to use some emotes only m times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than k times in a row (otherwise the opponent will think that you're trolling him).
Note that two emotes i and j (i ≠ j) such that a_i = a_j are considered different.
You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.
Input
The first line of the input contains three integers n, m and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ k ≤ m ≤ 2 ⋅ 10^9) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is value of the happiness of the i-th emote.
Output
Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.
Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence: 4, 4, 5, 4, 4, 5, 4, 4, 5. | instruction | 0 | 71,685 | 19 | 143,370 |
Tags: greedy, math, sortings
Correct Solution:
```
a=input()
n,m,k=int(a.split()[0]),int(a.split()[1]),int(a.split()[2])
A=input()
l=list(map(int,A.split()))
s=0
l.sort()
n=len(l)
ma=l[n-1]
ma1=l[n-2]
s=s+(m-(m//(k+1)))*ma
s=s+(m//(k+1))*ma1
print(s)
``` | output | 1 | 71,685 | 19 | 143,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The i-th emote increases the opponent's happiness by a_i units (we all know that emotes in this game are used to make opponents happy).
You have time to use some emotes only m times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than k times in a row (otherwise the opponent will think that you're trolling him).
Note that two emotes i and j (i ≠ j) such that a_i = a_j are considered different.
You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.
Input
The first line of the input contains three integers n, m and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ k ≤ m ≤ 2 ⋅ 10^9) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is value of the happiness of the i-th emote.
Output
Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.
Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence: 4, 4, 5, 4, 4, 5, 4, 4, 5. | instruction | 0 | 71,686 | 19 | 143,372 |
Tags: greedy, math, sortings
Correct Solution:
```
def go():
n, m, k = [int(i) for i in input().split(' ')]
a = [int(i) for i in input().split(' ')]
a.sort(reverse=True)
m1 = a[0]
m2 = a[1]
x = m // (k + 1)
return m1 * x * k + m2 * x + m % (k + 1) * m1
print(go())
``` | output | 1 | 71,686 | 19 | 143,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The i-th emote increases the opponent's happiness by a_i units (we all know that emotes in this game are used to make opponents happy).
You have time to use some emotes only m times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than k times in a row (otherwise the opponent will think that you're trolling him).
Note that two emotes i and j (i ≠ j) such that a_i = a_j are considered different.
You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.
Input
The first line of the input contains three integers n, m and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ k ≤ m ≤ 2 ⋅ 10^9) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is value of the happiness of the i-th emote.
Output
Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.
Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence: 4, 4, 5, 4, 4, 5, 4, 4, 5. | instruction | 0 | 71,687 | 19 | 143,374 |
Tags: greedy, math, sortings
Correct Solution:
```
N, M, K = [int(n) for n in input().split()]
a = sorted([int(n) for n in input().split()])
syou = M // (K + 1)
amari = M % (K + 1)
ans = syou * (K * a[-1] + a[-2]) + amari * a[-1]
print(ans)
``` | output | 1 | 71,687 | 19 | 143,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The i-th emote increases the opponent's happiness by a_i units (we all know that emotes in this game are used to make opponents happy).
You have time to use some emotes only m times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than k times in a row (otherwise the opponent will think that you're trolling him).
Note that two emotes i and j (i ≠ j) such that a_i = a_j are considered different.
You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.
Input
The first line of the input contains three integers n, m and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ k ≤ m ≤ 2 ⋅ 10^9) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is value of the happiness of the i-th emote.
Output
Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.
Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence: 4, 4, 5, 4, 4, 5, 4, 4, 5.
Submitted Solution:
```
n,m,k=map(int,input().split())
arr=[int(i) for i in input().split()]
x=max(arr)
i=arr.index(x)
del arr[i]
y=max(arr)
Z=x*k+y
Z=Z*(m//(k+1))
if (m%(k+1)!=0):
c=m-(m//(k+1))*(k+1)
Z+=x*c
if x==y or m==k:
print(x*m)
else:
print(Z)
``` | instruction | 0 | 71,688 | 19 | 143,376 |
Yes | output | 1 | 71,688 | 19 | 143,377 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.