Datasets:
context
stringlengths 88
7.54k
| groundtruth
stringlengths 9
28.8k
| groundtruth_language
stringclasses 3
values | type
stringclasses 2
values | code_test_cases
listlengths 1
565
β | dataset
stringclasses 6
values | code_language
stringclasses 1
value | difficulty
float64 0
1
β | mid
stringlengths 32
32
|
---|---|---|---|---|---|---|---|---|
Mr. Chanek has an integer represented by a string s. Zero or more digits have been erased and are denoted by the character _. There are also zero or more digits marked by the character X, meaning they're the same digit.
Mr. Chanek wants to count the number of possible integer s, where s is divisible by 25. Of course, s must not contain any leading zero. He can replace the character _ with any digit. He can also replace the character X with any digit, but it must be the same for every character X.
As a note, a leading zero is any 0 digit that comes before the first nonzero digit in a number string in positional notation. For example, 0025 has two leading zeroes. An exception is the integer zero, (0 has no leading zero, but 0000 has three leading zeroes).
Input
One line containing the string s (1 β€ |s| β€ 8). The string s consists of the characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, _, and X.
Output
Output an integer denoting the number of possible integer s.
Examples
Input
25
Output
1
Input
_00
Output
9
Input
_XX
Output
9
Input
0
Output
1
Input
0_25
Output
0
Note
In the first example, the only possible s is 25.
In the second and third example, s β \{100, 200,300,400,500,600,700,800,900\}.
In the fifth example, all possible s will have at least one leading zero.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | s = input()
ways = 0
if len(s) == 1:
if s in ['_', 'X', '0']:
ways = 1
elif len(s) == 2:
if s in ['25', '50', '75']:
ways = 1
elif s in ['__', '_X', 'X_']:
ways = 3
elif s == '_0':
ways = 1
elif s in ['_5', 'X5']:
ways = 2
elif s in ['2_', '5_', '7_', '2X', '5X', '7X', 'X0']:
ways = 1
else:
d = s[-2:]
s = s[:-2]
if d in ['00', '25', '50', '75', '__', '_X', 'X_', 'XX', '_0', '_5', '0_', '2_', '5_', '7_', 'X0', 'X5', '0X', '2X', '5X', '7X']:
ways = 1
xfound = False
if d in ['__', '_X', 'X_']:
ways *= 4
elif d in ['_0', '_5', 'X0', 'X5']:
ways *= 2
if 'X' in d:
xfound = True
if s[0] == '_' or (s[0] == 'X' and not xfound):
ways *= 9
if s[0] == 'X':
xfound = True
if s[0] == 'X' and d in ['_X', 'X_', 'XX', '0X', 'X0', '5X']:
if d == 'X0':
ways //= 2
elif d in ['_X', 'X_']:
ways //= 4
if d == 'X_':
ways *= 3
else:
ways *= 2
else:
ways = 0
if s[0] == '0':
ways = 0
for c in s[1:]:
if c == '_':
ways *= 10
elif c == 'X' and not xfound:
ways *= 10
xfound = True
print(ways) | python | code_algorithm | [
{
"input": "0\n",
"output": "1\n"
},
{
"input": "_XX\n",
"output": "9\n"
},
{
"input": "_00\n",
"output": "9\n"
},
{
"input": "0_25\n",
"output": "0\n"
},
{
"input": "25\n",
"output": "1\n"
},
{
"input": "X\n",
"output": "1\n"
},
{
"input": "6X\n",
"output": "0\n"
},
{
"input": "585X27X5\n",
"output": "2\n"
},
{
"input": "XX_X_3_X\n",
"output": "200\n"
},
{
"input": "_5\n",
"output": "2\n"
},
{
"input": "050\n",
"output": "0\n"
},
{
"input": "0X5\n",
"output": "0\n"
},
{
"input": "XX5_\n",
"output": "9\n"
},
{
"input": "X1_9_0X5\n",
"output": "200\n"
},
{
"input": "___X_X25\n",
"output": "90000\n"
},
{
"input": "0_5\n",
"output": "0\n"
},
{
"input": "5537___5\n",
"output": "200\n"
},
{
"input": "85X1X525\n",
"output": "10\n"
},
{
"input": "__X___X_\n",
"output": "360000\n"
},
{
"input": "_3472XXX\n",
"output": "9\n"
},
{
"input": "_X_X_3_X\n",
"output": "3600\n"
},
{
"input": "79XX_925\n",
"output": "100\n"
},
{
"input": "_4_7XXXX\n",
"output": "90\n"
},
{
"input": "X_1X56_5\n",
"output": "180\n"
},
{
"input": "_X0\n",
"output": "18\n"
},
{
"input": "X2XXX9X_\n",
"output": "3\n"
},
{
"input": "8_XX2_00\n",
"output": "1000\n"
},
{
"input": "_\n",
"output": "1\n"
},
{
"input": "5\n",
"output": "0\n"
},
{
"input": "_X_X_X__\n",
"output": "36000\n"
},
{
"input": "_87_8XXX\n",
"output": "90\n"
},
{
"input": "0025\n",
"output": "0\n"
},
{
"input": "0_____\n",
"output": "0\n"
},
{
"input": "__\n",
"output": "3\n"
},
{
"input": "0___\n",
"output": "0\n"
},
{
"input": "0____\n",
"output": "0\n"
},
{
"input": "X2\n",
"output": "0\n"
},
{
"input": "58994_00\n",
"output": "10\n"
},
{
"input": "________\n",
"output": "3600000\n"
},
{
"input": "0__\n",
"output": "0\n"
},
{
"input": "XX\n",
"output": "0\n"
},
{
"input": "_____X__\n",
"output": "3600000\n"
},
{
"input": "_11X__XX\n",
"output": "900\n"
},
{
"input": "X5\n",
"output": "2\n"
},
{
"input": "X6\n",
"output": "0\n"
},
{
"input": "6_76_000\n",
"output": "100\n"
},
{
"input": "_206_2_X\n",
"output": "360\n"
},
{
"input": "_X8__725\n",
"output": "9000\n"
},
{
"input": "X_\n",
"output": "3\n"
},
{
"input": "53X_94_X\n",
"output": "40\n"
},
{
"input": "X14___X5\n",
"output": "2000\n"
},
{
"input": "__X__X__\n",
"output": "360000\n"
},
{
"input": "1XXX9___\n",
"output": "400\n"
},
{
"input": "362__6__\n",
"output": "400\n"
},
{
"input": "_7XX16X_\n",
"output": "36\n"
},
{
"input": "0075\n",
"output": "0\n"
},
{
"input": "00\n",
"output": "0\n"
},
{
"input": "XX_2\n",
"output": "0\n"
},
{
"input": "X_X2___5\n",
"output": "18000\n"
},
{
"input": "2X3X7___\n",
"output": "400\n"
},
{
"input": "X01_X___\n",
"output": "3600\n"
},
{
"input": "4_3X__00\n",
"output": "10000\n"
},
{
"input": "XXXX3XXX\n",
"output": "0\n"
},
{
"input": "_XXXXXX\n",
"output": "9\n"
},
{
"input": "_X\n",
"output": "3\n"
},
{
"input": "X_______\n",
"output": "3600000\n"
},
{
"input": "92_6X8X5\n",
"output": "20\n"
},
{
"input": "XX9_X_X_\n",
"output": "300\n"
},
{
"input": "1_07XXX_\n",
"output": "40\n"
},
{
"input": "_______X\n",
"output": "3600000\n"
},
{
"input": "XX_289_X\n",
"output": "20\n"
},
{
"input": "2___2__X\n",
"output": "40000\n"
},
{
"input": "_X_X_X_X\n",
"output": "3600\n"
},
{
"input": "X5__X1_5\n",
"output": "1800\n"
}
] | code_contests | python | 0 | ed6f1257ca12e3c82eb8f21afb8ef487 |
Mr. Chanek wants to knit a batik, a traditional cloth from Indonesia. The cloth forms a grid a with size n Γ m. There are k colors, and each cell in the grid can be one of the k colors.
Define a sub-rectangle as an ordered pair of two cells ((x_1, y_1), (x_2, y_2)), denoting the top-left cell and bottom-right cell (inclusively) of a sub-rectangle in a. Two sub-rectangles ((x_1, y_1), (x_2, y_2)) and ((x_3, y_3), (x_4, y_4)) have the same pattern if and only if the following holds:
* they have the same width (x_2 - x_1 = x_4 - x_3);
* they have the same height (y_2 - y_1 = y_4 - y_3);
* for every pair (i, j) where 0 β€ i β€ x_2 - x_1 and 0 β€ j β€ y_2 - y_1, the color of cells (x_1 + i, y_1 + j) and (x_3 + i, y_3 + j) are equal.
Count the number of possible batik color combinations, such that the subrectangles ((a_x, a_y),(a_x + r - 1, a_y + c - 1)) and ((b_x, b_y),(b_x + r - 1, b_y + c - 1)) have the same pattern.
Output the answer modulo 10^9 + 7.
Input
The first line contains five integers n, m, k, r, and c (1 β€ n, m β€ 10^9, 1 β€ k β€ 10^9, 1 β€ r β€ min(10^6, n), 1 β€ c β€ min(10^6, m)) β the size of the batik, the number of colors, and size of the sub-rectangle.
The second line contains four integers a_x, a_y, b_x, and b_y (1 β€ a_x, b_x β€ n, 1 β€ a_y, b_y β€ m) β the top-left corners of the first and second sub-rectangle. Both of the sub-rectangles given are inside the grid (1 β€ a_x + r - 1, b_x + r - 1 β€ n, 1 β€ a_y + c - 1, b_y + c - 1 β€ m).
Output
Output an integer denoting the number of possible batik color combinations modulo 10^9 + 7.
Examples
Input
3 3 2 2 2
1 1 2 2
Output
32
Input
4 5 170845 2 2
1 4 3 1
Output
756680455
Note
The following are all 32 possible color combinations in the first example.
<image>
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n, m, k, r, c = map(int, input().split())
ax, ay, bx, by = map(int, input().split())
p = n * m
if ax != bx or ay != by:
p -= r * c
print(pow(k, p, 1000000007)) | python | code_algorithm | [
{
"input": "4 5 170845 2 2\n1 4 3 1\n",
"output": "756680455\n"
},
{
"input": "3 3 2 2 2\n1 1 2 2\n",
"output": "32\n"
},
{
"input": "997824195 298198038 671030405 831526 973640\n694897941 219757278 695597597 220039071\n",
"output": "885735196\n"
},
{
"input": "78 15 967084213 68 15\n6 1 9 1\n",
"output": "735990901\n"
},
{
"input": "1000000000 1000000000 1000000000 999987 999995\n555555555 555555555 555555553 555555554\n",
"output": "915290242\n"
},
{
"input": "284251850 196700229 713578389 995979 967011\n237351355 75633985 237356031 75775611\n",
"output": "889823267\n"
},
{
"input": "1 1 1 1 1\n1 1 1 1\n",
"output": "1\n"
},
{
"input": "509702566 262449977 834744439 159006 338084\n59257202 181127448 352364267 85148196\n",
"output": "26544849\n"
},
{
"input": "63 65 706363358 28 20\n33 3 35 13\n",
"output": "75550567\n"
},
{
"input": "1000000000 1000000000 1000000000 1000000 1000000\n69420 69420 69423 69423\n",
"output": "176638891\n"
},
{
"input": "39 48 925054140 32 26\n4 14 4 22\n",
"output": "493165595\n"
},
{
"input": "612244127 1 520629493 438701 1\n606433327 1 62611302 1\n",
"output": "352024843\n"
},
{
"input": "999876877 999999898 1000000000 999990 999978\n24082002 170845 24082000 170842\n",
"output": "113661238\n"
},
{
"input": "53 79 864470674 29 68\n16 7 17 10\n",
"output": "915799654\n"
},
{
"input": "224279712 681889278 586602384 995524 653256\n131527798 181313191 93668397 333798254\n",
"output": "796813183\n"
},
{
"input": "84 8 681138185 8 6\n2 2 65 3\n",
"output": "284376979\n"
},
{
"input": "317978118 516355503 563741456 823898 917145\n303368564 34438928 303547237 35074157\n",
"output": "832720962\n"
},
{
"input": "668775901 899736493 861668260 96121 972164\n628635372 503210393 364803336 59823197\n",
"output": "495638566\n"
},
{
"input": "500000003 865102056 740048609 449299 846600\n375530078 124770851 375530078 124770851\n",
"output": "1\n"
},
{
"input": "62 23 831213918 43 23\n19 1 20 1\n",
"output": "78933780\n"
},
{
"input": "606866924 608383673 964448402 895347 816649\n57638772 565191689 58154550 565259566\n",
"output": "644467392\n"
},
{
"input": "95 76 780165187 52 54\n21 7 25 20\n",
"output": "107285575\n"
},
{
"input": "55 60 883964366 47 37\n8 14 8 11\n",
"output": "575284735\n"
},
{
"input": "1 225896556 692281482 1 750856\n1 137400676 1 73477222\n",
"output": "28745794\n"
},
{
"input": "727799968 611623105 644959424 942303 807555\n372191613 152626658 372904242 153091978\n",
"output": "433633\n"
},
{
"input": "91 70 553300732 47 69\n31 2 41 2\n",
"output": "990706354\n"
},
{
"input": "649447930 484428518 896889113 19993 39197\n316188994 151830831 86040053 192975280\n",
"output": "482572283\n"
},
{
"input": "148372813 835742098 605539349 949785 368809\n98121767 705227816 2009992 83409357\n",
"output": "62183519\n"
},
{
"input": "2 500000003 712653643 2 896241\n1 343018144 1 343018144\n",
"output": "1\n"
},
{
"input": "74 46 616259587 58 26\n1 7 11 9\n",
"output": "894317354\n"
}
] | code_contests | python | 0 | d33e36bf94b08b4d06c6cb4c1381cc4f |
Casimir has a rectangular piece of paper with a checkered field of size n Γ m. Initially, all cells of the field are white.
Let us denote the cell with coordinates i vertically and j horizontally by (i, j). The upper left cell will be referred to as (1, 1) and the lower right cell as (n, m).
Casimir draws ticks of different sizes on the field. A tick of size d (d > 0) with its center in cell (i, j) is drawn as follows:
1. First, the center cell (i, j) is painted black.
2. Then exactly d cells on the top-left diagonally to the center and exactly d cells on the top-right diagonally to the center are also painted black.
3. That is all the cells with coordinates (i - h, j Β± h) for all h between 0 and d are painted. In particular, a tick consists of 2d + 1 black cells.
An already painted cell will remain black if painted again. Below you can find an example of the 4 Γ 9 box, with two ticks of sizes 2 and 3.
<image>
You are given a description of a checkered field of size n Γ m. Casimir claims that this field came about after he drew some (possibly 0) ticks on it. The ticks could be of different sizes, but the size of each tick is at least k (that is, d β₯ k for all the ticks).
Determine whether this field can indeed be obtained by drawing some (possibly none) ticks of sizes d β₯ k or not.
Input
The first line contains an integer t (1 β€ t β€ 100) β the number test cases.
The following lines contain the descriptions of the test cases.
The first line of the test case description contains the integers n, m, and k (1 β€ k β€ n β€ 10; 1 β€ m β€ 19) β the field size and the minimum size of the ticks that Casimir drew. The following n lines describe the field: each line consists of m characters either being '.' if the corresponding cell is not yet painted or '*' otherwise.
Output
Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be YES if the given field can be obtained by drawing ticks of at least the given size and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answers).
Example
Input
8
2 3 1
*.*
...
4 9 2
*.*.*...*
.*.*...*.
..*.*.*..
.....*...
4 4 1
*.*.
****
.**.
....
5 5 1
.....
*...*
.*.*.
..*.*
...*.
5 5 2
.....
*...*
.*.*.
..*.*
...*.
4 7 1
*.....*
.....*.
..*.*..
...*...
3 3 1
***
***
***
3 5 1
*...*
.***.
.**..
Output
NO
YES
YES
YES
NO
NO
NO
NO
Note
The first sample test case consists of two asterisks neither of which can be independent ticks since ticks of size 0 don't exist.
The second sample test case is already described in the statement (check the picture in the statement). This field can be obtained by drawing ticks of sizes 2 and 3, as shown in the figure.
The field in the third sample test case corresponds to three ticks of size 1. Their center cells are marked with \color{blue}{blue}, \color{red}{red} and \color{green}{green} colors: *.*.
---
*\color{blue}{*}**
.\color{green}{*}\color{red}{*}.
....
The field in the fourth sample test case could have been obtained by drawing two ticks of sizes 1 and 2. Their vertices are marked below with \color{blue}{blue} and \color{red}{red} colors respectively: .....
---
*...*
.*.*.
..\color{red}{*}.*
...\color{blue}{*}.
The field in the fifth sample test case can not be obtained because k = 2, and the last asterisk in the fourth row from the top with coordinates (4, 5) can only be a part of a tick of size 1.
The field in the sixth sample test case can not be obtained because the top left asterisk (1, 1) can't be an independent tick, since the sizes of the ticks must be positive, and cannot be part of a tick with the center cell in the last row, since it is separated from it by a gap (a point, '.') in (2, 2).
In the seventh sample test case, similarly, the field can not be obtained by the described process because the asterisks with coordinates (1, 2) (second cell in the first row), (3, 1) and (3, 3) (leftmost and rightmost cells in the bottom) can not be parts of any ticks.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
import math
import heapq
import bisect
from collections import Counter
from collections import defaultdict, deque
from io import BytesIO, IOBase
from itertools import permutations
import string
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
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
self.BUFSIZE = 8192
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.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 = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.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:
self.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")
def get_int():
return int(input())
def get_ints():
return list(map(int, input().split(' ')))
def get_int_grid(n):
return [get_ints() for _ in range(n)]
def get_str():
return input().strip()
def get_strs():
return get_str().split(' ')
def flat_list(arr):
return [item for subarr in arr for item in subarr]
def yes_no(b):
if b:
return "YES"
else:
return "NO"
def binary_search(good, left, right, delta=1, right_true=False):
"""
Performs binary search
----------
Parameters
----------
:param good: Function used to perform the binary search
:param left: Starting value of left limit
:param right: Starting value of the right limit
:param delta: Margin of error, defaults value of 1 for integer binary search
:param right_true: Boolean, for whether the right limit is the true invariant
:return: Returns the most extremal value interval [left, right] which is good function evaluates to True,
alternatively returns False if no such value found
"""
limits = [left, right]
while limits[1] - limits[0] > delta:
if delta == 1:
mid = sum(limits) // 2
else:
mid = sum(limits) / 2
if good(mid):
limits[int(right_true)] = mid
else:
limits[int(~right_true)] = mid
if good(limits[int(right_true)]):
return limits[int(right_true)]
else:
return False
def prefix_sums(a):
p = [0]
for x in a:
p.append(p[-1] + x)
return p
class SortedList:
def __init__(self, iterable=[], _load=200):
"""Initialize sorted list instance."""
values = sorted(iterable)
self._len = _len = len(values)
self._load = _load
self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
self._list_lens = [len(_list) for _list in _lists]
self._mins = [_list[0] for _list in _lists]
self._fen_tree = []
self._rebuild = True
def _fen_build(self):
"""Build a fenwick tree instance."""
self._fen_tree[:] = self._list_lens
_fen_tree = self._fen_tree
for i in range(len(_fen_tree)):
if i | i + 1 < len(_fen_tree):
_fen_tree[i | i + 1] += _fen_tree[i]
self._rebuild = False
def _fen_update(self, index, value):
"""Update `fen_tree[index] += value`."""
if not self._rebuild:
_fen_tree = self._fen_tree
while index < len(_fen_tree):
_fen_tree[index] += value
index |= index + 1
def _fen_query(self, end):
"""Return `sum(_fen_tree[:end])`."""
if self._rebuild:
self._fen_build()
_fen_tree = self._fen_tree
x = 0
while end:
x += _fen_tree[end - 1]
end &= end - 1
return x
def _fen_findkth(self, k):
"""Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`)."""
_list_lens = self._list_lens
if k < _list_lens[0]:
return 0, k
if k >= self._len - _list_lens[-1]:
return len(_list_lens) - 1, k + _list_lens[-1] - self._len
if self._rebuild:
self._fen_build()
_fen_tree = self._fen_tree
idx = -1
for d in reversed(range(len(_fen_tree).bit_length())):
right_idx = idx + (1 << d)
if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
idx = right_idx
k -= _fen_tree[idx]
return idx + 1, k
def _delete(self, pos, idx):
"""Delete value at the given `(pos, idx)`."""
_lists = self._lists
_mins = self._mins
_list_lens = self._list_lens
self._len -= 1
self._fen_update(pos, -1)
del _lists[pos][idx]
_list_lens[pos] -= 1
if _list_lens[pos]:
_mins[pos] = _lists[pos][0]
else:
del _lists[pos]
del _list_lens[pos]
del _mins[pos]
self._rebuild = True
def _loc_left(self, value):
"""Return an index pair that corresponds to the first position of `value` in the sorted list."""
if not self._len:
return 0, 0
_lists = self._lists
_mins = self._mins
lo, pos = -1, len(_lists) - 1
while lo + 1 < pos:
mi = (lo + pos) >> 1
if value <= _mins[mi]:
pos = mi
else:
lo = mi
if pos and value <= _lists[pos - 1][-1]:
pos -= 1
_list = _lists[pos]
lo, idx = -1, len(_list)
while lo + 1 < idx:
mi = (lo + idx) >> 1
if value <= _list[mi]:
idx = mi
else:
lo = mi
return pos, idx
def _loc_right(self, value):
"""Return an index pair that corresponds to the last position of `value` in the sorted list."""
if not self._len:
return 0, 0
_lists = self._lists
_mins = self._mins
pos, hi = 0, len(_lists)
while pos + 1 < hi:
mi = (pos + hi) >> 1
if value < _mins[mi]:
hi = mi
else:
pos = mi
_list = _lists[pos]
lo, idx = -1, len(_list)
while lo + 1 < idx:
mi = (lo + idx) >> 1
if value < _list[mi]:
idx = mi
else:
lo = mi
return pos, idx
def add(self, value):
"""Add `value` to sorted list."""
_load = self._load
_lists = self._lists
_mins = self._mins
_list_lens = self._list_lens
self._len += 1
if _lists:
pos, idx = self._loc_right(value)
self._fen_update(pos, 1)
_list = _lists[pos]
_list.insert(idx, value)
_list_lens[pos] += 1
_mins[pos] = _list[0]
if _load + _load < len(_list):
_lists.insert(pos + 1, _list[_load:])
_list_lens.insert(pos + 1, len(_list) - _load)
_mins.insert(pos + 1, _list[_load])
_list_lens[pos] = _load
del _list[_load:]
self._rebuild = True
else:
_lists.append([value])
_mins.append(value)
_list_lens.append(1)
self._rebuild = True
def discard(self, value):
"""Remove `value` from sorted list if it is a member."""
_lists = self._lists
if _lists:
pos, idx = self._loc_right(value)
if idx and _lists[pos][idx - 1] == value:
self._delete(pos, idx - 1)
def remove(self, value):
"""Remove `value` from sorted list; `value` must be a member."""
_len = self._len
self.discard(value)
if _len == self._len:
raise ValueError('{0!r} not in list'.format(value))
def pop(self, index=-1):
"""Remove and return value at `index` in sorted list."""
pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
value = self._lists[pos][idx]
self._delete(pos, idx)
return value
def bisect_left(self, value):
"""Return the first index to insert `value` in the sorted list."""
pos, idx = self._loc_left(value)
return self._fen_query(pos) + idx
def bisect_right(self, value):
"""Return the last index to insert `value` in the sorted list."""
pos, idx = self._loc_right(value)
return self._fen_query(pos) + idx
def count(self, value):
"""Return number of occurrences of `value` in the sorted list."""
return self.bisect_right(value) - self.bisect_left(value)
def __len__(self):
"""Return the size of the sorted list."""
return self._len
def __getitem__(self, index):
"""Lookup value at `index` in sorted list."""
pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
return self._lists[pos][idx]
def __delitem__(self, index):
"""Remove value at `index` from sorted list."""
pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
self._delete(pos, idx)
def __contains__(self, value):
"""Return true if `value` is an element of the sorted list."""
_lists = self._lists
if _lists:
pos, idx = self._loc_left(value)
return idx < len(_lists[pos]) and _lists[pos][idx] == value
return False
def __iter__(self):
"""Return an iterator over the sorted list."""
return (value for _list in self._lists for value in _list)
def __reversed__(self):
"""Return a reverse iterator over the sorted list."""
return (value for _list in reversed(self._lists) for value in reversed(_list))
def __repr__(self):
"""Return string representation of sorted list."""
return 'SortedList({0})'.format(list(self))
def solve_c():
n, m, k = get_ints()
h = []
s = set()
SL = SortedList([])
chk = set()
rem = set()
for i in range(n):
s = get_str()
for j in range(m):
if s[j] == '*':
chk.add((i, j))
rem.add((i, j))
for i, j in rem:
s = 0
while (i - s, j + s) in rem and (i - s, j - s) in rem:
s += 1
s -= 1
if s >= k:
for d in range(s + 1):
chk.discard((i - d, j + d))
chk.discard((i - d, j - d))
if len(chk) == 0:
return "YES"
else:
return "NO"
t = get_int()
for _ in range(t):
print(solve_c())
| python | code_algorithm | [
{
"input": "8\n2 3 1\n*.*\n...\n4 9 2\n*.*.*...*\n.*.*...*.\n..*.*.*..\n.....*...\n4 4 1\n*.*.\n****\n.**.\n....\n5 5 1\n.....\n*...*\n.*.*.\n..*.*\n...*.\n5 5 2\n.....\n*...*\n.*.*.\n..*.*\n...*.\n4 7 1\n*.....*\n.....*.\n..*.*..\n...*...\n3 3 1\n***\n***\n***\n3 5 1\n*...*\n.***.\n.**..\n",
"output": "NO\nYES\nYES\nYES\nNO\nNO\nNO\nNO\n"
},
{
"input": "1\n1 1 1\n*\n",
"output": "NO\n"
},
{
"input": "1\n5 5 2\n.....\n.....\n*.*.*\n.*.*.\n..*..\n",
"output": "NO\n"
},
{
"input": "1\n1 1 1\n.\n",
"output": "YES\n"
},
{
"input": "1\n3 9 1\n*.*...*.*\n.*.....*.\n....*....\n",
"output": "NO\n"
},
{
"input": "1\n4 5 1\n....*\n.*.*.\n..*..\n.....\n",
"output": "NO\n"
},
{
"input": "1\n1 5 1\n.....\n",
"output": "YES\n"
},
{
"input": "1\n5 6 2\n......\n.....*\n*...*.\n.*.*..\n..*...\n",
"output": "NO\n"
},
{
"input": "1\n3 6 1\n.*....\n..*.*.\n...*..\n",
"output": "NO\n"
},
{
"input": "1\n4 4 1\n...*\n*.*.\n.*..\n....\n",
"output": "NO\n"
}
] | code_contests | python | 0 | ca88f3cd16ba3a4859a25088141e9b4f |
CQXYM is counting permutations length of 2n.
A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A permutation p(length of 2n) will be counted only if the number of i satisfying p_i<p_{i+1} is no less than n. For example:
* Permutation [1, 2, 3, 4] will count, because the number of such i that p_i<p_{i+1} equals 3 (i = 1, i = 2, i = 3).
* Permutation [3, 2, 1, 4] won't count, because the number of such i that p_i<p_{i+1} equals 1 (i = 3).
CQXYM wants you to help him to count the number of such permutations modulo 1000000007 (10^9+7).
In addition, [modulo operation](https://en.wikipedia.org/wiki/Modulo_operation) is to get the remainder. For example:
* 7 mod 3=1, because 7 = 3 β
2 + 1,
* 15 mod 4=3, because 15 = 4 β
3 + 3.
Input
The input consists of multiple test cases.
The first line contains an integer t (t β₯ 1) β the number of test cases. The description of the test cases follows.
Only one line of each test case contains an integer n(1 β€ n β€ 10^5).
It is guaranteed that the sum of n over all test cases does not exceed 10^5
Output
For each test case, print the answer in a single line.
Example
Input
4
1
2
9
91234
Output
1
12
830455698
890287984
Note
n=1, there is only one permutation that satisfies the condition: [1,2].
In permutation [1,2], p_1<p_2, and there is one i=1 satisfy the condition. Since 1 β₯ n, this permutation should be counted. In permutation [2,1], p_1>p_2. Because 0<n, this permutation should not be counted.
n=2, there are 12 permutations: [1,2,3,4],[1,2,4,3],[1,3,2,4],[1,3,4,2],[1,4,2,3],[2,1,3,4],[2,3,1,4],[2,3,4,1],[2,4,1,3],[3,1,2,4],[3,4,1,2],[4,1,2,3].
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from sys import stdin, gettrace
if gettrace():
def inputi():
return input()
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
LIMIT = 200001
MOD = 1000000007
def solve(factorial):
n = int(input())
print( (factorial[2*n]*pow(2, MOD-2, MOD))%MOD)
def main():
factorial = [1]
for i in range(1, LIMIT):
factorial.append((factorial[-1]*i)%MOD)
t = int(input())
for _ in range(t):
solve(factorial)
if __name__ == "__main__":
main()
| python | code_algorithm | [
{
"input": "4\n1\n2\n9\n91234\n",
"output": "1\n12\n830455698\n890287984\n"
},
{
"input": "2\n99997\n3\n",
"output": "979296788\n360\n"
},
{
"input": "14\n13633\n739\n4\n1\n23481\n279\n5113\n2013\n48785\n12\n44\n5766\n3\n127\n",
"output": "265272552\n34141923\n20160\n1\n405591801\n643806863\n236814910\n315525538\n970428805\n328814650\n593679722\n792599852\n360\n386977425\n"
},
{
"input": "1\n100000\n",
"output": "553573229\n"
},
{
"input": "15\n17\n241\n6346\n1566\n260\n7162\n21482\n9\n5\n10746\n2052\n4\n49336\n768\n6\n",
"output": "971636156\n429596486\n536921451\n703613161\n682559158\n701278536\n911371831\n830455698\n1814400\n715095915\n638757676\n20160\n474160434\n379457362\n239500800\n"
},
{
"input": "20\n597\n2791\n71\n1\n6\n14\n196\n1\n4335\n14\n5133\n6\n17\n3620\n5111\n1\n119\n992\n76848\n127\n",
"output": "924058387\n309126958\n25034588\n1\n239500800\n517878947\n323758520\n1\n41675960\n517878947\n479930293\n239500800\n971636156\n715097853\n974503605\n1\n796779807\n613317322\n14480144\n386977425\n"
},
{
"input": "8\n48\n2\n44\n212\n2\n98869\n822\n1\n",
"output": "882862985\n12\n593679722\n904840727\n12\n299753863\n710075077\n1\n"
},
{
"input": "14\n3\n135\n20\n360\n23355\n4\n5184\n1\n64367\n408\n220\n23\n4548\n1372\n",
"output": "360\n876516086\n899717444\n228927089\n695947115\n20160\n655921475\n1\n171195395\n414386056\n781623358\n870706360\n133194630\n920390755\n"
},
{
"input": "19\n1\n1019\n1\n11812\n46726\n27618\n43\n3\n369\n1694\n85\n138\n8\n47\n7\n5748\n4405\n141\n135\n",
"output": "1\n242249437\n1\n255618610\n152921347\n449038492\n417266680\n360\n208304992\n183336760\n809670618\n93533682\n394870773\n559855581\n589145299\n14790749\n130154591\n924228990\n876516086\n"
}
] | code_contests | python | 0 | dded87415d219981884917305a6a7adc |
CQXYM wants to create a connected undirected graph with n nodes and m edges, and the diameter of the graph must be strictly less than k-1. Also, CQXYM doesn't want a graph that contains self-loops or multiple edges (i.e. each edge connects two different vertices and between each pair of vertices there is at most one edge).
The diameter of a graph is the maximum distance between any two nodes.
The distance between two nodes is the minimum number of the edges on the path which endpoints are the two nodes.
CQXYM wonders whether it is possible to create such a graph.
Input
The input consists of multiple test cases.
The first line contains an integer t (1 β€ t β€ 10^5) β the number of test cases. The description of the test cases follows.
Only one line of each test case contains three integers n(1 β€ n β€ 10^9), m, k (0 β€ m,k β€ 10^9).
Output
For each test case, print YES if it is possible to create the graph, or print NO if it is impossible. You can print each letter in any case (upper or lower).
Example
Input
5
1 0 3
4 5 3
4 6 3
5 4 1
2 1 1
Output
YES
NO
YES
NO
NO
Note
In the first test case, the graph's diameter equal to 0.
In the second test case, the graph's diameter can only be 2.
In the third test case, the graph's diameter can only be 1.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | for _ in range(int(input())):n, m, k = map(int, input().split());print('YES') if (k == 3 and m == (n * (n - 1)) // 2) or (k > 3 and m >= n - 1 and m <= (n * (n - 1)) // 2) or (n == 1 and m == 0 and k > 1) else print('NO') | python | code_algorithm | [
{
"input": "5\n1 0 3\n4 5 3\n4 6 3\n5 4 1\n2 1 1\n",
"output": "YES\nNO\nYES\nNO\nNO\n"
},
{
"input": "1\n1 0 0\n",
"output": "NO\n"
},
{
"input": "1\n5 7 0\n",
"output": "NO\n"
},
{
"input": "5\n1 0 2\n1 0 1\n5 20 3\n5 20 4\n5 20 5\n",
"output": "YES\nNO\nNO\nNO\nNO\n"
},
{
"input": "1\n1 0 1\n",
"output": "NO\n"
},
{
"input": "1\n3 3 0\n",
"output": "NO\n"
},
{
"input": "1\n2000000 691723712 3\n",
"output": "NO\n"
},
{
"input": "3\n1 1 1\n1 0 0\n1 2 1\n",
"output": "NO\nNO\nNO\n"
},
{
"input": "2\n1 0 0\n1 0 1\n",
"output": "NO\nNO\n"
}
] | code_contests | python | 0 | c778c5080a0af3985dbf796423726d96 |
The problem statement looms below, filling you with determination.
Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up.
Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't.
You are given a grid a of dimensions n Γ m , i. e. a grid with n rows and m columns. You need to answer q queries (1 β€ q β€ 2 β
10^5). Each query gives two integers x_1, x_2 (1 β€ x_1 β€ x_2 β€ m) and asks whether the subgrid of a consisting of the columns x_1, x_1 + 1, β¦, x_2 - 1, x_2 is determinable.
Input
The first line contains two integers n, m (1 β€ n, m β€ 10^6, nm β€ 10^6) β the dimensions of the grid a.
n lines follow. The y-th line contains m characters, the x-th of which is 'X' if the cell on the intersection of the the y-th row and x-th column is filled and "." if it is empty.
The next line contains a single integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
q lines follow. Each line contains two integers x_1 and x_2 (1 β€ x_1 β€ x_2 β€ m), representing a query asking whether the subgrid of a containing the columns x_1, x_1 + 1, β¦, x_2 - 1, x_2 is determinable.
Output
For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted).
Example
Input
4 5
..XXX
...X.
...X.
...X.
5
1 3
3 3
4 5
5 5
1 5
Output
YES
YES
NO
YES
NO
Note
For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise.
For the first query:
..X EEN
... EEE
... EEE
... EEE
For the second query:
X N
. E
. E
. E
Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid.
For the third query:
XX NN
X. NN
X. NN
X. NN
This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well:
XX
XX
XX
XX
For the fourth query:
X N
. E
. E
. E
For the fifth query:
..XXX EENNN
...X. EEENN
...X. EEENN
...X. EEENN
This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well:
..XXX
...XX
...XX
...XX
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | # SHRi GANESHA author: Kunal Verma #
import os
import sys
from collections import defaultdict, Counter
from io import BytesIO, IOBase
from math import gcd
def lcm(a, b):
return (a * b) // gcd(a, b)
'''
mod = 10 ** 9 + 7
fac = [1]
for i in range(1, 2 * 10 ** 5 + 1):
fac.append((fac[-1] * i) % mod)
fac_in = [pow(fac[-1], mod - 2, mod)]
for i in range(2 * 10 ** 5, 0, -1):
fac_in.append((fac_in[-1] * i) % mod)
fac_in.reverse()
def comb(a, b):
if a < b:
return 0
return (fac[a] * fac_in[b] * fac_in[a - b]) % mod
'''
# MAXN = 10000004
# spf = [0 for i in range(MAXN)]
# adj = [[] for i in range(MAXN)]
def sieve():
global spf, adj, MAXN
spf[1] = 1
for i in range(2, MAXN):
spf[i] = i
for i in range(2, MAXN):
if i * i > MAXN:
break
if (spf[i] == i):
for j in range(i * i, MAXN, i):
if (spf[j] == j):
spf[j] = i
def getdistinctFactorization(n):
global adj, spf, MAXN
for i in range(1, n + 1):
index = 1
x = i
if (x != 1):
adj[i].append(spf[x])
x = x // spf[x]
while (x != 1):
if (adj[i][index - 1] != spf[x]):
adj[i].append(spf[x])
index += 1
x = x // spf[x]
def printDivisors(n):
i = 2
z = [1, n]
while i <= sqrt(n):
if (n % i == 0):
if (n / i == i):
z.append(i)
else:
z.append(i)
z.append(n // i)
i = i + 1
return z
def create(n, x, f):
pq = len(bin(n)[2:])
if f == 0:
tt = min
else:
tt = max
dp = [[inf] * n for _ in range(pq)]
dp[0] = x
for i in range(1, pq):
for j in range(n - (1 << i) + 1):
dp[i][j] = tt(dp[i - 1][j], dp[i - 1][j + (1 << (i - 1))])
return dp
def enquiry(l, r, dp, f):
if l > r:
return inf if not f else -inf
if f == 1:
tt = max
else:
tt = min
pq1 = len(bin(r - l + 1)[2:]) - 1
return tt(dp[pq1][l], dp[pq1][r - (1 << pq1) + 1])
def SieveOfEratosthenes(n):
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
x = []
for i in range(2, n + 1):
if prime[i]:
x.append(i)
return x
def main():
n,m=map(int,input().split())
s=[input().strip() for i in range(n)]
dp=[0]*(m)
for i in range(m-1,0,-1):
for j in range(1,n):
if s[j-1][i]=='X' and s[j][i-1]=="X":
dp[i]=1
for i in range(1,m):
dp[i]+=dp[i-1]
for i in range(int(input())):
xx,yy=map(int,input().split())
if xx==yy:
print("YES")
else:
yy-=1
xx-=1
if dp[yy]-dp[xx]>0:
print("NO")
else:
print("YES")
# Fast IO Region
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")
if __name__ == '__main__':
main() | python | code_algorithm | [
{
"input": "4 5\n..XXX\n...X.\n...X.\n...X.\n5\n1 3\n3 3\n4 5\n5 5\n1 5\n",
"output": "YES\nYES\nNO\nYES\nNO\n"
},
{
"input": "3 3\n...\nXXX\nXX.\n10\n2 3\n1 2\n2 2\n1 3\n2 3\n1 2\n1 3\n1 3\n2 3\n1 1\n",
"output": "NO\nNO\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nYES\n"
},
{
"input": "1 1\n.\n1\n1 1\n",
"output": "YES\n"
},
{
"input": "3 3\n.XX\n..X\n.X.\n10\n2 3\n1 3\n2 3\n2 3\n1 3\n1 1\n2 2\n1 2\n3 3\n1 2\n",
"output": "NO\nNO\nNO\nNO\nNO\nYES\nYES\nYES\nYES\nYES\n"
},
{
"input": "3 3\nXX.\nXX.\nX..\n10\n2 3\n2 2\n2 3\n1 3\n2 3\n1 1\n2 3\n1 2\n3 3\n2 3\n",
"output": "YES\nYES\nYES\nNO\nYES\nYES\nYES\nNO\nYES\nYES\n"
},
{
"input": "3 3\nXXX\nXX.\nXX.\n10\n1 3\n1 2\n1 2\n1 3\n3 3\n2 2\n2 3\n2 3\n2 3\n2 2\n",
"output": "NO\nNO\nNO\nNO\nYES\nYES\nNO\nNO\nNO\nYES\n"
},
{
"input": "3 3\n...\nX.X\n..X\n10\n2 3\n3 3\n2 3\n1 2\n3 3\n1 1\n1 2\n2 3\n1 1\n1 2\n",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n"
},
{
"input": "3 3\n.XX\nX..\nXXX\n10\n2 2\n1 1\n1 2\n1 3\n1 3\n2 3\n1 2\n2 3\n1 3\n1 2\n",
"output": "YES\nYES\nNO\nNO\nNO\nYES\nNO\nYES\nNO\nNO\n"
},
{
"input": "3 3\nXXX\nX..\nX..\n10\n2 2\n1 2\n2 2\n2 3\n2 2\n3 3\n1 1\n2 3\n2 2\n1 3\n",
"output": "YES\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\n"
},
{
"input": "3 3\n.XX\nX..\nXXX\n10\n1 2\n1 1\n2 3\n1 2\n2 3\n2 3\n1 3\n2 3\n3 3\n2 3\n",
"output": "NO\nYES\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\n"
},
{
"input": "1 1\nX\n1\n1 1\n",
"output": "YES\n"
},
{
"input": "3 3\nXX.\nXXX\nXX.\n10\n2 2\n3 3\n3 3\n2 3\n2 3\n1 2\n1 3\n1 1\n1 3\n1 2\n",
"output": "YES\nYES\nYES\nNO\nNO\nNO\nNO\nYES\nNO\nNO\n"
},
{
"input": "3 3\nX.X\n..X\nXXX\n10\n3 3\n1 2\n3 3\n1 2\n1 3\n1 2\n1 1\n2 3\n2 3\n1 2\n",
"output": "YES\nYES\nYES\nYES\nNO\nYES\nYES\nNO\nNO\nYES\n"
},
{
"input": "3 3\nXX.\nXXX\n..X\n10\n1 2\n1 2\n2 3\n1 3\n1 1\n2 3\n1 3\n1 1\n2 3\n2 2\n",
"output": "NO\nNO\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\n"
},
{
"input": "3 3\n.X.\nX..\nX.X\n10\n1 2\n1 1\n2 3\n1 2\n2 2\n1 3\n1 2\n3 3\n2 3\n2 3\n",
"output": "NO\nYES\nYES\nNO\nYES\nNO\nNO\nYES\nYES\nYES\n"
},
{
"input": "3 3\nXX.\n...\n.XX\n10\n1 3\n1 3\n1 2\n1 2\n3 3\n1 3\n2 3\n3 3\n2 3\n2 2\n",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n"
},
{
"input": "3 3\nX.X\n.XX\n...\n10\n1 3\n1 2\n2 3\n2 3\n2 2\n3 3\n3 3\n1 3\n1 2\n1 3\n",
"output": "NO\nYES\nNO\nNO\nYES\nYES\nYES\nNO\nYES\nNO\n"
},
{
"input": "3 3\n..X\n.XX\nXXX\n10\n1 2\n1 1\n2 2\n1 2\n1 3\n1 2\n1 3\n2 2\n2 3\n2 2\n",
"output": "NO\nYES\nYES\nNO\nNO\nNO\nNO\nYES\nNO\nYES\n"
},
{
"input": "3 3\n..X\nX..\n...\n10\n3 3\n1 3\n1 3\n3 3\n1 3\n2 3\n3 3\n3 3\n2 3\n1 2\n",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n"
},
{
"input": "3 3\n..X\n.X.\nX..\n10\n1 3\n1 3\n1 3\n2 3\n1 3\n1 2\n1 3\n2 2\n2 3\n2 2\n",
"output": "NO\nNO\nNO\nNO\nNO\nNO\nNO\nYES\nNO\nYES\n"
},
{
"input": "3 3\nX.X\nXX.\n..X\n10\n1 3\n1 2\n1 2\n3 3\n1 2\n1 2\n3 3\n1 1\n1 3\n2 3\n",
"output": "NO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\n"
},
{
"input": "3 3\nX..\n..X\nXX.\n10\n1 3\n2 2\n1 2\n1 2\n2 3\n1 3\n1 1\n2 2\n1 3\n1 1\n",
"output": "NO\nYES\nYES\nYES\nNO\nNO\nYES\nYES\nNO\nYES\n"
},
{
"input": "3 3\n..X\nX.X\n..X\n10\n2 3\n2 2\n3 3\n1 3\n1 1\n3 3\n3 3\n1 1\n1 2\n1 1\n",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n"
}
] | code_contests | python | 0 | 027ae5229e7bff4483923b232d817be2 |
Omkar is creating a mosaic using colored square tiles, which he places in an n Γ n grid. When the mosaic is complete, each cell in the grid will have either a glaucous or sinoper tile. However, currently he has only placed tiles in some cells.
A completed mosaic will be a mastapeece if and only if each tile is adjacent to exactly 2 tiles of the same color (2 tiles are adjacent if they share a side.) Omkar wants to fill the rest of the tiles so that the mosaic becomes a mastapeece. Now he is wondering, is the way to do this unique, and if it is, what is it?
Input
The first line contains a single integer n (1 β€ n β€ 2000).
Then follow n lines with n characters in each line. The i-th character in the j-th line corresponds to the cell in row i and column j of the grid, and will be S if Omkar has placed a sinoper tile in this cell, G if Omkar has placed a glaucous tile, . if it's empty.
Output
On the first line, print UNIQUE if there is a unique way to get a mastapeece, NONE if Omkar cannot create any, and MULTIPLE if there is more than one way to do so. All letters must be uppercase.
If you print UNIQUE, then print n additional lines with n characters in each line, such that the i-th character in the j^{th} line is S if the tile in row i and column j of the mastapeece is sinoper, and G if it is glaucous.
Examples
Input
4
S...
..G.
....
...S
Output
MULTIPLE
Input
6
S.....
....G.
..S...
.....S
....G.
G.....
Output
NONE
Input
10
.S....S...
..........
...SSS....
..........
..........
...GS.....
....G...G.
..........
......G...
..........
Output
UNIQUE
SSSSSSSSSS
SGGGGGGGGS
SGSSSSSSGS
SGSGGGGSGS
SGSGSSGSGS
SGSGSSGSGS
SGSGGGGSGS
SGSSSSSSGS
SGGGGGGGGS
SSSSSSSSSS
Input
1
.
Output
NONE
Note
For the first test case, Omkar can make the mastapeeces
SSSS
SGGS
SGGS
SSSS
and
SSGG
SSGG
GGSS
GGSS.
For the second test case, it can be proven that it is impossible for Omkar to add tiles to create a mastapeece.
For the third case, it can be proven that the given mastapeece is the only mastapeece Omkar can create by adding tiles.
For the fourth test case, it's clearly impossible for the only tile in any mosaic Omkar creates to be adjacent to two tiles of the same color, as it will be adjacent to 0 tiles total.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
o = {'G':'S', 'S':'G'};n = int(input());d = [list(input()[:n]) for _ in range(n)];f = [1] * (n * n);finished = 1
if n % 2: print('NONE'); sys.exit()
x = [''] * (n // 2)
def findt(i, j): return abs(j - i) // 2 if (j - i) % 2 else min(i + j, 2 * (n - 1) - j - i) // 2
def findr(i, j, t):
if (j - i) % 2: return o[t] if min(i, j) % 2 else t
else:
if i + j < n: return o[t] if i % 2 else t
else: return t if i % 2 else o[t]
for i in range(n):
for j in range(n):
if d[i][j] != '.':
t = findt(i, j);r = findr(i, j, d[i][j])
if x[t] == o[r]: print('NONE'); sys.exit()
else: x[t] = r
for i in range(n // 2):
if not x[i]: print('MULTIPLE'); sys.exit()
for i in range(n):
for j in range(n):d[i][j] = findr(i, j, x[findt(i, j)])
print('UNIQUE')
print('\n'.join(''.join(d[i]) for i in range(n))) | python | code_algorithm | [
{
"input": "10\n.S....S...\n..........\n...SSS....\n..........\n..........\n...GS.....\n....G...G.\n..........\n......G...\n..........\n",
"output": "UNIQUE\nSSSSSSSSSS\nSGGGGGGGGS\nSGSSSSSSGS\nSGSGGGGSGS\nSGSGSSGSGS\nSGSGSSGSGS\nSGSGGGGSGS\nSGSSSSSSGS\nSGGGGGGGGS\nSSSSSSSSSS\n"
},
{
"input": "4\nS...\n..G.\n....\n...S\n",
"output": "MULTIPLE\n"
},
{
"input": "6\nS.....\n....G.\n..S...\n.....S\n....G.\nG.....\n",
"output": "NONE\n"
},
{
"input": "1\n.\n",
"output": "NONE\n"
},
{
"input": "8\n........\n........\n........\n........\n........\n........\n........\n........\n",
"output": "MULTIPLE\n"
},
{
"input": "20\n..............S.....\n....................\n.............S.....S\n....................\n....................\n...S................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n...................S\n.G..................\n....................\n....................\n...................S\n................G...\nS..................G\n",
"output": "UNIQUE\nGGSSSSSSGGSSSSSSSSSS\nGGSGGGGSGGSGGGGGGGGS\nSSSGSSGSSSSGSSSSSSGS\nSGGGSSGGGGGGSGGGGSGS\nSGSSGGSSSSSSSGSSGSGS\nSGSSGGSGGGGGGGSSGSGS\nSGGGSSSGSSSSSSGGGSGS\nSSSGSGGGSGGGGSGSSSGS\nGGSGSGSSSGSSGSGSGGGS\nGGSGSGSGGGSSGSGSGSSS\nSSSGSGSGSSGGGSGSGSGG\nSGGGSGSGSSGSSSGSGSGG\nSGSSSGSGGGGSGGGSGSSS\nSGSGGGSSSSSSGSSSGGGS\nSGSGSSGGGGGGGSGGSSGS\nSGSGSSGSSSSSSSGGSSGS\nSGSGGGGSGGGGGGSSGGGS\nSGSSSSSSGSSSSGSSGSSS\nSGGGGGGGGSGGSGGGGSGG\nSSSSSSSSSSGGSSSSSSGG\n"
},
{
"input": "6\n.S....\n......\n......\nS..S..\n......\n......\n",
"output": "UNIQUE\nSSSSSS\nSGGGGS\nSGSSGS\nSGSSGS\nSGGGGS\nSSSSSS\n"
},
{
"input": "9\n...S.G...\nG........\n.........\n.........\n....G....\n.........\n.........\nS........\n.........\n",
"output": "NONE\n"
},
{
"input": "8\n.S......\n.......S\n........\nG.......\n........\n........\n........\n...G....\n",
"output": "UNIQUE\nSSGGGGSS\nSSGSSGSS\nGGGSSGGG\nGSSGGSSG\nGSSGGSSG\nGGGSSGGG\nSSGSSGSS\nSSGGGGSS\n"
},
{
"input": "4\n....\n.G..\n...S\nG...\n",
"output": "NONE\n"
},
{
"input": "5\n.....\n...GS\n.S...\n.....\n.....\n",
"output": "NONE\n"
},
{
"input": "2\n..\n..\n",
"output": "MULTIPLE\n"
},
{
"input": "10\n....SS.G.S\nS..G.S.GSS\n.GSSG.S.G.\n.GSSGG..GG\n..G..S...S\nSS.GSSGG.S\nGG..GGS.G.\nGG.....S..\nSSG.S.GG.S\n.SGGSS..SS\n",
"output": "MULTIPLE\n"
},
{
"input": "1\nG\n",
"output": "NONE\n"
},
{
"input": "10\nS....S..SS\n..G....G.S\n.GSS..SSG.\n.GS.GG.SG.\nS..G.SG..S\n...GSS....\n.GS..G.SG.\n.GS...S.G.\nS..G..G..S\nSS......SS\n",
"output": "MULTIPLE\n"
},
{
"input": "10\n..........\n..........\n..........\n.G........\n..........\n..........\nG.........\n........S.\nG..G......\n..........\n",
"output": "MULTIPLE\n"
},
{
"input": "5\n.GSGS\nSGSG.\nSSSGS\nGGGG.\nS.SGS\n",
"output": "NONE\n"
},
{
"input": "1\nS\n",
"output": "NONE\n"
},
{
"input": "8\n......S.\n.....G.S\n....S.G.\n...G.S..\n..S.G...\n.G.S....\nS.G.....\nSS......\n",
"output": "MULTIPLE\n"
},
{
"input": "15\n..............G\n............G..\n...........S...\n...............\n...............\n...............\n............G..\n...............\n...............\n.............S.\n.........G.....\n...............\n...............\n...G...S.......\n...............\n",
"output": "NONE\n"
},
{
"input": "20\n..GG.SGG..G.GGGGSSS.\n.S.GS.G..SGSSSSGS.GS\nG....GSSGGG.GG.GSGGS\nGG.S.GSSGSS.G.SG.SSS\n.SG.S.GGG.GGSSSG.GGG\nSS.G...SSSGGSGGSSSS.\nGGSSG.G.G.SSS.GSG..G\nG.S.GS...GS..SSS..SG\nS..GG...S.S...GGSSSG\n..GSSSGG.G.S.SG.S.GG\nGGG.GG.SS.G.GGSSS.S.\nGS.SGG.GGS.S.GSGGGSS\n..G.SSSG.SG.S.SGSSG.\nG.G.SGGSS.G.....SSGG\nG..SSGGS.GS...S.GG..\nGGGGGSSSGGSGG..S.GS.\nS.SSGS.G.SS..SG.S.GG\nSGGSGSGGSGGGSS.G.S.G\nSGGSGSSS.GS....SG...\n.SSSG.G..G..G..SG...\n",
"output": "MULTIPLE\n"
},
{
"input": "4\nS.G.\n..S.\n....\nG...\n",
"output": "NONE\n"
},
{
"input": "6\nG.GSS.\nGSSGGS\n.SGGGG\nGGGS.G\nGSG.SG\nSG.GGG\n",
"output": "NONE\n"
},
{
"input": "5\n...SG\n.....\n.S...\n.....\n.....\n",
"output": "NONE\n"
},
{
"input": "8\n.SSGG.GG\n.GGGG.GS\nSGG.GSG.\nSSSS.GGG\nGG.G.SSG\nGSGSSGGS\nG...SGSS\n...GSGGS\n",
"output": "NONE\n"
},
{
"input": "4\nG..S\n....\n....\n....\n",
"output": "UNIQUE\nGGSS\nGGSS\nSSGG\nSSGG\n"
},
{
"input": "4\n....\n.G..\n...G\nS...\n",
"output": "UNIQUE\nGGSS\nGGSS\nSSGG\nSSGG\n"
},
{
"input": "11\n...........\n...........\n...........\n....G.....S\n....S......\n....G......\n...........\nS..........\n........G..\n.........S.\n.........G.\n",
"output": "NONE\n"
},
{
"input": "16\nG.G..........S..\n...S............\n................\n................\n......G.........\n.G..............\n................\n.....S..........\n................\n..........S.....\n.....S..........\n...........S....\n....G..........G\n......S.........\n.....S..........\nS...G...........\n",
"output": "NONE\n"
},
{
"input": "15\nG...........G..\n..............S\n.......S.......\n...............\n.G.G..G........\n...............\n...............\n...............\n.S.............\n.....S.........\n.G.............\n.........G.....\n.S..........S..\n.........G.....\n............G..\n",
"output": "NONE\n"
}
] | code_contests | python | 0 | 7340efb269e595e32d5cd4256348800d |
It is the easy version of the problem. The difference is that in this version, there are no nodes with already chosen colors.
Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem?
You have a perfect binary tree of 2^k - 1 nodes β a binary tree where all vertices i from 1 to 2^{k - 1} - 1 have exactly two children: vertices 2i and 2i + 1. Vertices from 2^{k - 1} to 2^k - 1 don't have any children. You want to color its vertices with the 6 Rubik's cube colors (White, Green, Red, Blue, Orange and Yellow).
Let's call a coloring good when all edges connect nodes with colors that are neighboring sides in the Rubik's cube.
<image>| <image>
---|---
A picture of Rubik's cube and its 2D map.
More formally:
* a white node can not be neighboring with white and yellow nodes;
* a yellow node can not be neighboring with white and yellow nodes;
* a green node can not be neighboring with green and blue nodes;
* a blue node can not be neighboring with green and blue nodes;
* a red node can not be neighboring with red and orange nodes;
* an orange node can not be neighboring with red and orange nodes;
You want to calculate the number of the good colorings of the binary tree. Two colorings are considered different if at least one node is colored with a different color.
The answer may be too large, so output the answer modulo 10^9+7.
Input
The first and only line contains the integers k (1 β€ k β€ 60) β the number of levels in the perfect binary tree you need to color.
Output
Print one integer β the number of the different colorings modulo 10^9+7.
Examples
Input
3
Output
24576
Input
14
Output
934234
Note
In the picture below, you can see one of the correct colorings of the first example.
<image>
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n = int(input())
vid = 6
k = 2
m = int(1e9 + 7)
for i in range(n - 1):
vid = (vid * pow(4, k, m)) % m
k *= 2
print(vid)
| python | code_algorithm | [
{
"input": "14\n",
"output": "934234\n"
},
{
"input": "3\n",
"output": "24576\n"
},
{
"input": "50\n",
"output": "902552662\n"
},
{
"input": "60\n",
"output": "937481864\n"
},
{
"input": "40\n",
"output": "622757975\n"
},
{
"input": "10\n",
"output": "153616455\n"
},
{
"input": "59\n",
"output": "950618124\n"
},
{
"input": "58\n",
"output": "166788441\n"
},
{
"input": "2\n",
"output": "96\n"
},
{
"input": "20\n",
"output": "61556388\n"
},
{
"input": "1\n",
"output": "6\n"
},
{
"input": "30\n",
"output": "686170158\n"
},
{
"input": "57\n",
"output": "690506414\n"
},
{
"input": "4\n",
"output": "610612729\n"
}
] | code_contests | python | 0 | 03949d61d7e32aef9a92ff98a89330ef |
It is the hard version of the problem. The difference is that in this version, there are nodes with already chosen colors.
Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem?
You have a perfect binary tree of 2^k - 1 nodes β a binary tree where all vertices i from 1 to 2^{k - 1} - 1 have exactly two children: vertices 2i and 2i + 1. Vertices from 2^{k - 1} to 2^k - 1 don't have any children. You want to color its vertices with the 6 Rubik's cube colors (White, Green, Red, Blue, Orange and Yellow).
Let's call a coloring good when all edges connect nodes with colors that are neighboring sides in the Rubik's cube.
<image>| <image>
---|---
A picture of Rubik's cube and its 2D map.
More formally:
* a white node can not be neighboring with white and yellow nodes;
* a yellow node can not be neighboring with white and yellow nodes;
* a green node can not be neighboring with green and blue nodes;
* a blue node can not be neighboring with green and blue nodes;
* a red node can not be neighboring with red and orange nodes;
* an orange node can not be neighboring with red and orange nodes;
However, there are n special nodes in the tree, colors of which are already chosen.
You want to calculate the number of the good colorings of the binary tree. Two colorings are considered different if at least one node is colored with a different color.
The answer may be too large, so output the answer modulo 10^9+7.
Input
The first line contains the integers k (1 β€ k β€ 60) β the number of levels in the perfect binary tree you need to color.
The second line contains the integer n (1 β€ n β€ min(2^k - 1, 2000)) β the number of nodes, colors of which are already chosen.
The next n lines contains integer v (1 β€ v β€ 2^k - 1) and string s β the index of the node and the color of the node (s is one of the white, yellow, green, blue, red and orange).
It is guaranteed that each node v appears in the input at most once.
Output
Print one integer β the number of the different colorings modulo 10^9+7.
Examples
Input
3
2
5 orange
2 white
Output
1024
Input
2
2
1 white
2 white
Output
0
Input
10
3
1 blue
4 red
5 orange
Output
328925088
Note
In the picture below, you can see one of the correct colorings of the first test example.
<image>
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import io,os
#input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = 10**9 + 7
colors = {'white':0,'yellow':0,'green':1,'blue':1,'red':2,'orange':2}
def getnext(index,n,colored,dic,layer):
if layer==n:
if index not in dic: return [2,2,2]
else:
dic[index][colored[index]] += 1
return dic[index]
if index not in dic:
rest = n - layer
sub = pow(2,(1<<(rest+2))-3, M )
# print(index,sub)
return [sub,sub,sub]
left = getnext(2*index,n,colored,dic,layer+1)
right = getnext(2*index+1,n,colored,dic,layer+1)
# print(left,right)
if index in colored:
for i in range(3):
if i != colored[index]: continue
dic[index][i] = ( sum(left) - left[i] ) * (sum(right) - right[i])
dic[index][i] = dic[index][i] % M
else:
for i in range(3):
dic[index][i] = 2 * ( sum(left) - left[i] ) * (sum(right) - right[i])
dic[index][i] = dic[index][i] % M
return dic[index]
def main(t):
M = 10**9 + 7
n = int(input())
q = int(input())
dic = {}
colored = {}
for _ in range(q):
i, color = input().split()
i = int(i)
colored[i] = colors[color]
while i>0:
if i not in dic: dic[i] = [0,0,0]
i = i >> 1
ans = getnext(1,n,colored,dic,1)
# print(dic)
print(sum(ans)%M)
main(1)
| python | code_algorithm | [
{
"input": "10\n3\n1 blue\n4 red\n5 orange\n",
"output": "328925088\n"
},
{
"input": "3\n2\n5 orange\n2 white\n",
"output": "1024\n"
},
{
"input": "2\n2\n1 white\n2 white\n",
"output": "0\n"
},
{
"input": "5\n9\n31 yellow\n30 green\n26 yellow\n13 red\n2 red\n22 white\n12 red\n6 orange\n29 blue\n",
"output": "0\n"
},
{
"input": "3\n1\n7 yellow\n",
"output": "4096\n"
},
{
"input": "2\n2\n1 green\n3 red\n",
"output": "4\n"
},
{
"input": "1\n1\n1 yellow\n",
"output": "1\n"
},
{
"input": "1\n1\n1 white\n",
"output": "1\n"
}
] | code_contests | python | 0 | 013e36f8f3dee7c47acbe89bf885a328 |
You are given a matrix, consisting of n rows and m columns. The rows are numbered top to bottom, the columns are numbered left to right.
Each cell of the matrix can be either free or locked.
Let's call a path in the matrix a staircase if it:
* starts and ends in the free cell;
* visits only free cells;
* has one of the two following structures:
1. the second cell is 1 to the right from the first one, the third cell is 1 to the bottom from the second one, the fourth cell is 1 to the right from the third one, and so on;
2. the second cell is 1 to the bottom from the first one, the third cell is 1 to the right from the second one, the fourth cell is 1 to the bottom from the third one, and so on.
In particular, a path, consisting of a single cell, is considered to be a staircase.
Here are some examples of staircases:
<image>
Initially all the cells of the matrix are free.
You have to process q queries, each of them flips the state of a single cell. So, if a cell is currently free, it makes it locked, and if a cell is currently locked, it makes it free.
Print the number of different staircases after each query. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Input
The first line contains three integers n, m and q (1 β€ n, m β€ 1000; 1 β€ q β€ 10^4) β the sizes of the matrix and the number of queries.
Each of the next q lines contains two integers x and y (1 β€ x β€ n; 1 β€ y β€ m) β the description of each query.
Output
Print q integers β the i-th value should be equal to the number of different staircases after i queries. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Examples
Input
2 2 8
1 1
1 1
1 1
2 2
1 1
1 2
2 1
1 1
Output
5
10
5
2
5
3
1
0
Input
3 4 10
1 4
1 2
2 3
1 2
2 3
3 2
1 3
3 4
1 3
3 1
Output
49
35
24
29
49
39
31
23
29
27
Input
1000 1000 2
239 634
239 634
Output
1332632508
1333333000
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | ''' E. Staircases
https://codeforces.com/contest/1598/problem/E
'''
import sys
input = sys.stdin.readline
output = sys.stdout.write
def solve(R, C, Q, queries):
blocked = [[0]*C for _ in range(R)]
# total staircases if no cell blocked
res = R*C # 1-block
for c in range(C): # right-down from row 0
for ln in range(1, R+C+1):
nr = ln // 2
nc = c + (ln+1) // 2
if nr >= R or nc >= C: break
res += ln
for r in range(R): # down-right from col 0
for ln in range(1, R+C+1):
nr = r + (ln+1) // 2
nc = ln // 2
if nr >= R or nc >= C: break
res += ln
# each query (qr, qc) changes total by num staircases passing through it
for qr, qc in queries:
# num staircases passing through (qr, qc)
change = 1
# type 1
r, c = qr, qc
up = down = 0
for ln in range(1, R+C+1): # go farthest down-right from (qr, qc)
nr = r + ln//2
nc = c + (ln+1)//2
if nr >= R or nc >= C or blocked[nr][nc]: break
down += 1
for ln in range(1, R+C+1): # left-up
nr = r - (ln+1)//2
nc = c - ln//2
if nr < 0 or nc < 0 or blocked[nr][nc]: break
up += 1
change += up + down + up * down
# type 2
r, c = qr, qc
up = down = 0
for ln in range(1, R+C+1): # right-down
nr = r + (ln+1)//2
nc = c + ln//2
if nr >= R or nc >= C or blocked[nr][nc]: break
down += 1
for ln in range(1, R+C+1): # up-left
nr = r - ln//2
nc = c - (ln+1)//2
if nr < 0 or nc < 0 or blocked[nr][nc]: break
up += 1
change += up + down + up * down
# add or subtract to total
res += change if blocked[qr][qc] else -change
blocked[qr][qc] ^= 1
output(str(res) + '\n')
def main():
N, M, Q = list(map(int, input().split()))
queries = []
for _ in range(Q):
x, y = list(map(int, input().split()))
queries.append((x-1, y-1))
solve(N, M, Q, queries)
if __name__ == '__main__':
main()
| python | code_algorithm | [
{
"input": "3 4 10\n1 4\n1 2\n2 3\n1 2\n2 3\n3 2\n1 3\n3 4\n1 3\n3 1\n",
"output": "49\n35\n24\n29\n49\n39\n31\n23\n29\n27\n"
},
{
"input": "1000 1000 2\n239 634\n239 634\n",
"output": "1332632508\n1333333000\n"
},
{
"input": "2 2 8\n1 1\n1 1\n1 1\n2 2\n1 1\n1 2\n2 1\n1 1\n",
"output": "5\n10\n5\n2\n5\n3\n1\n0\n"
},
{
"input": "1 1 1\n1 1\n",
"output": "0\n"
},
{
"input": "5 10 30\n5 3\n5 5\n5 10\n1 2\n5 7\n1 8\n4 9\n2 3\n4 10\n3 1\n4 5\n3 8\n1 7\n2 2\n1 5\n2 3\n3 2\n3 10\n3 2\n2 7\n2 4\n1 4\n4 8\n2 6\n4 1\n5 3\n4 6\n5 8\n4 5\n1 1\n",
"output": "399\n373\n355\n332\n306\n290\n261\n238\n233\n222\n207\n186\n180\n162\n140\n151\n144\n137\n144\n137\n111\n109\n91\n82\n77\n87\n78\n76\n83\n81\n"
},
{
"input": "3 3 10\n1 2\n1 3\n2 2\n2 2\n2 2\n3 2\n2 2\n3 2\n2 3\n3 1\n",
"output": "25\n23\n12\n23\n12\n8\n13\n23\n19\n15\n"
},
{
"input": "1 1 2\n1 1\n1 1\n",
"output": "0\n1\n"
}
] | code_contests | python | 0 | e4315068752313a050d2199207c8eaa7 |
Little Johnny Bubbles enjoys spending hours in front of his computer playing video games. His favorite game is Bubble Strike, fast-paced bubble shooting online game for two players.
Each game is set in one of the N maps, each having different terrain configuration. First phase of each game decides on which map the game will be played. The game system randomly selects three maps and shows them to the players. Each player must pick one of those three maps to be discarded. The game system then randomly selects one of the maps that were not picked by any of the players and starts the game.
Johnny is deeply enthusiastic about the game and wants to spend some time studying maps, thus increasing chances to win games played on those maps. However, he also needs to do his homework, so he does not have time to study all the maps. That is why he asked himself the following question: "What is the minimum number of maps I have to study, so that the probability to play one of those maps is at least P"?
Can you help Johnny find the answer for this question? You can assume Johnny's opponents do not know him, and they will randomly pick maps.
Input
The first line contains two integers N (3 β€ N β€ 10^{3}) and P (0 β€ P β€ 1) β total number of maps in the game and probability to play map Johnny has studied. P will have at most four digits after the decimal point.
Output
Output contains one integer number β minimum number of maps Johnny has to study.
Example
Input
7 1.0000
Output
6
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` |
n,target = map(float,input().split())
n = int(n)
def getprob(study):
tot = n*(n-1)*(n-2)//6
case1 = study*(study-1)*(study-2)//6
case2 = study*(study-1)*(n-study)//2
case3 = study*(n-study)*(n-study-1)//2
return (case1+case2+case3*0.5)*1.0/tot
front = 0
rear = n
while front<rear:
mid = (front+rear)//2
p = getprob(mid)
# print(mid,p)
if p < target: front = mid + 1
else: rear = mid
print(front)
| python | code_algorithm | [
{
"input": "7 1.0000\n",
"output": "6\n"
},
{
"input": "956 0.9733\n",
"output": "826\n"
},
{
"input": "444 0.0265\n",
"output": "8\n"
},
{
"input": "267 0.4122\n",
"output": "76\n"
},
{
"input": "840 0.5672\n",
"output": "336\n"
},
{
"input": "937 0.8022\n",
"output": "573\n"
},
{
"input": "504 0.2099\n",
"output": "71\n"
},
{
"input": "439 0.0404\n",
"output": "12\n"
},
{
"input": "200 0.9495\n",
"output": "162\n"
},
{
"input": "566 0.6282\n",
"output": "254\n"
},
{
"input": "995 0.4480\n",
"output": "307\n"
},
{
"input": "364 0.3555\n",
"output": "88\n"
},
{
"input": "891 0.6481\n",
"output": "415\n"
},
{
"input": "329 0.5879\n",
"output": "137\n"
},
{
"input": "622 0.8974\n",
"output": "452\n"
},
{
"input": "634 0.7906\n",
"output": "380\n"
},
{
"input": "440 0.9342\n",
"output": "344\n"
},
{
"input": "848 0.8576\n",
"output": "571\n"
},
{
"input": "38 0.2356\n",
"output": "6\n"
},
{
"input": "716 0.9851\n",
"output": "643\n"
},
{
"input": "141 0.0086\n",
"output": "1\n"
},
{
"input": "357 0.9853\n",
"output": "321\n"
},
{
"input": "1000 1.0000\n",
"output": "999\n"
},
{
"input": "999 1.0000\n",
"output": "998\n"
},
{
"input": "217 0.0744\n",
"output": "11\n"
},
{
"input": "1000 0.0000\n",
"output": "0\n"
},
{
"input": "571 0.5208\n",
"output": "208\n"
},
{
"input": "588 0.3851\n",
"output": "155\n"
},
{
"input": "517 0.4859\n",
"output": "174\n"
},
{
"input": "444 0.0180\n",
"output": "6\n"
},
{
"input": "847 0.3600\n",
"output": "208\n"
},
{
"input": "853 0.0684\n",
"output": "39\n"
},
{
"input": "3 1.0000\n",
"output": "2\n"
},
{
"input": "317 0.2190\n",
"output": "47\n"
},
{
"input": "195 0.5459\n",
"output": "75\n"
},
{
"input": "3 0.0000\n",
"output": "0\n"
}
] | code_contests | python | 0 | 37c5323b7eec8e56d57a3d9db5cb7b1b |
Alice and Bob are playing a game. They are given an array A of length N. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game?
Input
The first line contains one integer N (1 β€ N β€ 2*10^5) - the length of the array A.
The second line contains N integers A_1, A_2,...,A_N (0 β€ A_i β€ 10^9)
Output
The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob".
Examples
Input
1
5
Output
Alice
Input
3
5 4 5
Output
Alice
Input
6
5 8 2 1 10 9
Output
Bob
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
#from collections import deque, Counter, OrderedDict, defaultdict
#import heapq
#ceil,floor,log,sqrt,factorial,pow,pi,gcd
#import bisect
#from bisect import bisect_left,bisect_right
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")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def inp():
return(int(input()))
def inps():
return input().strip()
def inlt():
return(list(map(int,input().split())))
def insr():
s = input().strip()
return(list(s[:len(s)]))
def invr():
return(map(int,input().split()))
n=inp()
l=inlt()
array=0
chnce={'Alice':'Bob','Bob':'Alice'}
strt='Alice'
cntl=1
cntr=1
for i in range(1,n):
if l[i]>l[i-1]:
cntl+=1
else:
break
for i in range(n-2,-1,-1):
if l[i]>l[i+1]:
cntr+=1
else:
break
# print(cntl,cntr)
if cntl%2 or cntr%2:
print(strt)
else:
print(chnce[strt]) | python | code_algorithm | [
{
"input": "6\n5 8 2 1 10 9\n",
"output": "Bob\n"
},
{
"input": "3\n5 4 5\n",
"output": "Alice\n"
},
{
"input": "1\n5\n",
"output": "Alice\n"
},
{
"input": "3\n5 6 5\n",
"output": "Bob\n"
},
{
"input": "2\n5 12\n",
"output": "Alice\n"
}
] | code_contests | python | 0 | 0b82e39a18efc587bc5fec3b4162b3aa |
On the great island of Baltia, there live N people, numbered from 1 to N. There are exactly M pairs of people that are friends with each other. The people of Baltia want to organize a successful party, but they have very strict rules on what a party is and when the party is successful. On the island of Baltia, a party is a gathering of exactly 5 people. The party is considered to be successful if either all the people at the party are friends with each other (so that they can all talk to each other without having to worry about talking to someone they are not friends with) or no two people at the party are friends with each other (so that everyone can just be on their phones without anyone else bothering them). Please help the people of Baltia organize a successful party or tell them that it's impossible to do so.
Input
The first line contains two integer numbers, N (5 β€ N β€ 2*10^5) and M (0 β€ M β€ 2*10^5) β the number of people that live in Baltia, and the number of friendships. The next M lines each contains two integers U_i and V_i (1 β€ U_i,V_i β€ N) β meaning that person U_i is friends with person V_i. Two friends can not be in the list of friends twice (no pairs are repeated) and a person can be friends with themselves (U_i β V_i).
Output
If it's possible to organize a successful party, print 5 numbers indicating which 5 people should be invited to the party. If it's not possible to organize a successful party, print -1 instead. If there are multiple successful parties possible, print any.
Examples
Input
6 3
1 4
4 2
5 4
Output
1 2 3 5 6
Input
5 4
1 2
2 3
3 4
4 5
Output
-1
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
input = sys.stdin.readline
n,m = map(int,input().split())
G = [[] for _ in range(n)]
for _ in range(m):
u,v = map(int,input().split())
u -= 1
v -= 1
if u >= 48 or v >= 48:
continue
G[u].append(v)
G[v].append(u)
if n >= 48:
G = G[:48]
def ok(a,b,c,d,e):
allfd = True
nofd = True
people = [a,b,c,d,e]
for x in people:
for y in people:
if x == y:
continue
if x in G[y]:
nofd = False
else:
allfd = False
return allfd or nofd
sz = len(G)
for a in range(sz):
for b in range(a+1,sz):
for c in range(b+1,sz):
for d in range(c+1,sz):
for e in range(d+1,sz):
if ok(a,b,c,d,e):
ans = [a,b,c,d,e]
print(*[x+1 for x in ans])
exit()
print(-1)
| python | code_algorithm | [
{
"input": "5 4\n1 2\n2 3\n3 4\n4 5\n",
"output": "-1\n"
},
{
"input": "6 3\n1 4\n4 2\n5 4\n",
"output": "1 2 3 5 6\n"
},
{
"input": "6 13\n5 6\n2 5\n1 4\n6 2\n3 5\n4 5\n6 4\n3 1\n1 6\n1 5\n2 4\n6 3\n1 2\n",
"output": "1 2 4 5 6\n"
},
{
"input": "10 8\n5 2\n1 8\n5 7\n1 9\n6 4\n2 7\n8 9\n3 10\n",
"output": "-1\n"
},
{
"input": "20 57\n13 14\n12 20\n18 3\n17 20\n15 9\n18 13\n12 19\n2 4\n9 2\n12 11\n14 1\n16 11\n11 14\n16 4\n16 15\n11 19\n15 4\n10 15\n12 5\n9 3\n10 2\n10 4\n20 19\n14 7\n19 2\n5 8\n6 14\n4 17\n2 17\n17 9\n13 9\n19 9\n18 8\n12 16\n18 5\n7 1\n8 3\n11 20\n6 13\n20 5\n13 8\n17 19\n7 6\n9 11\n18 9\n13 1\n12 14\n7 3\n10 16\n20 2\n5 3\n10 17\n6 1\n8 9\n7 5\n12 15\n15 11\n",
"output": "-1\n"
},
{
"input": "5 0\n",
"output": "1 2 3 4 5\n"
},
{
"input": "10 8\n2 7\n2 9\n7 9\n8 10\n5 3\n1 4\n10 6\n6 8\n",
"output": "-1\n"
}
] | code_contests | python | 0.6 | 190d012411765eb1e1bcac2fefb45fe0 |
Berland State University has received a new update for the operating system. Initially it is installed only on the 1-st computer.
Update files should be copied to all n computers. The computers are not connected to the internet, so the only way to transfer update files from one computer to another is to copy them using a patch cable (a cable connecting two computers directly). Only one patch cable can be connected to a computer at a time. Thus, from any computer where the update files are installed, they can be copied to some other computer in exactly one hour.
Your task is to find the minimum number of hours required to copy the update files to all n computers if there are only k patch cables in Berland State University.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each test case consists of a single line that contains two integers n and k (1 β€ k β€ n β€ 10^{18}) β the number of computers and the number of patch cables.
Output
For each test case print one integer β the minimum number of hours required to copy the update files to all n computers.
Example
Input
4
8 3
6 6
7 1
1 1
Output
4
3
6
0
Note
Let's consider the test cases of the example:
* n=8, k=3:
1. during the first hour, we copy the update files from the computer 1 to the computer 2;
2. during the second hour, we copy the update files from the computer 1 to the computer 3, and from the computer 2 to the computer 4;
3. during the third hour, we copy the update files from the computer 1 to the computer 5, from the computer 2 to the computer 6, and from the computer 3 to the computer 7;
4. during the fourth hour, we copy the update files from the computer 2 to the computer 8.
* n=6, k=6:
1. during the first hour, we copy the update files from the computer 1 to the computer 2;
2. during the second hour, we copy the update files from the computer 1 to the computer 3, and from the computer 2 to the computer 4;
3. during the third hour, we copy the update files from the computer 1 to the computer 5, and from the computer 2 to the computer 6.
* n=7, k=1:
1. during the first hour, we copy the update files from the computer 1 to the computer 2;
2. during the second hour, we copy the update files from the computer 1 to the computer 3;
3. during the third hour, we copy the update files from the computer 1 to the computer 4;
4. during the fourth hour, we copy the update files from the computer 4 to the computer 5;
5. during the fifth hour, we copy the update files from the computer 4 to the computer 6;
6. during the sixth hour, we copy the update files from the computer 3 to the computer 7.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
for t in range(int(input())):
n, k = map(int, sys.stdin.readline().strip().split())
ans = 0
cur = 1
while cur <= k and cur < n:
cur *= 2
ans += 1
if cur <= n:
ans += (n - cur + k - 1) // k
print(ans)
| python | code_algorithm | [
{
"input": "4\n8 3\n6 6\n7 1\n1 1\n",
"output": "4\n3\n6\n0\n"
},
{
"input": "1\n576460752303423489 576460752303423489\n",
"output": "60\n"
},
{
"input": "1\n36028797018963968 18014398509481983\n",
"output": "56\n"
},
{
"input": "4\n576460752303423488 288230376151711743\n576460752303423488 288230376151711744\n576460752303423488 576460752303423488\n576460752303423489 576460752303423489\n",
"output": "60\n59\n59\n60\n"
},
{
"input": "4\n576460752303423487 576460752303423487\n796131459065721 796131459065721\n777 777\n64 64\n",
"output": "59\n50\n10\n6\n"
},
{
"input": "8\n576460752303423488 288230376151711743\n576460752303423489 288230376151711743\n576460752303423489 288230376151711742\n576460752303423489 288230376151711744\n576460752303423489 288230376151711745\n576460752303423488 288230376151711742\n576460752303423487 288230376151711743\n576460752303423487 288230376151711744\n",
"output": "60\n60\n60\n60\n60\n60\n59\n59\n"
},
{
"input": "7\n576460752303423487 576460752303423486\n576460752303423488 576460752303423486\n576460752303423489 576460752303423486\n1 1\n576460752303423487 576460752303423487\n576460752303423488 576460752303423487\n576460752303423489 576460752303423487\n",
"output": "59\n59\n60\n0\n59\n59\n60\n"
},
{
"input": "1\n12342 122\n",
"output": "108\n"
},
{
"input": "2\n576460752303423487 576460752303423487\n576460752303423488 288230376151711743\n",
"output": "59\n60\n"
},
{
"input": "3\n576460752303423487 576460752303423487\n576460752303423488 576460752303423488\n576460752303423489 576460752303423489\n",
"output": "59\n59\n60\n"
},
{
"input": "2\n576460752303423487 576460752303423487\n765228007342234864 765228007342234864\n",
"output": "59\n60\n"
},
{
"input": "3\n576460752303423487 576460752303423487\n796131459065721 796131459065721\n777 777\n",
"output": "59\n50\n10\n"
},
{
"input": "5\n576460752303423487 576460752303423487\n765228007342234864 765228007342234864\n576460752303423489 576460752303423489\n576460752303423486 576460752303423486\n576460752303423488 576460752303423488\n",
"output": "59\n60\n60\n59\n59\n"
},
{
"input": "5\n576460752303423486 576460752303423486\n576460752303423487 576460752303423487\n576460752303423488 576460752303423488\n576460752303423489 576460752303423489\n576460752303423490 576460752303423490\n",
"output": "59\n59\n59\n60\n60\n"
},
{
"input": "10\n576460752303423486 576460752303423486\n576460752303423487 576460752303423487\n576460752303423488 576460752303423488\n576460752303423489 576460752303423489\n576460752303423490 576460752303423490\n576460752303423486 576460752303423485\n576460752303423487 576460752303423486\n576460752303423488 576460752303423487\n576460752303423489 576460752303423488\n576460752303423490 576460752303423489\n",
"output": "59\n59\n59\n60\n60\n59\n59\n59\n60\n60\n"
},
{
"input": "13\n576460752303423486 576460752303423486\n576460752303423487 576460752303423487\n576460752303423488 576460752303423488\n576460752303423489 576460752303423489\n576460752303423490 576460752303423490\n576460752303423488 288230376151711743\n576460752303423488 288230376151711744\n576460752303423488 288230376151711745\n576460752303423486 576460752303423485\n576460752303423487 576460752303423486\n576460752303423488 576460752303423487\n576460752303423489 576460752303423488\n576460752303423490 576460752303423489\n",
"output": "59\n59\n59\n60\n60\n60\n59\n59\n59\n59\n59\n60\n60\n"
},
{
"input": "1\n9999999999999999 1\n",
"output": "9999999999999998\n"
},
{
"input": "5\n576460752303423487 576460752303423487\n796131459065721 796131459065721\n777 777\n33333333333 33333333333\n33333 33333\n",
"output": "59\n50\n10\n35\n16\n"
},
{
"input": "1\n288230376151711745 288230376151711744\n",
"output": "59\n"
},
{
"input": "1\n576460752303423497 576460752303423487\n",
"output": "60\n"
},
{
"input": "2\n576460752303423488 288230376151711743\n576460752303423488 288230376151711744\n",
"output": "60\n59\n"
},
{
"input": "1\n576460752303423487 576460752303423457\n",
"output": "59\n"
},
{
"input": "3\n100000000000000007 1\n1000000000000000000 1\n999999999999999997 1\n",
"output": "100000000000000006\n999999999999999999\n999999999999999996\n"
},
{
"input": "1\n2147483654 2147483653\n",
"output": "32\n"
},
{
"input": "1\n72057594037927935 1\n",
"output": "72057594037927934\n"
},
{
"input": "11\n576460752303423487 576460752303423486\n576460752303423488 576460752303423486\n576460752303423489 576460752303423486\n1 1\n576460752303423487 576460752303423487\n576460752303423488 576460752303423487\n576460752303423489 576460752303423487\n1 1\n576460752303423487 144115188075855872\n576460752303423488 144115188075855872\n576460752303423489 144115188075855872\n",
"output": "59\n59\n60\n0\n59\n59\n60\n0\n60\n60\n61\n"
},
{
"input": "1\n100000000000000007 1\n",
"output": "100000000000000006\n"
},
{
"input": "1\n576460752303423489 576460752303423488\n",
"output": "60\n"
},
{
"input": "6\n576460752303423487 576460752303423486\n576460752303423488 576460752303423486\n576460752303423489 576460752303423486\n576460752303423487 576460752303423487\n576460752303423488 576460752303423487\n576460752303423489 576460752303423487\n",
"output": "59\n59\n60\n59\n59\n60\n"
},
{
"input": "1\n18014398509481984 9007199254740990\n",
"output": "55\n"
},
{
"input": "15\n576460752303423488 576460752303423488\n576460752303423487 576460752303423487\n576460752303423487 33333333333\n576460752303423487 777\n576460752303423487 7777\n576460752303423487 7777\n1024 1024\n1024 2\n796131459065721 796131459065721\n777 777\n33333333333 33333333333\n33333 33333\n123141 123\n33333 33\n1231456 45\n",
"output": "59\n59\n17293857\n741905730120245\n74123794818506\n74123794818506\n10\n512\n50\n10\n35\n16\n1008\n1015\n27371\n"
},
{
"input": "2\n576460752303423488 288230376151711743\n1 1\n",
"output": "60\n0\n"
},
{
"input": "4\n576460752303423488 288230376151711743\n576460752303423488 288230376151711742\n576460752303423487 288230376151711743\n576460752303423487 288230376151711744\n",
"output": "60\n60\n59\n59\n"
},
{
"input": "5\n576460752303423488 288230376151711743\n576460752303423488 288230376151711744\n576460752303423489 288230376151711745\n576460752303423488 576460752303423488\n576460752303423489 576460752303423489\n",
"output": "60\n59\n60\n59\n60\n"
},
{
"input": "1\n576460752303423488 288230376151711743\n",
"output": "60\n"
},
{
"input": "5\n576460752303423487 576460752303423487\n796131459065721 796131459065721\n777 777\n64 64\n3333333 3333333\n",
"output": "59\n50\n10\n6\n22\n"
},
{
"input": "2\n100000000000000007 1\n1000000000000000000 1\n",
"output": "100000000000000006\n999999999999999999\n"
},
{
"input": "1\n576460752303423487 576460752303423487\n",
"output": "59\n"
},
{
"input": "1\n18014398509481984 9007199254740992\n",
"output": "54\n"
},
{
"input": "1\n99999999999999999 1\n",
"output": "99999999999999998\n"
},
{
"input": "15\n576460752303423488 576460752303423488\n576460752303423487 576460752303423487\n576460752303423487 33333333333\n576460752303423489 576460752303423489\n576460752303423487 7777\n576460752303423487 7777\n1024 1024\n1024 2\n796131459065721 796131459065721\n777 777\n33333333333 33333333333\n33333 33333\n123141 123\n33333 33\n1231456 45\n",
"output": "59\n59\n17293857\n60\n74123794818506\n74123794818506\n10\n512\n50\n10\n35\n16\n1008\n1015\n27371\n"
}
] | code_contests | python | 0 | eaaf5d959fe53391d06797f073326264 |
Monocarp wrote down two numbers on a whiteboard. Both numbers follow a specific format: a positive integer x with p zeros appended to its end.
Now Monocarp asks you to compare these two numbers. Can you help him?
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of testcases.
The first line of each testcase contains two integers x_1 and p_1 (1 β€ x_1 β€ 10^6; 0 β€ p_1 β€ 10^6) β the description of the first number.
The second line of each testcase contains two integers x_2 and p_2 (1 β€ x_2 β€ 10^6; 0 β€ p_2 β€ 10^6) β the description of the second number.
Output
For each testcase print the result of the comparison of the given two numbers. If the first number is smaller than the second one, print '<'. If the first number is greater than the second one, print '>'. If they are equal, print '='.
Example
Input
5
2 1
19 0
10 2
100 1
1999 0
2 3
1 0
1 0
99 0
1 2
Output
>
=
<
=
<
Note
The comparisons in the example are: 20 > 19, 1000 = 1000, 1999 < 2000, 1 = 1, 99 < 100.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | #for line in sys.stdin.readlines()...line = line.strip()
#list(map(int,input().split(" ")))
#list(itertools.permutations([..,..,..]))
#list(itertools.combinations([..,..,..], 3)
import sys
import math
from math import log2 as lg
from collections import deque
import random
import heapq
import itertools
const = 1000000007
n=int(input())
for i in range(n):
first = input().split(" ")
second = input().split(" ")
f,s = len(first[0]) + int(first[1]), len(second[0]) + int(second[1])
if f<s: print("<")
elif f>s: print(">")
else:
found = False
for i in range(min(len(first[0]),len(second[0]))):
if int(first[0][i]) < int(second[0][i]):
print("<")
found = True
break
elif int(first[0][i]) > int(second[0][i]):
print(">")
found = True
break
if not found:
if len(first[0]) > len(second[0]):
if int(first[0][len(second[0]):]) != 0:
print(">")
found = True
elif len(second[0]) > len(first[0]):
if int(second[0][len(first[0]):]) != 0:
print("<")
found = True
if not found: print("=")
| python | code_algorithm | [
{
"input": "5\n2 1\n19 0\n10 2\n100 1\n1999 0\n2 3\n1 0\n1 0\n99 0\n1 2\n",
"output": ">\n=\n<\n=\n<\n"
},
{
"input": "1\n2000 0\n2 3\n",
"output": "=\n"
},
{
"input": "1\n1 6\n1000000 0\n",
"output": "=\n"
},
{
"input": "3\n1 3\n100 1\n2 3\n200 1\n6 3\n600 1\n",
"output": "=\n=\n=\n"
},
{
"input": "1\n1201 0\n12 2\n",
"output": ">\n"
},
{
"input": "1\n50 2\n500 1\n",
"output": "=\n"
},
{
"input": "1\n21 4\n210010 0\n",
"output": "<\n"
},
{
"input": "1\n12 2\n1205 0\n",
"output": "<\n"
},
{
"input": "1\n12 2\n1204 0\n",
"output": "<\n"
},
{
"input": "1\n100000 1\n1000000 0\n",
"output": "=\n"
},
{
"input": "23\n1 3\n100 1\n2 3\n200 1\n3 3\n300 1\n4 3\n400 1\n5 3\n500 1\n6 3\n600 1\n1 10\n1000000 4\n10 9\n1000000 4\n100 8\n1000000 4\n1000 7\n1000000 4\n10000 6\n1000000 4\n100000 5\n1000000 4\n1000000 4\n1000000 4\n1000000 4\n100000 5\n1000000 4\n10000 6\n1000000 4\n1000 7\n1000000 4\n100 8\n1000000 4\n10 9\n1000000 4\n1 10\n999999 1\n1000000 1\n1000000 1\n999999 1\n999998 1\n999999 1\n999999 1\n999998 1\n",
"output": "=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n<\n>\n<\n>\n"
},
{
"input": "21\n1 3\n100 1\n2 3\n200 1\n3 3\n300 1\n4 3\n400 1\n5 3\n500 1\n6 3\n600 1\n1 10\n1000000 4\n10 9\n1000000 4\n100 8\n1000000 4\n1000 7\n1000000 4\n10000 6\n1000000 4\n100000 5\n1000000 4\n1000000 4\n1000000 4\n1000000 4\n100000 5\n1000000 4\n10000 6\n1000000 4\n1000 7\n1000000 4\n100 8\n1000000 4\n10 9\n1000000 4\n1 10\n999999 1\n1000000 1\n1000000 1\n999999 1\n",
"output": "=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n=\n<\n>\n"
},
{
"input": "1\n1000000 10\n1000000 10\n",
"output": "=\n"
},
{
"input": "1\n1 5\n101 3\n",
"output": "<\n"
},
{
"input": "1\n105 0\n1 2\n",
"output": ">\n"
},
{
"input": "1\n402 2\n4 4\n",
"output": ">\n"
},
{
"input": "1\n1011 2\n1023 2\n",
"output": "<\n"
},
{
"input": "2\n4 0\n7 0\n5 0\n6 0\n",
"output": "<\n<\n"
},
{
"input": "1\n12 2\n1201 0\n",
"output": "<\n"
},
{
"input": "1\n12001 59\n12 62\n",
"output": ">\n"
},
{
"input": "7\n1 3\n100 1\n2 3\n200 1\n3 3\n300 1\n4 3\n400 1\n5 3\n500 1\n6 3\n600 1\n1 10\n1000000 4\n",
"output": "=\n=\n=\n=\n=\n=\n=\n"
},
{
"input": "1\n1 6\n10101 2\n",
"output": "<\n"
},
{
"input": "1\n999999 0\n999999 0\n",
"output": "=\n"
},
{
"input": "1\n999999 0\n1000000 0\n",
"output": "<\n"
},
{
"input": "5\n2 1\n19 0\n10 2\n100 1\n1999 0\n2 3\n1 0\n1 0\n99 0\n1 2\n",
"output": ">\n=\n<\n=\n<\n"
},
{
"input": "1\n4 4\n402 2\n",
"output": "<\n"
},
{
"input": "1\n12 5\n1204 3\n",
"output": "<\n"
},
{
"input": "1\n100000 1\n100 4\n",
"output": "=\n"
}
] | code_contests | python | 0.7 | e2bf854e57e1f6db0f88220b81e7c86d |
You are given a sequence a_1, a_2, ..., a_n consisting of n pairwise distinct positive integers.
Find \leftβ \frac n 2 \rightβ different pairs of integers x and y such that:
* x β y;
* x and y appear in a;
* x~mod~y doesn't appear in a.
Note that some x or y can belong to multiple pairs.
β x β denotes the floor function β the largest integer less than or equal to x. x~mod~y denotes the remainder from dividing x by y.
If there are multiple solutions, print any of them. It can be shown that at least one solution always exists.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of testcases.
The first line of each testcase contains a single integer n (2 β€ n β€ 2 β
10^5) β the length of the sequence.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6).
All numbers in the sequence are pairwise distinct. The sum of n over all testcases doesn't exceed 2 β
10^5.
Output
The answer for each testcase should contain \leftβ \frac n 2 \rightβ different pairs of integers x and y such that x β y, x and y appear in a and x~mod~y doesn't appear in a. Print the pairs one after another.
You can print the pairs in any order. However, the order of numbers in the pair should be exactly such that the first number is x and the second number is y. All pairs should be pairwise distinct.
If there are multiple solutions, print any of them.
Example
Input
4
2
1 4
4
2 8 3 4
5
3 8 5 9 7
6
2 7 5 3 4 8
Output
4 1
8 2
8 4
9 5
7 5
8 7
4 3
5 2
Note
In the first testcase there are only two pairs: (1, 4) and (4, 1). \leftβ \frac 2 2 \rightβ=1, so we have to find one pair. 1~mod~4=1, and 1 appears in a, so that pair is invalid. Thus, the only possible answer is a pair (4, 1).
In the second testcase, we chose pairs 8~mod~2=0 and 8~mod~4=0. 0 doesn't appear in a, so that answer is valid. There are multiple possible answers for that testcase.
In the third testcase, the chosen pairs are 9~mod~5=4 and 7~mod~5=2. Neither 4, nor 2, appears in a, so that answer is valid.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | for _ in range(int(input())):
n=int(input())
a=list(map(int, input().split()))
a.sort()
y=a[0]
for i in range(1,n//2 + 1):
print(a[i] , y)
| python | code_algorithm | [
{
"input": "4\n2\n1 4\n4\n2 8 3 4\n5\n3 8 5 9 7\n6\n2 7 5 3 4 8\n",
"output": "4 1\n3 2\n4 2\n5 3\n7 3\n3 2\n4 2\n5 2\n"
},
{
"input": "1\n5\n200005 200006 200007 200008 200009\n",
"output": "200006 200005\n200007 200005\n"
},
{
"input": "1\n2\n4 2\n",
"output": "4 2\n"
},
{
"input": "4\n2\n1 4\n4\n2 8 3 4\n5\n3 8 5 9 7\n6\n2 7 5 3 4 8\n",
"output": "4 1\n3 2\n4 2\n5 3\n7 3\n3 2\n4 2\n5 2\n"
},
{
"input": "1\n4\n10000 10001 10002 10003\n",
"output": "10001 10000\n10002 10000\n"
}
] | code_contests | python | 0 | 7a181447fbe88b957d382a337e51b4a8 |
Monocarp is playing yet another computer game. In this game, his character has to kill a dragon. The battle with the dragon lasts 100^{500} seconds, during which Monocarp attacks the dragon with a poisoned dagger. The i-th attack is performed at the beginning of the a_i-th second from the battle start. The dagger itself does not deal damage, but it applies a poison effect on the dragon, which deals 1 damage during each of the next k seconds (starting with the same second when the dragon was stabbed by the dagger). However, if the dragon has already been poisoned, then the dagger updates the poison effect (i.e. cancels the current poison effect and applies a new one).
For example, suppose k = 4, and Monocarp stabs the dragon during the seconds 2, 4 and 10. Then the poison effect is applied at the start of the 2-nd second and deals 1 damage during the 2-nd and 3-rd seconds; then, at the beginning of the 4-th second, the poison effect is reapplied, so it deals exactly 1 damage during the seconds 4, 5, 6 and 7; then, during the 10-th second, the poison effect is applied again, and it deals 1 damage during the seconds 10, 11, 12 and 13. In total, the dragon receives 10 damage.
Monocarp knows that the dragon has h hit points, and if he deals at least h damage to the dragon during the battle β he slays the dragon. Monocarp has not decided on the strength of the poison he will use during the battle, so he wants to find the minimum possible value of k (the number of seconds the poison effect lasts) that is enough to deal at least h damage to the dragon.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of the test case contains two integers n and h (1 β€ n β€ 100; 1 β€ h β€ 10^{18}) β the number of Monocarp's attacks and the amount of damage that needs to be dealt.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9; a_i < a_{i + 1}), where a_i is the second when the i-th attack is performed.
Output
For each test case, print a single integer β the minimum value of the parameter k, such that Monocarp will cause at least h damage to the dragon.
Example
Input
4
2 5
1 5
3 10
2 4 10
5 3
1 2 4 5 7
4 1000
3 25 64 1337
Output
3
4
1
470
Note
In the first example, for k=3, damage is dealt in seconds [1, 2, 3, 5, 6, 7].
In the second example, for k=4, damage is dealt in seconds [2, 3, 4, 5, 6, 7, 10, 11, 12, 13].
In the third example, for k=1, damage is dealt in seconds [1, 2, 4, 5, 7].
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import math
#s = input()
#n= (map(int, input().split()))
#(map(int, input().split()))
#a, b = (map(int, input().split()))
for i in range(0, int(input())):
n, h =(map(int, input().split()))
a = list(map(int, input().split()))
dist = list()
for j in range(0, len(a)-1):
dist.append(a[j+1]-a[j])
dist.sort()
if(len(dist)==0):
dist.append(a[0])
else:
dist.append(dist[len(dist)-1])
sum = 0
pred_sum = 0
flag = 0
k = 0
for j in range(0, len(a)):
sum = pred_sum+(len(a)-j)*dist[j]
if(sum>=h):
flag = 1
k = j
break
pred_sum += dist[j]
if(flag == 0):
if(len(dist)==1):
print(h)
else:
print(h-pred_sum+dist[len(dist)-1])
else:
sum = 0
sum = (h-pred_sum)//(len(a)-k)
if((h-pred_sum)%(len(a)-k)):
sum += 1
print(sum)
| python | code_algorithm | [
{
"input": "4\n2 5\n1 5\n3 10\n2 4 10\n5 3\n1 2 4 5 7\n4 1000\n3 25 64 1337\n",
"output": "3\n4\n1\n470\n"
},
{
"input": "1\n2 1000000000000000000\n1 1000000000\n",
"output": "999999999000000001\n"
},
{
"input": "1\n2 1000000000000000000\n1000000 1000000000\n",
"output": "999999999001000000\n"
},
{
"input": "1\n1 1000000000000000000\n1000000\n",
"output": "1000000000000000000\n"
},
{
"input": "1\n1 1000000000000000000\n1000000000\n",
"output": "1000000000000000000\n"
}
] | code_contests | python | 0 | c9f6ae263bdc8682251c0d4c7108f19a |
You are given an array a of n integers, and another integer k such that 2k β€ n.
You have to perform exactly k operations with this array. In one operation, you have to choose two elements of the array (let them be a_i and a_j; they can be equal or different, but their positions in the array must not be the same), remove them from the array, and add β (a_i)/(a_j) β to your score, where β x/y β is the maximum integer not exceeding x/y.
Initially, your score is 0. After you perform exactly k operations, you add all the remaining elements of the array to the score.
Calculate the minimum possible score you can get.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases.
Each test case consists of two lines. The first line contains two integers n and k (1 β€ n β€ 100; 0 β€ k β€ β n/2 β).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5).
Output
Print one integer β the minimum possible score you can get.
Example
Input
5
7 3
1 1 1 2 1 3 1
5 1
5 5 5 5 5
4 2
1 3 3 7
2 0
4 2
9 2
1 10 10 1 10 2 7 10 3
Output
2
16
0
6
16
Note
Let's consider the example test.
In the first test case, one way to obtain a score of 2 is the following one:
1. choose a_7 = 1 and a_4 = 2 for the operation; the score becomes 0 + β 1/2 β = 0, the array becomes [1, 1, 1, 1, 3];
2. choose a_1 = 1 and a_5 = 3 for the operation; the score becomes 0 + β 1/3 β = 0, the array becomes [1, 1, 1];
3. choose a_1 = 1 and a_2 = 1 for the operation; the score becomes 0 + β 1/1 β = 1, the array becomes [1];
4. add the remaining element 1 to the score, so the resulting score is 2.
In the second test case, no matter which operations you choose, the resulting score is 16.
In the third test case, one way to obtain a score of 0 is the following one:
1. choose a_1 = 1 and a_2 = 3 for the operation; the score becomes 0 + β 1/3 β = 0, the array becomes [3, 7];
2. choose a_1 = 3 and a_2 = 7 for the operation; the score becomes 0 + β 3/7 β = 0, the array becomes empty;
3. the array is empty, so the score doesn't change anymore.
In the fourth test case, no operations can be performed, so the score is the sum of the elements of the array: 4 + 2 = 6.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import math
import os
import sys
from io import BytesIO, IOBase
M = 1000000007
import random
import heapq
import threading
import bisect
import time
#sys.setrecursionlimit(2*(10**5))
from functools import *
from collections import *
from itertools import *
BUFSIZE = 8192
import array
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")
def print(*args, **kwargs):
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input
def out(var): sys.stdout.write(str(var)) # for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def fsep(): return map(float, inp().split())
def inpu(): return int(inp())
def build(a,b,st,arr,node):
if a==b:
st[node] = arr[a]
return arr[a]
mid = (a+b)//2
p = build(a,mid,st,arr,2*node+1)
q = build(mid+1,b,st,arr,2*node+2)
st[node] = p+q
return st[node]
def update(st,a,b,x,val,node):
if x<a or x>b:
return
st[node] = st[node] + val
if a!=b:
mid = (a+b)//2
update(st,a,mid,x,val,2*node+1)
update(st,mid+1,b,x,val,2*node+2)
def getsum(st,a,b,l,r,node):
if l>b or r<a:
return 0
if l<=a and r>=b:
return st[node]
mid = (a+b)//2
return getsum(st,a,mid,l,r,2*node+1) + getsum(st,mid+1,b,l,r,2*node+2)
def dfs2(curr,parent,start,end,cnt,res,d,arr):
res[cnt[0]] = arr[curr-1]
start[curr] = cnt[0]
cnt[0]+=1
for i in d[curr]:
if i!=parent:
dfs2(i,curr,start,end,cnt,res,d,arr)
cnt[0]+=1
res[cnt[0]] = arr[curr-1]
end[curr] = cnt[0]
"""-------------------Subtree queries problem on cses
def main():
t = 1
#t = inpu()
for _ in range(t):
n,q = sep()
arr = lis()
d = defaultdict(list)
for i in range(n-1):
a,b = sep()
d[a].append(b)
d[b].append(a)
res = [0]*(2*n)
start = defaultdict(int)
end = defaultdict(int)
cnt = [0]
dfs2(1,-1,start,end,cnt,res,d,arr)
print(res)
st=[0]*(4*(len(res)))
build(0,len(res)-1,st,res,0)
#print(st)
for i in range(q):
l = lis()
if l[0]==1:
p,q = start[l[1]],end[l[1]]
diff = l[2] - res[p]
res[p] = l[2]
diff2 = l[2] - res[q]
res[q] = l[2]
update(st,0,len(res)-1,p,diff,0)
update(st,0,len(res)-1,q,diff2,0)
else:
p,q = start[l[1]],end[l[1]]
print(getsum(st,0,len(res)-1,p,q,0)//2)
"""
"""def build2(a,b,st,arr,node,s):
if a==b:
s.add(arr[a-1])
st[node] = len(s)
return s
mid = (a+b)//2
p = build2(a,mid,st,arr,2*node+1,s)
q = build2(mid+1,b,st,arr,2*node+2,s)
#print(p,q)
p.union(q)
st[node] = len(p)
return p
def ans(a,b,st,l,r,node):
if l>b or r<a:
return 0
if l<=a and r>=b:
return st[node]
mid=(a+b)//2
return ans(a,mid,st,l,r,2*node+1)+ans(mid+1,b,st,l,r,2*node+2)"""
def main():
t=1
t=inpu()
for _ in range(t):
n,k = sep()
arr = lis()
arr.sort()
ans = sum(arr[:n-2*k])
p = arr[n-2*k:]
c = Counter(p)
max1 = 0
for i in c:
max1 = max(max1,c[i])
res = max(0,(2*max1-2*k)//2)
print(ans+res)
if __name__ == '__main__':
"""
threading.stack_size(2 * 10 ** 8)
threading.Thread(target=main).start()
"""
main()
| python | code_algorithm | [
{
"input": "5\n7 3\n1 1 1 2 1 3 1\n5 1\n5 5 5 5 5\n4 2\n1 3 3 7\n2 0\n4 2\n9 2\n1 10 10 1 10 2 7 10 3\n",
"output": "2\n16\n0\n6\n16\n"
},
{
"input": "1\n6 3\n4 4 5 5 6 6\n",
"output": "0\n"
},
{
"input": "1\n6 3\n1 1 1 1 2 2\n",
"output": "1\n"
},
{
"input": "1\n9 3\n1 1 1 2 2 2 3 3 3\n",
"output": "3\n"
},
{
"input": "1\n10 5\n3 3 3 4 4 4 5 5 5 1\n",
"output": "0\n"
},
{
"input": "1\n10 4\n1 1 1 2 3 3 3 3 4 5\n",
"output": "2\n"
},
{
"input": "1\n77 9\n9 8 10 5 8 20 17 19 14 16 7 6 15 16 6 13 10 13 14 12 5 6 12 14 12 8 13 12 17 8 12 17 2 11 8 18 17 2 13 2 1 19 9 8 10 20 8 12 15 1 3 15 6 19 4 11 12 8 3 2 1 20 15 14 6 10 11 1 5 11 10 6 20 6 14 16 6\n",
"output": "489\n"
},
{
"input": "1\n14 7\n1 1 1 1 1 1 1 2 2 2 2 2 3 3\n",
"output": "0\n"
},
{
"input": "1\n88 38\n9 143 135 72 74 12 120 27 108 183 18 24 163 122 140 155 166 188 197 14 134 66 188 161 79 100 40 55 161 118 121 18 72 141 136 31 39 187 2 98 16 118 120 196 127 66 135 186 49 16 17 50 107 192 50 23 62 110 83 124 197 127 185 129 131 182 45 147 174 115 89 109 177 7 70 43 75 5 157 93 59 26 176 105 32 89 65 6\n",
"output": "140\n"
},
{
"input": "5\n7 3\n1 1 1 2 1 3 1\n5 1\n5 5 5 5 5\n4 2\n1 3 3 7\n2 0\n4 2\n10 5\n3 3 3 4 4 4 5 5 5 1\n",
"output": "2\n16\n0\n6\n0\n"
},
{
"input": "1\n87 12\n10 5 11 7 16 16 8 11 14 11 4 4 3 7 16 12 2 3 3 13 1 7 11 16 4 7 11 5 3 15 20 5 12 1 8 8 13 19 3 7 14 3 4 10 18 13 20 1 4 8 10 12 4 7 15 2 18 13 6 2 1 2 10 13 4 5 18 7 1 10 6 2 19 17 5 11 10 12 14 6 16 20 11 12 10 3 6\n",
"output": "401\n"
},
{
"input": "1\n16 8\n1 1 2 2 3 3 3 3 3 3 3 3 3 4 5 5\n",
"output": "1\n"
},
{
"input": "1\n8 4\n1 1 3 3 3 3 5 6\n",
"output": "0\n"
},
{
"input": "1\n12 6\n1 1 1 8 8 8 8 8 8 10 10 10\n",
"output": "0\n"
},
{
"input": "5\n7 3\n1 1 1 2 1 3 1\n5 1\n5 5 5 5 5\n4 2\n1 3 3 7\n6 3\n2 2 3 3 4 4\n9 2\n1 10 10 1 10 2 7 10 3\n",
"output": "2\n16\n0\n0\n16\n"
},
{
"input": "1\n12 6\n1 1 1 1 1 2 2 2 2 3 3 3\n",
"output": "0\n"
},
{
"input": "1\n100 34\n2 6 5 4 4 4 5 7 6 6 1 2 5 1 4 4 4 5 5 5 2 3 5 6 6 1 1 3 7 5 4 4 1 3 4 6 4 2 7 6 5 4 6 1 1 2 4 6 4 2 2 6 6 3 5 3 4 5 3 4 3 2 1 7 7 5 4 6 3 1 6 7 6 2 2 2 4 7 3 2 6 1 1 3 1 5 6 4 4 7 2 6 6 5 4 4 3 5 2 4\n",
"output": "58\n"
},
{
"input": "1\n85 3\n43 17 34 67 14 7 80 48 36 95 48 68 86 4 17 46 51 87 41 90 100 56 57 42 9 8 27 23 16 14 99 22 13 98 89 24 77 59 15 94 25 81 4 71 24 43 97 88 97 10 6 8 3 86 28 61 100 98 76 85 32 18 79 66 51 44 28 99 87 56 80 93 89 76 7 57 96 39 46 25 4 67 6 51 39\n",
"output": "3753\n"
},
{
"input": "1\n6 3\n1 1 2 2 3 3\n",
"output": "0\n"
},
{
"input": "1\n6 3\n2 3 3 3 4 4\n",
"output": "0\n"
},
{
"input": "1\n6 3\n1 7 7 7 10 10\n",
"output": "0\n"
},
{
"input": "1\n10 3\n1 7 4 3 2 2 6 7 4 7\n",
"output": "8\n"
},
{
"input": "1\n100 17\n5 1 1 7 3 2 5 1 1 3 4 5 5 5 7 5 7 5 1 7 6 6 1 3 6 3 4 5 4 6 3 1 7 6 2 3 2 5 3 3 6 5 5 6 5 7 5 3 5 4 7 3 5 1 7 3 4 4 3 3 3 5 1 1 1 7 1 3 2 3 2 7 2 1 6 5 6 6 1 5 4 6 1 7 6 7 6 1 1 1 5 3 5 7 4 6 4 6 3 2\n",
"output": "192\n"
},
{
"input": "1\n7 3\n1 1 1 2 2 3 3\n",
"output": "1\n"
},
{
"input": "1\n9 2\n1 10 10 1 10 2 7 10 191919\n",
"output": "22\n"
},
{
"input": "1\n6 3\n6 6 5 5 1 1\n",
"output": "0\n"
},
{
"input": "1\n8 3\n1 1 6 6 6 6 2 2\n",
"output": "3\n"
},
{
"input": "1\n91 9\n4 3 19 8 3 6 16 5 17 11 9 13 15 8 1 3 20 2 19 7 14 1 6 4 8 18 13 8 16 11 3 12 3 3 12 17 15 9 5 15 12 11 7 20 19 3 4 14 10 9 7 16 12 10 3 6 15 17 4 8 15 2 10 13 11 15 7 20 1 10 9 2 10 15 7 1 6 16 2 1 17 10 19 10 7 18 3 5 10 12 15\n",
"output": "569\n"
},
{
"input": "1\n67 22\n83 41 94 76 47 33 20 51 98 48 92 72 24 51 15 17 35 70 83 95 79 65 80 17 72 7 97 14 74 98 55 63 45 79 42 92 85 24 84 30 29 77 64 68 68 71 89 18 82 68 20 91 84 52 45 23 65 95 83 84 18 81 63 11 89 41 69\n",
"output": "616\n"
},
{
"input": "1\n4 2\n5 6 5 5\n",
"output": "1\n"
},
{
"input": "1\n6 3\n3 3 4 4 7 7\n",
"output": "0\n"
},
{
"input": "10\n7 3\n2 2 2 2 3 3 3\n7 3\n2 2 3 3 3 4 4\n7 3\n2 2 2 2 2 2 2\n7 3\n2 2 3 3 3 4 4\n7 3\n2 2 2 3 3 3 3\n7 3\n2 2 3 3 3 3 4\n7 3\n2 2 2 2 2 3 3\n7 3\n2 2 3 3 3 3 3\n7 3\n2 2 2 2 2 2 3\n7 3\n2 2 2 3 3 3 4\n",
"output": "2\n2\n5\n2\n3\n3\n3\n4\n4\n2\n"
},
{
"input": "1\n7 3\n2 2 2 2 7 8 2\n",
"output": "3\n"
},
{
"input": "1\n13 6\n1 2 2 2 2 3 3 3 3 4 4 4 4\n",
"output": "1\n"
},
{
"input": "1\n10 3\n1 1 1 1 1 1 2 2 3 3\n",
"output": "4\n"
},
{
"input": "1\n10 3\n2 15 14 13 12 15 18 2 9 15\n",
"output": "25\n"
},
{
"input": "1\n8 4\n1 2 2 2 2 3 3 3\n",
"output": "0\n"
}
] | code_contests | python | 0 | c843f5a976f762c8c026a79e8501c443 |
You are given two positive integers x and y. You can perform the following operation with x: write it in its binary form without leading zeros, add 0 or 1 to the right of it, reverse the binary form and turn it into a decimal number which is assigned as the new value of x.
For example:
* 34 can be turned into 81 via one operation: the binary form of 34 is 100010, if you add 1, reverse it and remove leading zeros, you will get 1010001, which is the binary form of 81.
* 34 can be turned into 17 via one operation: the binary form of 34 is 100010, if you add 0, reverse it and remove leading zeros, you will get 10001, which is the binary form of 17.
* 81 can be turned into 69 via one operation: the binary form of 81 is 1010001, if you add 0, reverse it and remove leading zeros, you will get 1000101, which is the binary form of 69.
* 34 can be turned into 69 via two operations: first you turn 34 into 81 and then 81 into 69.
Your task is to find out whether x can be turned into y after a certain number of operations (possibly zero).
Input
The only line of the input contains two integers x and y (1 β€ x, y β€ 10^{18}).
Output
Print YES if you can make x equal to y and NO if you can't.
Examples
Input
3 3
Output
YES
Input
7 4
Output
NO
Input
2 8
Output
NO
Input
34 69
Output
YES
Input
8935891487501725 71487131900013807
Output
YES
Note
In the first example, you don't even need to do anything.
The fourth example is described in the statement.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | #import io,os
#input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque
def main(t):
x, y = map(int,input().split())
sx, sy = bin(x)[2:],bin(y)[2:]
dic = {}
queue = deque()
queue.append(sx)
dic[sx] = 1
i = len(sx)-1
while i>=0 and sx[i]=='0': i -= 1
queue.append(sx[:i+1])
dic[sx[:i+1]] = 1
# print(dic)
while queue:
curr = queue.popleft()
nextcurr1 = curr[::-1]
i = 0
while nextcurr1[i]=='0': i += 1
nextcurr1 = nextcurr1[i:]
if nextcurr1 not in dic:
dic[nextcurr1] = 1
queue.append(nextcurr1)
nextcurr2 = (curr + '1')[::-1]
if nextcurr2 not in dic:
dic[nextcurr2] = 1
if len(nextcurr2) <= len(sy): queue.append(nextcurr2)
# print(dic)
if sy in dic:
print("YES")
else:
print("NO")
T = 1 #int(input())
t = 1
while t<=T:
main(t)
t += 1
| python | code_algorithm | [
{
"input": "2 8\n",
"output": "NO\n"
},
{
"input": "7 4\n",
"output": "NO\n"
},
{
"input": "8935891487501725 71487131900013807\n",
"output": "YES\n"
},
{
"input": "3 3\n",
"output": "YES\n"
},
{
"input": "34 69\n",
"output": "YES\n"
},
{
"input": "470060730774588924 727173667167621133\n",
"output": "NO\n"
},
{
"input": "23654897456254158 36584562123658749\n",
"output": "NO\n"
},
{
"input": "100 100\n",
"output": "YES\n"
},
{
"input": "1048576 1048576\n",
"output": "YES\n"
},
{
"input": "3165137368662540 34690334760256012\n",
"output": "NO\n"
},
{
"input": "2963671906804332 23709375254434663\n",
"output": "YES\n"
},
{
"input": "2 2\n",
"output": "YES\n"
},
{
"input": "469234491891472796 290944711594072288\n",
"output": "NO\n"
},
{
"input": "10 5\n",
"output": "YES\n"
},
{
"input": "662695912942035259 813128064161\n",
"output": "NO\n"
},
{
"input": "10 576460752303423487\n",
"output": "NO\n"
},
{
"input": "9762130370617853 135862919936991741\n",
"output": "YES\n"
},
{
"input": "407 113\n",
"output": "NO\n"
},
{
"input": "88888888888888888 99999999999999999\n",
"output": "NO\n"
},
{
"input": "20 607\n",
"output": "YES\n"
},
{
"input": "968503512949840 70798422886785671\n",
"output": "YES\n"
},
{
"input": "4997 4748\n",
"output": "NO\n"
},
{
"input": "20 20\n",
"output": "YES\n"
},
{
"input": "11 27\n",
"output": "YES\n"
},
{
"input": "2651 1994\n",
"output": "NO\n"
},
{
"input": "114514 114514\n",
"output": "YES\n"
},
{
"input": "642061520256 807582787377560508\n",
"output": "NO\n"
},
{
"input": "100 403\n",
"output": "YES\n"
},
{
"input": "95 427\n",
"output": "NO\n"
},
{
"input": "4 4\n",
"output": "YES\n"
},
{
"input": "98376470102098 8668311108715159\n",
"output": "YES\n"
},
{
"input": "2 6\n",
"output": "NO\n"
},
{
"input": "8935891487501725 71986286270688669\n",
"output": "YES\n"
},
{
"input": "6 1\n",
"output": "NO\n"
},
{
"input": "90 180\n",
"output": "NO\n"
},
{
"input": "5 55\n",
"output": "YES\n"
},
{
"input": "8 1\n",
"output": "YES\n"
},
{
"input": "11 13\n",
"output": "YES\n"
},
{
"input": "4529535624500812 36236284996006503\n",
"output": "YES\n"
},
{
"input": "1717 879\n",
"output": "NO\n"
},
{
"input": "54043195528445952 3\n",
"output": "YES\n"
},
{
"input": "32 457\n",
"output": "NO\n"
},
{
"input": "12312 12312\n",
"output": "YES\n"
},
{
"input": "1579442997370991 12635543978967935\n",
"output": "YES\n"
},
{
"input": "41 119\n",
"output": "NO\n"
},
{
"input": "272137586985970 17939699391684503\n",
"output": "YES\n"
},
{
"input": "8 8\n",
"output": "YES\n"
},
{
"input": "32 32\n",
"output": "YES\n"
},
{
"input": "2651 21215\n",
"output": "YES\n"
},
{
"input": "592255623895602343 956231061252005500\n",
"output": "NO\n"
},
{
"input": "22 107\n",
"output": "NO\n"
},
{
"input": "1 576460752303423487\n",
"output": "YES\n"
},
{
"input": "1 67108863\n",
"output": "YES\n"
},
{
"input": "470886973952672348 163402627036137273\n",
"output": "NO\n"
},
{
"input": "3991380546745964 35516577938339436\n",
"output": "NO\n"
},
{
"input": "435 1515\n",
"output": "NO\n"
},
{
"input": "4997 39983\n",
"output": "YES\n"
},
{
"input": "18014398509481984 54043195528445952\n",
"output": "NO\n"
},
{
"input": "6261 668\n",
"output": "NO\n"
},
{
"input": "45454 45454\n",
"output": "YES\n"
},
{
"input": "1 1125899906842623\n",
"output": "YES\n"
},
{
"input": "3145302420099927 25162419360799423\n",
"output": "YES\n"
},
{
"input": "26 47\n",
"output": "YES\n"
},
{
"input": "25 19\n",
"output": "YES\n"
},
{
"input": "999999999999999999 864691128455135231\n",
"output": "NO\n"
},
{
"input": "10 10\n",
"output": "YES\n"
},
{
"input": "1 2147483647\n",
"output": "YES\n"
},
{
"input": "4 5\n",
"output": "NO\n"
},
{
"input": "44 44\n",
"output": "YES\n"
},
{
"input": "22 11\n",
"output": "YES\n"
},
{
"input": "985 1653\n",
"output": "NO\n"
},
{
"input": "794746691033263 69408368311453055\n",
"output": "YES\n"
},
{
"input": "100 201\n",
"output": "YES\n"
},
{
"input": "10 11\n",
"output": "YES\n"
},
{
"input": "13 11\n",
"output": "YES\n"
},
{
"input": "753 271\n",
"output": "NO\n"
},
{
"input": "16 16\n",
"output": "YES\n"
},
{
"input": "2 1\n",
"output": "YES\n"
},
{
"input": "942220828365 412458936303\n",
"output": "NO\n"
},
{
"input": "407 3263\n",
"output": "YES\n"
},
{
"input": "5 10\n",
"output": "NO\n"
},
{
"input": "34 17\n",
"output": "YES\n"
},
{
"input": "44 29\n",
"output": "YES\n"
},
{
"input": "42 2193386110975\n",
"output": "YES\n"
},
{
"input": "753 6031\n",
"output": "YES\n"
}
] | code_contests | python | 0 | ee919416f7547b59f73998f57b6dcd6e |
Polycarp likes squares and cubes of positive integers. Here is the beginning of the sequence of numbers he likes: 1, 4, 8, 9, ....
For a given number n, count the number of integers from 1 to n that Polycarp likes. In other words, find the number of such x that x is a square of a positive integer number or a cube of a positive integer number (or both a square and a cube simultaneously).
Input
The first line contains an integer t (1 β€ t β€ 20) β the number of test cases.
Then t lines contain the test cases, one per line. Each of the lines contains one integer n (1 β€ n β€ 10^9).
Output
For each test case, print the answer you are looking for β the number of integers from 1 to n that Polycarp likes.
Example
Input
6
10
1
25
1000000000
999999999
500000000
Output
4
1
6
32591
32590
23125
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` |
for i in range(int(input())):
n=int(input())
count=[0]
if n==1:
print(1)
else:
for i in range(1,n):
if i*i>n:
break
if i*i<=n:
count.append(i*i)
if i*i*i<=n:
count.append(i*i*i)
else:
pass
print(len(set(count))-1)
| python | code_algorithm | [
{
"input": "6\n10\n1\n25\n1000000000\n999999999\n500000000\n",
"output": "4\n1\n6\n32591\n32590\n23125\n"
},
{
"input": "2\n64\n15625\n",
"output": "10\n145\n"
},
{
"input": "8\n64000000\n85766121\n113379904\n148035889\n191102976\n244140625\n594823321\n887503681\n",
"output": "8380\n9681\n11110\n12673\n14376\n16225\n25201\n30721\n"
},
{
"input": "2\n15625\n46656\n",
"output": "145\n246\n"
},
{
"input": "2\n1000000\n4096\n",
"output": "1090\n76\n"
},
{
"input": "1\n4095\n",
"output": "75\n"
},
{
"input": "1\n4096\n",
"output": "76\n"
},
{
"input": "20\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n",
"output": "2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n"
},
{
"input": "5\n64000000\n85766121\n113379904\n148035889\n8000\n",
"output": "8380\n9681\n11110\n12673\n105\n"
},
{
"input": "12\n64\n4096\n15625\n46656\n117649\n262144\n531441\n1000000\n1771561\n2985984\n4826809\n7529536\n",
"output": "10\n76\n145\n246\n385\n568\n801\n1090\n1441\n1860\n2353\n2926\n"
},
{
"input": "20\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n6012\n975\n3916\n4805\n7952\n3301\n647\n3799\n8657\n122\n",
"output": "1\n1\n1\n2\n2\n2\n2\n3\n4\n4\n91\n37\n74\n81\n104\n68\n31\n73\n109\n13\n"
},
{
"input": "1\n216\n",
"output": "18\n"
},
{
"input": "5\n62\n63\n64\n4096\n66\n",
"output": "9\n9\n10\n76\n10\n"
},
{
"input": "1\n125\n",
"output": "14\n"
},
{
"input": "1\n4090\n",
"output": "75\n"
},
{
"input": "1\n1000000\n",
"output": "1090\n"
},
{
"input": "18\n2515456\n2571353\n2628072\n2685619\n2744000\n2803221\n2924207\n3048625\n3112136\n3176523\n3307949\n3442951\n3511808\n3581577\n3652264\n3796416\n3869893\n3944312\n",
"output": "1711\n1729\n1748\n1766\n1785\n1804\n1842\n1879\n1898\n1917\n1955\n1994\n2013\n2033\n2053\n2092\n2112\n2132\n"
},
{
"input": "2\n5000\n8000\n",
"output": "83\n105\n"
},
{
"input": "5\n2985983\n4826807\n4826808\n7529533\n7529534\n",
"output": "1859\n2352\n2352\n2925\n2925\n"
},
{
"input": "1\n59319\n",
"output": "276\n"
},
{
"input": "1\n26\n",
"output": "6\n"
},
{
"input": "3\n1000\n8000\n1000000\n",
"output": "38\n105\n1090\n"
},
{
"input": "4\n4096\n15625\n117649\n262144\n",
"output": "76\n145\n385\n568\n"
},
{
"input": "10\n728999999\n729000001\n594823320\n594823322\n481890303\n308915775\n387420488\n191102975\n148035888\n113379903\n",
"output": "27869\n27870\n25200\n25201\n22707\n18225\n20384\n14375\n12672\n11109\n"
},
{
"input": "4\n64\n729\n728\n4096\n",
"output": "10\n33\n32\n76\n"
},
{
"input": "10\n3307949\n3375000\n3442951\n3511808\n3581577\n3652264\n3723875\n3796416\n3869893\n3944312\n",
"output": "1955\n1975\n1994\n2013\n2033\n2053\n2072\n2092\n2112\n2132\n"
},
{
"input": "1\n16777216\n",
"output": "4336\n"
},
{
"input": "7\n728\n1000\n10000\n100000\n1000000\n10000000\n100000000\n",
"output": "32\n38\n117\n356\n1090\n3363\n10443\n"
},
{
"input": "15\n7762392\n7880599\n8000000\n8120601\n8242408\n8365427\n8489664\n8615125\n8741816\n8998912\n9129329\n9261000\n9393931\n9663597\n9938375\n",
"output": "2970\n2992\n3014\n3036\n3058\n3081\n3103\n3126\n3148\n3193\n3216\n3239\n3261\n3307\n3353\n"
},
{
"input": "6\n887503681\n887503680\n887503682\n999887640\n999887641\n999887642\n",
"output": "30721\n30720\n30721\n32588\n32589\n32589\n"
},
{
"input": "1\n991026973\n",
"output": "32446\n"
},
{
"input": "1\n481890304\n",
"output": "22708\n"
},
{
"input": "1\n8000\n",
"output": "105\n"
},
{
"input": "20\n887503680\n887503679\n887503678\n887503677\n887503676\n887503675\n887503674\n887503673\n887503672\n887503671\n887503670\n887503669\n887503668\n887503667\n887503666\n887503665\n887503664\n887503663\n887503662\n887503661\n",
"output": "30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n30720\n"
},
{
"input": "1\n997002999\n",
"output": "32543\n"
},
{
"input": "2\n1000\n999\n",
"output": "38\n37\n"
},
{
"input": "10\n1\n64\n729\n4096\n15625\n46656\n117649\n262144\n531441\n1000000\n",
"output": "1\n10\n33\n76\n145\n246\n385\n568\n801\n1090\n"
},
{
"input": "10\n64000000\n32324\n4645758\n148035889\n382748428\n33627363\n36273373\n75675757\n9897\n34536\n",
"output": "8380\n205\n2309\n12673\n20263\n6103\n6335\n9101\n116\n212\n"
},
{
"input": "1\n46142\n",
"output": "244\n"
},
{
"input": "20\n117649\n262144\n531441\n1000000\n1771561\n7529536\n11390625\n24137569\n34012224\n85766121\n113379904\n148035889\n191102976\n244140625\n308915776\n387420489\n481890304\n594823321\n729000000\n887503681\n",
"output": "385\n568\n801\n1090\n1441\n2926\n3585\n5185\n6138\n9681\n11110\n12673\n14376\n16225\n18226\n20385\n22708\n25201\n27870\n30721\n"
},
{
"input": "9\n8489664\n8615125\n8741816\n8998912\n9129329\n9261000\n9393931\n9663597\n9938375\n",
"output": "3103\n3126\n3148\n3193\n3216\n3239\n3261\n3307\n3353\n"
},
{
"input": "1\n49\n",
"output": "9\n"
},
{
"input": "2\n49\n676\n",
"output": "9\n32\n"
},
{
"input": "4\n117649\n262144\n1000000\n1771561\n",
"output": "385\n568\n1090\n1441\n"
},
{
"input": "3\n64\n15625\n1000000\n",
"output": "10\n145\n1090\n"
},
{
"input": "3\n15625\n97336\n195112\n",
"output": "145\n351\n492\n"
},
{
"input": "5\n4657463\n4741632\n4913000\n5000211\n5088448\n",
"output": "2313\n2333\n2373\n2394\n2414\n"
},
{
"input": "20\n125\n216\n343\n512\n1000\n1331\n1728\n2197\n2744\n3375\n4913\n5832\n6859\n8000\n9261\n10648\n12167\n13824\n17576\n19683\n",
"output": "14\n18\n23\n28\n38\n44\n50\n56\n63\n70\n83\n90\n97\n105\n113\n121\n129\n137\n153\n162\n"
},
{
"input": "1\n262144\n",
"output": "568\n"
},
{
"input": "1\n134217728\n",
"output": "12075\n"
},
{
"input": "17\n7301384\n7414875\n7762392\n7880599\n8000000\n8120601\n8242408\n8365427\n8489664\n8615125\n8741816\n8998912\n9129329\n9261000\n9393931\n9663597\n9938375\n",
"output": "2883\n2905\n2970\n2992\n3014\n3036\n3058\n3081\n3103\n3126\n3148\n3193\n3216\n3239\n3261\n3307\n3353\n"
},
{
"input": "6\n4096\n15625\n46656\n117649\n262144\n531441\n",
"output": "76\n145\n246\n385\n568\n801\n"
},
{
"input": "1\n46655\n",
"output": "245\n"
},
{
"input": "1\n34012224\n",
"output": "6138\n"
},
{
"input": "7\n244140625\n308915776\n387420489\n481890304\n594823321\n729000000\n887503681\n",
"output": "16225\n18226\n20385\n22708\n25201\n27870\n30721\n"
},
{
"input": "9\n4096\n15625\n46656\n117649\n262144\n531441\n1000000\n1771561\n2985984\n",
"output": "76\n145\n246\n385\n568\n801\n1090\n1441\n1860\n"
},
{
"input": "1\n308915776\n",
"output": "18226\n"
},
{
"input": "5\n720\n721\n722\n723\n724\n",
"output": "32\n32\n32\n32\n32\n"
},
{
"input": "2\n4096\n720\n",
"output": "76\n32\n"
},
{
"input": "1\n42144192\n",
"output": "6821\n"
},
{
"input": "20\n1000000000\n999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n999999991\n999999990\n999999989\n999999988\n999999987\n999999986\n999999985\n999999984\n999999983\n999999982\n999999981\n",
"output": "32591\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n"
},
{
"input": "8\n728\n1000\n4095\n10000\n100000\n1000000\n10000000\n100000000\n",
"output": "32\n38\n75\n117\n356\n1090\n3363\n10443\n"
},
{
"input": "9\n728\n1000000\n35\n144\n4095\n10000\n100000\n10000000\n100000000\n",
"output": "32\n1090\n7\n15\n75\n117\n356\n3363\n10443\n"
},
{
"input": "3\n24389\n31329\n21025\n",
"output": "180\n203\n167\n"
},
{
"input": "2\n8000\n1000000\n",
"output": "105\n1090\n"
},
{
"input": "5\n1\n64\n729\n4096\n15625\n",
"output": "1\n10\n33\n76\n145\n"
},
{
"input": "1\n7529536\n",
"output": "2926\n"
},
{
"input": "1\n387420489\n",
"output": "20385\n"
},
{
"input": "8\n117649\n262144\n531441\n1000000\n1771561\n2985984\n4826809\n7529536\n",
"output": "385\n568\n801\n1090\n1441\n1860\n2353\n2926\n"
},
{
"input": "1\n117649\n",
"output": "385\n"
},
{
"input": "12\n64\n125\n216\n343\n512\n729\n1000\n1331\n1728\n2197\n2744\n3375\n",
"output": "10\n14\n18\n23\n28\n33\n38\n44\n50\n56\n63\n70\n"
},
{
"input": "1\n4913\n",
"output": "83\n"
},
{
"input": "20\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n999950883\n",
"output": "32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n32589\n"
},
{
"input": "2\n1000000\n9025699\n",
"output": "1090\n3198\n"
},
{
"input": "1\n64\n",
"output": "10\n"
},
{
"input": "2\n4096\n15625\n",
"output": "76\n145\n"
},
{
"input": "11\n4096\n15625\n46656\n117649\n262144\n531441\n1000000\n1771561\n2985984\n4826809\n7529536\n",
"output": "76\n145\n246\n385\n568\n801\n1090\n1441\n1860\n2353\n2926\n"
},
{
"input": "1\n728\n",
"output": "32\n"
},
{
"input": "20\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"output": "32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n32591\n"
},
{
"input": "20\n4096\n15625\n46656\n117649\n262144\n531441\n1000000\n1771561\n2985984\n4826809\n7529536\n11390625\n16777216\n24137569\n34012224\n47045881\n64000000\n85766121\n113379904\n148035889\n",
"output": "76\n145\n246\n385\n568\n801\n1090\n1441\n1860\n2353\n2926\n3585\n4336\n5185\n6138\n7201\n8380\n9681\n11110\n12673\n"
},
{
"input": "3\n887503681\n887503680\n887503682\n",
"output": "30721\n30720\n30721\n"
},
{
"input": "5\n125\n216\n343\n512\n729\n",
"output": "14\n18\n23\n28\n33\n"
},
{
"input": "20\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"output": "32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n32590\n"
},
{
"input": "2\n8000\n5000\n",
"output": "105\n83\n"
},
{
"input": "1\n1771561\n",
"output": "1441\n"
},
{
"input": "13\n64\n4096\n15625\n46656\n117649\n262144\n531441\n1000000\n1771561\n2985984\n4826809\n1000000000\n7529536\n",
"output": "10\n76\n145\n246\n385\n568\n801\n1090\n1441\n1860\n2353\n32591\n2926\n"
},
{
"input": "7\n887503681\n887503680\n887503682\n999887640\n999887641\n55240747\n3566977\n",
"output": "30721\n30720\n30721\n32588\n32589\n7793\n2028\n"
},
{
"input": "7\n728999999\n729000001\n594823320\n594823322\n481890303\n308915775\n387420488\n",
"output": "27869\n27870\n25200\n25201\n22707\n18225\n20384\n"
},
{
"input": "1\n1000\n",
"output": "38\n"
},
{
"input": "7\n4096\n64\n1000000000\n15625\n46656\n262144\n2985984\n",
"output": "76\n10\n32591\n145\n246\n568\n1860\n"
},
{
"input": "3\n4096\n64\n1000000000\n",
"output": "76\n10\n32591\n"
},
{
"input": "6\n64\n729\n4096\n15625\n46656\n117649\n",
"output": "10\n33\n76\n145\n246\n385\n"
},
{
"input": "2\n1000\n1000000\n",
"output": "38\n1090\n"
},
{
"input": "1\n15625\n",
"output": "145\n"
},
{
"input": "1\n11390625\n",
"output": "3585\n"
},
{
"input": "1\n728999999\n",
"output": "27869\n"
},
{
"input": "16\n8000000\n8120601\n8242408\n8365427\n8489664\n8615125\n8741816\n8869743\n8998912\n9129329\n9261000\n9393931\n9528128\n9663597\n9800344\n9938375\n",
"output": "3014\n3036\n3058\n3081\n3103\n3126\n3148\n3171\n3193\n3216\n3239\n3261\n3284\n3307\n3330\n3353\n"
},
{
"input": "5\n117649\n262144\n531441\n1000000\n1771561\n",
"output": "385\n568\n801\n1090\n1441\n"
},
{
"input": "3\n8000\n1000\n100000000\n",
"output": "105\n38\n10443\n"
},
{
"input": "13\n4096\n64\n1000000000\n15625\n46656\n262144\n2985984\n4826807\n7529533\n7529534\n7529535\n720\n4095\n",
"output": "76\n10\n32591\n145\n246\n568\n1860\n2352\n2925\n2925\n2925\n32\n75\n"
},
{
"input": "5\n8000\n64000000\n85766121\n113379904\n148035889\n",
"output": "105\n8380\n9681\n11110\n12673\n"
},
{
"input": "2\n4096\n64\n",
"output": "76\n10\n"
},
{
"input": "1\n24137569\n",
"output": "5185\n"
},
{
"input": "5\n15625\n97336\n195112\n205379\n274625\n",
"output": "145\n351\n492\n505\n581\n"
},
{
"input": "1\n238144\n",
"output": "542\n"
},
{
"input": "1\n531441\n",
"output": "801\n"
},
{
"input": "5\n62\n63\n64\n65\n66\n",
"output": "9\n9\n10\n10\n10\n"
},
{
"input": "1\n729\n",
"output": "33\n"
},
{
"input": "3\n5000\n8000\n1000000\n",
"output": "83\n105\n1090\n"
},
{
"input": "3\n64\n729\n728\n",
"output": "10\n33\n32\n"
},
{
"input": "1\n46656\n",
"output": "246\n"
},
{
"input": "1\n887483586\n",
"output": "30720\n"
},
{
"input": "4\n481890304\n594823321\n729000000\n887503681\n",
"output": "22708\n25201\n27870\n30721\n"
},
{
"input": "5\n9261000\n9393931\n9663597\n9800344\n9938375\n",
"output": "3239\n3261\n3307\n3330\n3353\n"
},
{
"input": "1\n65\n",
"output": "10\n"
},
{
"input": "4\n64000000\n85766121\n113379904\n148035889\n",
"output": "8380\n9681\n11110\n12673\n"
},
{
"input": "5\n125\n216\n2197\n2744\n3375\n",
"output": "14\n18\n56\n63\n70\n"
},
{
"input": "1\n720\n",
"output": "32\n"
},
{
"input": "1\n64000000\n",
"output": "8380\n"
},
{
"input": "5\n49\n50\n675\n676\n677\n",
"output": "9\n9\n31\n32\n32\n"
},
{
"input": "1\n97336\n",
"output": "351\n"
},
{
"input": "6\n64\n729\n4096\n117649\n262144\n531441\n",
"output": "10\n33\n76\n385\n568\n801\n"
},
{
"input": "11\n4096\n64\n1000000000\n15625\n46656\n262144\n2985984\n4826807\n7529533\n7529534\n7529535\n",
"output": "76\n10\n32591\n145\n246\n568\n1860\n2352\n2925\n2925\n2925\n"
},
{
"input": "3\n8000\n1000\n1000000\n",
"output": "105\n38\n1090\n"
},
{
"input": "10\n5050\n30404\n12345\n98765432\n1234564\n64\n456\n1\n23\n123\n",
"output": "84\n200\n130\n10379\n1208\n10\n26\n1\n5\n13\n"
},
{
"input": "1\n887503681\n",
"output": "30721\n"
},
{
"input": "2\n15625\n4096\n",
"output": "145\n76\n"
},
{
"input": "1\n5000\n",
"output": "83\n"
},
{
"input": "20\n50653\n54872\n59319\n64000\n68921\n74088\n79507\n85184\n91125\n97336\n103823\n110592\n125000\n132651\n140608\n148877\n157464\n166375\n175616\n185193\n",
"output": "256\n266\n276\n286\n297\n308\n318\n329\n340\n351\n363\n374\n396\n408\n419\n431\n443\n455\n468\n480\n"
},
{
"input": "7\n64000000\n85766121\n113379904\n148035889\n191102976\n244140625\n594823321\n",
"output": "8380\n9681\n11110\n12673\n14376\n16225\n25201\n"
},
{
"input": "2\n42144192\n887503681\n",
"output": "6821\n30721\n"
},
{
"input": "7\n100\n1000\n10000\n100000\n1000000\n10000000\n100000000\n",
"output": "12\n38\n117\n356\n1090\n3363\n10443\n"
},
{
"input": "1\n1729\n",
"output": "50\n"
},
{
"input": "5\n10000\n100000\n1000000\n10000000\n100000000\n",
"output": "117\n356\n1090\n3363\n10443\n"
},
{
"input": "18\n54872\n59319\n64000\n68921\n74088\n79507\n85184\n91125\n97336\n103823\n110592\n125000\n132651\n140608\n148877\n157464\n166375\n175616\n",
"output": "266\n276\n286\n297\n308\n318\n329\n340\n351\n363\n374\n396\n408\n419\n431\n443\n455\n468\n"
},
{
"input": "1\n887503149\n",
"output": "30720\n"
},
{
"input": "1\n729000000\n",
"output": "27870\n"
},
{
"input": "1\n512\n",
"output": "28\n"
}
] | code_contests | python | 0 | 6c9f898ead11fb33b5717e51cc446587 |
You are given a permutation p of n elements. A permutation of n elements is an array of length n containing each integer from 1 to n exactly once. For example, [1, 2, 3] and [4, 3, 5, 1, 2] are permutations, but [1, 2, 4] and [4, 3, 2, 1, 2] are not permutations. You should perform q queries.
There are two types of queries:
* 1 x y β swap p_x and p_y.
* 2 i k β print the number that i will become if we assign i = p_i k times.
Input
The first line contains two integers n and q (1 β€ n, q β€ 10^5).
The second line contains n integers p_1, p_2, ..., p_n.
Each of the next q lines contains three integers. The first integer is t (1 β€ t β€ 2) β type of query. If t = 1, then the next two integers are x and y (1 β€ x, y β€ n; x β y) β first-type query. If t = 2, then the next two integers are i and k (1 β€ i, k β€ n) β second-type query.
It is guaranteed that there is at least one second-type query.
Output
For every second-type query, print one integer in a new line β answer to this query.
Examples
Input
5 4
5 3 4 2 1
2 3 1
2 1 2
1 1 3
2 1 2
Output
4
1
2
Input
5 9
2 3 5 1 4
2 3 5
2 5 5
2 5 1
2 5 3
2 5 4
1 5 4
2 5 3
2 2 5
2 5 1
Output
3
5
4
2
3
3
3
1
Note
In the first example p = \{5, 3, 4, 2, 1\}.
The first query is to print p_3. The answer is 4.
The second query is to print p_{p_1}. The answer is 1.
The third query is to swap p_1 and p_3. Now p = \{4, 3, 5, 2, 1\}.
The fourth query is to print p_{p_1}. The answer is 2.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` |
def main():
n, q = readIntArr()
p = readIntArr()
for i in range(n):
p[i] -= 1 # make 0-index
base = int(n ** 0.5 + 1)
a = [-1] * n # a[i] is i after base operations
r = [-1] * n # r is reverse of p, i.e. base operations back. r[p[i]] = i
for i in range(n):
r[p[i]] = i
i2 = i
for _ in range(base):
i = p[i]
a[i2] = i
allans = []
# print('initial p:{} r:{} a:{} base:{}'.format(p, r, a, base))
for _ in range(q):
t, x, y = readIntArr()
if t == 1: # swap
x -= 1; y -= 1
temp = p[x]; p[x] = p[y]; p[y] = temp
# update the reverse
r[p[x]] = x; r[p[y]] = y
# print('x:{} y:{} p:{} r:{} a:{}'.format(x, y, p, r, a))
# re-run computation
for z in (x, y): # re-compute base items back in O(base) time
for __ in range(base + 1):
z = r[z]
j = i = z
for __ in range(base):
j = p[j]
for __ in range(base + 2):
# print('i:{} j:{}'.format(i, j))
a[i] = j
i = p[i]
j = p[j]
else:
i = x; k = y
i -= 1
while k >= base:
i = a[i]
k -= base
while k >= 1:
i = p[i]
k -= 1
allans.append(i + 1) # 1-index answer
multiLineArrayPrint(allans)
return
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
# input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultValFactory,dimensionArr): # eg. makeArr(lambda:0,[n,m])
dv=defaultValFactory;da=dimensionArr
if len(da)==1:return [dv() for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(a, b, c):
print('? {} {} {}'.format(a, b, c))
sys.stdout.flush()
return int(input())
def answerInteractive(ansArr):
print('! {}'.format(' '.join([str(x) for x in ansArr])))
sys.stdout.flush()
inf=float('inf')
# MOD=10**9+7
# MOD=998244353
from math import gcd,floor,ceil
import math
# from math import floor,ceil # for Python2
for _abc in range(1):
main() | python | code_algorithm | [
{
"input": "5 9\n2 3 5 1 4\n2 3 5\n2 5 5\n2 5 1\n2 5 3\n2 5 4\n1 5 4\n2 5 3\n2 2 5\n2 5 1\n",
"output": "3\n5\n4\n2\n3\n3\n3\n1\n"
},
{
"input": "5 4\n5 3 4 2 1\n2 3 1\n2 1 2\n1 1 3\n2 1 2\n",
"output": "4\n1\n2\n"
},
{
"input": "1 1\n1\n2 1 1\n",
"output": "1\n"
},
{
"input": "100 10\n71 72 20 93 38 81 19 44 88 57 3 37 86 64 63 97 15 85 53 99 61 66 75 96 48 67 6 31 92 70 27 54 74 43 7 5 84 33 60 65 95 76 11 14 49 24 77 21 50 98 46 25 83 45 94 8 68 91 28 4 80 18 17 1 47 34 40 26 69 35 58 9 36 32 22 16 10 79 13 90 78 29 23 41 100 51 55 59 62 56 12 30 82 2 89 52 73 87 39 42\n1 64 46\n1 97 45\n2 50 22\n2 1 72\n2 65 8\n1 18 37\n1 19 89\n1 86 6\n2 36 69\n1 100 59\n",
"output": "71\n6\n40\n54\n"
},
{
"input": "10 10\n6 7 4 1 5 3 10 2 8 9\n2 10 5\n1 7 6\n1 10 6\n2 3 3\n1 6 2\n2 9 4\n1 4 6\n1 7 9\n2 9 8\n1 3 6\n",
"output": "10\n6\n8\n4\n"
}
] | code_contests | python | 0.8 | e8a840ec24cea991b3b6d2646ecca5d8 |
You had n positive integers a_1, a_2, ..., a_n arranged in a circle. For each pair of neighboring numbers (a_1 and a_2, a_2 and a_3, ..., a_{n - 1} and a_n, and a_n and a_1), you wrote down: are the numbers in the pair equal or not.
Unfortunately, you've lost a piece of paper with the array a. Moreover, you are afraid that even information about equality of neighboring elements may be inconsistent. So, you are wondering: is there any array a which is consistent with information you have about equality or non-equality of corresponding pairs?
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Next t cases follow.
The first and only line of each test case contains a non-empty string s consisting of characters E and/or N. The length of s is equal to the size of array n and 2 β€ n β€ 50. For each i from 1 to n:
* if s_i = E then a_i is equal to a_{i + 1} (a_n = a_1 for i = n);
* if s_i = N then a_i is not equal to a_{i + 1} (a_n β a_1 for i = n).
Output
For each test case, print YES if it's possible to choose array a that are consistent with information from s you know. Otherwise, print NO.
It can be proved, that if there exists some array a, then there exists an array a of positive integers with values less or equal to 10^9.
Example
Input
4
EEE
EN
ENNEENE
NENN
Output
YES
NO
YES
YES
Note
In the first test case, you can choose, for example, a_1 = a_2 = a_3 = 5.
In the second test case, there is no array a, since, according to s_1, a_1 is equal to a_2, but, according to s_2, a_2 is not equal to a_1.
In the third test case, you can, for example, choose array a = [20, 20, 4, 50, 50, 50, 20].
In the fourth test case, you can, for example, choose a = [1, 3, 3, 7].
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from itertools import permutations as per
from math import factorial as fact
from difflib import SequenceMatcher
t = int(input())
# map(int,input().split())
# [int(i) for i in input().split()]
for _ in range(t):
s = input()
print("YES") if s.count('N') != 1 else print("NO") | python | code_algorithm | [
{
"input": "4\nEEE\nEN\nENNEENE\nNENN\n",
"output": "YES\nNO\nYES\nYES\n"
},
{
"input": "1\nNEEEEEEEEEEEEEEEEEEEEEEEEEEEEENNNNEENNE\n",
"output": "YES\n"
},
{
"input": "2\nEEEEEEN\nEEEEEEEN\n",
"output": "NO\nNO\n"
},
{
"input": "2\nEEEEEN\nEEEEEN\n",
"output": "NO\nNO\n"
},
{
"input": "2\nEEEEEN\nEE\n",
"output": "NO\nYES\n"
}
] | code_contests | python | 0 | b2054af6a0a54119a2c0860d5993b939 |
There are three sticks with integer lengths l_1, l_2 and l_3.
You are asked to break exactly one of them into two pieces in such a way that:
* both pieces have positive (strictly greater than 0) integer length;
* the total length of the pieces is equal to the original length of the stick;
* it's possible to construct a rectangle from the resulting four sticks such that each stick is used as exactly one of its sides.
A square is also considered a rectangle.
Determine if it's possible to do that.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of testcases.
The only line of each testcase contains three integers l_1, l_2, l_3 (1 β€ l_i β€ 10^8) β the lengths of the sticks.
Output
For each testcase, print "YES" if it's possible to break one of the sticks into two pieces with positive integer length in such a way that it's possible to construct a rectangle from the resulting four sticks. Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as a positive answer).
Example
Input
4
6 1 5
2 5 2
2 4 2
5 5 4
Output
YES
NO
YES
YES
Note
In the first testcase, the first stick can be broken into parts of length 1 and 5. We can construct a rectangle with opposite sides of length 1 and 5.
In the second testcase, breaking the stick of length 2 can only result in sticks of lengths 1, 1, 2, 5, which can't be made into a rectangle. Breaking the stick of length 5 can produce results 2, 3 or 1, 4 but neither of them can't be put into a rectangle.
In the third testcase, the second stick can be broken into parts of length 2 and 2. The resulting rectangle has opposite sides 2 and 2 (which is a square).
In the fourth testcase, the third stick can be broken into parts of length 2 and 2. The resulting rectangle has opposite sides 2 and 5.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | for _ in range(int(input())):
a, b, c = map(int, input().split())
if a == b + c or b == a + c or c == a + b:
print('YES')
elif a == b and c % 2 == 0:
print('YES')
elif a == c and b % 2 == 0:
print('YES')
elif b == c and a % 2 == 0:
print('YES')
else:
print('NO')
| python | code_algorithm | [
{
"input": "4\n6 1 5\n2 5 2\n2 4 2\n5 5 4\n",
"output": "YES\nNO\nYES\nYES\n"
},
{
"input": "2\n1 2 3\n2 2 4\n",
"output": "YES\nYES\n"
},
{
"input": "1\n1 98 99\n",
"output": "YES\n"
},
{
"input": "3\n1 1 1\n2 1 3\n5 6 7\n",
"output": "NO\nYES\nNO\n"
},
{
"input": "6\n2 2 4\n1 1 2\n3 5 6\n1 2 3\n6 7 8\n10 10 20\n",
"output": "YES\nYES\nNO\nYES\nNO\nYES\n"
},
{
"input": "4\n6 1 5\n2 5 2\n2 4 2\n7 101 108\n",
"output": "YES\nNO\nYES\nYES\n"
},
{
"input": "1\n100000 100000 100000\n",
"output": "YES\n"
},
{
"input": "1\n12344 1 1\n",
"output": "YES\n"
},
{
"input": "68\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n1 1 2\n",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n"
},
{
"input": "2\n1 2 3\n4 5 6\n",
"output": "YES\nNO\n"
}
] | code_contests | python | 0.1 | 2879205d306123ae9ce4dbcf776f3007 |
You are given an integer array a_1, a_2, ..., a_n and integer k.
In one step you can
* either choose some index i and decrease a_i by one (make a_i = a_i - 1);
* or choose two indices i and j and set a_i equal to a_j (make a_i = a_j).
What is the minimum number of steps you need to make the sum of array β_{i=1}^{n}{a_i} β€ k? (You are allowed to make values of array negative).
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 10^{15}) β the size of array a and upper bound on its sum.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the array itself.
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print one integer β the minimum number of steps to make β_{i=1}^{n}{a_i} β€ k.
Example
Input
4
1 10
20
2 69
6 9
7 8
1 2 1 3 1 2 1
10 1
1 2 3 1 2 6 1 6 8 10
Output
10
0
2
7
Note
In the first test case, you should decrease a_1 10 times to get the sum lower or equal to k = 10.
In the second test case, the sum of array a is already less or equal to 69, so you don't need to change it.
In the third test case, you can, for example:
1. set a_4 = a_3 = 1;
2. decrease a_4 by one, and get a_4 = 0.
As a result, you'll get array [1, 2, 1, 0, 1, 2, 1] with sum less or equal to 8 in 1 + 1 = 2 steps.
In the fourth test case, you can, for example:
1. choose a_7 and decrease in by one 3 times; you'll get a_7 = -2;
2. choose 4 elements a_6, a_8, a_9 and a_{10} and them equal to a_7 = -2.
As a result, you'll get array [1, 2, 3, 1, 2, -2, -2, -2, -2, -2] with sum less or equal to 1 in 3 + 4 = 7 steps.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
import os.path
from collections import *
import math
import bisect
import heapq as hq
from fractions import Fraction
from random import randint
if os.path.exists("input.txt"):
sys.stdin = open("input.txt", "r")
sys.stdout = open("output.txt", "w")
input = sys.stdin.readline
##########################################################
def solve():
n,k = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
x = sum(arr)
if(sum(arr) <= k):
print(0)
return
arr.sort()
pos = 0
Sum = 0
res = 1e16
while pos < n:
Sum += arr[pos]
x = (Sum - arr[0])
# print(x)
x = k - x
y = n - pos
z = min(x // y, arr[0])
# print(Sum,x,y,z)
res = min(res, arr[0] - z + y - 1)
pos += 1
print(res)
t = int(input())
while t:
t -= 1
solve()
##########################################################
| python | code_algorithm | [
{
"input": "4\n1 10\n20\n2 69\n6 9\n7 8\n1 2 1 3 1 2 1\n10 1\n1 2 3 1 2 6 1 6 8 10\n",
"output": "10\n0\n2\n7\n"
},
{
"input": "1\n84 781\n403 867 729 928 240 410 849 95 651 852 230 209 358 596 576 230 924 448 288 19 680 470 839 107 931 809 347 903 399 185 840 326 338 758 584 385 862 140 256 537 677 195 593 349 588 452 771 995 853 27 765 878 312 93 411 846 537 426 593 288 506 167 721 84 189 213 490 958 937 522 171 563 995 166 258 408 638 107 720 553 318 758 302 8\n",
"output": "80\n"
},
{
"input": "1\n77 816\n131 139 214 578 23 543 45 617 821 886 910 646 883 517 929 804 72 151 349 472 511 413 28 434 969 58 239 672 349 261 137 87 362 462 193 587 979 846 814 164 30 305 863 487 515 994 389 177 439 928 905 455 481 583 444 221 872 947 715 532 569 945 344 340 244 158 189 767 179 342 399 239 508 105 370 621 150\n",
"output": "86\n"
},
{
"input": "1\n91 734\n815 147 433 982 71 972 593 712 205 267 802 60 80 482 616 963 177 457 763 874 695 988 235 413 519 805 341 453 240 541 428 39 202 206 496 69 505 80 155 333 935 71 184 253 656 722 173 331 890 959 446 429 305 939 338 531 998 577 888 610 282 276 326 784 766 28 448 55 399 775 889 997 482 233 567 465 42 754 438 360 138 750 146 712 249 630 819 941 798 999 895\n",
"output": "107\n"
},
{
"input": "1\n95 902\n670 783 456 537 294 406 958 479 769 924 864 137 865 889 560 816 383 471 790 922 575 646 47 762 278 287 841 950 246 386 197 970 581 466 212 501 577 26 208 583 703 528 284 514 101 788 409 799 650 122 148 328 878 402 112 943 821 114 500 422 269 696 863 246 111 420 473 385 342 238 112 325 726 257 176 359 311 30 529 36 814 927 816 636 614 196 859 630 802 442 446 305 899 44 591\n",
"output": "108\n"
},
{
"input": "1\n98 282\n103 948 349 26 241 933 394 571 476 785 441 397 658 308 801 940 760 29 717 343 99 341 389 797 955 322 662 763 654 781 933 185 622 960 363 84 786 998 331 572 236 837 553 874 376 362 649 482 860 718 54 298 574 987 16 943 71 292 615 829 460 301 358 245 585 438 24 561 151 416 88 81 86 571 805 67 862 698 811 772 389 185 798 799 469 547 109 474 72 128 717 893 38 928 741 644 641 625\n",
"output": "106\n"
},
{
"input": "1\n174 243\n7 7 1 5 9 8 7 5 8 6 10 4 9 3 5 3 2 10 5 9 10 3 5 2 2 6 8 9 2 6 7 6 10 4 9 8 8 9 5 1 9 1 2 7 1 10 8 6 4 6 2 4 10 4 4 3 9 8 2 2 6 9 8 2 5 9 5 2 9 6 9 4 10 9 10 5 8 2 10 7 6 10 4 5 3 4 3 1 10 5 8 2 2 3 3 1 2 9 7 9 5 9 4 4 10 7 10 5 8 8 6 2 6 10 2 8 10 6 4 4 1 5 2 4 6 3 6 7 1 2 3 4 1 10 9 10 2 1 2 4 10 10 2 7 2 6 2 1 10 1 4 3 2 8 4 1 1 9 7 10 2 5 2 4 2 5 6 7 3 1 9 8 1 10\n",
"output": "44\n"
},
{
"input": "1\n77 340\n616 230 33 414 33 520 881 594 173 951 566 507 792 941 244 477 260 277 162 731 833 342 13 81 726 616 890 188 708 365 427 991 14 596 878 102 258 187 135 52 891 147 50 396 576 104 947 85 895 912 439 133 948 536 253 734 375 819 797 356 141 201 482 964 885 426 359 22 452 403 694 267 64 304 884 813 308\n",
"output": "81\n"
},
{
"input": "1\n15 11\n1 1 4 4 4 4 2 2 5 4 4 4 1 5 1\n",
"output": "8\n"
}
] | code_contests | python | 0 | dca475f9b068aea1d370ddd66b3407af |
You are given a binary string (i. e. a string consisting of characters 0 and/or 1) s of length n. You can perform the following operation with the string s at most once: choose a substring (a contiguous subsequence) of s having exactly k characters 1 in it, and shuffle it (reorder the characters in the substring as you wish).
Calculate the number of different strings which can be obtained from s by performing this operation at most once.
Input
The first line contains two integers n and k (2 β€ n β€ 5000; 0 β€ k β€ n).
The second line contains the string s of length n, consisting of characters 0 and/or 1.
Output
Print one integer β the number of different strings which can be obtained from s by performing the described operation at most once. Since the answer can be large, output it modulo 998244353.
Examples
Input
7 2
1100110
Output
16
Input
5 0
10010
Output
1
Input
8 1
10001000
Output
10
Input
10 8
0010011000
Output
1
Note
Some strings you can obtain in the first example:
* to obtain 0110110, you can take the substring from the 1-st character to the 4-th character, which is 1100, and reorder its characters to get 0110;
* to obtain 1111000, you can take the substring from the 3-rd character to the 7-th character, which is 00110, and reorder its characters to get 11000;
* to obtain 1100101, you can take the substring from the 5-th character to the 7-th character, which is 110, and reorder its characters to get 101.
In the second example, k = 0 so you can only choose the substrings consisting only of 0 characters. Reordering them doesn't change the string at all, so the only string you can obtain is 10010.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from sys import stdin, stdout
N = 998244353
def egcd(a, b):
"""return (g, x, y) such that a*x + b*y = g = gcd(a, b)"""
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m = N):
"""return inverse of a mod m"""
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
fact = [1]
invfact = [1]
for i in range(1,5001):
fact.append((fact[-1]*i)%N)
invfact.append((invfact[-1]*modinv(i))%N)
n, k = [int(x) for x in stdin.readline().split()]
s = stdin.readline().strip()
locations = []
for i in range(n):
if s[i] == '1':
locations.append(i)
if k == 0 or len(locations) < k:
stdout.write('1\n')
else:
answer = 0
for i in range(len(locations)-k+1):
if i == 0:
lower = 0
else:
lower = locations[i-1]+1
if i == len(locations)-k:
upper = n-1
else:
upper = locations[i+k]-1
answer = (answer + fact[upper-lower+1]*invfact[k]*invfact[upper-lower+1-k])%N
for i in range(len(locations)-k):
lower = locations[i]+1
upper = locations[i+k]-1
answer = (answer - fact[upper-lower+1]*invfact[k-1]*invfact[upper-lower+1-k+1])%N
stdout.write(str(answer)+'\n')
| python | code_algorithm | [
{
"input": "10 8\n0010011000\n",
"output": "1\n"
},
{
"input": "5 0\n10010\n",
"output": "1\n"
},
{
"input": "8 1\n10001000\n",
"output": "10\n"
},
{
"input": "7 2\n1100110\n",
"output": "16\n"
},
{
"input": "5 2\n00011\n",
"output": "10\n"
},
{
"input": "5 4\n11011\n",
"output": "5\n"
},
{
"input": "81 5\n100000101000000011001101010100001010111000000110000000001000101101111000001000110\n",
"output": "187538\n"
},
{
"input": "4 3\n1011\n",
"output": "4\n"
},
{
"input": "47 25\n11110011111001110100101010011000110111011011101\n",
"output": "314445058\n"
},
{
"input": "291 159\n011001110111110110001111000110001001111100010001011000110100110010001101100110111010101111100111110000110111001100111010001000011001010011011100101000101011010110010011010100100101111010111000001011110101001011101110100100111110011001101010001110110101010101100011101110111000011111011001110\n",
"output": "1\n"
},
{
"input": "459 15\n101010101010101010101010101010101010101010101111111111111111111111010101010101010101010101011111111111111111111101010101010101010101111111111111111111111101010101010101010111111111111111111111111101010101010101111111111111111111111111110110101010101011111111111111111111111111111001000111111111111111111111111111110000011111111111111111111111111111000001111111111111111111111111111100001111111111111111111111111111100111111111111111111111111111110111111111111\n",
"output": "0\n"
},
{
"input": "105 58\n110101000111100000011011011101110100001000110011010101100010111001011111010010111101011011010110011011110\n",
"output": "132732930\n"
},
{
"input": "52 11\n0110011111001000010000100010010001010100000100110100\n",
"output": "478323746\n"
}
] | code_contests | python | 0 | 0921790b4d1ff3f65f38259f2a60ad25 |
π€ Hugging Face π€ ModelScope π₯οΈ GitHub
Ring-lite-rl-data
This dataset is a curated subset of high-quality problems across mathematics and code domains designed for reinforcement learning in the Ring-lite model. This dataset contains:
- Mathematics: Over 39,000 rigorously curated problems sourced from:
- Open-source datasets (BigMath, DeepScaleR, DAPO, DeepMath-103K)
- Art of Problem Solving (AoPS) contest collections
- Code: Approximately 8,400 verified coding problems from:
- Programming competition resources (CodeContest, TACO, APPS)
- All problems include validated "Accepted" solutions and test cases
Note: Only a partial subset of the complete dataset is publicly released due to third-party data licensing restrictions and procurement agreements. The published portion has been carefully selected to comply with all copyright requirements while maintaining research utility.
Dataset Construction
Data Sources
- Mathematics: Problems collected from open-source datasets, filtered through strict quality control
- Code: Problems from open-source programming competition resources with verified solutions
Curation Pipeline
Our data undergoes a rigorous three-stage curation process:
Data Cleansing:
- Removal of problems with invalid characters, images, or multiple subquestions
- Strict character-based and semantic-based deduplication
- Exclusion of easily guessable problems (multiple-choice, True/False questions)
Answer Verification:
- LLM-based verification using models of different sizes
- Human expert annotation
- Problems failing verification are excluded
Data Annotation:
- Multi-dimensional labeling (source, educational level, domain knowledge)
- Mathematical Subject Classification (MSC) for math problems
- Model-aware difficulty assessment
Dataset Fields
The dataset contains the following fields for each domain:
Mathematics
- context: The problem statement
- groundtruth: Verified correct answer
- type: Problem category
- mid: Unique problem ID
Code
- context: Detailed programming problem description
- groundtruth: Verified correct Python solution code
- groundtruth_language: Implementation language
- type: Problem category
- code_test_cases: List of validated test cases with:
- input: Test input
- output: Expected output
- dataset: Source dataset
- code_language: Programming language
- difficulty: Problem difficulty score
- mid: Unique problem ID
Citation Information
Please consider citing our technical report Ring-lite if you use this dataset:
@misc{ringteam2025ringlitescalablereasoningc3postabilized,
title={Ring-lite: Scalable Reasoning via C3PO-Stabilized Reinforcement Learning for LLMs},
author={Ling Team and Bin Hu and Cai Chen and Deng Zhao and Ding Liu and Dingnan Jin and Feng Zhu and Hao Dai and Hongzhi Luan and Jia Guo and Jiaming Liu and Jiewei Wu and Jun Mei and Jun Zhou and Junbo Zhao and Junwu Xiong and Kaihong Zhang and Kuan Xu and Lei Liang and Liang Jiang and Liangcheng Fu and Longfei Zheng and Qiang Gao and Qing Cui and Quan Wan and Shaomian Zheng and Shuaicheng Li and Tongkai Yang and Wang Ren and Xiaodong Yan and Xiaopei Wan and Xiaoyun Feng and Xin Zhao and Xinxing Yang and Xinyu Kong and Xuemin Yang and Yang Li and Yingting Wu and Yongkang Liu and Zhankai Xu and Zhenduo Zhang and Zhenglei Zhou and Zhenyu Huang and Zhiqiang Zhang and Zihao Wang and Zujie Wen},
year={2025},
eprint={2506.14731},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2506.14731},
}
Intended Usage
This dataset is designed for:
- Training and evaluating LLMs on multi-domain reasoning tasks
- Reinforcement learning applications
- Benchmarking model performance across mathematics and code domains
Release Date
06/20/2025
Data Version
1.0
- Downloads last month
- 62