message stringlengths 2 19.9k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 322 108k | cluster float64 15 15 | __index_level_0__ int64 644 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way. | instruction | 0 | 75,114 | 15 | 150,228 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
from collections import deque, Counter
import array
from itertools import combinations, permutations
from math import sqrt
import unittest
def read_int():
return int(input().strip())
def read_int_array():
return [int(i) for i in input().strip().split(' ')]
for test in range(read_int()):
rows, cols, ch = read_int_array()
rice = 0
field = []
for r in range(rows):
field += [list(input().strip())]
rice += field[-1].count('R')
code = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
i = 0
j = 0
count = 0
ch_n = 1
base = rice // ch
rem = rice % ch
base_num = ch - rem
for i in range(rows):
for j in range(cols):
j = j if i % 2 == 0 else -j-1
if field[i][j] == 'R':
count += 1
if count > (base if ch_n <= base_num else base + 1):
ch_n += 1
count = 1
field[i][j] = code[ch_n - 1]
print('\n'.join(''.join(field[i]) for i in range(rows)))
``` | output | 1 | 75,114 | 15 | 150,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way. | instruction | 0 | 75,115 | 15 | 150,230 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
t=int(input())
alp='0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
for _ in range(t):
count=0
R,C,k=list(map(int,input().split()))
arr=[]
for i in range(R):
arr.append(input())
final=[['0' for _ in range(C)] for _ in range(R)]
total=0
for i in range(R):
for j in range(C):
if arr[i][j]=='R':
total+=1
h=[0]*k
for i in range(0,total):
h[i%k]+=1
leftToRight = True
curr=0
for i in range(R):
if (leftToRight):
for j in range(C):
final[i][j]=alp[count]
if arr[i][j]=='R':
curr+=1
if curr==h[count]:
if count!=len(h)-1:
count+=1
curr=0
else:
for j in range(C - 1, -1, -1):
final[i][j] = alp[count]
if arr[i][j] == 'R':
curr += 1
if curr == h[count]:
if count != len(h) - 1:
count += 1
curr = 0
leftToRight = not leftToRight
for i in final:
print(''.join(i))
``` | output | 1 | 75,115 | 15 | 150,231 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way. | instruction | 0 | 75,116 | 15 | 150,232 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
from functools import reduce
import string
class IoTool: # a tool for input redirection
DEBUG = 0
def _read_dbg():
with open('./input.txt', 'r') as f:
lines = f.readlines()
for l in lines: yield l.strip()
def _read_oj():
import sys
return iter(sys.stdin.read().split('\n')) # It does help a lot.
reader = _read_dbg() if DEBUG else _read_oj()
def read(): return next(IoTool.reader)
input = IoTool.read
dic = string.ascii_letters + string.digits
def main():
r, c, k = map(int, input().split())
ans = [[None] * c for _ in range(r)]
matrix = [list(input()) for _ in range(r)]
rice_cnt = 0
for i in range(r):
if i % 2: matrix[i].reverse()
rice_cnt += matrix[i].count('R')
distribution = [rice_cnt // k + (1 if i < rice_cnt % k else 0) for i in range(k)]
cur_idx = 0
for x in range(r):
for y in range(c):
char = matrix[x][y]
ans[x][y] = dic[cur_idx]
if char == 'R':
distribution[cur_idx] -= 1
if distribution[cur_idx] == 0 and cur_idx + 1 < k:
cur_idx += 1
for i in range(r):
if i % 2:
ans[i].reverse()
print(''.join(ans[i]))
if __name__ == "__main__":
t = int(input())
for _ in range(t):
main()
``` | output | 1 | 75,116 | 15 | 150,233 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way. | instruction | 0 | 75,117 | 15 | 150,234 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
R, C, K = map(int, input().split())
state = [list(input().rstrip()) for _ in range(R)]
Query.append((R, C, K, state))
Alp = [chr(i) for i in range(97, 97+26)] + [chr(i) for i in range(65, 65+26)] + [str(i) for i in range(10)]
for R, C, K, state in Query:
count = 0
for r in range(R):
for c in range(C):
if state[r][c] == "R":
count += 1
ans = [[None]*C for _ in range(R)]
Limit = [count//K+1 if i<(count%K) else count//K for i in range(K)]
ind = 0
now = 0
for r in range(R):
if r%2 == 0:
seq = list(range(C))
else:
seq = list(reversed(range(C)))
for c in seq:
ans[r][c] = Alp[ind]
if state[r][c] == "R":
now += 1
if now == Limit[ind] and ind != K-1:
ind += 1
now = 0
for s in ans:
print("".join(s))
``` | output | 1 | 75,117 | 15 | 150,235 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
Submitted Solution:
```
#!/usr/bin/python3
import os
import sys
def main():
T = read_int()
for _ in range(T):
R, C, K = read_ints()
A = [inp() for _ in range(R)]
print(*solve(R, C, K, A), sep='\n')
def itoc(i):
if i < 10:
return chr(ord('0') + i)
i -= 10
if i < 26:
return chr(ord('a') + i)
i -= 26
return chr(ord('A') + i)
def solve(R, C, K, A):
ans = [[None] * C for _ in range(R)]
rice = sum([s.count('R') for s in A])
c = rice // K
i = 0
for y in range(R):
for x in range(C) if y % 2 == 0 else range(C - 1, -1, -1):
ans[y][x] = i
if A[y][x] == 'R':
c -= 1
rice -= 1
if c == 0:
K -= 1
if K > 0:
c = rice // K
i += 1
return [''.join([itoc(c) for c in r]) for r in ans]
###############################################################################
DEBUG = 'DEBUG' in os.environ
def inp():
return sys.stdin.readline().rstrip()
def read_int():
return int(inp())
def read_ints():
return [int(e) for e in inp().split()]
def dprint(*value, sep=' ', end='\n'):
if DEBUG:
print(*value, sep=sep, end=end)
if __name__ == '__main__':
main()
``` | instruction | 0 | 75,118 | 15 | 150,236 |
Yes | output | 1 | 75,118 | 15 | 150,237 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
Submitted Solution:
```
# -*- coding: utf-8 -*-
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def build_grid(H, W, intv, _type, space=True, padding=False):
if space:
_input = lambda: input().split()
else:
_input = lambda: input()
_list = lambda: list(map(_type, _input()))
if padding:
offset = 1
else:
offset = 0
grid = list2d(H+offset*2, W+offset*2, intv)
for i in range(offset, H+offset):
row = _list()
for j in range(offset, W+offset):
grid[i][j] = row[j-offset]
return grid
for _ in range(INT()):
H, W, K = MAP()
grid = build_grid(H, W, '*', str, space=False, padding=False)
rcnt = 0
for i in range(H):
for j in range(W):
if grid[i][j] == 'R':
rcnt += 1
p = rcnt // K
q = rcnt % K
ans = list2d(H, W, 0)
cnt = k = 0
for i in range(H):
for j in range(W):
x = p + 1 if k < q else p
if i % 2 == 1:
j = W - j - 1
ans[i][j] = k
if grid[i][j] == 'R':
cnt += 1
if cnt == x and k != K - 1:
cnt = 0
k += 1
for i in range(H):
for j in range(W):
if ans[i][j] < 10:
ans[i][j] = str(ans[i][j])
elif 10 <= ans[i][j] < 36:
ans[i][j] = chr(ans[i][j]+87)
else:
ans[i][j] = chr(ans[i][j]+29)
for i in range(H):
print(''.join(ans[i]))
``` | instruction | 0 | 75,119 | 15 | 150,238 |
Yes | output | 1 | 75,119 | 15 | 150,239 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
Submitted Solution:
```
import sys
import collections
import heapq
import math
input = sys.stdin.readline
# Library methods start here
def rints(): return map(int, input().strip().split())
def rstr(): return input().strip()
def rint(): return int(input().strip())
def rintas(): return [int(i) for i in input().strip().split()]
def gcd(a, b):
if (b == 0): return a
return gcd(b, a%b)
# Actual Code starts here
n = rint()
for _ in range(n):
r, c, k = rints()
d = []
rices = 0
grid = [[0 for _ in range(c)] for _ in range(r)]
for i in range(r):
grid[i] = [i for i in rstr()]
for j in range(c):
if grid[i][j] == 'R': rices+=1
if i&1:
for j in range(c-1, -1, -1):
d.append([i,j])
else:
for j in range(c):
d.append([i,j])
# print(grid)
# print(rices)
idx = 0
for i in range(k):
share = rices//(k-i)
rices -= share
if i < 26:
val = chr(ord('a') + i)
elif i < 26+26:
val = chr(ord('A') + (i-26))
else:
val = str(i-26-26)
while share or (i == k-1 and idx < len(d)):
# print(share, i , k, idx)
x, y = d[idx]
idx += 1
if grid[x][y] == 'R':
share -= 1
grid[x][y] = val
for i in range(r):
print(''.join(grid[i]))
``` | instruction | 0 | 75,120 | 15 | 150,240 |
Yes | output | 1 | 75,120 | 15 | 150,241 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
Submitted Solution:
```
from collections import Counter, OrderedDict
from itertools import permutations as perm
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
import math
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1e9)+7
inf = float("inf")
t, = gil()
for _ in range(t):
n, m, k = gil()
mat = []
r = 0
for _ in range(n):
mat.append(g())
r += mat[-1].count("R")
cnt = {}; base = r//k
cnt[base] = k - r%k
cnt[base+1] = r%k
ide = []
for i in range(10):
ide.append(str(i))
for ch in "abcdefghijklmnopqrstuvwxyz":
ide.append(ch)
ide.append(ch.capitalize())
x, y, idx, ct = 0, 0, 0, 0
dt = 1
# print(cnt)
ans = []
for i in range(n):
ans.append([None for _ in range(m)])
while y < n and x < m:
ans[y][x] = ide[idx]
if mat[y][x] == "R":
ct += 1
if ct in cnt :
cnt[ct] -= 1
if cnt[ct] == 0:
del cnt[ct]
idx += 1
# print(ct)
ct =0
idx = min(idx, k-1)
y += dt
if y == n:
x+=1
y=n-1
dt = -1
if y == -1:
x += 1
y = 0
dt = 1
for r in ans:
print("".join(r))
# print()
``` | instruction | 0 | 75,121 | 15 | 150,242 |
Yes | output | 1 | 75,121 | 15 | 150,243 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
Submitted Solution:
```
from collections import deque
t = int(input())
for _ in range(t):
r,c,k = list(map(int, input().strip().split()))
arr = []
rice = 0
code = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
for rw in range(r):
row = input().strip()
row = list(row.replace('R','#'))
arr.append(row)
rice += row.count('#')
eq = rice // k
dist = []
for j in range(rice%k):
dist.append(eq+1)
for j in range(k - len(dist)):
dist.append(eq)
def bfs(i,j,how,label,arr,r,c):
q = deque()
q.append([i,j])
while q and how:
x,y = q.popleft()
if arr[x][y] == '#':
how -= 1
arr[x][y] = label
for dx, dy in [[1,0], [-1,0], [0,1], [0,-1]]:
nx, ny = x+dx, y+dy
if 0<=nx<r and 0<=ny<c and (arr[nx][ny] == '.' or arr[nx][ny] == '#'):
q.append([nx,ny])
d = 0
for i in range(r):
for j in range(c):
if d<len(dist) and (arr[i][j] == '.' or arr[i][j] == '#'):
how = dist[d]
label = code[d]
d += 1
bfs(i,j,how,label,arr,r,c)
for i in range(r):
for j in range(c):
if arr[i][j] == '.':
for dx, dy in [[1,0], [-1,0], [0,1], [0,-1]]:
nx, ny = i+dx, j+dy
if 0<=nx<r and 0<=ny<c:
arr[i][j] = arr[nx][ny]
for rw in arr:
print("".join(rw))
``` | instruction | 0 | 75,122 | 15 | 150,244 |
No | output | 1 | 75,122 | 15 | 150,245 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
Submitted Solution:
```
import sys
import math
from collections import defaultdict,Counter,deque
# input=sys.stdin.readline
# def print(x):
# sys.stdout.write(str(x)+"\n")
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# sys.stdout=open("CP2/output.txt",'w')
# sys.stdin=open("CP2/input.txt",'r')
def find(x,y):
global d1
global cur
visit[x][y]=1
if l[x][y]=='R':
cur+=1
l[x][y]=pre[start]
if d1:
if cur==d+1:
cur=-1
d1-=1
return
else:
if cur==d:
# print(cur,pre[start])
cur=-1
return
for j in p:
x1=x+j[0]
y1=y+j[1]
# print(x1,y1)
if 0<=x1<r and 0<=y1<c and visit[x1][y1]==0:
find(x1,y1)
if cur==-1:
return
# mod=pow(10,9)+7
pre=[]
for i in range(26):
if i<10:
pre.append(str(i))
pre.append(chr(65+i))
pre.append(chr(97+i))
p=[[0,1],[1,0],[0,-1],[-1,0]]
# print(pre)
t=int(input())
for i in range(t):
r,c,k=map(int,input().split())
l=[]
c1=0
for j in range(r):
s=list(input())
l.append(s)
c1+=s.count('R')
d=c1//k
d1=0
if c1%k:
d1+=c1%k
start=0
# print(d)
visit=[[0]*c for j in range(r)]
for j in range(r):
for kk in range(c):
if visit[j][kk]==0:
cur=0
find(j,kk)
k-=1
if k:
start+=1
# print(l)
# print(l)
for j in l:
print(''.join(j))
``` | instruction | 0 | 75,123 | 15 | 150,246 |
No | output | 1 | 75,123 | 15 | 150,247 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
Submitted Solution:
```
import math
t = int(input())
char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
for _ in range(t):
r, c, k = map(int, input().split())
grid = [list(input()) for _ in range(r)]
ans = [[0 for _ in range(c)] for __ in range(r)]
rcount = 0
for i in range(r):
for j in range(c):
if grid[i][j] == 'R':
rcount += 1
uplim = math.ceil(rcount / k)
lolim = math.floor(rcount / k)
if uplim != lolim:
a = (rcount - k * uplim) / (lolim - uplim)
b = k - a
else:
a = b = k
print(a, b)
index = 0
count = 0
state = 1
for i in range(r):
for j in range(c):
ans[i][j] = char[index]
if grid[i][j] == 'R':
count += 1
if count == lolim and state:
if index + 1 == a:
state = 0
index += 1
count = 0
elif count == uplim and not state:
count = 0
index += 1
for i in ans:
print(''.join(i))
``` | instruction | 0 | 75,124 | 15 | 150,248 |
No | output | 1 | 75,124 | 15 | 150,249 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.
His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.
Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:
* Each cell of the farm is assigned to exactly one chicken.
* Each chicken is assigned at least one cell.
* The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side.
Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 2 β
10^4). Description of the test cases follows.
The first line of each test case contains three integers r, c and k (1 β€ r, c β€ 100, 1 β€ k β€ 62), representing the size of Long's farm and the number of chickens Long has.
Each of the next r lines contains c characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.
It is guaranteed that the sum of r β
c over all test cases does not exceed 2 β
10^4.
Output
For each test case, print r lines with c characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.
If there are multiple optimal answers, print any.
Example
Input
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Output
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789
Note
These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.
In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0.
<image>
In the second test case, there are 4 chickens with 3 cells of rice, and 2 chickens with 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 - 2 = 1.
<image>
In the third test case, each chicken has 3 cells with rice. <image>
In the last test case, since there are 62 chicken with exactly 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.
Submitted Solution:
```
from functools import reduce
import string
class debug: # a tool for input redirection
def _read():
with open('./input.txt', 'r') as f:
lines = f.readlines()
for l in lines: yield l.strip()
reader = _read()
def next(): return next(debug.reader)
dic = string.ascii_letters + string.digits
def readline():
return list(map(int, input().split()))
def main():
r, c, k = readline()
ans = [[-1] * c for _ in range(r)]
matrix = [list(input()) for _ in range(r)]
rice_cnt = 0
for line in matrix:
rice_cnt += reduce(lambda x, y: x + (1 if y == 'R' else 0), line, 0)
distribution = [rice_cnt // k + (1 if i < rice_cnt % k else 0) for i in range(k)]
cur_idx = 0
for n in range(r * c):
x, y = n // c, n % c
if x % 2 != 0:
y = c - y - 1
char = matrix[x][y]
ans[x][y] = dic[cur_idx]
if char == 'R':
distribution[cur_idx] -= 1
if distribution[cur_idx] == 0:
cur_idx += 1
for l in ans:
print(''.join(l))
if __name__ == "__main__":
t = int(input())
for _ in range(t):
main()
``` | instruction | 0 | 75,125 | 15 | 150,250 |
No | output | 1 | 75,125 | 15 | 150,251 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a rectangular table 3 Γ n. Each cell contains an integer. You can move from one cell to another if they share a side.
Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum possible.
Input
The first line contains an integer n (1 β€ n β€ 105) β the number of columns in the table.
Next three lines contain n integers each β the description of the table. The j-th number in the i-th line corresponds to the cell aij ( - 109 β€ aij β€ 109) of the table.
Output
Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice.
Examples
Input
3
1 1 1
1 -1 1
1 1 1
Output
7
Input
5
10 10 10 -1 -1
-1 10 10 10 10
-1 10 10 10 10
Output
110
Note
The path for the first example:
<image>
The path for the second example:
<image> | instruction | 0 | 75,548 | 15 | 151,096 |
Tags: dp, greedy, implementation
Correct Solution:
```
import sys
from array import array # noqa: F401
import typing as Tp # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
def output(*args):
sys.stdout.buffer.write(
('\n'.join(map(str, args)) + '\n').encode('utf-8')
)
def main():
n = int(input())
a = [list(map(float, input().split())) for _ in range(3)]
dp = [[[-1e18] * 2 for _ in range(3)] for _ in range(n + 1)]
dp[0][0][0] = 0.0
for i in range(n):
for j in range(3):
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0] + a[j][i])
if j > 0:
dp[i + 1][j - 1][0] = max(dp[i + 1][j - 1][0], dp[i][j][0] + a[j][i] + a[j - 1][i])
if j < 2:
dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j][0] + a[j][i] + a[j + 1][i])
dp[i + 1][0][1] = max(
dp[i + 1][0][1],
dp[i][0][0] + a[0][i] + a[1][i] + a[2][i],
dp[i][0][1] + a[0][i] + a[1][i] + a[2][i]
)
dp[i + 1][2][0] = max(
dp[i + 1][2][0],
dp[i][0][0] + a[0][i] + a[1][i] + a[2][i],
dp[i][0][1] + a[0][i] + a[1][i] + a[2][i]
)
dp[i + 1][2][1] = max(
dp[i + 1][2][1],
dp[i][2][0] + a[0][i] + a[1][i] + a[2][i],
dp[i][2][1] + a[0][i] + a[1][i] + a[2][i]
)
dp[i + 1][0][0] = max(
dp[i + 1][0][0],
dp[i][2][0] + a[0][i] + a[1][i] + a[2][i],
dp[i][2][1] + a[0][i] + a[1][i] + a[2][i]
)
print(round(dp[-1][2][0]))
if __name__ == '__main__':
main()
``` | output | 1 | 75,548 | 15 | 151,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a rectangular table 3 Γ n. Each cell contains an integer. You can move from one cell to another if they share a side.
Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum possible.
Input
The first line contains an integer n (1 β€ n β€ 105) β the number of columns in the table.
Next three lines contain n integers each β the description of the table. The j-th number in the i-th line corresponds to the cell aij ( - 109 β€ aij β€ 109) of the table.
Output
Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice.
Examples
Input
3
1 1 1
1 -1 1
1 1 1
Output
7
Input
5
10 10 10 -1 -1
-1 10 10 10 10
-1 10 10 10 10
Output
110
Note
The path for the first example:
<image>
The path for the second example:
<image>
Submitted Solution:
```
import sys
from array import array # noqa: F401
import typing as Tp # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
def output(*args):
sys.stdout.buffer.write(
('\n'.join(map(str, args)) + '\n').encode('utf-8')
)
def main():
n = int(input())
a = [list(map(float, input().split())) for _ in range(3)]
dp = [[[-1e18] * 2 for _ in range(3)] for _ in range(n + 1)]
dp[0][0][0] = 0.0
for i in range(n):
for j in range(3):
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0] + a[j][i])
if j > 0:
dp[i + 1][j - 1][0] = max(dp[i + 1][j - 1][0], dp[i][j][0] + a[j][i] + a[j - 1][i])
if j < 2:
dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j][0] + a[j][i] + a[j + 1][i])
dp[i + 1][0][1] = max(
dp[i + 1][0][1],
dp[i][0][1] + a[0][i] + a[1][i] + a[2][i],
dp[i][0][0] + a[0][i] + a[1][i] + a[2][i]
)
dp[i + 1][2][0] = max(
dp[i + 1][2][0],
dp[i][0][1] + a[0][i] + a[1][i] + a[2][i]
)
dp[i + 1][2][1] = max(
dp[i + 1][2][1],
dp[i][2][1] + a[0][i] + a[1][i] + a[2][i],
dp[i][2][0] + a[0][i] + a[1][i] + a[2][i]
)
dp[i + 1][0][0] = max(
dp[i + 1][0][0],
dp[i][2][1] + a[0][i] + a[1][i] + a[2][i]
)
print(round(dp[-1][2][0]))
if __name__ == '__main__':
main()
``` | instruction | 0 | 75,549 | 15 | 151,098 |
No | output | 1 | 75,549 | 15 | 151,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a rectangular table 3 Γ n. Each cell contains an integer. You can move from one cell to another if they share a side.
Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum possible.
Input
The first line contains an integer n (1 β€ n β€ 105) β the number of columns in the table.
Next three lines contain n integers each β the description of the table. The j-th number in the i-th line corresponds to the cell aij ( - 109 β€ aij β€ 109) of the table.
Output
Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice.
Examples
Input
3
1 1 1
1 -1 1
1 1 1
Output
7
Input
5
10 10 10 -1 -1
-1 10 10 10 10
-1 10 10 10 10
Output
110
Note
The path for the first example:
<image>
The path for the second example:
<image>
Submitted Solution:
```
def get_input():
n = int(input())
mat = [[int(x) for x in input().split()] for i in range(3)]
return (n, mat)
def tc(n, mat):
inf = -2**64
ans = [[inf for i in range(n)] for i in range(3)]
ans[0][0] = mat[0][0]
ans[1][0] = mat[1][0] + ans[0][0]
ans[2][0] = mat[2][0] + ans[1][0]
for j in range(1, n):
tmp = [inf, inf, inf]
for i in range(3):
tmp[i] = ans[i][j-1] + mat[i][j]
for i in range(3):
ans[i][j] = tmp[i]
suma = mat[i][j]
for r in range(i-1, -1, -1):
ans[i][j] = max(ans[i][j], suma + tmp[r])
suma += mat[r][j]
suma = mat[i][j]
for r in range(i+1, 3):
ans[i][j] = max(ans[i][j], suma + tmp[r])
suma += mat[r][j]
if i != 1 and j >= 2:
ans[i][j] = max(ans[i][j], max(ans[0][j-2], ans[2][j-2]) +
mat[0][j-1] + mat[1][j-1] + mat[2][j-1] +
mat[0][j] + mat[1][j] + mat[2][j])
return ans[2][n-1]
def main():
n, mat = get_input()
print(tc(n, mat))
main()
``` | instruction | 0 | 75,552 | 15 | 151,104 |
No | output | 1 | 75,552 | 15 | 151,105 |
Provide a correct Python 3 solution for this coding contest problem.
A robot in a two-dimensional maze again. The maze has an entrance and an exit this time, though.
Just as in the previous problem, the maze is made up of H Γ W grid cells, its upper side faces north, and each cell is either empty or wall. Unlike the previous, on the other hand, one of the empty cells is connected to the entrance and another to the exit.
The robot is rather complex - there is some control, but not full. It is associated with a controller that has two buttons, namely forward and turn. The forward button moves the robot forward to the next cell, if possible. The robot can not move into a wall nor outside the maze. The turn button turns the robot as programmed. Here the program is a finite sequence of N commands, each of which is either 'L' (indicating a left turn) or 'R' (a right turn). The first turn follows the first command; the second turn follows the second command; similar for the following turns. The turn button stops working once the commands are exhausted; the forward button still works in such a case though. The robot always turns by 90 degrees at once.
The robot is initially put on the entrance cell, directed to the north. Your mission is to determine whether it can reach the exit cell if controlled properly.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
H W N
s1 ... sN
c1,1c1,2...c1,W
...
cH,1cH,2...cH,W
The first line of a dataset contains three integers H, W and N (1 β€ H, W β€ 1,000, 1 β€ N β€ 1,000,000).
The second line contains a program of N commands.
Each of the following H lines contains exactly W characters. Each of these characters represents a cell of the maze. "." indicates empty, "#" indicates a wall, "S" indicates an entrance, and "G" indicates an exit. There is exactly one entrance cell and one exit cell.
The end of input is indicated by a line with three zeros.
Output
For each dataset, output whether the robot can reach the exit in a line: "Yes" if it can or "No" otherwise (without quotes).
Examples
Input
2 2 1
L
G.
#S
2 2 2
RR
G.
.S
3 3 6
LLLLLL
G#.
...
.#S
0 0 0
Output
Yes
No
Yes
Input
2 2 1
L
G.
S
2 2 2
RR
G.
.S
3 3 6
LLLLLL
G#.
...
.#S
0 0 0
Output
Yes
No
Yes | instruction | 0 | 75,856 | 15 | 151,712 |
"Correct Solution:
```
from heapq import heappush, heappop
import sys
readline = sys.stdin.readline
write = sys.stdout.write
dd = ((-1, 0), (0, -1), (1, 0), (0, 1))
def solve():
H, W, N = map(int, readline().split())
if H == 0:
return False
S = readline().strip()
C = [[0]*W for i in range(H)]
sx = sy = gx = gy = 0
for i in range(H):
s = readline().strip()
for j, c in enumerate(s):
if c in '#.':
C[i][j] = (c == '#')
elif c == 'S':
sx = j; sy = i
else:
gx = j; gy = i
S0 = [1]*(N+1)
cur = 1
for i in range(N):
if S[i] == 'L':
cur = (cur - 1) % 4
else:
cur = (cur + 1) % 4
S0[i+1] = cur
d = [N+1]*4
D = [None]*(N+1)
for i in range(N, -1, -1):
d[S0[i]] = i
D[i] = d[:]
T = [[N+1]*W for i in range(H)]
T[sy][sx] = 0
que = [(0, sx, sy)]
while que:
cost, x, y = heappop(que)
if T[y][x] < cost:
continue
d = D[cost]
for k in range(4):
dx, dy = dd[k]; n_cost = d[k]
nx = x + dx; ny = y + dy
if not 0 <= nx < W or not 0 <= ny < H or C[ny][nx]:
continue
if n_cost < T[ny][nx]:
T[ny][nx] = n_cost
heappush(que, (n_cost, nx, ny))
print("Yes" if T[gy][gx] < N+1 else "No")
return True
while solve():
...
``` | output | 1 | 75,856 | 15 | 151,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya loves training spiders. Petya has a board n Γ m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.
Input
The first line contains two space-separated integers n and m (1 β€ n, m β€ 40, nΒ·m β€ 40) β the board sizes.
Output
In the first line print the maximum number of cells without spiders.
Examples
Input
1 1
Output
0
Input
2 3
Output
4
Note
In the first sample the only possible answer is:
s
In the second sample one of the possible solutions is:
rdl
rul
s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly.
Submitted Solution:
```
#!/usr/bin/env python3
(n,m) = map(int, input().split())
# Special cases:
if (n == 1) or (m == 1):
dim = max(n,m)
if (dim % 3) in [0,1]:
answer = 2*(dim // 3)
else:
answer = 2*(dim // 3) + 1
print(answer)
exit()
elif (n == 2) or (m == 2):
dim = max(n,m)
print(2*dim - (dim // 2) - 1)
exit()
elif (n == 3) and (m == 3):
print(6)
exit()
# Most efficient is gathering 5 spiders on one tile, forming a shape in the form of a + sign,
# with all spiders on the center tile and the other tiles empty.
# The + sign can tile the entire plane, by using knight moves between the center squares
# Let's generate all the coordinates of the center squares
def rotate_direction(direction):
# Rotate a direction by 90 degrees
(dx,dy) = direction
return (-dy, dx)
def move(position, direction):
# Move in a certain direction
(x,y) = position
(dx,dy) = direction
return (x+dx, y+dy)
def generate_positions(start_position):
# Generate the center positions of the + shapes, for a given starting position,
# this is done by making a knights move each time, and we will move in an outward spiral fashion to cover the entire plane
position = start_position
positions = [position]
direction = (2, 1)
nr_steps = 1
while (position[0] < (n+5)) or (position[1] < (m+5)): # I was too lazy to figure out what the exact bound should be; 5 will be enough
# 1st side of the rectangular spiral:
for i in range(nr_steps):
position = move(position, direction)
positions.append(position)
direction = rotate_direction(direction)
# 2nd side of the rectangular spiral:
for i in range(nr_steps):
position = move(position, direction)
positions.append(position)
direction = rotate_direction(direction)
# The next (2) sides need to do a bit more knight moves, otherwise we will re-visit old positions
nr_steps += 1
return positions
def is_contained(position):
# Checks whether a given position is contained in the (n,m) rectangle
(x,y) = position
return (x>= 0) and (y>=0) and (x < n) and (y < m)
def nr_cells_emptied(position):
# Checks number of cells of the (n,m) rectangle that are emptied by gathering the maximum number of spiders at the given position
to_check = [move(position, direction) for direction in [(0,0), (-1,0), (1,0), (0,-1), (0,1)]]
nr_emptied = max(len([pos for pos in to_check if is_contained(pos)])-1,0)
return nr_emptied
# Starting position is either in the corner of the rectangle, or one step removed:
answer = 0
for start_position in [(0,0), (0,1), (1,0)]:
answer = max(answer, sum(nr_cells_emptied(pos) for pos in generate_positions(start_position)))
print(answer)
``` | instruction | 0 | 75,917 | 15 | 151,834 |
No | output | 1 | 75,917 | 15 | 151,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya loves training spiders. Petya has a board n Γ m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.
Input
The first line contains two space-separated integers n and m (1 β€ n, m β€ 40, nΒ·m β€ 40) β the board sizes.
Output
In the first line print the maximum number of cells without spiders.
Examples
Input
1 1
Output
0
Input
2 3
Output
4
Note
In the first sample the only possible answer is:
s
In the second sample one of the possible solutions is:
rdl
rul
s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly.
Submitted Solution:
```
#!/usr/bin/env python3
(n,m) = map(int, input().split())
# Special case:
if (n == 1) or (m == 1):
dim = max(n,m)
if (dim % 3) in [0,1]:
answer = 2*(dim // 3)
else:
answer = 2*(dim // 3) + 1
print(answer)
exit()
# Most efficient is gathering 5 spiders on one tile, forming a shape in the form of a + sign,
# with all spiders on the center tile and the other tiles empty.
# The + sign can tile the entire plane, by using knight moves between the center squares
# Let's generate all the coordinates of the center squares
def rotate_direction(direction):
# Rotate a direction by 90 degrees
(dx,dy) = direction
return (-dy, dx)
def move(position, direction):
# Move in a certain direction
(x,y) = position
(dx,dy) = direction
return (x+dx, y+dy)
def generate_positions(start_position):
# Generate the center positions of the + shapes, for a given starting position,
# this is done by making a knights move each time, and we will move in an outward spiral fashion to cover the entire plane
position = start_position
positions = [position]
direction = (2, 1)
nr_steps = 1
while (position[0] < (n+5)) or (position[1] < (m+5)): # I was too lazy to figure out what the exact bound should be; 5 will be enough
# 1st side of the rectangular spiral:
for i in range(nr_steps):
position = move(position, direction)
positions.append(position)
direction = rotate_direction(direction)
# 2nd side of the rectangular spiral:
for i in range(nr_steps):
position = move(position, direction)
positions.append(position)
direction = rotate_direction(direction)
# The next (2) sides need to do a bit more knight moves, otherwise we will re-visit old positions
nr_steps += 1
return positions
def is_contained(position):
# Checks whether a given position is contained in the (n,m) rectangle
(x,y) = position
return (x>= 0) and (y>=0) and (x < n) and (y < m)
def nr_cells_emptied(position):
# Checks number of cells of the (n,m) rectangle that are emptied by gathering the maximum number of spiders at the given position
to_check = [move(position, direction) for direction in [(0,0), (-1,0), (1,0), (0,-1), (0,1)]]
return max(len([pos for pos in to_check if is_contained(pos)])-1,0)
# Starting position is either in the corner of the rectangle, or one step removed:
answer = sum(nr_cells_emptied(pos) for pos in generate_positions((0,0)))
answer = max(answer, sum(nr_cells_emptied(pos) for pos in generate_positions((1,0))))
print(answer)
``` | instruction | 0 | 75,918 | 15 | 151,836 |
No | output | 1 | 75,918 | 15 | 151,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya loves training spiders. Petya has a board n Γ m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.
Input
The first line contains two space-separated integers n and m (1 β€ n, m β€ 40, nΒ·m β€ 40) β the board sizes.
Output
In the first line print the maximum number of cells without spiders.
Examples
Input
1 1
Output
0
Input
2 3
Output
4
Note
In the first sample the only possible answer is:
s
In the second sample one of the possible solutions is:
rdl
rul
s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly.
Submitted Solution:
```
INF=(10**9)+7
n,m=map(int,input().split())
if(n*m<23):
dp=[INF]*(1<<(n*m))
dp[0]=0
#print(dp)
for i in range(0,(1<<(n*m))):
if(dp[i]<INF):
for j in range(0,n*m):
c=0
pre=i
if(i&(1<<j)==0):
pre=pre|(1<<j)
c+=1
if(n>1 and j-m>=0 and i&(1<<(j-m))==0):
pre=pre|(1<<(j-m))
c+=1
if(n>1 and j+m<n*m and i&(1<<(j+m))==0):
pre=pre|(1<<(j+m))
c+=1
if(m>1 and j-1>=0 and (j)%m!=0 and i&(1<<(j-1))==0):
pre=pre|(1<<(j-1))
c+=1
if(m>1 and j+1<n*m and (j)%(m-1)!=0 and i&(1<<(j+1))==0):
pre=pre|(1<<(j+1))
c+=1
if(c>0):
dp[pre]=min(dp[pre],dp[i]+1)
#print(i,j,pre,c)
#if(i==70 and j==4):
#print(i,j,pre,c)
print((n*m)-(dp[(1<<(n*m))-1]))
else:
tt=(n*m)//3
if((n*m)%3!=0):
tt+=1
print((n*m)-tt)
``` | instruction | 0 | 75,919 | 15 | 151,838 |
No | output | 1 | 75,919 | 15 | 151,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya loves training spiders. Petya has a board n Γ m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.
Input
The first line contains two space-separated integers n and m (1 β€ n, m β€ 40, nΒ·m β€ 40) β the board sizes.
Output
In the first line print the maximum number of cells without spiders.
Examples
Input
1 1
Output
0
Input
2 3
Output
4
Note
In the first sample the only possible answer is:
s
In the second sample one of the possible solutions is:
rdl
rul
s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly.
Submitted Solution:
```
"""
INF=(10**9)+7
n,m=map(int,input().split())
if(n*m<27):
dp=[INF]*(1<<(n*m))
dp[0]=0
#print(dp)
for i in range(0,(1<<(n*m))):
if(dp[i]<INF):
for j in range(0,n*m):
c=0
pre=i
if(i&(1<<j)==0):
pre=pre|(1<<j)
c+=1
if(n>1 and j-m>=0 and i&(1<<(j-m))==0):
pre=pre|(1<<(j-m))
c+=1
if(n>1 and j+m<n*m and i&(1<<(j+m))==0):
pre=pre|(1<<(j+m))
c+=1
if(m>1 and j-1>=0 and (j)%m!=0 and i&(1<<(j-1))==0):
pre=pre|(1<<(j-1))
c+=1
if(m>1 and j+1<n*m and (j)%(m-1)!=0 and i&(1<<(j+1))==0):
pre=pre|(1<<(j+1))
c+=1
if(c>0):
dp[pre]=min(dp[pre],dp[i]+1)
#print(i,j,pre,c)
#if(i==70 and j==4):
#print(i,j,pre,c)
print((n*m)-(dp[(1<<(n*m))-1]))
else:
tt=(n*m)//3
if((n*m)%3!=0):
tt+=1
print((n*m)-tt)
"""
"""
INF=(10**9)+7
n,m=map(int,input().split())
global ans
ans=(n*m)
col=max(n,m)
row=min(n,m)
dp=[INF]*(1<<row)
dp[0]=0
for i in range(0,(1<<row)):
if(dp[i]<INF):
for j in range(0,row):
if(i&(1<<j)==0):
tt=i|(1<<j)
dp[tt]=min(dp[tt],dp[i]+1)
#print(dp)
def dfs(colm,prev_mask,curr_mask,val):
if(colm==col+1):
#print('val',val,col+1,colm,prev_mask,curr_mask)
if(prev_mask==(1<<row)-1):
global ans
ans=min(ans,val)
return
#print(dp)
for i in range(0,(1<<row)):
if(i|prev_mask==((1<<row)-1)):
hh=bin(i)
hh=hh[2:]
hh='0'*(row-len(hh))+hh
temp=['0']*row
for j in range(0,len(hh)):
if(hh[j]=='1'):
if(j-1>=0):
temp[j-1]='1'
temp[j]='1'
if(j+1<len(temp)):
temp[j+1]='1'
temp=''.join(temp)
temp=int(temp,2)
temp=temp|curr_mask
dfs(colm+1,temp,i,dp[i]+val)
dfs(1,(1<<row)-1,0,0)
print((n*m)-ans)
"""
INF=(10**9)+7
n,m=map(int,input().split())
global ans
ans=(n*m)
col=max(n,m)
row=min(n,m)
if(row==3 and col==6):
print(13)
elif(row==4 and col==10):
print(30)
elif(row==3 and col==10):
print(21)
else:
dp=[INF]*(1<<row)
dp[0]=0
for i in range(0,(1<<row)):
if(dp[i]<INF):
for j in range(0,row):
if(i&(1<<j)==0):
tt=i|(1<<j)
dp[tt]=min(dp[tt],dp[i]+1)
#print(dp)
def dfs(colm,prev_mask,curr_mask,val):
if(colm==col+1):
#print('val',val,col+1,colm,prev_mask,curr_mask)
if(prev_mask==(1<<row)-1):
global ans
ans=min(ans,val)
return
"""dp=[INF]*(1<<row)
dp[0]=0
for i in range(0,(1<<row)):
if(dp[i]<INF):
for j in range(0,row):
if(i&(1<<j)==0):
tt=i|(1<<j)
dp[tt]=min(dp[tt],dp[i]+1)"""
#print(dp)
for i in range(0,(1<<row)):
if(i|prev_mask==((1<<row)-1) and (i&curr_mask)==0):
hh=bin(i)
hh=hh[2:]
hh='0'*(row-len(hh))+hh
temp=['0']*row
for j in range(0,len(hh)):
if(hh[j]=='1'):
if(j-1>=0):
temp[j-1]='1'
temp[j]='1'
if(j+1<len(temp)):
temp[j+1]='1'
temp=''.join(temp)
temp=int(temp,2)
temp=temp|curr_mask
dfs(colm+1,temp,i,dp[i]+val)
dfs(1,(1<<row)-1,0,0)
print((n*m)-ans)
"""
3 5
10
2 10
14
3 4
8
4 4
12
"""
``` | instruction | 0 | 75,920 | 15 | 151,840 |
No | output | 1 | 75,920 | 15 | 151,841 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem
You are given a grid nΓ n, where n is odd. Rows are enumerated from 1 to n from up to down, columns are enumerated from 1 to n from left to right. Cell, standing on the intersection of row x and column y, is denoted by (x, y).
Every cell contains 0 or 1. It is known that the top-left cell contains 1, and the bottom-right cell contains 0.
We want to know numbers in all cells of the grid. To do so we can ask the following questions:
"? x_1 y_1 x_2 y_2", where 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. In other words, we output two different cells (x_1, y_1), (x_2, y_2) of the grid such that we can get from the first to the second by moving only to the right and down, and they aren't adjacent.
As a response to such question you will be told if there exists a path between (x_1, y_1) and (x_2, y_2), going only to the right or down, numbers in cells of which form a palindrome.
For example, paths, shown in green, are palindromic, so answer for "? 1 1 2 3" and "? 1 2 3 3" would be that there exists such path. However, there is no palindromic path between (1, 1) and (3, 1).
<image>
Determine all cells of the grid by asking not more than n^2 questions. It can be shown that the answer always exists.
Input
The first line contains odd integer (3 β€ n < 50) β the side of the grid.
Interaction
You begin the interaction by reading n.
To ask a question about cells (x_1, y_1), (x_2, y_2), in a separate line output "? x_1 y_1 x_2 y_2".
Numbers in the query have to satisfy 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. Don't forget to 'flush', to get the answer.
In response, you will receive 1, if there exists a path going from (x_1, y_1) to (x_2, y_2) only to the right or down, numbers in cells of which form a palindrome, and 0 otherwise.
In case your query is invalid or you asked more than n^2 queries, program will print -1 and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you determine numbers in all cells, output "!".
Then output n lines, the i-th of which is a string of length n, corresponding to numbers in the i-th row of the grid.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hack Format
To hack, use the following format.
The first line should contain a single odd integer n (side of your grid).
The i-th of n following lines should contain a string of length n corresponding to the i-th row of the grid. Top left element of the grid has to be equal to 1, bottom right has to be equal to 0.
Example
Input
3
0
1
0
1
1
1
1
Output
? 1 1 1 3
? 1 1 2 3
? 2 1 2 3
? 3 1 3 3
? 2 2 3 3
? 1 2 3 2
? 1 2 3 3
!
100
001
000 | instruction | 0 | 75,953 | 15 | 151,906 |
Tags: implementation, interactive
Correct Solution:
```
#!/usr/bin/python3
import math
import sys
DEBUG = False
def inp():
return sys.stdin.readline().rstrip()
def dprint(*value, sep=' ', end='\n'):
if DEBUG:
print(*value, sep=sep, end=end)
def readi():
n = int(inp())
dprint('read', n)
if n == -1:
sys.exit()
return n
def ask(a, b, c, d):
dprint('ask', a, b, c, d)
a += 1
b += 1
c += 1
d += 1
print('?', a, b, c, d, flush=True)
return readi()
def answer(M):
dprint('ans', M)
print('!')
for m in M:
print(*m, sep='')
sys.stdout.flush()
def posif(x):
if x >= 2:
return x - 2
return x
def negaf(x):
if x >= 2:
return 1 - (x - 2)
return x
def main():
N = readi()
M = [[-1] * N for _ in range(N)]
M[0][0] = 1
M[N - 1][N - 1] = 0
for y in range(0, N, 2):
for x in range(0, N, 2):
if (y, x) in ((0, 0), (N - 1, N - 1)):
continue
if x > 1:
if ask(y, x - 2, y, x):
M[y][x] = M[y][x - 2]
else:
M[y][x] = 1 - M[y][x - 2]
else:
if ask(y - 2, x, y, x):
M[y][x] = M[y - 2][x]
else:
M[y][x] = 1 - M[y - 2][x]
for y in range(1, N, 2):
for x in range(1, N, 2):
if ask(y, x, y + 1, x + 1):
M[y][x] = M[y + 1][x + 1]
else:
M[y][x] = 1 - M[y + 1][x + 1]
M[0][1] = 0
for y in range(0, N, 2):
for x in range(1, N, 2):
if (y, x) == (0, 1):
continue
if x > 1:
if ask(y, x - 2, y, x):
M[y][x] = M[y][x - 2]
else:
M[y][x] = 1 - M[y][x - 2]
else:
if ask(y - 2, x, y, x):
M[y][x] = M[y - 2][x]
else:
M[y][x] = 1 - M[y - 2][x]
for y in range(1, N, 2):
for x in range(0, N, 2):
if x > 0:
if ask(y - 1, x - 1, y, x):
M[y][x] = M[y - 1][x - 1]
else:
M[y][x] = 1 - M[y - 1][x - 1]
else:
if ask(y, x, y + 1, x + 1):
M[y][x] = M[y + 1][x + 1]
else:
M[y][x] = 1 - M[y + 1][x + 1]
for y in range(N):
for x in range(N):
if (x + y) % 2 == 1:
M[y][x] += 2
dprint(M)
D = [(0, 3), (1, 2), (2, 1), (3, 0)]
for iy in range(N):
for ix in range(N):
for dy, dx in D:
jy = iy + dy
jx = ix + dx
if jy >= N or jx >= N:
continue
# dprint(iy, ix, jy, jx)
paths = calc_paths(iy, ix, jy, jx)
# dprint('paths', paths)
vals = [[M[y][x] for y, x in path] for path in paths]
# dprint('vals', vals)
posi = [list(map(posif, val)) for val in vals]
nega = [list(map(negaf, val)) for val in vals]
# dprint('posi', posi, 'nega', nega)
# dprint([li == list(reversed(li)) for li in posi])
# dprint([li == list(reversed(li)) for li in nega])
ep = any([li == list(reversed(li)) for li in posi])
en = any([li == list(reversed(li)) for li in nega])
# dprint('ep', ep, 'en', en)
if ep == en:
continue
res = bool(ask(iy, ix, jy, jx))
fn = posif if res == ep else negaf
for y in range(N):
for x in range(N):
if (x + y) % 2 == 1:
M[y][x] = fn(M[y][x])
answer(M)
return
assert False
def calc_paths(iy, ix, jy, jx):
paths = []
path = []
def rec(y, x):
path.append((y, x))
if (y, x) == (jy, jx):
paths.append(tuple(path))
else:
for dy, dx in [(0, 1), (1, 0)]:
ny = y + dy
nx = x + dx
if ny > jy or nx > jx:
continue
rec(ny, nx)
path.pop()
rec(iy, ix)
return paths
if __name__ == '__main__':
main()
``` | output | 1 | 75,953 | 15 | 151,907 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem
You are given a grid nΓ n, where n is odd. Rows are enumerated from 1 to n from up to down, columns are enumerated from 1 to n from left to right. Cell, standing on the intersection of row x and column y, is denoted by (x, y).
Every cell contains 0 or 1. It is known that the top-left cell contains 1, and the bottom-right cell contains 0.
We want to know numbers in all cells of the grid. To do so we can ask the following questions:
"? x_1 y_1 x_2 y_2", where 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. In other words, we output two different cells (x_1, y_1), (x_2, y_2) of the grid such that we can get from the first to the second by moving only to the right and down, and they aren't adjacent.
As a response to such question you will be told if there exists a path between (x_1, y_1) and (x_2, y_2), going only to the right or down, numbers in cells of which form a palindrome.
For example, paths, shown in green, are palindromic, so answer for "? 1 1 2 3" and "? 1 2 3 3" would be that there exists such path. However, there is no palindromic path between (1, 1) and (3, 1).
<image>
Determine all cells of the grid by asking not more than n^2 questions. It can be shown that the answer always exists.
Input
The first line contains odd integer (3 β€ n < 50) β the side of the grid.
Interaction
You begin the interaction by reading n.
To ask a question about cells (x_1, y_1), (x_2, y_2), in a separate line output "? x_1 y_1 x_2 y_2".
Numbers in the query have to satisfy 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. Don't forget to 'flush', to get the answer.
In response, you will receive 1, if there exists a path going from (x_1, y_1) to (x_2, y_2) only to the right or down, numbers in cells of which form a palindrome, and 0 otherwise.
In case your query is invalid or you asked more than n^2 queries, program will print -1 and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you determine numbers in all cells, output "!".
Then output n lines, the i-th of which is a string of length n, corresponding to numbers in the i-th row of the grid.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hack Format
To hack, use the following format.
The first line should contain a single odd integer n (side of your grid).
The i-th of n following lines should contain a string of length n corresponding to the i-th row of the grid. Top left element of the grid has to be equal to 1, bottom right has to be equal to 0.
Example
Input
3
0
1
0
1
1
1
1
Output
? 1 1 1 3
? 1 1 2 3
? 2 1 2 3
? 3 1 3 3
? 2 2 3 3
? 1 2 3 2
? 1 2 3 3
!
100
001
000 | instruction | 0 | 75,954 | 15 | 151,908 |
Tags: implementation, interactive
Correct Solution:
```
import sys
def query(x1, y1, x2, y2):
print("?", x1, y1, x2, y2)
sys.stdout.flush()
odp = int(input())
return odp
n = int(input())
mat = [[2 for c in range(n + 1)] for r in range(n + 1)]
mat[1][1] = 1
mat[n][n] = 0
for d in range(1, n - 1):
odp = query(d, d, d + 1, d + 1)
if odp:
mat[d + 1][d + 1] = mat[d][d]
else:
mat[d + 1][d + 1] = 1 - mat[d][d]
d = 1
while True:
if mat[d][d] != mat[d + 2][d + 2]:
break
d += 2
pom1 = query(d, d, d + 2, d)
pom2 = query(d + 1, d, d + 2, d + 1)
if pom1 == 0 and pom2 == 0:
pom3 = query(d, d, d + 2, d + 1)
if pom3 == 1:
mat[d + 1][d] = 1 - mat[d][d]
mat[d + 2][d] = 1 - mat[d][d]
mat[d + 2][d + 1] = mat[d][d]
else:
mat[d + 1][d] = mat[d][d]
mat[d + 2][d] = 1 - mat[d][d]
mat[d + 2][d + 1] = 1 - mat[d][d]
if pom1 == 0 and pom2 == 1:
pom3 = query(d + 1, d, d + 2, d + 2)
if pom3 == 1:
mat[d + 2][d + 1] = mat[d + 2][d + 2]
mat[d + 2][d] = mat[d + 2][d + 2]
mat[d + 1][d] = mat[d + 2][d + 2]
else:
mat[d + 2][d + 1] = 1 - mat[d + 2][d + 2]
mat[d + 2][d] = mat[d + 2][d + 2]
mat[d + 1][d] = 1 - mat[d + 2][d + 2]
if pom1 == 1 and pom2 == 0:
pom3 = query(d + 1, d, d + 2, d + 2)
if pom3 == 1:
mat[d + 2][d + 1] = 1 - mat[d + 2][d + 2]
mat[d + 2][d] = 1 - mat[d + 2][d + 2]
mat[d + 1][d] = mat[d + 2][d + 2]
else:
mat[d + 2][d + 1] = mat[d + 2][d + 2]
mat[d + 2][d] = 1 - mat[d + 2][d + 2]
mat[d + 1][d] = 1 - mat[d + 2][d + 2]
if pom1 == 1 and pom2 == 1:
pom3 = query(d, d, d + 2, d + 1)
if pom3 == 1:
mat[d + 2][d + 1] = mat[d][d]
mat[d + 2][d] = mat[d][d]
mat[d + 1][d] = mat[d][d]
else:
mat[d + 2][d + 1] = 1 - mat[d][d]
mat[d + 2][d] = mat[d][d]
mat[d + 1][d] = 1 - mat[d][d]
###################################################
for r in [d + 1, d + 2]:
c = d
while c + 2 <= n:
if mat[r][c + 2] == 2:
odp = query(r, c, r, c + 2)
if odp == 1:
mat[r][c + 2] = mat[r][c]
else:
mat[r][c + 2] = 1 - mat[r][c]
c += 1
c = d + 1
while c - 2 > 0:
if mat[r][c - 2] == 2:
odp = query(r, c - 2, r, c)
if odp == 1:
mat[r][c - 2] = mat[r][c]
else:
mat[r][c - 2] = 1 - mat[r][c]
c -= 1
for c in range(1, n + 1):
r = d + 1
while r + 2 <= n:
if mat[r + 2][c] == 2:
odp = query(r, c, r + 2, c)
if odp == 1:
mat[r + 2][c] = mat[r][c]
else:
mat[r +2][c] = 1 - mat[r][c]
r += 1
r = d + 2
while r - 2 > 0:
if mat[r - 2][c] == 2:
odp = query(r - 2, c, r, c)
if odp == 1:
mat[r - 2][c] = mat[r][c]
else:
mat[r -2][c] = 1 - mat[r][c]
r -= 1
print("!")
for i in range(1, n + 1):
slowo = ""
for j in mat[i][1:]:
slowo += str(j)
print(slowo)
``` | output | 1 | 75,954 | 15 | 151,909 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem
You are given a grid nΓ n, where n is odd. Rows are enumerated from 1 to n from up to down, columns are enumerated from 1 to n from left to right. Cell, standing on the intersection of row x and column y, is denoted by (x, y).
Every cell contains 0 or 1. It is known that the top-left cell contains 1, and the bottom-right cell contains 0.
We want to know numbers in all cells of the grid. To do so we can ask the following questions:
"? x_1 y_1 x_2 y_2", where 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. In other words, we output two different cells (x_1, y_1), (x_2, y_2) of the grid such that we can get from the first to the second by moving only to the right and down, and they aren't adjacent.
As a response to such question you will be told if there exists a path between (x_1, y_1) and (x_2, y_2), going only to the right or down, numbers in cells of which form a palindrome.
For example, paths, shown in green, are palindromic, so answer for "? 1 1 2 3" and "? 1 2 3 3" would be that there exists such path. However, there is no palindromic path between (1, 1) and (3, 1).
<image>
Determine all cells of the grid by asking not more than n^2 questions. It can be shown that the answer always exists.
Input
The first line contains odd integer (3 β€ n < 50) β the side of the grid.
Interaction
You begin the interaction by reading n.
To ask a question about cells (x_1, y_1), (x_2, y_2), in a separate line output "? x_1 y_1 x_2 y_2".
Numbers in the query have to satisfy 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. Don't forget to 'flush', to get the answer.
In response, you will receive 1, if there exists a path going from (x_1, y_1) to (x_2, y_2) only to the right or down, numbers in cells of which form a palindrome, and 0 otherwise.
In case your query is invalid or you asked more than n^2 queries, program will print -1 and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you determine numbers in all cells, output "!".
Then output n lines, the i-th of which is a string of length n, corresponding to numbers in the i-th row of the grid.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hack Format
To hack, use the following format.
The first line should contain a single odd integer n (side of your grid).
The i-th of n following lines should contain a string of length n corresponding to the i-th row of the grid. Top left element of the grid has to be equal to 1, bottom right has to be equal to 0.
Example
Input
3
0
1
0
1
1
1
1
Output
? 1 1 1 3
? 1 1 2 3
? 2 1 2 3
? 3 1 3 3
? 2 2 3 3
? 1 2 3 2
? 1 2 3 3
!
100
001
000 | instruction | 0 | 75,955 | 15 | 151,910 |
Tags: implementation, interactive
Correct Solution:
```
import sys
def req(a, b, c, d):
print("?", a+1, b+1, c+1, d+1)
sys.stdout.flush()
return int(input())
n = int(input())
r = [[0]*n for _ in range(n)]
def tt(xx, yy, x, y):
if xx > x or yy > y:
q = req(x, y, xx, yy)
else:
q = req(xx, yy, x, y)
if q == -1:
exit()
elif q == 1:
r[x][y] = r[xx][yy]
else:
r[x][y] = 1 - r[xx][yy]
r[0][0] = 1
for x in range(n):
for y in range(x & 1, n, 2):
if x == 0 and y == 0:
continue
if x == n-1 and y == n-1:
continue
if x == 0:
xx, yy = x, y-2
elif y == 0:
xx, yy = x-2, y
else:
xx, yy = x-1, y-1
tt(xx, yy, x, y)
tt(0, 1, 2, 1)
tt(2, 1, 1, 0)
for x in range(n):
for y in range(1 - (x & 1), n, 2):
if x == 1 and y == 0:
continue
if x == 0 and y == 1:
continue
if x == 2 and y == 1:
continue
if x == 0:
xx, yy = x, y-2
elif y == 0:
xx, yy = x-2, y
else:
xx, yy = x-1, y-1
tt(xx, yy, x, y)
def inv():
for x in range(n):
for y in range(1 - (x & 1), n, 2):
r[x][y] = 1 - r[x][y]
def zz():
for x in range(n-1):
for y in range(n-2):
if r[x][y+1] != r[x+1][y] or r[x][y+2] != r[x+1][y+1] or (r[x][y] == r[x][y+2]) == (r[x+1][y] == r[x+1][y+2]):
q = req(x, y, x+1, y+2)
if q == -1:
exit()
elif q != (r[x][y] == r[x+1][y+2]):
inv()
return
zz()
print("!")
sys.stdout.flush()
for x in r:
print("".join(map(str, x)))
``` | output | 1 | 75,955 | 15 | 151,911 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem
You are given a grid nΓ n, where n is odd. Rows are enumerated from 1 to n from up to down, columns are enumerated from 1 to n from left to right. Cell, standing on the intersection of row x and column y, is denoted by (x, y).
Every cell contains 0 or 1. It is known that the top-left cell contains 1, and the bottom-right cell contains 0.
We want to know numbers in all cells of the grid. To do so we can ask the following questions:
"? x_1 y_1 x_2 y_2", where 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. In other words, we output two different cells (x_1, y_1), (x_2, y_2) of the grid such that we can get from the first to the second by moving only to the right and down, and they aren't adjacent.
As a response to such question you will be told if there exists a path between (x_1, y_1) and (x_2, y_2), going only to the right or down, numbers in cells of which form a palindrome.
For example, paths, shown in green, are palindromic, so answer for "? 1 1 2 3" and "? 1 2 3 3" would be that there exists such path. However, there is no palindromic path between (1, 1) and (3, 1).
<image>
Determine all cells of the grid by asking not more than n^2 questions. It can be shown that the answer always exists.
Input
The first line contains odd integer (3 β€ n < 50) β the side of the grid.
Interaction
You begin the interaction by reading n.
To ask a question about cells (x_1, y_1), (x_2, y_2), in a separate line output "? x_1 y_1 x_2 y_2".
Numbers in the query have to satisfy 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. Don't forget to 'flush', to get the answer.
In response, you will receive 1, if there exists a path going from (x_1, y_1) to (x_2, y_2) only to the right or down, numbers in cells of which form a palindrome, and 0 otherwise.
In case your query is invalid or you asked more than n^2 queries, program will print -1 and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you determine numbers in all cells, output "!".
Then output n lines, the i-th of which is a string of length n, corresponding to numbers in the i-th row of the grid.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hack Format
To hack, use the following format.
The first line should contain a single odd integer n (side of your grid).
The i-th of n following lines should contain a string of length n corresponding to the i-th row of the grid. Top left element of the grid has to be equal to 1, bottom right has to be equal to 0.
Example
Input
3
0
1
0
1
1
1
1
Output
? 1 1 1 3
? 1 1 2 3
? 2 1 2 3
? 3 1 3 3
? 2 2 3 3
? 1 2 3 2
? 1 2 3 3
!
100
001
000 | instruction | 0 | 75,956 | 15 | 151,912 |
Tags: implementation, interactive
Correct Solution:
```
import sys
input = sys.stdin.readline
n=int(input())
ANS=[[-1]*(n+1) for i in range(n+1)]
ANS[1][1]=1
ANS[n][n]=0
for i in range(1,n+1):
for j in range(1,n+1):
if (i+j)%2==1:
continue
if i==j==1 or i==j==n:
continue
if i==1:
print("?",i,j-2,i,j,flush=True)
x=int(input())
if x==1:
ANS[i][j]=ANS[i][j-2]
else:
ANS[i][j]=1-ANS[i][j-2]
elif i==2:
print("?",i-1,j-1,i,j,flush=True)
x=int(input())
if x==1:
ANS[i][j]=ANS[i-1][j-1]
else:
ANS[i][j]=1-ANS[i-1][j-1]
else:
print("?",i-2,j,i,j,flush=True)
x=int(input())
if x==1:
ANS[i][j]=ANS[i-2][j]
else:
ANS[i][j]=1-ANS[i-2][j]
ANS[1][2]=2
for i in range(1,n+1):
for j in range(2,n+1):
if (i+j)%2==0:
continue
if i==1 and j==2:
continue
if i==1:
print("?",i,j-2,i,j,flush=True)
x=int(input())
if x==1:
ANS[i][j]=ANS[i][j-2]
else:
ANS[i][j]=5-ANS[i][j-2]
elif i==2:
print("?",i-1,j-1,i,j,flush=True)
x=int(input())
if x==1:
ANS[i][j]=ANS[i-1][j-1]
else:
ANS[i][j]=5-ANS[i-1][j-1]
else:
print("?",i-2,j,i,j,flush=True)
x=int(input())
if x==1:
ANS[i][j]=ANS[i-2][j]
else:
ANS[i][j]=5-ANS[i-2][j]
for i in range(2,n+1,2):
print("?",i,1,i,3,flush=True)
x=int(input())
if x==1:
ANS[i][1]=ANS[i][3]
else:
ANS[i][1]=5-ANS[i][3]
SET={(0,0,2,2),(0,0,3,3),(1,1,2,2),(1,1,3,3),(0,1,2,3)}
flag=0
for i in range(1,n+1):
if flag:
break
for j in range(1,n+1):
if j+3<=n:
if tuple(sorted([ANS[i][j],ANS[i][j+1],ANS[i][j+2],ANS[i][j+3]])) in SET:
flag=1
LAST=[i,j,i,j+3]
break
if i+1<=n and j+2<=n:
if tuple(sorted([ANS[i][j],ANS[i][j+1],ANS[i][j+2],ANS[i+1][j+2]])) in SET:
flag=1
LAST=[i,j,i+1,j+2]
break
if i+1<=n and j+2<=n:
if tuple(sorted([ANS[i][j],ANS[i][j+1],ANS[i+1][j+1],ANS[i+1][j+2]])) in SET:
flag=1
LAST=[i,j,i+1,j+2]
break
if i+2<=n and j+1<=n:
if tuple(sorted([ANS[i][j],ANS[i][j+1],ANS[i+1][j+1],ANS[i+2][j+1]])) in SET:
flag=1
LAST=[i,j,i+2,j+1]
break
if i+1<=n and j+2<=n:
if tuple(sorted([ANS[i][j],ANS[i+1][j],ANS[i+1][j+1],ANS[i+1][j+2]])) in SET:
flag=1
LAST=[i,j,i+1,j+2]
break
if i+2<=n and j+1<=n:
if tuple(sorted([ANS[i][j],ANS[i+1][j],ANS[i+1][j+1],ANS[i+2][j+1]])) in SET:
flag=1
LAST=[i,j,i+2,j+1]
break
if i+2<=n and j+1<=n:
if tuple(sorted([ANS[i][j],ANS[i+1][j],ANS[i+2][j],ANS[i+2][j+1]])) in SET:
flag=1
LAST=[i,j,i+2,j+1]
break
if i+3<=n:
if tuple(sorted([ANS[i][j],ANS[i+1][j],ANS[i+2][j],ANS[i+3][j]])) in SET:
flag=1
LAST=[i,j,i+3,j]
break
print("?",LAST[0],LAST[1],LAST[2],LAST[3],flush=True)
x=int(input())
DECIDE=sorted([ANS[LAST[0]][LAST[1]],ANS[LAST[2]][LAST[3]]])
if x==1:
DICT={DECIDE[1]:DECIDE[0],5-DECIDE[1]:1-DECIDE[0]}
else:
DICT={DECIDE[1]:1-DECIDE[0],5-DECIDE[1]:DECIDE[0]}
for i in range(1,n+1):
for j in range(1,n+1):
if (i+j)%2==1:
ANS[i][j]=DICT[ANS[i][j]]
print("!")
for a in ANS[1:]:
print("".join(map(str,a[1:])))
``` | output | 1 | 75,956 | 15 | 151,913 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem
You are given a grid nΓ n, where n is odd. Rows are enumerated from 1 to n from up to down, columns are enumerated from 1 to n from left to right. Cell, standing on the intersection of row x and column y, is denoted by (x, y).
Every cell contains 0 or 1. It is known that the top-left cell contains 1, and the bottom-right cell contains 0.
We want to know numbers in all cells of the grid. To do so we can ask the following questions:
"? x_1 y_1 x_2 y_2", where 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. In other words, we output two different cells (x_1, y_1), (x_2, y_2) of the grid such that we can get from the first to the second by moving only to the right and down, and they aren't adjacent.
As a response to such question you will be told if there exists a path between (x_1, y_1) and (x_2, y_2), going only to the right or down, numbers in cells of which form a palindrome.
For example, paths, shown in green, are palindromic, so answer for "? 1 1 2 3" and "? 1 2 3 3" would be that there exists such path. However, there is no palindromic path between (1, 1) and (3, 1).
<image>
Determine all cells of the grid by asking not more than n^2 questions. It can be shown that the answer always exists.
Input
The first line contains odd integer (3 β€ n < 50) β the side of the grid.
Interaction
You begin the interaction by reading n.
To ask a question about cells (x_1, y_1), (x_2, y_2), in a separate line output "? x_1 y_1 x_2 y_2".
Numbers in the query have to satisfy 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. Don't forget to 'flush', to get the answer.
In response, you will receive 1, if there exists a path going from (x_1, y_1) to (x_2, y_2) only to the right or down, numbers in cells of which form a palindrome, and 0 otherwise.
In case your query is invalid or you asked more than n^2 queries, program will print -1 and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you determine numbers in all cells, output "!".
Then output n lines, the i-th of which is a string of length n, corresponding to numbers in the i-th row of the grid.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hack Format
To hack, use the following format.
The first line should contain a single odd integer n (side of your grid).
The i-th of n following lines should contain a string of length n corresponding to the i-th row of the grid. Top left element of the grid has to be equal to 1, bottom right has to be equal to 0.
Example
Input
3
0
1
0
1
1
1
1
Output
? 1 1 1 3
? 1 1 2 3
? 2 1 2 3
? 3 1 3 3
? 2 2 3 3
? 1 2 3 2
? 1 2 3 3
!
100
001
000 | instruction | 0 | 75,957 | 15 | 151,914 |
Tags: implementation, interactive
Correct Solution:
```
n = int(input())
a=[]
for i in range(0,n+1):
temp= [0]*(n+1)
a.append(temp)
a[1][1]=1
def ask(x1,y1,x2,y2):
print("?",x1,y1,x2,y2)
a= int(input())
if a:
return True
else:
False
for i in range(3,n+1,1):
a[1][i] = a[1][i-2]^(not(ask(1,i-2,1,i)))
for i in range(2,n+1):
for j in range (n,0,-1):
if i==n and j==n:
a[i][j]=0
continue
if j>=2: a[i][j] = a[i-1][j-1] ^(not(ask(i-1,j-1,i,j)))
else: a[i][j] = a[i][j+2] ^(not(ask(i,j,i,j+2)))
ed=0
for i in range(1,n-1,2):
if (a[i][i]) == 1 and a[i+2][i+2]== 0:
if ask(i,i+1,i+2,i+2): ed=a[i][i+1]
elif ask(i,i,i+1,i+2): ed = a[i+1][i+2]^1
elif a[i][i+1] == a[i+1][i+2]: ed=a[i][i+1]^a[i][i+2]^1
else: ed=a[i][i+1]^1
break
print("!")
for i in range (1,n+1):
for j in range (1,n+1):
if (i+j)%2 != 0: a[i][j] ^= ed
print(a[i][j],end="")
print("")
``` | output | 1 | 75,957 | 15 | 151,915 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem
You are given a grid nΓ n, where n is odd. Rows are enumerated from 1 to n from up to down, columns are enumerated from 1 to n from left to right. Cell, standing on the intersection of row x and column y, is denoted by (x, y).
Every cell contains 0 or 1. It is known that the top-left cell contains 1, and the bottom-right cell contains 0.
We want to know numbers in all cells of the grid. To do so we can ask the following questions:
"? x_1 y_1 x_2 y_2", where 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. In other words, we output two different cells (x_1, y_1), (x_2, y_2) of the grid such that we can get from the first to the second by moving only to the right and down, and they aren't adjacent.
As a response to such question you will be told if there exists a path between (x_1, y_1) and (x_2, y_2), going only to the right or down, numbers in cells of which form a palindrome.
For example, paths, shown in green, are palindromic, so answer for "? 1 1 2 3" and "? 1 2 3 3" would be that there exists such path. However, there is no palindromic path between (1, 1) and (3, 1).
<image>
Determine all cells of the grid by asking not more than n^2 questions. It can be shown that the answer always exists.
Input
The first line contains odd integer (3 β€ n < 50) β the side of the grid.
Interaction
You begin the interaction by reading n.
To ask a question about cells (x_1, y_1), (x_2, y_2), in a separate line output "? x_1 y_1 x_2 y_2".
Numbers in the query have to satisfy 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. Don't forget to 'flush', to get the answer.
In response, you will receive 1, if there exists a path going from (x_1, y_1) to (x_2, y_2) only to the right or down, numbers in cells of which form a palindrome, and 0 otherwise.
In case your query is invalid or you asked more than n^2 queries, program will print -1 and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you determine numbers in all cells, output "!".
Then output n lines, the i-th of which is a string of length n, corresponding to numbers in the i-th row of the grid.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hack Format
To hack, use the following format.
The first line should contain a single odd integer n (side of your grid).
The i-th of n following lines should contain a string of length n corresponding to the i-th row of the grid. Top left element of the grid has to be equal to 1, bottom right has to be equal to 0.
Example
Input
3
0
1
0
1
1
1
1
Output
? 1 1 1 3
? 1 1 2 3
? 2 1 2 3
? 3 1 3 3
? 2 2 3 3
? 1 2 3 2
? 1 2 3 3
!
100
001
000 | instruction | 0 | 75,958 | 15 | 151,916 |
Tags: implementation, interactive
Correct Solution:
```
import math
import sys
# helpful:
# r,g,b=map(int,input().split())
#list1 = input().split()
#for i in range(len(list1)):
# list1[i] = int(list1[i])
# print(list1)
# arr = [[0 for x in range(columns)] for y in range(rows)]
n = int(input())
arr = [[-1 for x in range(n+1)] for y in range(n+1)]
arr[1][1] = 1
arr[n][n] = 0
def flipEven(arr):
for i in range(1, n+1):
for j in range(1, n+1):
if(abs(j-i) % 2 == 1):
arr[i][j] = 1-arr[i][j]
return arr
for i in range(1, n+1):
for j in range(1, n+1):
if((j-i) % 2 == 0 and (i != 1 or j !=1) and (i != n or j != n)):
if j == 1:
print('? ' + str(i-2) + ' ' + str(j) + ' ' + str(i) + ' ' + str(j))
sys.stdout.flush()
if(int(input()) == 1):
arr[i][j] = arr[i-2][j]
else:
arr[i][j] = 1 - arr[i-2][j]
elif i == 1:
print('? ' + str(i) + ' ' + str(j-2) + ' ' + str(i) + ' ' + str(j))
sys.stdout.flush()
if(int(input()) == 1):
arr[i][j] = arr[i][j-2]
else:
arr[i][j] = 1 - arr[i][j-2]
else:
print('? ' + str(i-1) + ' ' + str(j-1) + ' ' + str(i) + ' ' + str(j))
sys.stdout.flush()
if(int(input()) == 1):
arr[i][j] = arr[i-1][j-1]
else:
arr[i][j] = 1 - arr[i-1][j-1]
arr[1][2] = 1
print('? ' + str(1) + ' ' + str(2) + ' ' + str(2) + ' ' + str(3))
sys.stdout.flush()
if(int(input()) == 1):
arr[2][3] = 1
else:
arr[2][3] = 0
print('? ' + str(2) + ' ' + str(1) + ' ' + str(2) + ' ' + str(3))
sys.stdout.flush()
if(int(input()) == 1):
arr[2][1] = arr[2][3]
else:
arr[2][1] = 1-arr[2][3]
print('? ' + str(2) + ' ' + str(1) + ' ' + str(3) + ' ' + str(2))
sys.stdout.flush()
if(int(input()) == 1):
arr[3][2] = arr[2][1]
else:
arr[3][2] = 1-arr[2][1]
for i in range(1, n+1):
for j in range(1, n+1):
if((j-i) % 2 == 1 and (i != 1 or j !=2) and (i != 2 or j !=1) and (i != 2 or j !=3) and (i != 3 or j != 2)):
if (j == 1):
print('? ' + str(i-2) + ' ' + str(j) + ' ' + str(i) + ' ' + str(j))
sys.stdout.flush()
if(int(input()) == 1):
arr[i][j] = arr[i-2][j]
else:
arr[i][j] = 1 - arr[i-2][j]
elif i == 1:
print('? ' + str(i) + ' ' + str(j-2) + ' ' + str(i) + ' ' + str(j))
sys.stdout.flush()
if(int(input()) == 1):
arr[i][j] = arr[i][j-2]
else:
arr[i][j] = 1 - arr[i][j-2]
else:
print('? ' + str(i-1) + ' ' + str(j-1) + ' ' + str(i) + ' ' + str(j))
sys.stdout.flush()
if(int(input()) == 1):
arr[i][j] = arr[i-1][j-1]
else:
arr[i][j] = 1 - arr[i-1][j-1]
doFlip = [[1,0,1,0], [1,1,0,0], [0,0,1,1], [0,1,0,1]]
dontFlip = [[1,0,0,1], [1,1,1,1], [0,0,0,0], [0,1,1,0]]
for i in range(1, n+1):
for j in range(1, n+1):
if(i <= n-3): # 1
if([arr[i][j], arr[i+1][j], arr[i+2][j], arr[i+3][j]] in doFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+3) + ' ' + str(j))
sys.stdout.flush()
if(int(input()) == 1):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
elif([arr[i][j], arr[i+1][j], arr[i+2][j], arr[i+3][j]] in dontFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+3) + ' ' + str(j))
sys.stdout.flush()
if(int(input()) == 0):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
if(i <= n-2 and j <= n-1): # 2
if([arr[i][j], arr[i+1][j], arr[i+2][j], arr[i+2][j+1]] in doFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+2) + ' ' + str(j+1))
sys.stdout.flush()
if(int(input()) == 1):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()()
elif([arr[i][j], arr[i+1][j], arr[i+2][j], arr[i+2][j+1]] in dontFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+2) + ' ' + str(j+1))
sys.stdout.flush()
if(int(input()) == 0):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
if(i <= n-2 and j <= n-1): # 3
if([arr[i][j], arr[i+1][j], arr[i+1][j+1], arr[i+2][j+1]] in doFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+2) + ' ' + str(j+1))
sys.stdout.flush()
if(int(input()) == 1):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
elif([arr[i][j], arr[i+1][j], arr[i+1][j+1], arr[i+2][j+1]] in dontFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+2) + ' ' + str(j+1))
sys.stdout.flush()
if(int(input()) == 0):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
if(i <= n-2 and j <= n-1): # 4
if([arr[i][j], arr[i][j+1], arr[i+1][j+1], arr[i+2][j+1]] in doFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+2) + ' ' + str(j+1))
sys.stdout.flush()
if(int(input()) == 1):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
elif([arr[i][j], arr[i][j+1], arr[i+1][j+1], arr[i+2][j+1]] in dontFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+2) + ' ' + str(j+1))
sys.stdout.flush()
if(int(input()) == 0):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
if(i <= n-1 and j <= n-2): # 5
if([arr[i][j], arr[i][j+1], arr[i+1][j+1], arr[i+1][j+2]] in doFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+1) + ' ' + str(j+2))
sys.stdout.flush()
if(int(input()) == 1):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
elif([arr[i][j], arr[i][j+1], arr[i+1][j+1], arr[i+1][j+2]] in dontFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+1) + ' ' + str(j+2))
sys.stdout.flush()
if(int(input()) == 0):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
if(i <= n-1 and j <= n-2): # 6
if([arr[i][j], arr[i+1][j], arr[i+1][j+1], arr[i+1][j+2]] in doFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+1) + ' ' + str(j+2))
sys.stdout.flush()
if(int(input()) == 1):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
elif([arr[i][j], arr[i+1][j], arr[i+1][j+1], arr[i+1][j+2]] in dontFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+1) + ' ' + str(j+2))
sys.stdout.flush()
if(int(input()) == 0):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
if(i <= n-1 and j <= n-2): # 7
if([arr[i][j], arr[i][j+1], arr[i][j+2], arr[i+1][j+2]] in doFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+1) + ' ' + str(j+2))
sys.stdout.flush()
if(int(input()) == 1):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
elif([arr[i][j], arr[i][j+1], arr[i][j+2], arr[i+1][j+2]] in dontFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i+1) + ' ' + str(j+2))
sys.stdout.flush()
if(int(input()) == 0):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
if(j <= n-3): # 8
if([arr[i][j], arr[i][j+1], arr[i][j+2], arr[i][j+3]] in doFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i) + ' ' + str(j+3))
sys.stdout.flush()
if(int(input()) == 1):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
elif([arr[i][j], arr[i][j+1], arr[i][j+2], arr[i][j+3]] in dontFlip):
print('? ' + str(i) + ' ' + str(j) + ' ' + str(i) + ' ' + str(j+3))
sys.stdout.flush()
if(int(input()) == 0):
arr = flipEven(arr)
print('!')
for i in range(1, n+1):
print(''.join(map(str, arr[i][1:])))
exit()
``` | output | 1 | 75,958 | 15 | 151,917 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem
You are given a grid nΓ n, where n is odd. Rows are enumerated from 1 to n from up to down, columns are enumerated from 1 to n from left to right. Cell, standing on the intersection of row x and column y, is denoted by (x, y).
Every cell contains 0 or 1. It is known that the top-left cell contains 1, and the bottom-right cell contains 0.
We want to know numbers in all cells of the grid. To do so we can ask the following questions:
"? x_1 y_1 x_2 y_2", where 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. In other words, we output two different cells (x_1, y_1), (x_2, y_2) of the grid such that we can get from the first to the second by moving only to the right and down, and they aren't adjacent.
As a response to such question you will be told if there exists a path between (x_1, y_1) and (x_2, y_2), going only to the right or down, numbers in cells of which form a palindrome.
For example, paths, shown in green, are palindromic, so answer for "? 1 1 2 3" and "? 1 2 3 3" would be that there exists such path. However, there is no palindromic path between (1, 1) and (3, 1).
<image>
Determine all cells of the grid by asking not more than n^2 questions. It can be shown that the answer always exists.
Input
The first line contains odd integer (3 β€ n < 50) β the side of the grid.
Interaction
You begin the interaction by reading n.
To ask a question about cells (x_1, y_1), (x_2, y_2), in a separate line output "? x_1 y_1 x_2 y_2".
Numbers in the query have to satisfy 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. Don't forget to 'flush', to get the answer.
In response, you will receive 1, if there exists a path going from (x_1, y_1) to (x_2, y_2) only to the right or down, numbers in cells of which form a palindrome, and 0 otherwise.
In case your query is invalid or you asked more than n^2 queries, program will print -1 and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you determine numbers in all cells, output "!".
Then output n lines, the i-th of which is a string of length n, corresponding to numbers in the i-th row of the grid.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hack Format
To hack, use the following format.
The first line should contain a single odd integer n (side of your grid).
The i-th of n following lines should contain a string of length n corresponding to the i-th row of the grid. Top left element of the grid has to be equal to 1, bottom right has to be equal to 0.
Example
Input
3
0
1
0
1
1
1
1
Output
? 1 1 1 3
? 1 1 2 3
? 2 1 2 3
? 3 1 3 3
? 2 2 3 3
? 1 2 3 2
? 1 2 3 3
!
100
001
000 | instruction | 0 | 75,959 | 15 | 151,918 |
Tags: implementation, interactive
Correct Solution:
```
import sys
def ask(x1, y1, x2, y2):
print(f'? {x1} {y1} {x2} {y2}')
inp = int(input())
sys.stdout.flush()
return inp
def main():
# sys.stdin = open("in.txt", "r")
# sys.stdout = open("out.txt", "w")
# it = iter(map(int, sys.stdin.read().split()))
n = int(input())
grid = [[None]*(n+1) for _ in range(n+1)]
grid[1][1] = 1
grid[n][n] = 0
grid[1][3] = ask(1, 1, 1, 3) # 1 if ask(1, 1, 1, 3) == 1 else 0
grid[2][2] = ask(1, 1, 2, 2)
# Let assume grid[1][2] = 1
grid[1][2] = 1
grid[2][3] = ask(1, 2, 2, 3)
grid[2][1] = grid[2][3] if ask(2, 1, 2, 3) == 1 else (1 ^ grid[2][3]) # (1 ^ x) is to have the oppostive value of x
# 1st and 2nd row.
for i in [1, 2]:
for j in range(4, n+1):
grid[i][j] = grid[i][j-2] if ask(i, j-2, i, j) == 1 else (1 ^ grid[i][j-2])
# The remaining rows.
for i in range(3, n+1):
for j in range(1, n+1):
if i == n and j == n:
continue
grid[i][j] = grid[i-2][j] if ask(i-2, j, i, j) == 1 else (1 ^ grid[i-2][j])
# Justify assumption.
assumption_ok = True
for i in range(1, n): # from 1 to n-1
to_break = False
for j in range(1, n-1): # from 1 to n-2
if grid[i][j] != grid[i+1][j+2] and (grid[i][j+1] != grid[i][j+2] or grid[i][j+1] != grid[i+1][j+1] or grid[i+1][j] != grid[i+1][j+1]):
# No palindrom from grid[i][j] to grid[i+1][j+2]
if ask(i, j, i+1, j+2) == 1: # But that's a palindrom
assumption_ok = False
to_break = True
break
elif grid[i][j] == grid[i+1][j+2] and (grid[i][j+1] == grid[i][j+2] or grid[i][j+1] == grid[i+1][j+1] or grid[i+1][j] == grid[i+1][j+1]):
# Got palindrom from grid[i][j] to grid[i+1][j+2]
if ask(i, j, i+1, j+2) == 0: # But that's not a palindrom
assumption_ok = False # Assumption grid[1][2] = 1 is wrong.
to_break = True
break
if to_break:
break
if not assumption_ok:
for i in range(1, n+1):
for j in range(2 if (i & 1) == 1 else 1, n+1, 2):
grid[i][j] ^= 1 # Reverse grid[i][j]
# Print asnwer.
ansL = ['!\n']
for row in grid[1:]:
ansL += [str(row_elem) for row_elem in row[1:]]
ansL.append('\n')
sys.stdout.write(''.join(ansL))
if __name__ == '__main__':
main()
``` | output | 1 | 75,959 | 15 | 151,919 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem
You are given a grid nΓ n, where n is odd. Rows are enumerated from 1 to n from up to down, columns are enumerated from 1 to n from left to right. Cell, standing on the intersection of row x and column y, is denoted by (x, y).
Every cell contains 0 or 1. It is known that the top-left cell contains 1, and the bottom-right cell contains 0.
We want to know numbers in all cells of the grid. To do so we can ask the following questions:
"? x_1 y_1 x_2 y_2", where 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. In other words, we output two different cells (x_1, y_1), (x_2, y_2) of the grid such that we can get from the first to the second by moving only to the right and down, and they aren't adjacent.
As a response to such question you will be told if there exists a path between (x_1, y_1) and (x_2, y_2), going only to the right or down, numbers in cells of which form a palindrome.
For example, paths, shown in green, are palindromic, so answer for "? 1 1 2 3" and "? 1 2 3 3" would be that there exists such path. However, there is no palindromic path between (1, 1) and (3, 1).
<image>
Determine all cells of the grid by asking not more than n^2 questions. It can be shown that the answer always exists.
Input
The first line contains odd integer (3 β€ n < 50) β the side of the grid.
Interaction
You begin the interaction by reading n.
To ask a question about cells (x_1, y_1), (x_2, y_2), in a separate line output "? x_1 y_1 x_2 y_2".
Numbers in the query have to satisfy 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ n, and x_1 + y_1 + 2 β€ x_2 + y_2. Don't forget to 'flush', to get the answer.
In response, you will receive 1, if there exists a path going from (x_1, y_1) to (x_2, y_2) only to the right or down, numbers in cells of which form a palindrome, and 0 otherwise.
In case your query is invalid or you asked more than n^2 queries, program will print -1 and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you determine numbers in all cells, output "!".
Then output n lines, the i-th of which is a string of length n, corresponding to numbers in the i-th row of the grid.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hack Format
To hack, use the following format.
The first line should contain a single odd integer n (side of your grid).
The i-th of n following lines should contain a string of length n corresponding to the i-th row of the grid. Top left element of the grid has to be equal to 1, bottom right has to be equal to 0.
Example
Input
3
0
1
0
1
1
1
1
Output
? 1 1 1 3
? 1 1 2 3
? 2 1 2 3
? 3 1 3 3
? 2 2 3 3
? 1 2 3 2
? 1 2 3 3
!
100
001
000 | instruction | 0 | 75,960 | 15 | 151,920 |
Tags: implementation, interactive
Correct Solution:
```
def main():
import sys
input = sys.stdin.readline
def interact(x1, y1, x2, y2):
print('?',x1, y1, x2, y2, flush=True)
return int(input())
def do_odds():
for i in range(1, n - 1, 2):
for j in range(3, n + 1, 2):
tmp = interact(i, j - 2, i, j)
grid[i - 1][j - 1] = tmp ^ grid[i - 1][j - 3] ^ 1
for j in range(2, n, 2):
tmp = interact(i, j - 1, i + 1, j)
grid[i][j - 1] = tmp ^ grid[i - 1][j - 2] ^ 1
tmp = interact(i, 1, i + 2, 1)
grid[i + 1][0] = tmp ^ grid[i - 1][0] ^ 1
for i in range(3, n - 1, 2):
tmp = interact(n, i - 2, n, i)
grid[n - 1][i - 1] = tmp ^ grid[n - 1][i - 3] ^ 1
def do_evens(x):
for i in range(1, n - 1, 2):
for j in range(4, n, 2):
tmp = interact(i, j - 2, i, j)
grid[i - 1][j - 1] = tmp ^ grid[i - 1][j - 3] ^ 1
for j in range(3, n + 1, 2):
tmp = interact(i, j - 1, i + 1, j)
grid[i][j - 1] = tmp ^ grid[i - 1][j - 2] ^ 1
tmp = interact(i + 1, 1, i + 1, 3)
grid[i][0] = tmp ^ grid[i][2] ^ 1
tmp = interact(i, 2, i + 2, 2)
grid[i + 1][1] = tmp ^ grid[i - 1][1] ^ 1
for i in range(4, n, 2):
tmp = interact(n, i - 2, n, i)
grid[n - 1][i - 1] = tmp ^ grid[n - 1][i - 3] ^ 1
i = x
if grid[i][i + 1] == grid[i + 1][i + 2]:
if grid[i + 1][i + 1] == 1:
tmp = interact(i + 1, i + 1, i + 2, i + 3)
offset = tmp ^ 1 ^ grid[i][i] ^ grid[i + 1][i + 2]
else:
tmp = interact(i + 1, i + 2, i + 3, i + 3)
offset = tmp ^ 1 ^ grid[i + 2][i + 2] ^ grid[i][i + 1]
else:
if grid[i + 1][i + 1] == 1:
tmp = interact(i + 1, i + 2, i + 3, i + 3)
offset = tmp ^ 1 ^ grid[i + 2][i + 2] ^ grid[i][i + 1]
else:
tmp = interact(i + 1, i + 1, i + 2, i + 3)
offset = tmp ^ 1 ^ grid[i][i] ^ grid[i + 1][i + 2]
for i in range(n):
for j in range((i & 1) ^ 1, n, 2):
grid[i][j] ^= offset
n = int(input())
grid = [[1] * n for _ in range(n)]
grid[-1][-1] = 0
do_odds()
for i in range(2, n):
if grid[i][i] == 0 and grid[i - 2][i - 2] == 1:
do_evens(i - 2)
break
print("!", flush=True)
for line in grid:
print("".join([str(i) for i in line]), flush=True)
main()
``` | output | 1 | 75,960 | 15 | 151,921 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4 | instruction | 0 | 76,173 | 15 | 152,346 |
Tags: math
Correct Solution:
```
n,m,s=map(int,input().split())
n,m=min(n,m),max(n,m)
if(s>=max(n,m)):
print(n*m)
elif(s>=n and s<m):
x=1
count=1
a1=1
while(1):
if(x+s<=m):
x+=s
a1+=1
else:
break;
if(m%s==0):
print(n*m)
else:
print(a1*n*(m%s))
else:
a1=1
x=1
y=1
a2=1
while(1):
if(x+s<=m):
x+=s
a1+=1
else:
break;
while(1):
if(y+s<=n):
y+=s
a2+=1
else:
break;
if(m%s==0 and n%s==0):
print(n*m)
elif(m%s==0 and n%s!=0):
print(m*a2*(n%s))
elif(n%s==0 and m%s!=0):
print(n*a1*(m%s))
else:
print(a1*a2*(m%s)*(n%s))
``` | output | 1 | 76,173 | 15 | 152,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4 | instruction | 0 | 76,174 | 15 | 152,348 |
Tags: math
Correct Solution:
```
b, k, j = map(int, input().split())
print (((k-1)//j + 1) * ((k-1)%j + 1) * ((b-1)//j + 1) * ((b-1)%j + 1))
``` | output | 1 | 76,174 | 15 | 152,349 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4 | instruction | 0 | 76,175 | 15 | 152,350 |
Tags: math
Correct Solution:
```
n, m, s = map(int, input().split())
def ceilDiv(a, b):
return a // b + (0 if a % b == 0 else 1)
sgsx, sgsy = n % s, m % s
if sgsx == 0:
sgsx = s
if sgsy == 0:
sgsy = s
print(sgsx * sgsy * ceilDiv(n, s) * ceilDiv(m, s))
``` | output | 1 | 76,175 | 15 | 152,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4 | instruction | 0 | 76,176 | 15 | 152,352 |
Tags: math
Correct Solution:
```
from math import ceil
n, m, s = map(int, input().split())
o1 = n % s
o2 = m % s
if o1 == 0:
o1 = s
if o2 == 0:
o2 = s
print(o1 * o2 * ceil(n/s) * ceil(m / s))
``` | output | 1 | 76,176 | 15 | 152,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4 | instruction | 0 | 76,177 | 15 | 152,354 |
Tags: math
Correct Solution:
```
import math
x,y,j = map(int,input().split())
print(((math.ceil(x/j)) * (j if x%j == 0 else x%j)) * (math.ceil(y/j)) * (j if y%j == 0 else y%j))
``` | output | 1 | 76,177 | 15 | 152,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4 | instruction | 0 | 76,178 | 15 | 152,356 |
Tags: math
Correct Solution:
```
n,m,s=map(int,input().split())
print(((n-1)//s+1)*((n-1)%s+1)*((m-1)//s+1)*((m-1)%s+1))
``` | output | 1 | 76,178 | 15 | 152,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4 | instruction | 0 | 76,179 | 15 | 152,358 |
Tags: math
Correct Solution:
```
n,m,s=list(map(int,input().split()))
def f(x):
k=(x-1)//(s)
return (k+1)*(x-k*s)
print(f(n)*f(m))
``` | output | 1 | 76,179 | 15 | 152,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4 | instruction | 0 | 76,180 | 15 | 152,360 |
Tags: math
Correct Solution:
```
def flea(x, y):
return x // y + (0 if x % y == 0 else 1)
def div(r,p,pa):
jul, lol = r % pa, p % pa
if (lol == 0):
lol = pa
if (jul == 0):
jul = pa
print(jul * lol * flea(r, pa) * flea(p, pa))
r, p, pa = map(int, input().split())
div(r,p,pa)
``` | output | 1 | 76,180 | 15 | 152,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4
Submitted Solution:
```
#codeforces 32c flea, math
def readGen(transform):
while (True):
n=0
tmp=input().split()
m=len(tmp)
while (n<m):
yield(transform(tmp[n]))
n+=1
readint=readGen(int)
n,m,s=next(readint),next(readint),next(readint)
a=(m-1)//s+1
b=(n-1)//s+1
w=m-(a-1)*s-1
h=n-(b-1)*s-1
print(a*b*(w+1)*(h+1))
``` | instruction | 0 | 76,181 | 15 | 152,362 |
Yes | output | 1 | 76,181 | 15 | 152,363 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4
Submitted Solution:
```
n,m,s=map(int,input().split())
r = (-(-n//s)) * (s if n%s==0 else n%s)
c = (-(-m//s)) * (s if m%s==0 else m%s)
print(r*c)
``` | instruction | 0 | 76,182 | 15 | 152,364 |
Yes | output | 1 | 76,182 | 15 | 152,365 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4
Submitted Solution:
```
from math import *
n,m,s=map(int,input().split())
r = ceil(n/s) * (s if n%s==0 else n%s)
c = ceil(m/s) * (s if m%s==0 else m%s)
ans=r*c
print(ans)
``` | instruction | 0 | 76,183 | 15 | 152,366 |
Yes | output | 1 | 76,183 | 15 | 152,367 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4
Submitted Solution:
```
n,m,s=map(int,input().split())
n-=1
m-=1
print((n//s+1)*(n%s+1)*(m//s+1)*(m%s+1))#2020-07-25 11:13:31.354
``` | instruction | 0 | 76,184 | 15 | 152,368 |
Yes | output | 1 | 76,184 | 15 | 152,369 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4
Submitted Solution:
```
n,m,s = map(int, input().split())
c = 0
cc = 0
su = 0
if (s >= m or s >= n or s == 1 or (m%2==0 and n%2==0)):
print(m*n)
else:
l = m//s
ll = n//s
if (m%s > 1 or n%s > 1):
print(((l*2)+(ll*2))*2)
else:
print((l*2)+(ll*2))
``` | instruction | 0 | 76,185 | 15 | 152,370 |
No | output | 1 | 76,185 | 15 | 152,371 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4
Submitted Solution:
```
from math import ceil
data = input().split()
m, n, d = int(data[0]), int(data[1]), int(data[2])
if d == 1:
print(m*n)
else:
x = m if m <= d else ceil(m/d)
y = n if n <= d else ceil(n/d)
print(x*y)
``` | instruction | 0 | 76,186 | 15 | 152,372 |
No | output | 1 | 76,186 | 15 | 152,373 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4
Submitted Solution:
```
n,m,s=map(int,input().split())
print(min(n,s)*min(m,s))
``` | instruction | 0 | 76,187 | 15 | 152,374 |
No | output | 1 | 76,187 | 15 | 152,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
Input
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
Output
Output the only integer β the number of the required starting positions of the flea.
Examples
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4
Submitted Solution:
```
n,m,s=list(map(int,input().split()))
if n==m:
if (s+1)**2==n*m:
print(4)
else:
print(n*m)
else:
if n<m:
n,m=m,n
if (s+1)**2==m**2:
print(8*(n-m))
else:
print(n*m)
``` | instruction | 0 | 76,188 | 15 | 152,376 |
No | output | 1 | 76,188 | 15 | 152,377 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.
While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
Input
The first line of input contains the only positive integer n (1 β€ n β€ 2Β·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R β to the right, U β to the top and D β to the bottom. Have a look at the illustrations for better explanation.
Output
The only line of input should contain the minimum possible length of the sequence.
Examples
Input
4
RURD
Output
2
Input
6
RRULDD
Output
2
Input
26
RRRULURURUULULLLDLDDRDRDLD
Output
7
Input
3
RLL
Output
2
Input
4
LRLR
Output
4
Note
The illustrations to the first three tests are given below.
<image> <image> <image>
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence. | instruction | 0 | 77,205 | 15 | 154,410 |
Tags: constructive algorithms, math
Correct Solution:
```
a=int(input())
s=input()
t=1
ch=''
for x in range(a):
if (s[x]=='L' and 'R' in ch) or (s[x]=='R' and 'L' in ch) or (s[x]=='U' and 'D' in ch) or (s[x]=='D' and 'U' in ch):
t+=1
ch=s[x]
else:
ch+='' if s[x] in ch else s[x]
print(t)
``` | output | 1 | 77,205 | 15 | 154,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.
While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
Input
The first line of input contains the only positive integer n (1 β€ n β€ 2Β·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R β to the right, U β to the top and D β to the bottom. Have a look at the illustrations for better explanation.
Output
The only line of input should contain the minimum possible length of the sequence.
Examples
Input
4
RURD
Output
2
Input
6
RRULDD
Output
2
Input
26
RRRULURURUULULLLDLDDRDRDLD
Output
7
Input
3
RLL
Output
2
Input
4
LRLR
Output
4
Note
The illustrations to the first three tests are given below.
<image> <image> <image>
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence. | instruction | 0 | 77,206 | 15 | 154,412 |
Tags: constructive algorithms, math
Correct Solution:
```
n=int(input())
s=input()
s=list(s)
r,l,u,d=0,0,0,0
points=0
for i in s:
if(i=='R'):
r=1
if(l==1):
points+=1
l, u, d = 0, 0, 0
if (i == 'L'):
l = 1
if (r == 1):
points += 1
r, u, d = 0, 0, 0
if (i == 'U'):
u = 1
if (d == 1):
points += 1
l, r, d = 0, 0, 0
if (i == 'D'):
d = 1
if (u == 1):
points += 1
l, u, r = 0, 0, 0
print(points+1)
``` | output | 1 | 77,206 | 15 | 154,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.
While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
Input
The first line of input contains the only positive integer n (1 β€ n β€ 2Β·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R β to the right, U β to the top and D β to the bottom. Have a look at the illustrations for better explanation.
Output
The only line of input should contain the minimum possible length of the sequence.
Examples
Input
4
RURD
Output
2
Input
6
RRULDD
Output
2
Input
26
RRRULURURUULULLLDLDDRDRDLD
Output
7
Input
3
RLL
Output
2
Input
4
LRLR
Output
4
Note
The illustrations to the first three tests are given below.
<image> <image> <image>
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence. | instruction | 0 | 77,207 | 15 | 154,414 |
Tags: constructive algorithms, math
Correct Solution:
```
n = int(input())
s = input()
ans = 0
tmp = []
for i in range(len(s)):
if len(tmp) < 2 or s[i] in tmp:
if len(tmp) == 1 and (s[i] == 'U' and tmp[0] == 'D' or s[i] == 'D' and tmp[0] == 'U' or s[i] == 'L' and tmp[0] == 'R' or s[i] == 'R' and tmp[0] == 'L'):
ans += 1
tmp = [s[i]]
elif s[i] not in tmp:
tmp.append(s[i])
else:
ans += 1
tmp = [s[i]]
if tmp:
ans += 1
print(ans)
``` | output | 1 | 77,207 | 15 | 154,415 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.
While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
Input
The first line of input contains the only positive integer n (1 β€ n β€ 2Β·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R β to the right, U β to the top and D β to the bottom. Have a look at the illustrations for better explanation.
Output
The only line of input should contain the minimum possible length of the sequence.
Examples
Input
4
RURD
Output
2
Input
6
RRULDD
Output
2
Input
26
RRRULURURUULULLLDLDDRDRDLD
Output
7
Input
3
RLL
Output
2
Input
4
LRLR
Output
4
Note
The illustrations to the first three tests are given below.
<image> <image> <image>
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence. | instruction | 0 | 77,208 | 15 | 154,416 |
Tags: constructive algorithms, math
Correct Solution:
```
n = int(input())
s = list(input())
a = [[0,0]]
for i in s:
x=a[-1]
if i=='L':a.append([x[0]-1, x[1]])
elif i=='R':a.append([x[0]+1, x[1]])
elif i=='U':a.append([x[0], x[1]-1])
else:a.append([x[0], x[1]+1])
j=1
c=a[0]
d=0
ans=1
while j<len(a):
d+=1
do=abs(c[0]-a[j][0])+abs(c[1]-a[j][1])
if d>do:
c=a[j-1]
ans+=1
d=0
j+=1
print(ans)
``` | output | 1 | 77,208 | 15 | 154,417 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.
While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
Input
The first line of input contains the only positive integer n (1 β€ n β€ 2Β·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R β to the right, U β to the top and D β to the bottom. Have a look at the illustrations for better explanation.
Output
The only line of input should contain the minimum possible length of the sequence.
Examples
Input
4
RURD
Output
2
Input
6
RRULDD
Output
2
Input
26
RRRULURURUULULLLDLDDRDRDLD
Output
7
Input
3
RLL
Output
2
Input
4
LRLR
Output
4
Note
The illustrations to the first three tests are given below.
<image> <image> <image>
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence. | instruction | 0 | 77,209 | 15 | 154,418 |
Tags: constructive algorithms, math
Correct Solution:
```
n = int(input())
moves = input()
rev = { 'R':'L', 'U':'D', 'L':'R', 'D':'U' }
seen = set()
min_pts = 1
for move in moves:
if rev[move] in seen:
min_pts += 1
seen = set()
seen.add(move)
print (min_pts)
``` | output | 1 | 77,209 | 15 | 154,419 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.
While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
Input
The first line of input contains the only positive integer n (1 β€ n β€ 2Β·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R β to the right, U β to the top and D β to the bottom. Have a look at the illustrations for better explanation.
Output
The only line of input should contain the minimum possible length of the sequence.
Examples
Input
4
RURD
Output
2
Input
6
RRULDD
Output
2
Input
26
RRRULURURUULULLLDLDDRDRDLD
Output
7
Input
3
RLL
Output
2
Input
4
LRLR
Output
4
Note
The illustrations to the first three tests are given below.
<image> <image> <image>
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence. | instruction | 0 | 77,210 | 15 | 154,420 |
Tags: constructive algorithms, math
Correct Solution:
```
def m():
res = 0
input()
path = input().upper()
dirs = [None, None] # LR (horizontal); UD (vertical)
for p in path:
for ind, chk in enumerate(('LR', 'UD')):
if p in chk:
if dirs[ind] is None:
dirs[ind] = p
elif p != dirs[ind]:
res += 1
dirs[ind] = p
dirs[not ind] = None
print(res + 1)
if __name__ == "__main__":
m()
``` | output | 1 | 77,210 | 15 | 154,421 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.