input
stringlengths 50
13.9k
| output_program
stringlengths 5
655k
| output_answer
stringlengths 5
655k
| split
stringclasses 1
value | dataset
stringclasses 1
value |
---|---|---|---|---|
Assume we take a number `x` and perform any one of the following operations:
```Pearl
a) Divide x by 3 (if it is divisible by 3), or
b) Multiply x by 2
```
After each operation, we write down the result. If we start with `9`, we can get a sequence such as:
```
[9,3,6,12,4,8] -- 9/3=3 -> 3*2=6 -> 6*2=12 -> 12/3=4 -> 4*2=8
```
You will be given a shuffled sequence of integers and your task is to reorder them so that they conform to the above sequence. There will always be an answer.
```
For the above example:
solve([12,3,9,4,6,8]) = [9,3,6,12,4,8].
```
More examples in the test cases. Good luck!
|
def solve(arr):
stack = []
for num in arr:
branches = arr.copy()
branches.remove(num)
stack = [([num], branches)]
while len(stack) != 0:
(current, remaining) = stack.pop()
if len(remaining) == 0:
return current
x = current[len(current) - 1]
valid_leaves = [leaf for leaf in remaining if x / 3 == leaf or x * 2 == leaf]
for valid in valid_leaves:
valid_current = current.copy()
valid_current.append(valid)
valid_remaining = remaining.copy()
valid_remaining.remove(valid)
stack.append((valid_current, valid_remaining))
|
def solve(arr):
stack = []
for num in arr:
branches = arr.copy()
branches.remove(num)
stack = [([num], branches)]
while len(stack) != 0:
(current, remaining) = stack.pop()
if len(remaining) == 0:
return current
x = current[len(current) - 1]
valid_leaves = [leaf for leaf in remaining if x / 3 == leaf or x * 2 == leaf]
for valid in valid_leaves:
valid_current = current.copy()
valid_current.append(valid)
valid_remaining = remaining.copy()
valid_remaining.remove(valid)
stack.append((valid_current, valid_remaining))
|
train
|
APPS_structured
|
Your job is to create a calculator which evaluates expressions in [Reverse Polish notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).
For example expression `5 1 2 + 4 * + 3 -` (which is equivalent to `5 + ((1 + 2) * 4) - 3` in normal notation) should evaluate to `14`.
For your convenience, the input is formatted such that a space is provided between every token.
Empty expression should evaluate to `0`.
Valid operations are `+`, `-`, `*`, `/`.
You may assume that there won't be exceptional situations (like stack underflow or division by zero).
|
def calc(expr):
stack = []
[stack.append(eval("{1}{2}{0}".format(stack.pop(), stack.pop(), e)) if e in ('+', '-', '/', '*') else e) for e in expr.split()]
return float(stack.pop()) if stack else 0
|
def calc(expr):
stack = []
[stack.append(eval("{1}{2}{0}".format(stack.pop(), stack.pop(), e)) if e in ('+', '-', '/', '*') else e) for e in expr.split()]
return float(stack.pop()) if stack else 0
|
train
|
APPS_structured
|
Polo, the Penguin, likes numbers. He says that the goodness of a number is itself multiplied by the number of digits in it's decimal representation. For example, the goodness of the integer 474 is 474*3 = 1422.
Help him to count the sum of goodness of all integers from L to R, inclusive. Since the answer can be too large, output it modulo 1,000,000,007 (10^9+7).
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains the pair of integers L and R, separated by a single space.
-----Output-----
For each test case, output a single line containing the answer to the corresponding test case.
-----Constraints-----
- 1 ≤ T ≤ 1,000
- 1 ≤ L ≤ R ≤ 1,000,000,000 (10^9)
-----Example-----
Input:
1
9 12
Output:
75
-----Explanation-----
Example case 1. The answer is 9*1 + 10*2 + 11*2 + 12*2 = 75.
|
def len(n):
x=0
while n>0:
x+=1
n=n//10
return x
e=10**9+7
t=int(input())
#l=[0]
#for i in range(1,10**7):
#l.append((l[-1]+i*len(i))%e)
f=[0]
a=9
b=1
for i in range(10):
x=(2*b+a-1)%e
x=(x*a)%e
y=pow(2,e-2,e)
x=(x*y)%e
x=(x*(i+1))%e
x=(x+f[-1])%e
b*=10
a*=10
f.append(x)
#print(f)
def ans(n):
if n==0:
return 0
x=len(n)
y = f[x-1]
a=pow(10,x-1,e)
b=((n+a)*(n-a+1))%e
b =(b*pow(2,e-2,e))%e
b=(b*x)%e
#print(y,b,n)
return (b+y)%e
for _ in range(t):
a,b=map(int,input().split())
#print(l[b]-l[a-1])
b = ans(b)
a=ans(a-1)
print((b-a)%e)
|
def len(n):
x=0
while n>0:
x+=1
n=n//10
return x
e=10**9+7
t=int(input())
#l=[0]
#for i in range(1,10**7):
#l.append((l[-1]+i*len(i))%e)
f=[0]
a=9
b=1
for i in range(10):
x=(2*b+a-1)%e
x=(x*a)%e
y=pow(2,e-2,e)
x=(x*y)%e
x=(x*(i+1))%e
x=(x+f[-1])%e
b*=10
a*=10
f.append(x)
#print(f)
def ans(n):
if n==0:
return 0
x=len(n)
y = f[x-1]
a=pow(10,x-1,e)
b=((n+a)*(n-a+1))%e
b =(b*pow(2,e-2,e))%e
b=(b*x)%e
#print(y,b,n)
return (b+y)%e
for _ in range(t):
a,b=map(int,input().split())
#print(l[b]-l[a-1])
b = ans(b)
a=ans(a-1)
print((b-a)%e)
|
train
|
APPS_structured
|
If you have not ever heard the term **Arithmetic Progrossion**, refer to:
http://www.codewars.com/kata/find-the-missing-term-in-an-arithmetic-progression/python
And here is an unordered version. Try if you can survive lists of **MASSIVE** numbers (which means time limit should be considered). :D
Note: Don't be afraid that the minimum or the maximum element in the list is missing, e.g. [4, 6, 3, 5, 2] is missing 1 or 7, but this case is excluded from the kata.
Example:
```python
find([3, 9, 1, 11, 13, 5]) # => 7
```
|
def find(seq):
seq.sort()
pattern = abs(seq[0]-seq[1])
for i in range(len(seq)-1):
if seq[i]!=seq[i+1]-pattern:
return seq[i]+pattern
|
def find(seq):
seq.sort()
pattern = abs(seq[0]-seq[1])
for i in range(len(seq)-1):
if seq[i]!=seq[i+1]-pattern:
return seq[i]+pattern
|
train
|
APPS_structured
|
Implement pow(x, n), which calculates x raised to the power n (xn).
Example 1:
Input: 2.00000, 10
Output: 1024.00000
Example 2:
Input: 2.10000, 3
Output: 9.26100
Example 3:
Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Note:
-100.0 < x < 100.0
n is a 32-bit signed integer, within the range [−231, 231 − 1]
|
class Solution:
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if n < 0:
return 1 / self.powRecu(x, -n)
return self.powRecu(x, n)
def powRecu(self, x, n):
if n == 0:
return 1.0
if n % 2 == 0:
return self.powRecu(x * x, n // 2)
else:
return x * self.powRecu(x * x, n // 2)
|
class Solution:
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if n < 0:
return 1 / self.powRecu(x, -n)
return self.powRecu(x, n)
def powRecu(self, x, n):
if n == 0:
return 1.0
if n % 2 == 0:
return self.powRecu(x * x, n // 2)
else:
return x * self.powRecu(x * x, n // 2)
|
train
|
APPS_structured
|
In this kata you will be given a sequence of the dimensions of rectangles ( sequence with width and length ) and circles ( radius - just a number ).
Your task is to return a new sequence of dimensions, sorted ascending by area.
For example,
```python
seq = [ (4.23, 6.43), 1.23, 3.444, (1.342, 3.212) ] # [ rectangle, circle, circle, rectangle ]
sort_by_area(seq) => [ ( 1.342, 3.212 ), 1.23, ( 4.23, 6.43 ), 3.444 ]
```
This kata inspired by [Sort rectangles and circles by area](https://www.codewars.com/kata/sort-rectangles-and-circles-by-area/).
|
from math import pi as PI
def circle(r): return r*r*PI
def rect(a,b): return a*b
def getArea(r): return rect(*r) if isinstance(r,tuple) else circle(r)
def sort_by_area(seq): return sorted(seq, key=getArea)
|
from math import pi as PI
def circle(r): return r*r*PI
def rect(a,b): return a*b
def getArea(r): return rect(*r) if isinstance(r,tuple) else circle(r)
def sort_by_area(seq): return sorted(seq, key=getArea)
|
train
|
APPS_structured
|
On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes.
Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
Now we view the projection of these cubes onto the xy, yz, and zx planes.
A projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane.
Here, we are viewing the "shadow" when looking at the cubes from the top, the front, and the side.
Return the total area of all three projections.
Example 1:
Input: [[2]]
Output: 5
Example 2:
Input: [[1,2],[3,4]]
Output: 17
Explanation:
Here are the three projections ("shadows") of the shape made with each axis-aligned plane.
Example 3:
Input: [[1,0],[0,2]]
Output: 8
Example 4:
Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 14
Example 5:
Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 21
Note:
1 <= grid.length = grid[0].length <= 50
0 <= grid[i][j] <= 50
|
class Solution:
def projectionArea(self, grid: List[List[int]]) -> int:
max_rows = [0 for _ in grid]
max_cols = [0 for _ in grid]
total_nonzero = 0
for r in range(len(grid)):
for c in range(len(grid[r])):
if grid[r][c] > max_cols[c]:
max_cols[c] = grid[r][c]
if grid[r][c] > max_rows[r]:
max_rows[r] = grid[r][c]
if grid[r][c] > 0:
total_nonzero += 1
print(max_rows)
print(max_cols)
print(total_nonzero)
return sum(max_rows) + sum(max_cols) + total_nonzero
|
class Solution:
def projectionArea(self, grid: List[List[int]]) -> int:
max_rows = [0 for _ in grid]
max_cols = [0 for _ in grid]
total_nonzero = 0
for r in range(len(grid)):
for c in range(len(grid[r])):
if grid[r][c] > max_cols[c]:
max_cols[c] = grid[r][c]
if grid[r][c] > max_rows[r]:
max_rows[r] = grid[r][c]
if grid[r][c] > 0:
total_nonzero += 1
print(max_rows)
print(max_cols)
print(total_nonzero)
return sum(max_rows) + sum(max_cols) + total_nonzero
|
train
|
APPS_structured
|
Your task is to convert a given number into a string with commas added for easier readability. The number should be rounded to 3 decimal places and the commas should be added at intervals of three digits before the decimal point. There does not need to be a comma at the end of the number.
You will receive both positive and negative numbers.
## Examples
```python
commas(1) == "1"
commas(1000) == "1,000"
commas(100.2346) == "100.235"
commas(1000000000.23) == "1,000,000,000.23"
commas(-1) == "-1"
commas(-1000000.123) == "-1,000,000.123"
```
|
def commas(num):
i = round(num, 3)
f = int(i)
return f'{f if f==i else i:,}'
|
def commas(num):
i = round(num, 3)
f = int(i)
return f'{f if f==i else i:,}'
|
train
|
APPS_structured
|
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.
There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation).
Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$.
Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number.
For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines.
In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$.
It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$.
-----Output-----
Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists.
-----Example-----
Input
6
4
1 0 2 3
6
10 7 14 8 3 12
2
0 2
3
1 2 3
6
1 4 6 10 11 12
2
0 1023
Output
1
4
2
-1
-1
1023
-----Note-----
In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
|
T = int(input())
for t in range(T):
n = int(input())
S = [int(_) for _ in input().split()]
setS = set(S)
for k in range(1, 1025):
for el in setS:
if el ^ k not in setS:
break
else:
print(k)
break
else:
print(-1)
|
T = int(input())
for t in range(T):
n = int(input())
S = [int(_) for _ in input().split()]
setS = set(S)
for k in range(1, 1025):
for el in setS:
if el ^ k not in setS:
break
else:
print(k)
break
else:
print(-1)
|
train
|
APPS_structured
|
Imagine there's a big cube consisting of n^3 small cubes. Calculate, how many small cubes are not visible from outside.
For example, if we have a cube which has 4 cubes in a row, then the function should return 8, because there are 8 cubes inside our cube (2 cubes in each dimension)
|
import math
def not_visible_cubes(n):
if (n < 2) : return 0
else :
# return long(math.pow(n-2,3))
return (n-2)*(n-2)*(n-2)
|
import math
def not_visible_cubes(n):
if (n < 2) : return 0
else :
# return long(math.pow(n-2,3))
return (n-2)*(n-2)*(n-2)
|
train
|
APPS_structured
|
Toad Pimple has an array of integers $a_1, a_2, \ldots, a_n$.
We say that $y$ is reachable from $x$ if $x<y$ and there exists an integer array $p$ such that $x = p_1 < p_2 < \ldots < p_k=y$, and $a_{p_i}\, \&\, a_{p_{i+1}} > 0$ for all integers $i$ such that $1 \leq i < k$.
Here $\&$ denotes the bitwise AND operation.
You are given $q$ pairs of indices, check reachability for each of them.
-----Input-----
The first line contains two integers $n$ and $q$ ($2 \leq n \leq 300\,000$, $1 \leq q \leq 300\,000$) — the number of integers in the array and the number of queries you need to answer.
The second line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 300\,000$) — the given array.
The next $q$ lines contain two integers each. The $i$-th of them contains two space-separated integers $x_i$ and $y_i$ ($1 \leq x_i < y_i \leq n$). You need to check if $y_i$ is reachable from $x_i$.
-----Output-----
Output $q$ lines. In the $i$-th of them print "Shi" if $y_i$ is reachable from $x_i$, otherwise, print "Fou".
-----Example-----
Input
5 3
1 3 0 2 1
1 3
2 4
1 4
Output
Fou
Shi
Shi
-----Note-----
In the first example, $a_3 = 0$. You can't reach it, because AND with it is always zero. $a_2\, \&\, a_4 > 0$, so $4$ is reachable from $2$, and to go from $1$ to $4$ you can use $p = [1, 2, 4]$.
|
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
from functools import lru_cache
M = mod = 998244353
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
n, q = li()
queue = [-1] * 20
ans = [[-1] * 20 for i in range(n + 1)]
l = li()
for i, curr in enumerate(l):
for j in range(20):
if curr >> j & 1:
for k in range(20):
ans[i][k] = max(ans[i][k], ans[queue[j]][k])
ans[i][j] = i
for j in range(20):queue[j] = max(queue[j], ans[i][j])
queries = []
for i in range(q):queries.append(li())
for i in range(q):
a, b = queries[i]
a -= 1
b -= 1
currans = 0
for j in range(20):
if (l[a] >> j) & 1 and ans[b][j] >= a:
currans = 1
break
print('Shi' if currans else 'Fou')
|
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
from functools import lru_cache
M = mod = 998244353
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
n, q = li()
queue = [-1] * 20
ans = [[-1] * 20 for i in range(n + 1)]
l = li()
for i, curr in enumerate(l):
for j in range(20):
if curr >> j & 1:
for k in range(20):
ans[i][k] = max(ans[i][k], ans[queue[j]][k])
ans[i][j] = i
for j in range(20):queue[j] = max(queue[j], ans[i][j])
queries = []
for i in range(q):queries.append(li())
for i in range(q):
a, b = queries[i]
a -= 1
b -= 1
currans = 0
for j in range(20):
if (l[a] >> j) & 1 and ans[b][j] >= a:
currans = 1
break
print('Shi' if currans else 'Fou')
|
train
|
APPS_structured
|
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move.
Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.
Example 1:
Input: nums = [5,3,2,4]
Output: 0
Explanation: Change the array [5,3,2,4] to [2,2,2,2].
The difference between the maximum and minimum is 2-2 = 0.
Example 2:
Input: nums = [1,5,0,10,14]
Output: 1
Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1].
The difference between the maximum and minimum is 1-0 = 1.
Example 3:
Input: nums = [6,6,0,1,1,4,6]
Output: 2
Example 4:
Input: nums = [1,5,6,14,15]
Output: 1
Constraints:
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
|
class Solution:
def minDifference(self, nums: List[int]) -> int:
nums = sorted(nums)
if len(nums)<=4:
return 0
print(nums)
return min(nums[-1]-nums[3],
nums[-4]-nums[0],
nums[-2]-nums[2],
nums[-3]-nums[1]
)
|
class Solution:
def minDifference(self, nums: List[int]) -> int:
nums = sorted(nums)
if len(nums)<=4:
return 0
print(nums)
return min(nums[-1]-nums[3],
nums[-4]-nums[0],
nums[-2]-nums[2],
nums[-3]-nums[1]
)
|
train
|
APPS_structured
|
Chef is playing a game with his brother Chefu. He asked Chefu to choose a positive integer $N$, multiply it by a given integer $A$, then choose a divisor of $N$ (possibly $N$ itself) and add it to the product. Let's denote the resulting integer by $M$; more formally, $M = A \cdot N + d$, where $d$ is some divisor of $N$.
Chefu told Chef the value of $M$ and now, Chef should guess $N$. Help him find all values of $N$ which Chefu could have chosen.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $A$ and $M$.
-----Output-----
For each test case, print two lines. The first line should contain a single integer $C$ denoting the number of possible values of $N$. The second line should contain $C$ space-separated integers denoting all possible values of $N$ in increasing order.
It is guaranteed that the sum of $C$ over all test cases does not exceed $10^7$.
-----Constraints-----
- $1 \le T \le 100$
- $2 \le M \le 10^{10}$
- $1 \le A < M$
-----Subtasks-----
Subtask #1 (50 points):
- $M \le 10^6$
Subtask #2 (50 points): original constraints
-----Example Input-----
3
3 35
5 50
4 65
-----Example Output-----
1
10
0
3
13 15 16
|
t=int(input())
for i in range(t):
am=input().split()
a=int(am[0])
m=int(am[1])
l=list()
c=0
indx=m//a
for i in range(indx,0,-1):
diff=m-i*a
if(diff>i):
break
if(diff!=0):
if(i%diff==0):
c+=1
l.append(i)
l.sort()
res=str()
for i in range(len(l)):
res=res+str(l[i])+" "
res.rstrip()
print(c)
print(res)
|
t=int(input())
for i in range(t):
am=input().split()
a=int(am[0])
m=int(am[1])
l=list()
c=0
indx=m//a
for i in range(indx,0,-1):
diff=m-i*a
if(diff>i):
break
if(diff!=0):
if(i%diff==0):
c+=1
l.append(i)
l.sort()
res=str()
for i in range(len(l)):
res=res+str(l[i])+" "
res.rstrip()
print(c)
print(res)
|
train
|
APPS_structured
|
Computation of the date either previous or forthcoming dates is quiet easy. But it is quiet difficult to calculate the day from a particular given date.
You are required to find a day from a particular date given to you.
-----Input-----
It consists of a single line entry consisting of date in format dd mm yyyy.
i.e. the input line consists of the three numbers written in order followed by spaces.
Eg. Input for 18-12-1990 is be written as 18 12 1990
-----Output-----
It consists of single line output showing the day for that particular date.
-----Example-----
Input:
14 3 2012
Output:
Wednesday
|
import sys
from datetime import date
def main():
s = sys.stdin.readline
dateis = list(map(int, s().strip().split()))
day = dateis[0]
month = dateis[1]
year = dateis[2]
ans = date(year, month, day).weekday()
if ans == 0:
print("Monday")
elif ans == 1:
print("Tuesday")
elif ans == 2:
print("Wednesday")
elif ans == 3:
print("Thursday")
elif ans == 4:
print("Friday")
elif ans == 5:
print("Saturday")
else:
print("Sunday")
def __starting_point():
main()
__starting_point()
|
import sys
from datetime import date
def main():
s = sys.stdin.readline
dateis = list(map(int, s().strip().split()))
day = dateis[0]
month = dateis[1]
year = dateis[2]
ans = date(year, month, day).weekday()
if ans == 0:
print("Monday")
elif ans == 1:
print("Tuesday")
elif ans == 2:
print("Wednesday")
elif ans == 3:
print("Thursday")
elif ans == 4:
print("Friday")
elif ans == 5:
print("Saturday")
else:
print("Sunday")
def __starting_point():
main()
__starting_point()
|
train
|
APPS_structured
|
Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer x_{i} to the first a_{i} elements of the sequence. Append an integer k_{i} to the end of the sequence. (And hence the size of the sequence increases by 1) Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence.
After each operation, the cows would like to know the average of all the numbers in the sequence. Help them!
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 2·10^5) — the number of operations. The next n lines describe the operations. Each line will start with an integer t_{i} (1 ≤ t_{i} ≤ 3), denoting the type of the operation (see above). If t_{i} = 1, it will be followed by two integers a_{i}, x_{i} (|x_{i}| ≤ 10^3; 1 ≤ a_{i}). If t_{i} = 2, it will be followed by a single integer k_{i} (|k_{i}| ≤ 10^3). If t_{i} = 3, it will not be followed by anything.
It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence.
-----Output-----
Output n lines each containing the average of the numbers in the sequence after the corresponding operation.
The answer will be considered correct if its absolute or relative error doesn't exceed 10^{ - 6}.
-----Examples-----
Input
5
2 1
3
2 3
2 1
3
Output
0.500000
0.000000
1.500000
1.333333
1.500000
Input
6
2 1
1 2 20
2 2
1 2 -3
3
3
Output
0.500000
20.500000
14.333333
12.333333
17.500000
17.000000
-----Note-----
In the second sample, the sequence becomes $\{0 \} \rightarrow \{0,1 \} \rightarrow \{20,21 \} \rightarrow \{20,21,2 \} \rightarrow \{17,18,2 \} \rightarrow \{17,18 \} \rightarrow \{17 \}$
|
def main():
n, res, le, tot = int(input()), [], 1, 0
l, base = [0] * (n + 2), [0] * (n + 2)
for _ in range(n):
s = input()
c = s[0]
if c == '1':
a, x = list(map(int, s[2:].split()))
base[a] += x
tot += a * x
elif c == '2':
l[le] = x = int(s[2:])
tot += x
le += 1
else:
x = base[le]
if x:
base[le] = 0
tot -= x
le -= 1
base[le] += x
else:
le -= 1
tot -= l[le]
res.append(tot / le)
print('\n'.join(map(str, res)))
def __starting_point():
main()
__starting_point()
|
def main():
n, res, le, tot = int(input()), [], 1, 0
l, base = [0] * (n + 2), [0] * (n + 2)
for _ in range(n):
s = input()
c = s[0]
if c == '1':
a, x = list(map(int, s[2:].split()))
base[a] += x
tot += a * x
elif c == '2':
l[le] = x = int(s[2:])
tot += x
le += 1
else:
x = base[le]
if x:
base[le] = 0
tot -= x
le -= 1
base[le] += x
else:
le -= 1
tot -= l[le]
res.append(tot / le)
print('\n'.join(map(str, res)))
def __starting_point():
main()
__starting_point()
|
train
|
APPS_structured
|
There is an automatic door at the entrance of a factory. The door works in the following way: when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, when one or several people come to the door and it is open, all people immediately come inside, opened door immediately closes in d seconds after its opening, if the door is closing and one or several people are coming to the door at the same moment, then all of them will have enough time to enter and only after that the door will close.
For example, if d = 3 and four people are coming at four different moments of time t_1 = 4, t_2 = 7, t_3 = 9 and t_4 = 13 then the door will open three times: at moments 4, 9 and 13. It will close at moments 7 and 12.
It is known that n employees will enter at moments a, 2·a, 3·a, ..., n·a (the value a is positive integer). Also m clients will enter at moments t_1, t_2, ..., t_{m}.
Write program to find the number of times the automatic door will open. Assume that the door is initially closed.
-----Input-----
The first line contains four integers n, m, a and d (1 ≤ n, a ≤ 10^9, 1 ≤ m ≤ 10^5, 1 ≤ d ≤ 10^18) — the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes.
The second line contains integer sequence t_1, t_2, ..., t_{m} (1 ≤ t_{i} ≤ 10^18) — moments of time when clients will come. The values t_{i} are given in non-decreasing order.
-----Output-----
Print the number of times the door will open.
-----Examples-----
Input
1 1 3 4
7
Output
1
Input
4 3 4 2
7 9 11
Output
4
-----Note-----
In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time.
|
def solve():
n1, m, a, d = list(map(int, input().split()))
t = list(map(int, input().split()))
from bisect import insort
from math import floor
insort(t, a * n1)
pred = 0
k = 0
kpred = 0
n = 0
step = d // a + 1
sol = 0
fl = 0
for i in t:
if (i > pred):
if fl == 0:
n = (i - pred + (pred % a)) // a
if n != 0:
k += (n // step) * step - step * (n % step == 0) + 1
if k > n1:
k = n1
fl = 1
# print(k)
if (k * a + d >= i) and (n != 0):
pred = k * a + d
else:
pred = i + d
k = floor(pred // a)
sol += 1
# if n==0:
k = min(floor(pred // a), n1)
sol += n // step + (n % step != 0)
else:
sol += 1
pred = i + d
if i == a * n1:
fl = 1
# print(i,pred,sol,n,step,k, fl)
print(sol)
solve()
# Made By Mostafa_Khaled
|
def solve():
n1, m, a, d = list(map(int, input().split()))
t = list(map(int, input().split()))
from bisect import insort
from math import floor
insort(t, a * n1)
pred = 0
k = 0
kpred = 0
n = 0
step = d // a + 1
sol = 0
fl = 0
for i in t:
if (i > pred):
if fl == 0:
n = (i - pred + (pred % a)) // a
if n != 0:
k += (n // step) * step - step * (n % step == 0) + 1
if k > n1:
k = n1
fl = 1
# print(k)
if (k * a + d >= i) and (n != 0):
pred = k * a + d
else:
pred = i + d
k = floor(pred // a)
sol += 1
# if n==0:
k = min(floor(pred // a), n1)
sol += n // step + (n % step != 0)
else:
sol += 1
pred = i + d
if i == a * n1:
fl = 1
# print(i,pred,sol,n,step,k, fl)
print(sol)
solve()
# Made By Mostafa_Khaled
|
train
|
APPS_structured
|
### Description:
Remove all exclamation marks from sentence except at the end.
### Examples
```
remove("Hi!") == "Hi!"
remove("Hi!!!") == "Hi!!!"
remove("!Hi") == "Hi"
remove("!Hi!") == "Hi!"
remove("Hi! Hi!") == "Hi Hi!"
remove("Hi") == "Hi"
```
|
def remove(s):
return __import__('re').sub(r'!+(?!!*$)','',s)
|
def remove(s):
return __import__('re').sub(r'!+(?!!*$)','',s)
|
train
|
APPS_structured
|
At the annual family gathering, the family likes to find the oldest living family member’s age and the youngest family member’s age and calculate the difference between them.
You will be given an array of all the family members' ages, in any order. The ages will be given in whole numbers, so a baby of 5 months, will have an ascribed ‘age’ of 0. Return a new array (a tuple in Python) with [youngest age, oldest age, difference between the youngest and oldest age].
|
def difference_in_ages(ages):
minimum =min(ages)
maximum =max(ages)
diff = maximum-minimum
return (minimum,maximum,diff)
|
def difference_in_ages(ages):
minimum =min(ages)
maximum =max(ages)
diff = maximum-minimum
return (minimum,maximum,diff)
|
train
|
APPS_structured
|
Spider-Man ("Spidey") needs to get across town for a date with Mary Jane and his web-shooter is low on web fluid. He travels by slinging his web rope to latch onto a building rooftop, allowing him to swing to the opposite end of the latch point.
Write a function that, when given a list of buildings, returns a list of optimal rooftop latch points for minimal web expenditure.
Input
Your function will receive an array whose elements are subarrays in the form [h,w] where h and w represent the height and width, respectively, of each building in sequence
Output
An array of latch points (integers) to get from 0 to the end of the last building in the input list
Technical Details
An optimal latch point is one that yields the greatest horizontal distance gain relative to length of web rope used. Specifically, the highest horizontal distance yield per length unit of web rope used. Give this value rounded down to the nearest integer as a distance from Spidey's origin point (0)
At the start of each swing, Spidey selects a latch point based on his current position.
At the end of each swing, Spidey ends up on the opposite side of the latch point equal to his distance from the latch point before swing.
Spidey's initial altitude is 50, and will always be the same at the start and end of each swing. His initial horizontal position is 0.
To avoid collision with people/traffic below, Spidey must maintain a minimum altitude of 20 at all times.
Building height (h) range limits: 125 <= h < 250
Building width (w) range limits: 50 <= w <= 100
Inputs will always be valid.
Test Example
- Spidey's initial position is at `0`. His first latch point is marked at `76` (horizontal position) on `buildings[0]`.
- At the end of the swing, Spidey's position is at the point marked `B` with a horizontal position of `152`. The green arc represents Spidey's path during swing.
- The marker on the 3rd building and the arc going from point `B` to point `C` represent the latch point (`258`) and arc path for the next swing.
```python
buildings = [[162,76], [205,96], [244,86], [212,68], [174,57], [180,89], [208,70], [214,82], [181,69], [203,67]]
spidey_swings(buildings)# [76,258,457,643,748]
|
def spidey_swings(Q) :
End = At = Pos = 0
Left = []
for V in Q :
Left.append(End)
End += V[1]
Left.append(End)
R = []
while At < len(Q) :
L = I = 0
F = At
while F < len(Q) :
W = Q[F][0] - 20
T = int((W * W - (W - 30) ** 2) ** .5)
if Left[F] <= Pos + T :
if End < Pos + T + T : T = max(1 + End - Pos >> 1,Left[F] - Pos)
if Left[1 + F] < Pos + T : T = Left[1 + F] - Pos
U = End <= Pos + T + T
W = (T * T + (Q[F][0] - 50) ** 2) ** .5
W = (T + T if Pos + T + T < End else End - Pos) / W
if L < W or not I and U : N,L = T,W
I = U
F += 1
R.append(Pos + N)
Pos += N + N
while At < len(Left) and Left[At] <= Pos : At += 1
At -= 1
return R
|
def spidey_swings(Q) :
End = At = Pos = 0
Left = []
for V in Q :
Left.append(End)
End += V[1]
Left.append(End)
R = []
while At < len(Q) :
L = I = 0
F = At
while F < len(Q) :
W = Q[F][0] - 20
T = int((W * W - (W - 30) ** 2) ** .5)
if Left[F] <= Pos + T :
if End < Pos + T + T : T = max(1 + End - Pos >> 1,Left[F] - Pos)
if Left[1 + F] < Pos + T : T = Left[1 + F] - Pos
U = End <= Pos + T + T
W = (T * T + (Q[F][0] - 50) ** 2) ** .5
W = (T + T if Pos + T + T < End else End - Pos) / W
if L < W or not I and U : N,L = T,W
I = U
F += 1
R.append(Pos + N)
Pos += N + N
while At < len(Left) and Left[At] <= Pos : At += 1
At -= 1
return R
|
train
|
APPS_structured
|
Your team is writing a fancy new text editor and you've been tasked with implementing the line numbering.
Write a function which takes a list of strings and returns each line prepended by the correct number.
The numbering starts at 1. The format is `n: string`. Notice the colon and space in between.
**Examples:**
```python
number([]) # => []
number(["a", "b", "c"]) # => ["1: a", "2: b", "3: c"]
```
|
def number(lines):
n = 0
l = []
for i in lines:
n += 1
l.append(str(n) +': '+ i)
return l
|
def number(lines):
n = 0
l = []
for i in lines:
n += 1
l.append(str(n) +': '+ i)
return l
|
train
|
APPS_structured
|
When we have a 2x2 square matrix we may have up to 24 different ones changing the positions of the elements.
We show some of them
```
a b a b a c a c a d a d b a b a
c d d c d b b d b c c b c d d c
```
You may think to generate the remaining ones until completing the set of 24 matrices.
Given a certain matrix of numbers, that may be repeated or not, calculate the total number of possible matrices that may be generated, changing the position of the elements.
E.g:
Case one
```
A = [[1,2,3],
[3,4,5]] #a 2x3 rectangle matrix with number 3 twice
```
generates a set of ```360``` different matrices
Case two
```
A = [[1,1,1],
[2,2,3],
[3,3,3]]
```
generates a set of ```1260``` different matrices.
Case three
```
A = [[1,2,3],
[4,5,6],
[7,8,9]]
```
generates a set of ```362880``` different matrices
This kata is not meant to apply a brute force algorithm to try to count the total amount of marices.
Features of The Random Tests
```
number of tests = 100
2 ≤ m ≤ 9
2 ≤ n ≤ 9
```
Enjoy it!
Available only in Python 2, Javascript and Ruby by the moment.
|
from collections import Counter
from math import factorial
def count_perms(matrix):
m, n = len(matrix), len(matrix[0])
c = Counter([x for row in matrix for x in row])
factors = []
for x, count in c.most_common():
if count > 1:
factors.append(factorial(count))
return factorial(m * n) / reduce(lambda a, b: a * b, factors, 1)
|
from collections import Counter
from math import factorial
def count_perms(matrix):
m, n = len(matrix), len(matrix[0])
c = Counter([x for row in matrix for x in row])
factors = []
for x, count in c.most_common():
if count > 1:
factors.append(factorial(count))
return factorial(m * n) / reduce(lambda a, b: a * b, factors, 1)
|
train
|
APPS_structured
|
Prime numbers are arranged in a ordered list U$U$, in increasing order. Let S$S$ be a sublist of U$U$ with a unique property that for every element A$A$ belonging to list S$S$, if i$i$ denotes the index of A$A$ in list U$U$, than i$i$ also belongs to list U$U$.
Given N$N$, find sum of first N$N$ elements of list S$S$, assuming 1-based indexing.
As the sum can be very large, print the sum modulo 109+7$10^{9}+7$.
-----Input:-----
-The first line of the input contains a single integer T$T$ denoting the number of test cases.
-Only line of each test case has an integer N$N$ .
-----Output:-----
For each test case, print a single integer denoting the sum of first N$N$ elements of set S$S$ modulo 109+7$10^{9}+7$.
-----Constraints-----
- 1≤T≤10000$1 \leq T \leq 10000$
- 1≤N≤1000$1 \leq N \leq 1000$
-----Subtasks-----
-
20 points :
-
1≤T≤10000$1 \leq T \leq 10000$
-
1≤N≤10$1 \leq N \leq 10$
-
20 points :
-
1≤T≤100$1 \leq T \leq 100$
-
1≤N≤100$1 \leq N \leq 100$
-
60 points : Original Constraints
-----Sample Input:-----
2
1
2
-----Sample Output:-----
3
8
-----EXPLANATION:-----
Example case 1:
First few elements of set S$S$ are {3,5,11…} , so sum is 3.
Example case 2:
Sum is 3+5=8.
|
a=[-1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,
103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,
199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,
313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,
433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,
563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,
673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,
811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,
941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,
1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,
1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,
1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,
1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,
1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,
1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,
1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,
1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,
1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,
2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,
2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,
2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,
2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,
2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,
2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,
2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,
2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,
3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,
3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,
3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,
3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,
3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,
3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,
3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,
3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,
4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,
4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,
4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,
4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,
4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,
4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,
4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,
4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,
5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,
5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,
5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,
5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,
5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,
5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,
5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,
5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,
6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,
6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,
6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,
6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,
6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,
6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,
6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,
6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,
7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,
7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,
7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,
7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,
7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,
7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919,7927,
7933,7937,7949,7951,7963,7993,8009,8011,8017,8039,8053,8059,8069,8081,8087,
8089,8093,8101,8111,8117,8123,8147,8161,8167,8171,8179,8191,8209,8219,8221,
8231,8233,8237,8243,8263,8269,8273,8287,8291,8293,8297,8311,8317,8329,8353,
8363,8369,8377,8387,8389,8419,8423,8429,8431,8443,8447,8461,8467,8501,8513,
8521,8527,8537,8539,8543,8563,8573,8581,8597,8599,8609,8623,8627,8629,8641,
8647,8663,8669,8677,8681,8689,8693,8699,8707,8713,8719,8731,8737,8741,8747,
8753,8761,8779,8783,8803,8807,8819,8821,8831,8837,8839,8849,8861,8863,8867,
8887,8893,8923,8929,8933,8941,8951,8963,8969,8971,8999,9001,9007,9011,9013,
9029,9041,9043,9049,9059,9067,9091,9103,9109,9127,9133,9137,9151,9157,9161,
9173,9181,9187,9199,9203,9209,9221,9227,9239,9241,9257,9277,9281,9283,9293,
9311,9319,9323,9337,9341,9343,9349,9371,9377,9391,9397,9403,9413,9419,9421,
9431,9433,9437,9439,9461,9463,9467,9473,9479,9491,9497,9511,9521,9533,9539,
9547,9551,9587,9601,9613,9619,9623,9629,9631,9643,9649,9661,9677,9679,9689,
9697,9719,9721,9733,9739,9743,9749,9767,9769,9781,9787,9791,9803,9811,9817,
9829,9833,9839,9851,9857,9859,9871,9883,9887,9901,9907,9923,9929,9931,9941,
9949,9967,9973,10007,10009,10037,10039,10061,10067,10069,10079,10091,10093,
10099,10103,10111,10133,10139,10141,10151,10159,10163,10169,10177,10181,
10193,10211,10223,10243,10247,10253,10259,10267,10271,10273,10289,10301,
10303,10313,10321,10331,10333,10337,10343,10357,10369,10391,10399,10427,
10429,10433,10453,10457,10459,10463,10477,10487,10499,10501,10513,10529,
10531,10559,10567,10589,10597,10601,10607,10613,10627,10631,10639,10651,
10657,10663,10667,10687,10691,10709,10711,10723,10729,10733,10739,10753,
10771,10781,10789,10799,10831,10837,10847,10853,10859,10861,10867,10883,
10889,10891,10903,10909,10937,10939,10949,10957,10973,10979,10987,10993,
11003,11027,11047,11057,11059,11069,11071,11083,11087,11093,11113,11117,
11119,11131,11149,11159,11161,11171,11173,11177,11197,11213,11239,11243,
11251,11257,11261,11273,11279,11287,11299,11311,11317,11321,11329,11351,
11353,11369,11383,11393,11399,11411,11423,11437,11443,11447,11467,11471,
11483,11489,11491,11497,11503,11519,11527,11549,11551,11579,11587,11593,
11597,11617,11621,11633,11657,11677,11681,11689,11699,11701,11717,11719,
11731,11743,11777,11779,11783,11789,11801,11807,11813,11821,11827,11831,
11833,11839,11863,11867,11887,11897,11903,11909,11923,11927,11933,11939,
11941,11953,11959,11969,11971,11981,11987,12007,12011,12037,12041,12043,
12049,12071,12073,12097,12101,12107,12109,12113,12119,12143,12149,12157,
12161,12163,12197,12203,12211,12227,12239,12241,12251,12253,12263,12269,
12277,12281,12289,12301,12323,12329,12343,12347,12373,12377,12379,12391,
12401,12409,12413,12421,12433,12437,12451,12457,12473,12479,12487,12491,
12497,12503,12511,12517,12527,12539,12541,12547,12553,12569,12577,12583,
12589,12601,12611,12613,12619,12637,12641,12647,12653,12659,12671,12689,
12697,12703,12713,12721,12739,12743,12757,12763,12781,12791,12799,12809,
12821,12823,12829,12841,12853,12889,12893,12899,12907,12911,12917,12919,
12923,12941,12953,12959,12967,12973,12979,12983,13001,13003,13007,13009,
13033,13037,13043,13049,13063,13093,13099,13103,13109,13121,13127,13147,
13151,13159,13163,13171,13177,13183,13187,13217,13219,13229,13241,13249,
13259,13267,13291,13297,13309,13313,13327,13331,13337,13339,13367,13381,
13397,13399,13411,13417,13421,13441,13451,13457,13463,13469,13477,13487,
13499,13513,13523,13537,13553,13567,13577,13591,13597,13613,13619,13627,
13633,13649,13669,13679,13681,13687,13691,13693,13697,13709,13711,13721,
13723,13729,13751,13757,13759,13763,13781,13789,13799,13807,13829,13831,
13841,13859,13873,13877,13879,13883,13901,13903,13907,13913,13921,13931,
13933,13963,13967,13997,13999,14009,14011,14029,14033,14051,14057,14071,
14081,14083,14087,14107,14143,14149,14153,14159,14173,14177,14197,14207,
14221,14243,14249,14251,14281,14293,14303,14321,14323,14327,14341,14347,
14369,14387,14389,14401,14407,14411,14419,14423,14431,14437,14447,14449,
14461,14479,14489,14503,14519,14533,14537,14543,14549,14551,14557,14561,
14563,14591,14593,14621,14627,14629,14633,14639,14653,14657,14669,14683,
14699,14713,14717,14723,14731,14737,14741,14747,14753,14759,14767,14771,
14779,14783,14797,14813,14821,14827,14831,14843,14851,14867,14869,14879,
14887,14891,14897,14923,14929,14939,14947,14951,14957,14969,14983,15013,
15017,15031,15053,15061,15073,15077,15083,15091,15101,15107,15121,15131,
15137,15139,15149,15161,15173,15187,15193,15199,15217,15227,15233,15241,
15259,15263,15269,15271,15277,15287,15289,15299,15307,15313,15319,15329,
15331,15349,15359,15361,15373,15377,15383,15391,15401,15413,15427,15439,
15443,15451,15461,15467,15473,15493,15497,15511,15527,15541,15551,15559,
15569,15581,15583,15601,15607,15619,15629,15641,15643,15647,15649,15661,
15667,15671,15679,15683,15727,15731,15733,15737,15739,15749,15761,15767,
15773,15787,15791,15797,15803,15809,15817,15823,15859,15877,15881,15887,
15889,15901,15907,15913,15919,15923,15937,15959,15971,15973,15991,16001,
16007,16033,16057,16061,16063,16067,16069,16073,16087,16091,16097,16103,
16111,16127,16139,16141,16183,16187,16189,16193,16217,16223,16229,16231,
16249,16253,16267,16273,16301,16319,16333,16339,16349,16361,16363,16369,
16381,16411,16417,16421,16427,16433,16447,16451,16453,16477,16481,16487,
16493,16519,16529,16547,16553,16561,16567,16573,16603,16607,16619,16631,
16633,16649,16651,16657,16661,16673,16691,16693,16699,16703,16729,16741,
16747,16759,16763,16787,16811,16823,16829,16831,16843,16871,16879,16883,
16889,16901,16903,16921,16927,16931,16937,16943,16963,16979,16981,16987,
16993,17011,17021,17027,17029,17033,17041,17047,17053,17077,17093,17099,
17107,17117,17123,17137,17159,17167,17183,17189,17191,17203,17207,17209,
17231,17239,17257,17291,17293,17299,17317,17321,17327,17333,17341,17351,
17359,17377,17383,17387,17389,17393,17401,17417,17419,17431,17443,17449,
17467,17471,17477,17483,17489,17491,17497,17509,17519,17539,17551,17569,
17573,17579,17581,17597,17599,17609,17623,17627,17657,17659,17669,17681,
17683,17707,17713,17729,17737,17747,17749,17761,17783,17789,17791,17807,
17827,17837,17839,17851,17863,17881,17891,17903,17909,17911,17921,17923,
17929,17939,17957,17959,17971,17977,17981,17987,17989,18013,18041,18043,
18047,18049,18059,18061,18077,18089,18097,18119,18121,18127,18131,18133,
18143,18149,18169,18181,18191,18199,18211,18217,18223,18229,18233,18251,
18253,18257,18269,18287,18289,18301,18307,18311,18313,18329,18341,18353,
18367,18371,18379,18397,18401,18413,18427,18433,18439,18443,18451,18457,
18461,18481,18493,18503,18517,18521,18523,18539,18541,18553,18583,18587,
18593,18617,18637,18661,18671,18679,18691,18701,18713,18719,18731,18743,
18749,18757,18773,18787,18793,18797,18803,18839,18859,18869,18899,18911,
18913,18917,18919,18947,18959,18973,18979,19001,19009,19013,19031,19037,
19051,19069,19073,19079,19081,19087,19121,19139,19141,19157,19163,19181,
19183,19207,19211,19213,19219,19231,19237,19249,19259,19267,19273,19289,
19301,19309,19319,19333,19373,19379,19381,19387,19391,19403,19417,19421,
19423,19427,19429,19433,19441,19447,19457,19463,19469,19471,19477,19483,
19489,19501,19507,19531,19541,19543,19553,19559,19571,19577,19583,19597,
19603,19609,19661,19681,19687,19697,19699,19709,19717,19727,19739,19751,
19753,19759,19763,19777,19793,19801,19813,19819,19841,19843,19853,19861,
19867,19889,19891,19913,19919,19927,19937,19949,19961,19963,19973,19979,
19991,19993,19997,20011,20021,20023,20029,20047,20051,20063,20071,20089,
20101,20107,20113,20117,20123,20129,20143,20147,20149,20161,20173,20177,
20183,20201,20219,20231,20233,20249,20261,20269,20287,20297,20323,20327,
20333,20341,20347,20353,20357,20359,20369,20389,20393,20399,20407,20411,
20431,20441,20443,20477,20479,20483,20507,20509,20521,20533,20543,20549,
20551,20563,20593,20599,20611,20627,20639,20641,20663,20681,20693,20707,
20717,20719,20731,20743,20747,20749,20753,20759,20771,20773,20789,20807,
20809,20849,20857,20873,20879,20887,20897,20899,20903,20921,20929,20939,
20947,20959,20963,20981,20983,21001,21011,21013,21017,21019,21023,21031,
21059,21061,21067,21089,21101,21107,21121,21139,21143,21149,21157,21163,
21169,21179,21187,21191,21193,21211,21221,21227,21247,21269,21277,21283,
21313,21317,21319,21323,21341,21347,21377,21379,21383,21391,21397,21401,
21407,21419,21433,21467,21481,21487,21491,21493,21499,21503,21517,21521,
21523,21529,21557,21559,21563,21569,21577,21587,21589,21599,21601,21611,
21613,21617,21647,21649,21661,21673,21683,21701,21713,21727,21737,21739,
21751,21757,21767,21773,21787,21799,21803,21817,21821,21839,21841,21851,
21859,21863,21871,21881,21893,21911,21929,21937,21943,21961,21977,21991,
21997,22003,22013,22027,22031,22037,22039,22051,22063,22067,22073,22079,
22091,22093,22109,22111,22123,22129,22133,22147,22153,22157,22159,22171,
22189,22193,22229,22247,22259,22271,22273,22277,22279,22283,22291,22303,
22307,22343,22349,22367,22369,22381,22391,22397,22409,22433,22441,22447,
22453,22469,22481,22483,22501,22511,22531,22541,22543,22549,22567,22571,
22573,22613,22619,22621,22637,22639,22643,22651,22669,22679,22691,22697,
22699,22709,22717,22721,22727,22739,22741,22751,22769,22777,22783,22787,
22807,22811,22817,22853,22859,22861,22871,22877,22901,22907,22921,22937,
22943,22961,22963,22973,22993,23003,23011,23017,23021,23027,23029,23039,
23041,23053,23057,23059,23063,23071,23081,23087,23099,23117,23131,23143,
23159,23167,23173,23189,23197,23201,23203,23209,23227,23251,23269,23279,
23291,23293,23297,23311,23321,23327,23333,23339,23357,23369,23371,23399,
23417,23431,23447,23459,23473,23497,23509,23531,23537,23539,23549,23557,
23561,23563,23567,23581,23593,23599,23603,23609,23623,23627,23629,23633,
23663,23669,23671,23677,23687,23689,23719,23741,23743,23747,23753,23761,
23767,23773,23789,23801,23813,23819,23827,23831,23833,23857,23869,23873,
23879,23887,23893,23899,23909,23911,23917,23929,23957,23971,23977,23981,
23993,24001,24007,24019,24023,24029,24043,24049,24061,24071,24077,24083,
24091,24097,24103,24107,24109,24113,24121,24133,24137,24151,24169,24179,
24181,24197,24203,24223,24229,24239,24247,24251,24281,24317,24329,24337,
24359,24371,24373,24379,24391,24407,24413,24419,24421,24439,24443,24469,
24473,24481,24499,24509,24517,24527,24533,24547,24551,24571,24593,24611,
24623,24631,24659,24671,24677,24683,24691,24697,24709,24733,24749,24763,
24767,24781,24793,24799,24809,24821,24841,24847,24851,24859,24877,24889,
24907,24917,24919,24923,24943,24953,24967,24971,24977,24979,24989,25013,
25031,25033,25037,25057,25073,25087,25097,25111,25117,25121,25127,25147,
25153,25163,25169,25171,25183,25189,25219,25229,25237,25243,25247,25253,
25261,25301,25303,25307,25309,25321,25339,25343,25349,25357,25367,25373,
25391,25409,25411,25423,25439,25447,25453,25457,25463,25469,25471,25523,
25537,25541,25561,25577,25579,25583,25589,25601,25603,25609,25621,25633,
25639,25643,25657,25667,25673,25679,25693,25703,25717,25733,25741,25747,
25759,25763,25771,25793,25799,25801,25819,25841,25847,25849,25867,25873,
25889,25903,25913,25919,25931,25933,25939,25943,25951,25969,25981,25997,
25999,26003,26017,26021,26029,26041,26053,26083,26099,26107,26111,26113,
26119,26141,26153,26161,26171,26177,26183,26189,26203,26209,26227,26237,
26249,26251,26261,26263,26267,26293,26297,26309,26317,26321,26339,26347,
26357,26371,26387,26393,26399,26407,26417,26423,26431,26437,26449,26459,
26479,26489,26497,26501,26513,26539,26557,26561,26573,26591,26597,26627,
26633,26641,26647,26669,26681,26683,26687,26693,26699,26701,26711,26713,
26717,26723,26729,26731,26737,26759,26777,26783,26801,26813,26821,26833,
26839,26849,26861,26863,26879,26881,26891,26893,26903,26921,26927,26947,
26951,26953,26959,26981,26987,26993,27011,27017,27031,27043,27059,27061,
27067,27073,27077,27091,27103,27107,27109,27127,27143,27179,27191,27197,
27211,27239,27241,27253,27259,27271,27277,27281,27283,27299,27329,27337,
27361,27367,27397,27407,27409,27427,27431,27437,27449,27457,27479,27481,
27487,27509,27527,27529,27539,27541,27551,27581,27583,27611,27617,27631,
27647,27653,27673,27689,27691,27697,27701,27733,27737,27739,27743,27749,
27751,27763,27767,27773,27779,27791,27793,27799,27803,27809,27817,27823,
27827,27847,27851,27883,27893,27901,27917,27919,27941,27943,27947,27953,
27961,27967,27983,27997,28001,28019,28027,28031,28051,28057,28069,28081,
28087,28097,28099,28109,28111,28123,28151,28163,28181,28183,28201,28211,
28219,28229,28277,28279,28283,28289,28297,28307,28309,28319,28349,28351,
28387,28393,28403,28409,28411,28429,28433,28439,28447,28463,28477,28493,
28499,28513,28517,28537,28541,28547,28549,28559,28571,28573,28579,28591,
28597,28603,28607,28619,28621,28627,28631,28643,28649,28657,28661,28663,
28669,28687,28697,28703,28711,28723,28729,28751,28753,28759,28771,28789,
28793,28807,28813,28817,28837,28843,28859,28867,28871,28879,28901,28909,
28921,28927,28933,28949,28961,28979,29009,29017,29021,29023,29027,29033,
29059,29063,29077,29101,29123,29129,29131,29137,29147,29153,29167,29173,
29179,29191,29201,29207,29209,29221,29231,29243,29251,29269,29287,29297,
29303,29311,29327,29333,29339,29347,29363,29383,29387,29389,29399,29401,
29411,29423,29429,29437,29443,29453,29473,29483,29501,29527,29531,29537,
29567,29569,29573,29581,29587,29599,29611,29629,29633,29641,29663,29669,
29671,29683,29717,29723,29741,29753,29759,29761,29789,29803,29819,29833,
29837,29851,29863,29867,29873,29879,29881,29917,29921,29927,29947,29959,
29983,29989,30011,30013,30029,30047,30059,30071,30089,30091,30097,30103,
30109,30113,30119,30133,30137,30139,30161,30169,30181,30187,30197,30203,
30211,30223,30241,30253,30259,30269,30271,30293,30307,30313,30319,30323,
30341,30347,30367,30389,30391,30403,30427,30431,30449,30467,30469,30491,
30493,30497,30509,30517,30529,30539,30553,30557,30559,30577,30593,30631,
30637,30643,30649,30661,30671,30677,30689,30697,30703,30707,30713,30727,
30757,30763,30773,30781,30803,30809,30817,30829,30839,30841,30851,30853,
30859,30869,30871,30881,30893,30911,30931,30937,30941,30949,30971,30977,
30983,31013,31019,31033,31039,31051,31063,31069,31079,31081,31091,31121,
31123,31139,31147,31151,31153,31159,31177,31181,31183,31189,31193,31219,
31223,31231,31237,31247,31249,31253,31259,31267,31271,31277,31307,31319,
31321,31327,31333,31337,31357,31379,31387,31391,31393,31397,31469,31477,
31481,31489,31511,31513,31517,31531,31541,31543,31547,31567,31573,31583,
31601,31607,31627,31643,31649,31657,31663,31667,31687,31699,31721,31723,
31727,31729,31741,31751,31769,31771,31793,31799,31817,31847,31849,31859,
31873,31883,31891,31907,31957,31963,31973,31981,31991,32003,32009,32027,
32029,32051,32057,32059,32063,32069,32077,32083,32089,32099,32117,32119,
32141,32143,32159,32173,32183,32189,32191,32203,32213,32233,32237,32251,
32257,32261,32297,32299,32303,32309,32321,32323,32327,32341,32353,32359,
32363,32369,32371,32377,32381,32401,32411,32413,32423,32429,32441,32443,
32467,32479,32491,32497,32503,32507,32531,32533,32537,32561,32563,32569,
32573,32579,32587,32603,32609,32611,32621,32633,32647,32653,32687,32693,
32707,32713,32717,32719,32749,32771,32779,32783,32789,32797,32801,32803,
32831,32833,32839,32843,32869,32887,32909,32911,32917,32933,32939,32941,
32957,32969,32971,32983,32987,32993,32999,33013,33023,33029,33037,33049,
33053,33071,33073,33083,33091,33107,33113,33119,33149,33151,33161,33179,
33181,33191,33199,33203,33211,33223,33247,33287,33289,33301,33311,33317,
33329,33331,33343,33347,33349,33353,33359,33377,33391,33403,33409,33413,
33427,33457,33461,33469,33479,33487,33493,33503,33521,33529,33533,33547,
33563,33569,33577,33581,33587,33589,33599,33601,33613,33617,33619,33623,
33629,33637,33641,33647,33679,33703,33713,33721,33739,33749,33751,33757,
33767,33769,33773,33791,33797,33809,33811,33827,33829,33851,33857,33863,
33871,33889,33893,33911,33923,33931,33937,33941,33961,33967,33997,34019,
34031,34033,34039,34057,34061,34123,34127,34129,34141,34147,34157,34159,
34171,34183,34211,34213,34217,34231,34253,34259,34261,34267,34273,34283,
34297,34301,34303,34313,34319,34327,34337,34351,34361,34367,34369,34381,
34403,34421,34429,34439,34457,34469,34471,34483,34487,34499,34501,34511,
34513,34519,34537,34543,34549,34583,34589,34591,34603,34607,34613,34631,
34649,34651,34667,34673,34679,34687,34693,34703,34721,34729,34739,34747,
34757,34759,34763,34781,34807,34819,34841,34843,34847,34849,34871,34877,
34883,34897,34913,34919,34939,34949,34961,34963,34981,35023,35027,35051,
35053,35059,35069,35081,35083,35089,35099,35107,35111,35117,35129,35141,
35149,35153,35159,35171,35201,35221,35227,35251,35257,35267,35279,35281,
35291,35311,35317,35323,35327,35339,35353,35363,35381,35393,35401,35407,
35419,35423,35437,35447,35449,35461,35491,35507,35509,35521,35527,35531,
35533,35537,35543,35569,35573,35591,35593,35597,35603,35617,35671,35677,
35729,35731,35747,35753,35759,35771,35797,35801,35803,35809,35831,35837,
35839,35851,35863,35869,35879,35897,35899,35911,35923,35933,35951,35963,
35969,35977,35983,35993,35999,36007,36011,36013,36017,36037,36061,36067,
36073,36083,36097,36107,36109,36131,36137,36151,36161,36187,36191,36209,
36217,36229,36241,36251,36263,36269,36277,36293,36299,36307,36313,36319,
36341,36343,36353,36373,36383,36389,36433,36451,36457,36467,36469,36473,
36479,36493,36497,36523,36527,36529,36541,36551,36559,36563,36571,36583,
36587,36599,36607,36629,36637,36643,36653,36671,36677,36683,36691,36697,
36709,36713,36721,36739,36749,36761,36767,36779,36781,36787,36791,36793,
36809,36821,36833,36847,36857,36871,36877,36887,36899,36901,36913,36919,
36923,36929,36931,36943,36947,36973,36979,36997,37003,37013,37019,37021,
37039,37049,37057,37061,37087,37097,37117,37123,37139,37159,37171,37181,
37189,37199,37201,37217,37223,37243,37253,37273,37277,37307,37309,37313,
37321,37337,37339,37357,37361,37363,37369,37379,37397,37409,37423,37441,
37447,37463,37483,37489,37493,37501,37507,37511,37517,37529,37537,37547,
37549,37561,37567,37571,37573,37579,37589,37591,37607,37619,37633,37643,
37649,37657,37663,37691,37693,37699,37717,37747,37781,37783,37799,37811,
37813,37831,37847,37853,37861,37871,37879,37889,37897,37907,37951,37957,
37963,37967,37987,37991,37993,37997,38011,38039,38047,38053,38069,38083,
38113,38119,38149,38153,38167,38177,38183,38189,38197,38201,38219,38231,
38237,38239,38261,38273,38281,38287,38299,38303,38317,38321,38327,38329,
38333,38351,38371,38377,38393,38431,38447,38449,38453,38459,38461,38501,
38543,38557,38561,38567,38569,38593,38603,38609,38611,38629,38639,38651,
38653,38669,38671,38677,38693,38699,38707,38711,38713,38723,38729,38737,
38747,38749,38767,38783,38791,38803,38821,38833,38839,38851,38861,38867,
38873,38891,38903,38917,38921,38923,38933,38953,38959,38971,38977,38993,
39019,39023,39041,39043,39047,39079,39089,39097,39103,39107,39113,39119,
39133,39139,39157,39161,39163,39181,39191,39199,39209,39217,39227,39229,
39233,39239,39241,39251,39293,39301,39313,39317,39323,39341,39343,39359,
39367,39371,39373,39383,39397,39409,39419,39439,39443,39451,39461,39499,
39503,39509,39511,39521,39541,39551,39563,39569,39581,39607,39619,39623,
39631,39659,39667,39671,39679,39703,39709,39719,39727,39733,39749,39761,
39769,39779,39791,39799,39821,39827,39829,39839,39841,39847,39857,39863,
39869,39877,39883,39887,39901,39929,39937,39953,39971,39979,39983,39989,
40009,40013,40031,40037,40039,40063,40087,40093,40099,40111,40123,40127,
40129,40151,40153,40163,40169,40177,40189,40193,40213,40231,40237,40241,
40253,40277,40283,40289,40343,40351,40357,40361,40387,40423,40427,40429,
40433,40459,40471,40483,40487,40493,40499,40507,40519,40529,40531,40543,
40559,40577,40583,40591,40597,40609,40627,40637,40639,40693,40697,40699,
40709,40739,40751,40759,40763,40771,40787,40801,40813,40819,40823,40829,
40841,40847,40849,40853,40867,40879,40883,40897,40903,40927,40933,40939,
40949,40961,40973,40993,41011,41017,41023,41039,41047,41051,41057,41077,
41081,41113,41117,41131,41141,41143,41149,41161,41177,41179,41183,41189,
41201,41203,41213,41221,41227,41231,41233,41243,41257,41263,41269,41281,
41299,41333,41341,41351,41357,41381,41387,41389,41399,41411,41413,41443,
41453,41467,41479,41491,41507,41513,41519,41521,41539,41543,41549,41579,
41593,41597,41603,41609,41611,41617,41621,41627,41641,41647,41651,41659,
41669,41681,41687,41719,41729,41737,41759,41761,41771,41777,41801,41809,
41813,41843,41849,41851,41863,41879,41887,41893,41897,41903,41911,41927,
41941,41947,41953,41957,41959,41969,41981,41983,41999,42013,42017,42019,
42023,42043,42061,42071,42073,42083,42089,42101,42131,42139,42157,42169,
42179,42181,42187,42193,42197,42209,42221,42223,42227,42239,42257,42281,
42283,42293,42299,42307,42323,42331,42337,42349,42359,42373,42379,42391,
42397,42403,42407,42409,42433,42437,42443,42451,42457,42461,42463,42467,
42473,42487,42491,42499,42509,42533,42557,42569,42571,42577,42589,42611,
42641,42643,42649,42667,42677,42683,42689,42697,42701,42703,42709,42719,
42727,42737,42743,42751,42767,42773,42787,42793,42797,42821,42829,42839,
42841,42853,42859,42863,42899,42901,42923,42929,42937,42943,42953,42961,
42967,42979,42989,43003,43013,43019,43037,43049,43051,43063,43067,43093,
43103,43117,43133,43151,43159,43177,43189,43201,43207,43223,43237,43261,
43271,43283,43291,43313,43319,43321,43331,43391,43397,43399,43403,43411,
43427,43441,43451,43457,43481,43487,43499,43517,43541,43543,43573,43577,
43579,43591,43597,43607,43609,43613,43627,43633,43649,43651,43661,43669,
43691,43711,43717,43721,43753,43759,43777,43781,43783,43787,43789,43793,
43801,43853,43867,43889,43891,43913,43933,43943,43951,43961,43963,43969,
43973,43987,43991,43997,44017,44021,44027,44029,44041,44053,44059,44071,
44087,44089,44101,44111,44119,44123,44129,44131,44159,44171,44179,44189,
44201,44203,44207,44221,44249,44257,44263,44267,44269,44273,44279,44281,
44293,44351,44357,44371,44381,44383,44389,44417,44449,44453,44483,44491,
44497,44501,44507,44519,44531,44533,44537,44543,44549,44563,44579,44587,
44617,44621,44623,44633,44641,44647,44651,44657,44683,44687,44699,44701,
44711,44729,44741,44753,44771,44773,44777,44789,44797,44809,44819,44839,
44843,44851,44867,44879,44887,44893,44909,44917,44927,44939,44953,44959,
44963,44971,44983,44987,45007,45013,45053,45061,45077,45083,45119,45121,
45127,45131,45137,45139,45161,45179,45181,45191,45197,45233,45247,45259,
45263,45281,45289,45293,45307,45317,45319,45329,45337,45341,45343,45361,
45377,45389,45403,45413,45427,45433,45439,45481,45491,45497,45503,45523,
45533,45541,45553,45557,45569,45587,45589,45599,45613,45631,45641,45659,
45667,45673,45677,45691,45697,45707,45737,45751,45757,45763,45767,45779,
45817,45821,45823,45827,45833,45841,45853,45863,45869,45887,45893,45943,
45949,45953,45959,45971,45979,45989,46021,46027,46049,46051,46061,46073,
46091,46093,46099,46103,46133,46141,46147,46153,46171,46181,46183,46187,
46199,46219,46229,46237,46261,46271,46273,46279,46301,46307,46309,46327,
46337,46349,46351,46381,46399,46411,46439,46441,46447,46451,46457,46471,
46477,46489,46499,46507,46511,46523,46549,46559,46567,46573,46589,46591,
46601,46619,46633,46639,46643,46649,46663,46679,46681,46687,46691,46703,
46723,46727,46747,46751,46757,46769,46771,46807,46811,46817,46819,46829,
46831,46853,46861,46867,46877,46889,46901,46919,46933,46957,46993,46997,
47017,47041,47051,47057,47059,47087,47093,47111,47119,47123,47129,47137,
47143,47147,47149,47161,47189,47207,47221,47237,47251,47269,47279,47287,
47293,47297,47303,47309,47317,47339,47351,47353,47363,47381,47387,47389,
47407,47417,47419,47431,47441,47459,47491,47497,47501,47507,47513,47521,
47527,47533,47543,47563,47569,47581,47591,47599,47609,47623,47629,47639,
47653,47657,47659,47681,47699,47701,47711,47713,47717,47737,47741,47743,
47777,47779,47791,47797,47807,47809,47819,47837,47843,47857,47869,47881,
47903,47911,47917,47933,47939,47947,47951,47963,47969,47977,47981,48017,
48023,48029,48049,48073,48079,48091,48109,48119,48121,48131,48157,48163,
48179,48187,48193,48197,48221,48239,48247,48259,48271,48281,48299,48311,
48313,48337,48341,48353,48371,48383,48397,48407,48409,48413,48437,48449,
48463,48473,48479,48481,48487,48491,48497,48523,48527,48533,48539,48541,
48563,48571,48589,48593,48611,48619,48623,48647,48649,48661,48673,48677,
48679,48731,48733,48751,48757,48761,48767,48779,48781,48787,48799,48809,
48817,48821,48823,48847,48857,48859,48869,48871,48883,48889,48907,48947,
48953,48973,48989,48991,49003,49009,49019,49031,49033,49037,49043,49057,
49069,49081,49103,49109,49117,49121,49123,49139,49157,49169,49171,49177,
49193,49199,49201,49207,49211,49223,49253,49261,49277,49279,49297,49307,
49331,49333,49339,49363,49367,49369,49391,49393,49409,49411,49417,49429,
49433,49451,49459,49463,49477,49481,49499,49523,49529,49531,49537,49547,
49549,49559,49597,49603,49613,49627,49633,49639,49663,49667,49669,49681,
49697,49711,49727,49739,49741,49747,49757,49783,49787,49789,49801,49807,
49811,49823,49831,49843,49853,49871,49877,49891,49919,49921,49927,49937,
49939,49943,49957,49991,49993,49999,50021,50023,50033,50047,50051,50053,
50069,50077,50087,50093,50101,50111,50119,50123,50129,50131,50147,50153,
50159,50177,50207,50221,50227,50231,50261,50263,50273,50287,50291,50311,
50321,50329,50333,50341,50359,50363,50377,50383,50387,50411,50417,50423,
50441,50459,50461,50497,50503,50513,50527,50539,50543,50549,50551,50581,
50587,50591,50593,50599,50627,50647,50651,50671,50683,50707,50723,50741,
50753,50767,50773,50777,50789,50821,50833,50839,50849,50857,50867,50873,
50891,50893,50909,50923,50929,50951,50957,50969,50971,50989,50993,51001,
51031,51043,51047,51059,51061,51071,51109,51131,51133,51137,51151,51157,
51169,51193,51197,51199,51203,51217,51229,51239,51241,51257,51263,51283,
51287,51307,51329,51341,51343,51347,51349,51361,51383,51407,51413,51419,
51421,51427,51431,51437,51439,51449,51461,51473,51479,51481,51487,51503,
51511,51517,51521,51539,51551,51563,51577,51581,51593,51599,51607,51613,
51631,51637,51647,51659,51673,51679,51683,51691,51713,51719,51721,51749,
51767,51769,51787,51797,51803,51817,51827,51829,51839,51853,51859,51869,
51871,51893,51899,51907,51913,51929,51941,51949,51971,51973,51977,51991,
52009,52021,52027,52051,52057,52067,52069,52081,52103,52121,52127,52147,
52153,52163,52177,52181,52183,52189,52201,52223,52237,52249,52253,52259,
52267,52289,52291,52301,52313,52321,52361,52363,52369,52379,52387,52391,
52433,52453,52457,52489,52501,52511,52517,52529,52541,52543,52553,52561,
52567,52571,52579,52583,52609,52627,52631,52639,52667,52673,52691,52697,
52709,52711,52721,52727,52733,52747,52757,52769,52783,52807,52813,52817,
52837,52859,52861,52879,52883,52889,52901,52903,52919,52937,52951,52957,
52963,52967,52973,52981,52999,53003,53017,53047,53051,53069,53077,53087,
53089,53093,53101,53113,53117,53129,53147,53149,53161,53171,53173,53189,
53197,53201,53231,53233,53239,53267,53269,53279,53281,53299,53309,53323,
53327,53353,53359,53377,53381,53401,53407,53411,53419,53437,53441,53453,
53479,53503,53507,53527,53549,53551,53569,53591,53593,53597,53609,53611,
53617,53623,53629,53633,53639,53653,53657,53681,53693,53699,53717,53719,
53731,53759,53773,53777,53783,53791,53813,53819,53831,53849,53857,53861,
53881,53887,53891,53897,53899,53917,53923,53927,53939,53951,53959,53987,
53993,54001,54011,54013,54037,54049,54059,54083,54091,54101,54121,54133,
54139,54151,54163,54167,54181,54193,54217,54251,54269,54277,54287,54293,
54311,54319,54323,54331,54347,54361,54367,54371,54377,54401,54403,54409,
54413,54419,54421,54437,54443,54449,54469,54493,54497,54499,54503,54517,
54521,54539,54541,54547,54559,54563,54577,54581,54583,54601,54617,54623,
54629,54631,54647,54667,54673,54679,54709,54713,54721,54727,54751,54767,
54773,54779,54787,54799,54829,54833,54851,54869,54877,54881,54907,54917,
54919,54941,54949,54959,54973,54979,54983,55001,55009,55021,55049,55051,
55057,55061,55073,55079,55103,55109,55117,55127,55147,55163,55171,55201,
55207,55213,55217,55219,55229,55243,55249,55259,55291,55313,55331,55333,
55337,55339,55343,55351,55373,55381,55399,55411,55439,55441,55457,55469,
55487,55501,55511,55529,55541,55547,55579,55589,55603,55609,55619,55621,
55631,55633,55639,55661,55663,55667,55673,55681,55691,55697,55711,55717,
55721,55733,55763,55787,55793,55799,55807,55813,55817,55819,55823,55829,
55837,55843,55849,55871,55889,55897,55901,55903,55921,55927,55931,55933,
55949,55967,55987,55997,56003,56009,56039,56041,56053,56081,56087,56093,
56099,56101,56113,56123,56131,56149,56167,56171,56179,56197,56207,56209,
56237,56239,56249,56263,56267,56269,56299,56311,56333,56359,56369,56377,
56383,56393,56401,56417,56431,56437,56443,56453,56467,56473,56477,56479,
56489,56501,56503,56509,56519,56527,56531,56533,56543,56569,56591,56597,
56599,56611,56629,56633,56659,56663,56671,56681,56687,56701,56711,56713,
56731,56737,56747,56767,56773,56779,56783,56807,56809,56813,56821,56827,
56843,56857,56873,56891,56893,56897,56909,56911,56921,56923,56929,56941,
56951,56957,56963,56983,56989,56993,56999,57037,57041,57047,57059,57073,
57077,57089,57097,57107,57119,57131,57139,57143,57149,57163,57173,57179,
57191,57193,57203,57221,57223,57241,57251,57259,57269,57271,57283,57287,
57301,57329,57331,57347,57349,57367,57373,57383,57389,57397,57413,57427,
57457,57467,57487,57493,57503,57527,57529,57557,57559,57571,57587,57593,
57601,57637,57641,57649,57653,57667,57679,57689,57697,57709,57713,57719,
57727,57731,57737,57751,57773,57781,57787,57791,57793,57803,57809,57829,
57839,57847,57853,57859,57881,57899,57901,57917,57923,57943,57947,57973,
57977,57991,58013,58027,58031,58043,58049,58057,58061,58067,58073,58099,
58109,58111,58129,58147,58151,58153,58169,58171,58189,58193,58199,58207,
58211,58217,58229,58231,58237,58243,58271,58309,58313,58321,58337,58363,
58367,58369,58379,58391,58393,58403,58411,58417,58427,58439,58441,58451,
58453,58477,58481,58511,58537,58543,58549,58567,58573,58579,58601,58603,
58613,58631,58657,58661,58679,58687,58693,58699,58711,58727,58733,58741,
58757,58763,58771,58787,58789,58831,58889,58897,58901,58907,58909,58913,
58921,58937,58943,58963,58967,58979,58991,58997,59009,59011,59021,59023,
59029,59051,59053,59063,59069,59077,59083,59093,59107,59113,59119,59123,
59141,59149,59159,59167,59183,59197,59207,59209,59219,59221,59233,59239,
59243,59263,59273,59281,59333,59341,59351,59357,59359,59369,59377,59387,
59393,59399,59407,59417,59419,59441,59443,59447,59453,59467,59471,59473,
59497,59509,59513,59539,59557,59561,59567,59581,59611,59617,59621,59627,
59629,59651,59659,59663,59669,59671,59693,59699,59707,59723,59729,59743,
59747,59753,59771,59779,59791,59797,59809,59833,59863,59879,59887,59921,
59929,59951,59957,59971,59981,59999,60013,60017,60029,60037,60041,60077,
60083,60089,60091,60101,60103,60107,60127,60133,60139,60149,60161,60167,
60169,60209,60217,60223,60251,60257,60259,60271,60289,60293,60317,60331,
60337,60343,60353,60373,60383,60397,60413,60427,60443,60449,60457,60493,
60497,60509,60521,60527,60539,60589,60601,60607,60611,60617,60623,60631,
60637,60647,60649,60659,60661,60679,60689,60703,60719,60727,60733,60737,
60757,60761,60763,60773,60779,60793,60811,60821,60859,60869,60887,60889,
60899,60901,60913,60917,60919,60923,60937,60943,60953,60961,61001,61007,
61027,61031,61043,61051,61057,61091,61099,61121,61129,61141,61151,61153,
61169,61211,61223,61231,61253,61261,61283,61291,61297,61331,61333,61339,
61343,61357,61363,61379,61381,61403,61409,61417,61441,61463,61469,61471,
61483,61487,61493,61507,61511,61519,61543,61547,61553,61559,61561,61583,
61603,61609,61613,61627,61631,61637,61643,61651,61657,61667,61673,61681,
61687,61703,61717,61723,61729,61751,61757,61781,61813,61819,61837,61843,
61861,61871,61879,61909,61927,61933,61949,61961,61967,61979,61981,61987,
61991,62003,62011,62017,62039,62047,62053,62057,62071,62081,62099,62119,
62129,62131,62137,62141,62143,62171,62189,62191,62201,62207,62213,62219,
62233,62273,62297,62299,62303,62311,62323,62327,62347,62351,62383,62401,
62417,62423,62459,62467,62473,62477,62483,62497,62501,62507,62533,62539,
62549,62563,62581,62591,62597,62603,62617,62627,62633,62639,62653,62659,
62683,62687,62701,62723,62731,62743,62753,62761,62773,62791,62801,62819,
62827,62851,62861,62869,62873,62897,62903,62921,62927,62929,62939,62969,
62971,62981,62983,62987,62989,63029,63031,63059,63067,63073,63079,63097,
63103,63113,63127,63131,63149,63179,63197,63199,63211,63241,63247,63277,
63281,63299,63311,63313,63317,63331,63337,63347,63353,63361,63367,63377,
63389,63391,63397,63409,63419,63421,63439,63443,63463,63467,63473,63487,
63493,63499,63521,63527,63533,63541,63559,63577,63587,63589,63599,63601,
63607,63611,63617,63629,63647,63649,63659,63667,63671,63689,63691,63697,
63703,63709,63719,63727,63737,63743,63761,63773,63781,63793,63799,63803,
63809,63823,63839,63841,63853,63857,63863,63901,63907,63913,63929,63949,
63977,63997,64007,64013,64019,64033,64037,64063,64067,64081,64091,64109,
64123,64151,64153,64157,64171,64187,64189,64217,64223,64231,64237,64271,
64279,64283,64301,64303,64319,64327,64333,64373,64381,64399,64403,64433,
64439,64451,64453,64483,64489,64499,64513,64553,64567,64577,64579,64591,
64601,64609,64613,64621,64627,64633,64661,64663,64667,64679,64693,64709,
64717,64747,64763,64781,64783,64793,64811,64817,64849,64853,64871,64877,
64879,64891,64901,64919,64921,64927,64937,64951,64969,64997,65003,65011,
65027,65029,65033,65053,65063,65071,65089,65099,65101,65111,65119,65123,
65129,65141,65147,65167,65171,65173,65179,65183,65203,65213,65239,65257,
65267,65269,65287,65293,65309,65323,65327,65353,65357,65371,65381,65393,
65407,65413,65419,65423,65437,65447,65449,65479,65497,65519,65521,65537,
65539,65543,65551,65557,65563,65579,65581,65587,65599,65609,65617,65629,
65633,65647,65651,65657,65677,65687,65699,65701,65707,65713,65717,65719,
65729,65731,65761,65777,65789,65809,65827,65831,65837,65839,65843,65851,
65867,65881,65899,65921,65927,65929,65951,65957,65963,65981,65983,65993,
66029,66037,66041,66047,66067,66071,66083,66089,66103,66107,66109,66137,
66161,66169,66173,66179,66191,66221,66239,66271,66293,66301,66337,66343,
66347,66359,66361,66373,66377,66383,66403,66413,66431,66449,66457,66463,
66467,66491,66499,66509,66523,66529,66533,66541,66553,66569,66571,66587,
66593,66601,66617,66629,66643,66653,66683,66697,66701,66713,66721,66733,
66739,66749,66751,66763,66791,66797,66809,66821,66841,66851,66853,66863,
66877,66883,66889,66919,66923,66931,66943,66947,66949,66959,66973,66977,
67003,67021,67033,67043,67049,67057,67061,67073,67079,67103,67121,67129,
67139,67141,67153,67157,67169,67181,67187,67189,67211,67213,67217,67219,
67231,67247,67261,67271,67273,67289,67307,67339,67343,67349,67369,67391,
67399,67409,67411,67421,67427,67429,67433,67447,67453,67477,67481,67489,
67493,67499,67511,67523,67531,67537,67547,67559,67567,67577,67579,67589,
67601,67607,67619,67631,67651,67679,67699,67709,67723,67733,67741,67751,
67757,67759,67763,67777,67783,67789,67801,67807,67819,67829,67843,67853,
67867,67883,67891,67901,67927,67931,67933,67939,67943,67957,67961,67967,
67979,67987,67993,68023,68041,68053,68059,68071,68087,68099,68111,68113,
68141,68147,68161,68171,68207,68209,68213,68219,68227,68239,68261,68279,
68281,68311,68329,68351,68371,68389,68399,68437,68443,68447,68449,68473,
68477,68483,68489,68491,68501,68507,68521,68531,68539,68543,68567,68581,
68597,68611,68633,68639,68659,68669,68683,68687,68699,68711,68713,68729,
68737,68743,68749,68767,68771,68777,68791,68813,68819,68821,68863,68879,
68881,68891,68897,68899,68903,68909,68917,68927,68947,68963,68993,69001,
69011,69019,69029,69031,69061,69067,69073,69109,69119,69127,69143,69149,
69151,69163,69191,69193,69197,69203,69221,69233,69239,69247,69257,69259,
69263,69313,69317,69337,69341,69371,69379,69383,69389,69401,69403,69427,
69431,69439,69457,69463,69467,69473,69481,69491,69493,69497,69499,69539,
69557,69593,69623,69653,69661,69677,69691,69697,69709,69737,69739,69761,
69763,69767,69779,69809,69821,69827,69829,69833,69847,69857,69859,69877,
69899,69911,69929,69931,69941,69959,69991,69997,70001,70003,70009,70019,
70039,70051,70061,70067,70079,70099,70111,70117,70121,70123,70139,70141,
70157,70163,70177,70181,70183,70199,70201,70207,70223,70229,70237,70241,
70249,70271,70289,70297,70309,70313,70321,70327,70351,70373,70379,70381,
70393,70423,70429,70439,70451,70457,70459,70481,70487,70489,70501,70507,
70529,70537,70549,70571,70573,70583,70589,70607,70619,70621,70627,70639,
70657,70663,70667,70687,70709,70717,70729,70753,70769,70783,70793,70823,
70841,70843,70849,70853,70867,70877,70879,70891,70901,70913,70919,70921,
70937,70949,70951,70957,70969,70979,70981,70991,70997,70999,71011,71023,
71039,71059,71069,71081,71089,71119,71129,71143,71147,71153,71161,71167,
71171,71191,71209,71233,71237,71249,71257,71261,71263,71287,71293,71317,
71327,71329,71333,71339,71341,71347,71353,71359,71363,71387,71389,71399,
71411,71413,71419,71429,71437,71443,71453,71471,71473,71479,71483,71503,
71527,71537,71549,71551,71563,71569,71593,71597,71633,71647,71663,71671,
71693,71699,71707,71711,71713,71719,71741,71761,71777,71789,71807,71809,
71821,71837,71843,71849,71861,71867,71879,71881,71887,71899,71909,71917,
71933,71941,71947,71963,71971,71983,71987,71993,71999,72019,72031,72043,
72047,72053,72073,72077,72089,72091,72101,72103,72109,72139,72161,72167,
72169,72173,72211,72221,72223,72227,72229,72251,72253,72269,72271,72277,
72287,72307,72313,72337,72341,72353,72367,72379,72383,72421,72431,72461,
72467,72469,72481,72493,72497,72503,72533,72547,72551,72559,72577,72613,
72617,72623,72643,72647,72649,72661,72671,72673,72679,72689,72701,72707,
72719,72727,72733,72739,72763,72767,72797,72817,72823,72859,72869,72871,
72883,72889,72893,72901,72907,72911,72923,72931,72937,72949,72953,72959,
72973,72977,72997,73009,73013,73019,73037,73039,73043,73061,73063,73079,
73091,73121,73127,73133,73141,73181,73189,73237,73243,73259,73277,73291,
73303,73309,73327,73331,73351,73361,73363,73369,73379,73387,73417,73421,
73433,73453,73459,73471,73477,73483,73517,73523,73529,73547,73553,73561,
73571,73583,73589,73597,73607,73609,73613,73637,73643,73651,73673,73679,
73681,73693,73699,73709,73721,73727,73751,73757,73771,73783,73819,73823,
73847,73849,73859,73867,73877,73883,73897,73907,73939,73943,73951,73961,
73973,73999,74017,74021,74027,74047,74051,74071,74077,74093,74099,74101,
74131,74143,74149,74159,74161,74167,74177,74189,74197,74201,74203,74209,
74219,74231,74257,74279,74287,74293,74297,74311,74317,74323,74353,74357,
74363,74377,74381,74383,74411,74413,74419,74441,74449,74453,74471,74489,
74507,74509,74521,74527,74531,74551,74561,74567,74573,74587,74597,74609,
74611,74623,74653,74687,74699,74707,74713,74717,74719,74729,74731,74747,
74759,74761,74771,74779,74797,74821,74827,74831,74843,74857,74861,74869,
74873,74887,74891,74897,74903,74923,74929,74933,74941,74959,75011,75013,
75017,75029,75037,75041,75079,75083,75109,75133,75149,75161,75167,75169,
75181,75193,75209,75211,75217,75223,75227,75239,75253,75269,75277,75289,
75307,75323,75329,75337,75347,75353,75367,75377,75389,75391,75401,75403,
75407,75431,75437,75479,75503,75511,75521,75527,75533,75539,75541,75553,
75557,75571,75577,75583,75611,75617,75619,75629,75641,75653,75659,75679,
75683,75689,75703,75707,75709,75721,75731,75743,75767,75773,75781,75787,
75793,75797,75821,75833,75853,75869,75883,75913,75931,75937,75941,75967,
75979,75983,75989,75991,75997,76001,76003,76031,76039,76079,76081,76091,
76099,76103,76123,76129,76147,76157,76159,76163,76207,76213,76231,76243,
76249,76253,76259,76261,76283,76289,76303,76333,76343,76367,76369,76379,
76387,76403,76421,76423,76441,76463,76471,76481,76487,76493,76507,76511,
76519,76537,76541,76543,76561,76579,76597,76603,76607,76631,76649,76651,76667,76673,76679,76697,76717,76733,76753,76757,76771,76777,76781,76801,
76819,76829,76831,76837,76847,76871,76873,76883,76907,76913,76919,76943,
76949,76961,76963,76991,77003,77017,77023,77029,77041,77047,77069,77081,
77093,77101,77137,77141,77153,77167,77171,77191,77201,77213,77237,77239,
77243,77249,77261,77263,77267,77269,77279,77291,77317,77323,77339,77347,
77351,77359,77369,77377,77383,77417,77419,77431,77447,77471,77477,77479,
77489,77491,77509,77513,77521,77527,77543,77549,77551,77557,77563,77569,
77573,77587,77591,77611,77617,77621,77641,77647,77659,77681,77687,77689,
77699,77711,77713,77719,77723,77731,77743,77747,77761,77773,77783,77797,
77801,77813,77839,77849,77863,77867,77893,77899,77929,77933,77951,77969,
77977,77983,77999,78007,78017,78031,78041,78049,78059,78079,78101,78121,
78137,78139,78157,78163,78167,78173,78179,78191,78193,78203,78229,78233,
78241,78259,78277,78283,78301,78307,78311,78317,78341,78347,78367,78401,
78427,78437,78439,78467,78479,78487,78497,78509,78511,78517,78539,78541,
78553,78569,78571,78577,78583,78593,78607,78623,78643,78649,78653,78691,
78697,78707,78713,78721,78737,78779,78781,78787,78791,78797,78803,78809,
78823,78839,78853,78857,78877,78887,78889,78893,78901,78919,78929,78941,
78977,78979,78989,79031,79039,79043,79063,79087,79103,79111,79133,79139,
79147,79151,79153,79159,79181,79187,79193,79201,79229,79231,79241,79259,
79273,79279,79283,79301,79309,79319,79333,79337,79349,79357,79367,79379,
79393,79397,79399,79411,79423,79427,79433,79451,79481,79493,79531,79537,
79549,79559,79561,79579,79589,79601,79609,79613,79621,79627,79631,79633,
79657,79669,79687,79691,79693,79697,79699,79757,79769,79777,79801,79811,
79813,79817,79823,79829,79841,79843,79847,79861,79867,79873,79889,79901,
79903,79907,79939,79943,79967,79973,79979,79987,79997,79999,80021,80039,
80051,80071,80077,80107,80111,80141,80147,80149,80153,80167,80173,80177,
80191,80207,80209,80221,80231,80233,80239,80251,80263,80273,80279,80287,
80309,80317,80329,80341,80347,80363,80369,80387,80407,80429,80447,80449,
80471,80473,80489,80491,80513,80527,80537,80557,80567,80599,80603,80611,
80621,80627,80629,80651,80657,80669,80671,80677,80681,80683,80687,80701,
80713,80737,80747,80749,80761,80777,80779,80783,80789,80803,80809,80819,
80831,80833,80849,80863,80897,80909,80911,80917,80923,80929,80933,80953]
b=[]
for m in a:
x=a.index(m)
if(x in a):
b.append(m)
s=[]
s.append(b[0])
for i in range(1,1000):
s.append((b[i]+s[i-1])%1000000007)
for j in range(int(input())):
n=int(input())
print(s[n-1])
|
a=[-1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,
103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,
199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,
313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,
433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,
563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,
673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,
811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,
941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,
1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,
1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,
1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,
1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,
1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,
1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,
1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,
1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,
1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,
2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,
2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,
2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,
2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,
2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,
2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,
2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,
2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,
3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,
3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,
3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,
3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,
3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,
3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,
3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,
3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,
4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,
4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,
4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,
4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,
4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,
4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,
4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,
4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,
5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,
5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,
5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,
5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,
5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,
5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,
5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,
5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,
6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,
6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,
6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,
6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,
6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,
6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,
6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,
6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,
7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,
7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,
7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,
7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,
7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,
7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919,7927,
7933,7937,7949,7951,7963,7993,8009,8011,8017,8039,8053,8059,8069,8081,8087,
8089,8093,8101,8111,8117,8123,8147,8161,8167,8171,8179,8191,8209,8219,8221,
8231,8233,8237,8243,8263,8269,8273,8287,8291,8293,8297,8311,8317,8329,8353,
8363,8369,8377,8387,8389,8419,8423,8429,8431,8443,8447,8461,8467,8501,8513,
8521,8527,8537,8539,8543,8563,8573,8581,8597,8599,8609,8623,8627,8629,8641,
8647,8663,8669,8677,8681,8689,8693,8699,8707,8713,8719,8731,8737,8741,8747,
8753,8761,8779,8783,8803,8807,8819,8821,8831,8837,8839,8849,8861,8863,8867,
8887,8893,8923,8929,8933,8941,8951,8963,8969,8971,8999,9001,9007,9011,9013,
9029,9041,9043,9049,9059,9067,9091,9103,9109,9127,9133,9137,9151,9157,9161,
9173,9181,9187,9199,9203,9209,9221,9227,9239,9241,9257,9277,9281,9283,9293,
9311,9319,9323,9337,9341,9343,9349,9371,9377,9391,9397,9403,9413,9419,9421,
9431,9433,9437,9439,9461,9463,9467,9473,9479,9491,9497,9511,9521,9533,9539,
9547,9551,9587,9601,9613,9619,9623,9629,9631,9643,9649,9661,9677,9679,9689,
9697,9719,9721,9733,9739,9743,9749,9767,9769,9781,9787,9791,9803,9811,9817,
9829,9833,9839,9851,9857,9859,9871,9883,9887,9901,9907,9923,9929,9931,9941,
9949,9967,9973,10007,10009,10037,10039,10061,10067,10069,10079,10091,10093,
10099,10103,10111,10133,10139,10141,10151,10159,10163,10169,10177,10181,
10193,10211,10223,10243,10247,10253,10259,10267,10271,10273,10289,10301,
10303,10313,10321,10331,10333,10337,10343,10357,10369,10391,10399,10427,
10429,10433,10453,10457,10459,10463,10477,10487,10499,10501,10513,10529,
10531,10559,10567,10589,10597,10601,10607,10613,10627,10631,10639,10651,
10657,10663,10667,10687,10691,10709,10711,10723,10729,10733,10739,10753,
10771,10781,10789,10799,10831,10837,10847,10853,10859,10861,10867,10883,
10889,10891,10903,10909,10937,10939,10949,10957,10973,10979,10987,10993,
11003,11027,11047,11057,11059,11069,11071,11083,11087,11093,11113,11117,
11119,11131,11149,11159,11161,11171,11173,11177,11197,11213,11239,11243,
11251,11257,11261,11273,11279,11287,11299,11311,11317,11321,11329,11351,
11353,11369,11383,11393,11399,11411,11423,11437,11443,11447,11467,11471,
11483,11489,11491,11497,11503,11519,11527,11549,11551,11579,11587,11593,
11597,11617,11621,11633,11657,11677,11681,11689,11699,11701,11717,11719,
11731,11743,11777,11779,11783,11789,11801,11807,11813,11821,11827,11831,
11833,11839,11863,11867,11887,11897,11903,11909,11923,11927,11933,11939,
11941,11953,11959,11969,11971,11981,11987,12007,12011,12037,12041,12043,
12049,12071,12073,12097,12101,12107,12109,12113,12119,12143,12149,12157,
12161,12163,12197,12203,12211,12227,12239,12241,12251,12253,12263,12269,
12277,12281,12289,12301,12323,12329,12343,12347,12373,12377,12379,12391,
12401,12409,12413,12421,12433,12437,12451,12457,12473,12479,12487,12491,
12497,12503,12511,12517,12527,12539,12541,12547,12553,12569,12577,12583,
12589,12601,12611,12613,12619,12637,12641,12647,12653,12659,12671,12689,
12697,12703,12713,12721,12739,12743,12757,12763,12781,12791,12799,12809,
12821,12823,12829,12841,12853,12889,12893,12899,12907,12911,12917,12919,
12923,12941,12953,12959,12967,12973,12979,12983,13001,13003,13007,13009,
13033,13037,13043,13049,13063,13093,13099,13103,13109,13121,13127,13147,
13151,13159,13163,13171,13177,13183,13187,13217,13219,13229,13241,13249,
13259,13267,13291,13297,13309,13313,13327,13331,13337,13339,13367,13381,
13397,13399,13411,13417,13421,13441,13451,13457,13463,13469,13477,13487,
13499,13513,13523,13537,13553,13567,13577,13591,13597,13613,13619,13627,
13633,13649,13669,13679,13681,13687,13691,13693,13697,13709,13711,13721,
13723,13729,13751,13757,13759,13763,13781,13789,13799,13807,13829,13831,
13841,13859,13873,13877,13879,13883,13901,13903,13907,13913,13921,13931,
13933,13963,13967,13997,13999,14009,14011,14029,14033,14051,14057,14071,
14081,14083,14087,14107,14143,14149,14153,14159,14173,14177,14197,14207,
14221,14243,14249,14251,14281,14293,14303,14321,14323,14327,14341,14347,
14369,14387,14389,14401,14407,14411,14419,14423,14431,14437,14447,14449,
14461,14479,14489,14503,14519,14533,14537,14543,14549,14551,14557,14561,
14563,14591,14593,14621,14627,14629,14633,14639,14653,14657,14669,14683,
14699,14713,14717,14723,14731,14737,14741,14747,14753,14759,14767,14771,
14779,14783,14797,14813,14821,14827,14831,14843,14851,14867,14869,14879,
14887,14891,14897,14923,14929,14939,14947,14951,14957,14969,14983,15013,
15017,15031,15053,15061,15073,15077,15083,15091,15101,15107,15121,15131,
15137,15139,15149,15161,15173,15187,15193,15199,15217,15227,15233,15241,
15259,15263,15269,15271,15277,15287,15289,15299,15307,15313,15319,15329,
15331,15349,15359,15361,15373,15377,15383,15391,15401,15413,15427,15439,
15443,15451,15461,15467,15473,15493,15497,15511,15527,15541,15551,15559,
15569,15581,15583,15601,15607,15619,15629,15641,15643,15647,15649,15661,
15667,15671,15679,15683,15727,15731,15733,15737,15739,15749,15761,15767,
15773,15787,15791,15797,15803,15809,15817,15823,15859,15877,15881,15887,
15889,15901,15907,15913,15919,15923,15937,15959,15971,15973,15991,16001,
16007,16033,16057,16061,16063,16067,16069,16073,16087,16091,16097,16103,
16111,16127,16139,16141,16183,16187,16189,16193,16217,16223,16229,16231,
16249,16253,16267,16273,16301,16319,16333,16339,16349,16361,16363,16369,
16381,16411,16417,16421,16427,16433,16447,16451,16453,16477,16481,16487,
16493,16519,16529,16547,16553,16561,16567,16573,16603,16607,16619,16631,
16633,16649,16651,16657,16661,16673,16691,16693,16699,16703,16729,16741,
16747,16759,16763,16787,16811,16823,16829,16831,16843,16871,16879,16883,
16889,16901,16903,16921,16927,16931,16937,16943,16963,16979,16981,16987,
16993,17011,17021,17027,17029,17033,17041,17047,17053,17077,17093,17099,
17107,17117,17123,17137,17159,17167,17183,17189,17191,17203,17207,17209,
17231,17239,17257,17291,17293,17299,17317,17321,17327,17333,17341,17351,
17359,17377,17383,17387,17389,17393,17401,17417,17419,17431,17443,17449,
17467,17471,17477,17483,17489,17491,17497,17509,17519,17539,17551,17569,
17573,17579,17581,17597,17599,17609,17623,17627,17657,17659,17669,17681,
17683,17707,17713,17729,17737,17747,17749,17761,17783,17789,17791,17807,
17827,17837,17839,17851,17863,17881,17891,17903,17909,17911,17921,17923,
17929,17939,17957,17959,17971,17977,17981,17987,17989,18013,18041,18043,
18047,18049,18059,18061,18077,18089,18097,18119,18121,18127,18131,18133,
18143,18149,18169,18181,18191,18199,18211,18217,18223,18229,18233,18251,
18253,18257,18269,18287,18289,18301,18307,18311,18313,18329,18341,18353,
18367,18371,18379,18397,18401,18413,18427,18433,18439,18443,18451,18457,
18461,18481,18493,18503,18517,18521,18523,18539,18541,18553,18583,18587,
18593,18617,18637,18661,18671,18679,18691,18701,18713,18719,18731,18743,
18749,18757,18773,18787,18793,18797,18803,18839,18859,18869,18899,18911,
18913,18917,18919,18947,18959,18973,18979,19001,19009,19013,19031,19037,
19051,19069,19073,19079,19081,19087,19121,19139,19141,19157,19163,19181,
19183,19207,19211,19213,19219,19231,19237,19249,19259,19267,19273,19289,
19301,19309,19319,19333,19373,19379,19381,19387,19391,19403,19417,19421,
19423,19427,19429,19433,19441,19447,19457,19463,19469,19471,19477,19483,
19489,19501,19507,19531,19541,19543,19553,19559,19571,19577,19583,19597,
19603,19609,19661,19681,19687,19697,19699,19709,19717,19727,19739,19751,
19753,19759,19763,19777,19793,19801,19813,19819,19841,19843,19853,19861,
19867,19889,19891,19913,19919,19927,19937,19949,19961,19963,19973,19979,
19991,19993,19997,20011,20021,20023,20029,20047,20051,20063,20071,20089,
20101,20107,20113,20117,20123,20129,20143,20147,20149,20161,20173,20177,
20183,20201,20219,20231,20233,20249,20261,20269,20287,20297,20323,20327,
20333,20341,20347,20353,20357,20359,20369,20389,20393,20399,20407,20411,
20431,20441,20443,20477,20479,20483,20507,20509,20521,20533,20543,20549,
20551,20563,20593,20599,20611,20627,20639,20641,20663,20681,20693,20707,
20717,20719,20731,20743,20747,20749,20753,20759,20771,20773,20789,20807,
20809,20849,20857,20873,20879,20887,20897,20899,20903,20921,20929,20939,
20947,20959,20963,20981,20983,21001,21011,21013,21017,21019,21023,21031,
21059,21061,21067,21089,21101,21107,21121,21139,21143,21149,21157,21163,
21169,21179,21187,21191,21193,21211,21221,21227,21247,21269,21277,21283,
21313,21317,21319,21323,21341,21347,21377,21379,21383,21391,21397,21401,
21407,21419,21433,21467,21481,21487,21491,21493,21499,21503,21517,21521,
21523,21529,21557,21559,21563,21569,21577,21587,21589,21599,21601,21611,
21613,21617,21647,21649,21661,21673,21683,21701,21713,21727,21737,21739,
21751,21757,21767,21773,21787,21799,21803,21817,21821,21839,21841,21851,
21859,21863,21871,21881,21893,21911,21929,21937,21943,21961,21977,21991,
21997,22003,22013,22027,22031,22037,22039,22051,22063,22067,22073,22079,
22091,22093,22109,22111,22123,22129,22133,22147,22153,22157,22159,22171,
22189,22193,22229,22247,22259,22271,22273,22277,22279,22283,22291,22303,
22307,22343,22349,22367,22369,22381,22391,22397,22409,22433,22441,22447,
22453,22469,22481,22483,22501,22511,22531,22541,22543,22549,22567,22571,
22573,22613,22619,22621,22637,22639,22643,22651,22669,22679,22691,22697,
22699,22709,22717,22721,22727,22739,22741,22751,22769,22777,22783,22787,
22807,22811,22817,22853,22859,22861,22871,22877,22901,22907,22921,22937,
22943,22961,22963,22973,22993,23003,23011,23017,23021,23027,23029,23039,
23041,23053,23057,23059,23063,23071,23081,23087,23099,23117,23131,23143,
23159,23167,23173,23189,23197,23201,23203,23209,23227,23251,23269,23279,
23291,23293,23297,23311,23321,23327,23333,23339,23357,23369,23371,23399,
23417,23431,23447,23459,23473,23497,23509,23531,23537,23539,23549,23557,
23561,23563,23567,23581,23593,23599,23603,23609,23623,23627,23629,23633,
23663,23669,23671,23677,23687,23689,23719,23741,23743,23747,23753,23761,
23767,23773,23789,23801,23813,23819,23827,23831,23833,23857,23869,23873,
23879,23887,23893,23899,23909,23911,23917,23929,23957,23971,23977,23981,
23993,24001,24007,24019,24023,24029,24043,24049,24061,24071,24077,24083,
24091,24097,24103,24107,24109,24113,24121,24133,24137,24151,24169,24179,
24181,24197,24203,24223,24229,24239,24247,24251,24281,24317,24329,24337,
24359,24371,24373,24379,24391,24407,24413,24419,24421,24439,24443,24469,
24473,24481,24499,24509,24517,24527,24533,24547,24551,24571,24593,24611,
24623,24631,24659,24671,24677,24683,24691,24697,24709,24733,24749,24763,
24767,24781,24793,24799,24809,24821,24841,24847,24851,24859,24877,24889,
24907,24917,24919,24923,24943,24953,24967,24971,24977,24979,24989,25013,
25031,25033,25037,25057,25073,25087,25097,25111,25117,25121,25127,25147,
25153,25163,25169,25171,25183,25189,25219,25229,25237,25243,25247,25253,
25261,25301,25303,25307,25309,25321,25339,25343,25349,25357,25367,25373,
25391,25409,25411,25423,25439,25447,25453,25457,25463,25469,25471,25523,
25537,25541,25561,25577,25579,25583,25589,25601,25603,25609,25621,25633,
25639,25643,25657,25667,25673,25679,25693,25703,25717,25733,25741,25747,
25759,25763,25771,25793,25799,25801,25819,25841,25847,25849,25867,25873,
25889,25903,25913,25919,25931,25933,25939,25943,25951,25969,25981,25997,
25999,26003,26017,26021,26029,26041,26053,26083,26099,26107,26111,26113,
26119,26141,26153,26161,26171,26177,26183,26189,26203,26209,26227,26237,
26249,26251,26261,26263,26267,26293,26297,26309,26317,26321,26339,26347,
26357,26371,26387,26393,26399,26407,26417,26423,26431,26437,26449,26459,
26479,26489,26497,26501,26513,26539,26557,26561,26573,26591,26597,26627,
26633,26641,26647,26669,26681,26683,26687,26693,26699,26701,26711,26713,
26717,26723,26729,26731,26737,26759,26777,26783,26801,26813,26821,26833,
26839,26849,26861,26863,26879,26881,26891,26893,26903,26921,26927,26947,
26951,26953,26959,26981,26987,26993,27011,27017,27031,27043,27059,27061,
27067,27073,27077,27091,27103,27107,27109,27127,27143,27179,27191,27197,
27211,27239,27241,27253,27259,27271,27277,27281,27283,27299,27329,27337,
27361,27367,27397,27407,27409,27427,27431,27437,27449,27457,27479,27481,
27487,27509,27527,27529,27539,27541,27551,27581,27583,27611,27617,27631,
27647,27653,27673,27689,27691,27697,27701,27733,27737,27739,27743,27749,
27751,27763,27767,27773,27779,27791,27793,27799,27803,27809,27817,27823,
27827,27847,27851,27883,27893,27901,27917,27919,27941,27943,27947,27953,
27961,27967,27983,27997,28001,28019,28027,28031,28051,28057,28069,28081,
28087,28097,28099,28109,28111,28123,28151,28163,28181,28183,28201,28211,
28219,28229,28277,28279,28283,28289,28297,28307,28309,28319,28349,28351,
28387,28393,28403,28409,28411,28429,28433,28439,28447,28463,28477,28493,
28499,28513,28517,28537,28541,28547,28549,28559,28571,28573,28579,28591,
28597,28603,28607,28619,28621,28627,28631,28643,28649,28657,28661,28663,
28669,28687,28697,28703,28711,28723,28729,28751,28753,28759,28771,28789,
28793,28807,28813,28817,28837,28843,28859,28867,28871,28879,28901,28909,
28921,28927,28933,28949,28961,28979,29009,29017,29021,29023,29027,29033,
29059,29063,29077,29101,29123,29129,29131,29137,29147,29153,29167,29173,
29179,29191,29201,29207,29209,29221,29231,29243,29251,29269,29287,29297,
29303,29311,29327,29333,29339,29347,29363,29383,29387,29389,29399,29401,
29411,29423,29429,29437,29443,29453,29473,29483,29501,29527,29531,29537,
29567,29569,29573,29581,29587,29599,29611,29629,29633,29641,29663,29669,
29671,29683,29717,29723,29741,29753,29759,29761,29789,29803,29819,29833,
29837,29851,29863,29867,29873,29879,29881,29917,29921,29927,29947,29959,
29983,29989,30011,30013,30029,30047,30059,30071,30089,30091,30097,30103,
30109,30113,30119,30133,30137,30139,30161,30169,30181,30187,30197,30203,
30211,30223,30241,30253,30259,30269,30271,30293,30307,30313,30319,30323,
30341,30347,30367,30389,30391,30403,30427,30431,30449,30467,30469,30491,
30493,30497,30509,30517,30529,30539,30553,30557,30559,30577,30593,30631,
30637,30643,30649,30661,30671,30677,30689,30697,30703,30707,30713,30727,
30757,30763,30773,30781,30803,30809,30817,30829,30839,30841,30851,30853,
30859,30869,30871,30881,30893,30911,30931,30937,30941,30949,30971,30977,
30983,31013,31019,31033,31039,31051,31063,31069,31079,31081,31091,31121,
31123,31139,31147,31151,31153,31159,31177,31181,31183,31189,31193,31219,
31223,31231,31237,31247,31249,31253,31259,31267,31271,31277,31307,31319,
31321,31327,31333,31337,31357,31379,31387,31391,31393,31397,31469,31477,
31481,31489,31511,31513,31517,31531,31541,31543,31547,31567,31573,31583,
31601,31607,31627,31643,31649,31657,31663,31667,31687,31699,31721,31723,
31727,31729,31741,31751,31769,31771,31793,31799,31817,31847,31849,31859,
31873,31883,31891,31907,31957,31963,31973,31981,31991,32003,32009,32027,
32029,32051,32057,32059,32063,32069,32077,32083,32089,32099,32117,32119,
32141,32143,32159,32173,32183,32189,32191,32203,32213,32233,32237,32251,
32257,32261,32297,32299,32303,32309,32321,32323,32327,32341,32353,32359,
32363,32369,32371,32377,32381,32401,32411,32413,32423,32429,32441,32443,
32467,32479,32491,32497,32503,32507,32531,32533,32537,32561,32563,32569,
32573,32579,32587,32603,32609,32611,32621,32633,32647,32653,32687,32693,
32707,32713,32717,32719,32749,32771,32779,32783,32789,32797,32801,32803,
32831,32833,32839,32843,32869,32887,32909,32911,32917,32933,32939,32941,
32957,32969,32971,32983,32987,32993,32999,33013,33023,33029,33037,33049,
33053,33071,33073,33083,33091,33107,33113,33119,33149,33151,33161,33179,
33181,33191,33199,33203,33211,33223,33247,33287,33289,33301,33311,33317,
33329,33331,33343,33347,33349,33353,33359,33377,33391,33403,33409,33413,
33427,33457,33461,33469,33479,33487,33493,33503,33521,33529,33533,33547,
33563,33569,33577,33581,33587,33589,33599,33601,33613,33617,33619,33623,
33629,33637,33641,33647,33679,33703,33713,33721,33739,33749,33751,33757,
33767,33769,33773,33791,33797,33809,33811,33827,33829,33851,33857,33863,
33871,33889,33893,33911,33923,33931,33937,33941,33961,33967,33997,34019,
34031,34033,34039,34057,34061,34123,34127,34129,34141,34147,34157,34159,
34171,34183,34211,34213,34217,34231,34253,34259,34261,34267,34273,34283,
34297,34301,34303,34313,34319,34327,34337,34351,34361,34367,34369,34381,
34403,34421,34429,34439,34457,34469,34471,34483,34487,34499,34501,34511,
34513,34519,34537,34543,34549,34583,34589,34591,34603,34607,34613,34631,
34649,34651,34667,34673,34679,34687,34693,34703,34721,34729,34739,34747,
34757,34759,34763,34781,34807,34819,34841,34843,34847,34849,34871,34877,
34883,34897,34913,34919,34939,34949,34961,34963,34981,35023,35027,35051,
35053,35059,35069,35081,35083,35089,35099,35107,35111,35117,35129,35141,
35149,35153,35159,35171,35201,35221,35227,35251,35257,35267,35279,35281,
35291,35311,35317,35323,35327,35339,35353,35363,35381,35393,35401,35407,
35419,35423,35437,35447,35449,35461,35491,35507,35509,35521,35527,35531,
35533,35537,35543,35569,35573,35591,35593,35597,35603,35617,35671,35677,
35729,35731,35747,35753,35759,35771,35797,35801,35803,35809,35831,35837,
35839,35851,35863,35869,35879,35897,35899,35911,35923,35933,35951,35963,
35969,35977,35983,35993,35999,36007,36011,36013,36017,36037,36061,36067,
36073,36083,36097,36107,36109,36131,36137,36151,36161,36187,36191,36209,
36217,36229,36241,36251,36263,36269,36277,36293,36299,36307,36313,36319,
36341,36343,36353,36373,36383,36389,36433,36451,36457,36467,36469,36473,
36479,36493,36497,36523,36527,36529,36541,36551,36559,36563,36571,36583,
36587,36599,36607,36629,36637,36643,36653,36671,36677,36683,36691,36697,
36709,36713,36721,36739,36749,36761,36767,36779,36781,36787,36791,36793,
36809,36821,36833,36847,36857,36871,36877,36887,36899,36901,36913,36919,
36923,36929,36931,36943,36947,36973,36979,36997,37003,37013,37019,37021,
37039,37049,37057,37061,37087,37097,37117,37123,37139,37159,37171,37181,
37189,37199,37201,37217,37223,37243,37253,37273,37277,37307,37309,37313,
37321,37337,37339,37357,37361,37363,37369,37379,37397,37409,37423,37441,
37447,37463,37483,37489,37493,37501,37507,37511,37517,37529,37537,37547,
37549,37561,37567,37571,37573,37579,37589,37591,37607,37619,37633,37643,
37649,37657,37663,37691,37693,37699,37717,37747,37781,37783,37799,37811,
37813,37831,37847,37853,37861,37871,37879,37889,37897,37907,37951,37957,
37963,37967,37987,37991,37993,37997,38011,38039,38047,38053,38069,38083,
38113,38119,38149,38153,38167,38177,38183,38189,38197,38201,38219,38231,
38237,38239,38261,38273,38281,38287,38299,38303,38317,38321,38327,38329,
38333,38351,38371,38377,38393,38431,38447,38449,38453,38459,38461,38501,
38543,38557,38561,38567,38569,38593,38603,38609,38611,38629,38639,38651,
38653,38669,38671,38677,38693,38699,38707,38711,38713,38723,38729,38737,
38747,38749,38767,38783,38791,38803,38821,38833,38839,38851,38861,38867,
38873,38891,38903,38917,38921,38923,38933,38953,38959,38971,38977,38993,
39019,39023,39041,39043,39047,39079,39089,39097,39103,39107,39113,39119,
39133,39139,39157,39161,39163,39181,39191,39199,39209,39217,39227,39229,
39233,39239,39241,39251,39293,39301,39313,39317,39323,39341,39343,39359,
39367,39371,39373,39383,39397,39409,39419,39439,39443,39451,39461,39499,
39503,39509,39511,39521,39541,39551,39563,39569,39581,39607,39619,39623,
39631,39659,39667,39671,39679,39703,39709,39719,39727,39733,39749,39761,
39769,39779,39791,39799,39821,39827,39829,39839,39841,39847,39857,39863,
39869,39877,39883,39887,39901,39929,39937,39953,39971,39979,39983,39989,
40009,40013,40031,40037,40039,40063,40087,40093,40099,40111,40123,40127,
40129,40151,40153,40163,40169,40177,40189,40193,40213,40231,40237,40241,
40253,40277,40283,40289,40343,40351,40357,40361,40387,40423,40427,40429,
40433,40459,40471,40483,40487,40493,40499,40507,40519,40529,40531,40543,
40559,40577,40583,40591,40597,40609,40627,40637,40639,40693,40697,40699,
40709,40739,40751,40759,40763,40771,40787,40801,40813,40819,40823,40829,
40841,40847,40849,40853,40867,40879,40883,40897,40903,40927,40933,40939,
40949,40961,40973,40993,41011,41017,41023,41039,41047,41051,41057,41077,
41081,41113,41117,41131,41141,41143,41149,41161,41177,41179,41183,41189,
41201,41203,41213,41221,41227,41231,41233,41243,41257,41263,41269,41281,
41299,41333,41341,41351,41357,41381,41387,41389,41399,41411,41413,41443,
41453,41467,41479,41491,41507,41513,41519,41521,41539,41543,41549,41579,
41593,41597,41603,41609,41611,41617,41621,41627,41641,41647,41651,41659,
41669,41681,41687,41719,41729,41737,41759,41761,41771,41777,41801,41809,
41813,41843,41849,41851,41863,41879,41887,41893,41897,41903,41911,41927,
41941,41947,41953,41957,41959,41969,41981,41983,41999,42013,42017,42019,
42023,42043,42061,42071,42073,42083,42089,42101,42131,42139,42157,42169,
42179,42181,42187,42193,42197,42209,42221,42223,42227,42239,42257,42281,
42283,42293,42299,42307,42323,42331,42337,42349,42359,42373,42379,42391,
42397,42403,42407,42409,42433,42437,42443,42451,42457,42461,42463,42467,
42473,42487,42491,42499,42509,42533,42557,42569,42571,42577,42589,42611,
42641,42643,42649,42667,42677,42683,42689,42697,42701,42703,42709,42719,
42727,42737,42743,42751,42767,42773,42787,42793,42797,42821,42829,42839,
42841,42853,42859,42863,42899,42901,42923,42929,42937,42943,42953,42961,
42967,42979,42989,43003,43013,43019,43037,43049,43051,43063,43067,43093,
43103,43117,43133,43151,43159,43177,43189,43201,43207,43223,43237,43261,
43271,43283,43291,43313,43319,43321,43331,43391,43397,43399,43403,43411,
43427,43441,43451,43457,43481,43487,43499,43517,43541,43543,43573,43577,
43579,43591,43597,43607,43609,43613,43627,43633,43649,43651,43661,43669,
43691,43711,43717,43721,43753,43759,43777,43781,43783,43787,43789,43793,
43801,43853,43867,43889,43891,43913,43933,43943,43951,43961,43963,43969,
43973,43987,43991,43997,44017,44021,44027,44029,44041,44053,44059,44071,
44087,44089,44101,44111,44119,44123,44129,44131,44159,44171,44179,44189,
44201,44203,44207,44221,44249,44257,44263,44267,44269,44273,44279,44281,
44293,44351,44357,44371,44381,44383,44389,44417,44449,44453,44483,44491,
44497,44501,44507,44519,44531,44533,44537,44543,44549,44563,44579,44587,
44617,44621,44623,44633,44641,44647,44651,44657,44683,44687,44699,44701,
44711,44729,44741,44753,44771,44773,44777,44789,44797,44809,44819,44839,
44843,44851,44867,44879,44887,44893,44909,44917,44927,44939,44953,44959,
44963,44971,44983,44987,45007,45013,45053,45061,45077,45083,45119,45121,
45127,45131,45137,45139,45161,45179,45181,45191,45197,45233,45247,45259,
45263,45281,45289,45293,45307,45317,45319,45329,45337,45341,45343,45361,
45377,45389,45403,45413,45427,45433,45439,45481,45491,45497,45503,45523,
45533,45541,45553,45557,45569,45587,45589,45599,45613,45631,45641,45659,
45667,45673,45677,45691,45697,45707,45737,45751,45757,45763,45767,45779,
45817,45821,45823,45827,45833,45841,45853,45863,45869,45887,45893,45943,
45949,45953,45959,45971,45979,45989,46021,46027,46049,46051,46061,46073,
46091,46093,46099,46103,46133,46141,46147,46153,46171,46181,46183,46187,
46199,46219,46229,46237,46261,46271,46273,46279,46301,46307,46309,46327,
46337,46349,46351,46381,46399,46411,46439,46441,46447,46451,46457,46471,
46477,46489,46499,46507,46511,46523,46549,46559,46567,46573,46589,46591,
46601,46619,46633,46639,46643,46649,46663,46679,46681,46687,46691,46703,
46723,46727,46747,46751,46757,46769,46771,46807,46811,46817,46819,46829,
46831,46853,46861,46867,46877,46889,46901,46919,46933,46957,46993,46997,
47017,47041,47051,47057,47059,47087,47093,47111,47119,47123,47129,47137,
47143,47147,47149,47161,47189,47207,47221,47237,47251,47269,47279,47287,
47293,47297,47303,47309,47317,47339,47351,47353,47363,47381,47387,47389,
47407,47417,47419,47431,47441,47459,47491,47497,47501,47507,47513,47521,
47527,47533,47543,47563,47569,47581,47591,47599,47609,47623,47629,47639,
47653,47657,47659,47681,47699,47701,47711,47713,47717,47737,47741,47743,
47777,47779,47791,47797,47807,47809,47819,47837,47843,47857,47869,47881,
47903,47911,47917,47933,47939,47947,47951,47963,47969,47977,47981,48017,
48023,48029,48049,48073,48079,48091,48109,48119,48121,48131,48157,48163,
48179,48187,48193,48197,48221,48239,48247,48259,48271,48281,48299,48311,
48313,48337,48341,48353,48371,48383,48397,48407,48409,48413,48437,48449,
48463,48473,48479,48481,48487,48491,48497,48523,48527,48533,48539,48541,
48563,48571,48589,48593,48611,48619,48623,48647,48649,48661,48673,48677,
48679,48731,48733,48751,48757,48761,48767,48779,48781,48787,48799,48809,
48817,48821,48823,48847,48857,48859,48869,48871,48883,48889,48907,48947,
48953,48973,48989,48991,49003,49009,49019,49031,49033,49037,49043,49057,
49069,49081,49103,49109,49117,49121,49123,49139,49157,49169,49171,49177,
49193,49199,49201,49207,49211,49223,49253,49261,49277,49279,49297,49307,
49331,49333,49339,49363,49367,49369,49391,49393,49409,49411,49417,49429,
49433,49451,49459,49463,49477,49481,49499,49523,49529,49531,49537,49547,
49549,49559,49597,49603,49613,49627,49633,49639,49663,49667,49669,49681,
49697,49711,49727,49739,49741,49747,49757,49783,49787,49789,49801,49807,
49811,49823,49831,49843,49853,49871,49877,49891,49919,49921,49927,49937,
49939,49943,49957,49991,49993,49999,50021,50023,50033,50047,50051,50053,
50069,50077,50087,50093,50101,50111,50119,50123,50129,50131,50147,50153,
50159,50177,50207,50221,50227,50231,50261,50263,50273,50287,50291,50311,
50321,50329,50333,50341,50359,50363,50377,50383,50387,50411,50417,50423,
50441,50459,50461,50497,50503,50513,50527,50539,50543,50549,50551,50581,
50587,50591,50593,50599,50627,50647,50651,50671,50683,50707,50723,50741,
50753,50767,50773,50777,50789,50821,50833,50839,50849,50857,50867,50873,
50891,50893,50909,50923,50929,50951,50957,50969,50971,50989,50993,51001,
51031,51043,51047,51059,51061,51071,51109,51131,51133,51137,51151,51157,
51169,51193,51197,51199,51203,51217,51229,51239,51241,51257,51263,51283,
51287,51307,51329,51341,51343,51347,51349,51361,51383,51407,51413,51419,
51421,51427,51431,51437,51439,51449,51461,51473,51479,51481,51487,51503,
51511,51517,51521,51539,51551,51563,51577,51581,51593,51599,51607,51613,
51631,51637,51647,51659,51673,51679,51683,51691,51713,51719,51721,51749,
51767,51769,51787,51797,51803,51817,51827,51829,51839,51853,51859,51869,
51871,51893,51899,51907,51913,51929,51941,51949,51971,51973,51977,51991,
52009,52021,52027,52051,52057,52067,52069,52081,52103,52121,52127,52147,
52153,52163,52177,52181,52183,52189,52201,52223,52237,52249,52253,52259,
52267,52289,52291,52301,52313,52321,52361,52363,52369,52379,52387,52391,
52433,52453,52457,52489,52501,52511,52517,52529,52541,52543,52553,52561,
52567,52571,52579,52583,52609,52627,52631,52639,52667,52673,52691,52697,
52709,52711,52721,52727,52733,52747,52757,52769,52783,52807,52813,52817,
52837,52859,52861,52879,52883,52889,52901,52903,52919,52937,52951,52957,
52963,52967,52973,52981,52999,53003,53017,53047,53051,53069,53077,53087,
53089,53093,53101,53113,53117,53129,53147,53149,53161,53171,53173,53189,
53197,53201,53231,53233,53239,53267,53269,53279,53281,53299,53309,53323,
53327,53353,53359,53377,53381,53401,53407,53411,53419,53437,53441,53453,
53479,53503,53507,53527,53549,53551,53569,53591,53593,53597,53609,53611,
53617,53623,53629,53633,53639,53653,53657,53681,53693,53699,53717,53719,
53731,53759,53773,53777,53783,53791,53813,53819,53831,53849,53857,53861,
53881,53887,53891,53897,53899,53917,53923,53927,53939,53951,53959,53987,
53993,54001,54011,54013,54037,54049,54059,54083,54091,54101,54121,54133,
54139,54151,54163,54167,54181,54193,54217,54251,54269,54277,54287,54293,
54311,54319,54323,54331,54347,54361,54367,54371,54377,54401,54403,54409,
54413,54419,54421,54437,54443,54449,54469,54493,54497,54499,54503,54517,
54521,54539,54541,54547,54559,54563,54577,54581,54583,54601,54617,54623,
54629,54631,54647,54667,54673,54679,54709,54713,54721,54727,54751,54767,
54773,54779,54787,54799,54829,54833,54851,54869,54877,54881,54907,54917,
54919,54941,54949,54959,54973,54979,54983,55001,55009,55021,55049,55051,
55057,55061,55073,55079,55103,55109,55117,55127,55147,55163,55171,55201,
55207,55213,55217,55219,55229,55243,55249,55259,55291,55313,55331,55333,
55337,55339,55343,55351,55373,55381,55399,55411,55439,55441,55457,55469,
55487,55501,55511,55529,55541,55547,55579,55589,55603,55609,55619,55621,
55631,55633,55639,55661,55663,55667,55673,55681,55691,55697,55711,55717,
55721,55733,55763,55787,55793,55799,55807,55813,55817,55819,55823,55829,
55837,55843,55849,55871,55889,55897,55901,55903,55921,55927,55931,55933,
55949,55967,55987,55997,56003,56009,56039,56041,56053,56081,56087,56093,
56099,56101,56113,56123,56131,56149,56167,56171,56179,56197,56207,56209,
56237,56239,56249,56263,56267,56269,56299,56311,56333,56359,56369,56377,
56383,56393,56401,56417,56431,56437,56443,56453,56467,56473,56477,56479,
56489,56501,56503,56509,56519,56527,56531,56533,56543,56569,56591,56597,
56599,56611,56629,56633,56659,56663,56671,56681,56687,56701,56711,56713,
56731,56737,56747,56767,56773,56779,56783,56807,56809,56813,56821,56827,
56843,56857,56873,56891,56893,56897,56909,56911,56921,56923,56929,56941,
56951,56957,56963,56983,56989,56993,56999,57037,57041,57047,57059,57073,
57077,57089,57097,57107,57119,57131,57139,57143,57149,57163,57173,57179,
57191,57193,57203,57221,57223,57241,57251,57259,57269,57271,57283,57287,
57301,57329,57331,57347,57349,57367,57373,57383,57389,57397,57413,57427,
57457,57467,57487,57493,57503,57527,57529,57557,57559,57571,57587,57593,
57601,57637,57641,57649,57653,57667,57679,57689,57697,57709,57713,57719,
57727,57731,57737,57751,57773,57781,57787,57791,57793,57803,57809,57829,
57839,57847,57853,57859,57881,57899,57901,57917,57923,57943,57947,57973,
57977,57991,58013,58027,58031,58043,58049,58057,58061,58067,58073,58099,
58109,58111,58129,58147,58151,58153,58169,58171,58189,58193,58199,58207,
58211,58217,58229,58231,58237,58243,58271,58309,58313,58321,58337,58363,
58367,58369,58379,58391,58393,58403,58411,58417,58427,58439,58441,58451,
58453,58477,58481,58511,58537,58543,58549,58567,58573,58579,58601,58603,
58613,58631,58657,58661,58679,58687,58693,58699,58711,58727,58733,58741,
58757,58763,58771,58787,58789,58831,58889,58897,58901,58907,58909,58913,
58921,58937,58943,58963,58967,58979,58991,58997,59009,59011,59021,59023,
59029,59051,59053,59063,59069,59077,59083,59093,59107,59113,59119,59123,
59141,59149,59159,59167,59183,59197,59207,59209,59219,59221,59233,59239,
59243,59263,59273,59281,59333,59341,59351,59357,59359,59369,59377,59387,
59393,59399,59407,59417,59419,59441,59443,59447,59453,59467,59471,59473,
59497,59509,59513,59539,59557,59561,59567,59581,59611,59617,59621,59627,
59629,59651,59659,59663,59669,59671,59693,59699,59707,59723,59729,59743,
59747,59753,59771,59779,59791,59797,59809,59833,59863,59879,59887,59921,
59929,59951,59957,59971,59981,59999,60013,60017,60029,60037,60041,60077,
60083,60089,60091,60101,60103,60107,60127,60133,60139,60149,60161,60167,
60169,60209,60217,60223,60251,60257,60259,60271,60289,60293,60317,60331,
60337,60343,60353,60373,60383,60397,60413,60427,60443,60449,60457,60493,
60497,60509,60521,60527,60539,60589,60601,60607,60611,60617,60623,60631,
60637,60647,60649,60659,60661,60679,60689,60703,60719,60727,60733,60737,
60757,60761,60763,60773,60779,60793,60811,60821,60859,60869,60887,60889,
60899,60901,60913,60917,60919,60923,60937,60943,60953,60961,61001,61007,
61027,61031,61043,61051,61057,61091,61099,61121,61129,61141,61151,61153,
61169,61211,61223,61231,61253,61261,61283,61291,61297,61331,61333,61339,
61343,61357,61363,61379,61381,61403,61409,61417,61441,61463,61469,61471,
61483,61487,61493,61507,61511,61519,61543,61547,61553,61559,61561,61583,
61603,61609,61613,61627,61631,61637,61643,61651,61657,61667,61673,61681,
61687,61703,61717,61723,61729,61751,61757,61781,61813,61819,61837,61843,
61861,61871,61879,61909,61927,61933,61949,61961,61967,61979,61981,61987,
61991,62003,62011,62017,62039,62047,62053,62057,62071,62081,62099,62119,
62129,62131,62137,62141,62143,62171,62189,62191,62201,62207,62213,62219,
62233,62273,62297,62299,62303,62311,62323,62327,62347,62351,62383,62401,
62417,62423,62459,62467,62473,62477,62483,62497,62501,62507,62533,62539,
62549,62563,62581,62591,62597,62603,62617,62627,62633,62639,62653,62659,
62683,62687,62701,62723,62731,62743,62753,62761,62773,62791,62801,62819,
62827,62851,62861,62869,62873,62897,62903,62921,62927,62929,62939,62969,
62971,62981,62983,62987,62989,63029,63031,63059,63067,63073,63079,63097,
63103,63113,63127,63131,63149,63179,63197,63199,63211,63241,63247,63277,
63281,63299,63311,63313,63317,63331,63337,63347,63353,63361,63367,63377,
63389,63391,63397,63409,63419,63421,63439,63443,63463,63467,63473,63487,
63493,63499,63521,63527,63533,63541,63559,63577,63587,63589,63599,63601,
63607,63611,63617,63629,63647,63649,63659,63667,63671,63689,63691,63697,
63703,63709,63719,63727,63737,63743,63761,63773,63781,63793,63799,63803,
63809,63823,63839,63841,63853,63857,63863,63901,63907,63913,63929,63949,
63977,63997,64007,64013,64019,64033,64037,64063,64067,64081,64091,64109,
64123,64151,64153,64157,64171,64187,64189,64217,64223,64231,64237,64271,
64279,64283,64301,64303,64319,64327,64333,64373,64381,64399,64403,64433,
64439,64451,64453,64483,64489,64499,64513,64553,64567,64577,64579,64591,
64601,64609,64613,64621,64627,64633,64661,64663,64667,64679,64693,64709,
64717,64747,64763,64781,64783,64793,64811,64817,64849,64853,64871,64877,
64879,64891,64901,64919,64921,64927,64937,64951,64969,64997,65003,65011,
65027,65029,65033,65053,65063,65071,65089,65099,65101,65111,65119,65123,
65129,65141,65147,65167,65171,65173,65179,65183,65203,65213,65239,65257,
65267,65269,65287,65293,65309,65323,65327,65353,65357,65371,65381,65393,
65407,65413,65419,65423,65437,65447,65449,65479,65497,65519,65521,65537,
65539,65543,65551,65557,65563,65579,65581,65587,65599,65609,65617,65629,
65633,65647,65651,65657,65677,65687,65699,65701,65707,65713,65717,65719,
65729,65731,65761,65777,65789,65809,65827,65831,65837,65839,65843,65851,
65867,65881,65899,65921,65927,65929,65951,65957,65963,65981,65983,65993,
66029,66037,66041,66047,66067,66071,66083,66089,66103,66107,66109,66137,
66161,66169,66173,66179,66191,66221,66239,66271,66293,66301,66337,66343,
66347,66359,66361,66373,66377,66383,66403,66413,66431,66449,66457,66463,
66467,66491,66499,66509,66523,66529,66533,66541,66553,66569,66571,66587,
66593,66601,66617,66629,66643,66653,66683,66697,66701,66713,66721,66733,
66739,66749,66751,66763,66791,66797,66809,66821,66841,66851,66853,66863,
66877,66883,66889,66919,66923,66931,66943,66947,66949,66959,66973,66977,
67003,67021,67033,67043,67049,67057,67061,67073,67079,67103,67121,67129,
67139,67141,67153,67157,67169,67181,67187,67189,67211,67213,67217,67219,
67231,67247,67261,67271,67273,67289,67307,67339,67343,67349,67369,67391,
67399,67409,67411,67421,67427,67429,67433,67447,67453,67477,67481,67489,
67493,67499,67511,67523,67531,67537,67547,67559,67567,67577,67579,67589,
67601,67607,67619,67631,67651,67679,67699,67709,67723,67733,67741,67751,
67757,67759,67763,67777,67783,67789,67801,67807,67819,67829,67843,67853,
67867,67883,67891,67901,67927,67931,67933,67939,67943,67957,67961,67967,
67979,67987,67993,68023,68041,68053,68059,68071,68087,68099,68111,68113,
68141,68147,68161,68171,68207,68209,68213,68219,68227,68239,68261,68279,
68281,68311,68329,68351,68371,68389,68399,68437,68443,68447,68449,68473,
68477,68483,68489,68491,68501,68507,68521,68531,68539,68543,68567,68581,
68597,68611,68633,68639,68659,68669,68683,68687,68699,68711,68713,68729,
68737,68743,68749,68767,68771,68777,68791,68813,68819,68821,68863,68879,
68881,68891,68897,68899,68903,68909,68917,68927,68947,68963,68993,69001,
69011,69019,69029,69031,69061,69067,69073,69109,69119,69127,69143,69149,
69151,69163,69191,69193,69197,69203,69221,69233,69239,69247,69257,69259,
69263,69313,69317,69337,69341,69371,69379,69383,69389,69401,69403,69427,
69431,69439,69457,69463,69467,69473,69481,69491,69493,69497,69499,69539,
69557,69593,69623,69653,69661,69677,69691,69697,69709,69737,69739,69761,
69763,69767,69779,69809,69821,69827,69829,69833,69847,69857,69859,69877,
69899,69911,69929,69931,69941,69959,69991,69997,70001,70003,70009,70019,
70039,70051,70061,70067,70079,70099,70111,70117,70121,70123,70139,70141,
70157,70163,70177,70181,70183,70199,70201,70207,70223,70229,70237,70241,
70249,70271,70289,70297,70309,70313,70321,70327,70351,70373,70379,70381,
70393,70423,70429,70439,70451,70457,70459,70481,70487,70489,70501,70507,
70529,70537,70549,70571,70573,70583,70589,70607,70619,70621,70627,70639,
70657,70663,70667,70687,70709,70717,70729,70753,70769,70783,70793,70823,
70841,70843,70849,70853,70867,70877,70879,70891,70901,70913,70919,70921,
70937,70949,70951,70957,70969,70979,70981,70991,70997,70999,71011,71023,
71039,71059,71069,71081,71089,71119,71129,71143,71147,71153,71161,71167,
71171,71191,71209,71233,71237,71249,71257,71261,71263,71287,71293,71317,
71327,71329,71333,71339,71341,71347,71353,71359,71363,71387,71389,71399,
71411,71413,71419,71429,71437,71443,71453,71471,71473,71479,71483,71503,
71527,71537,71549,71551,71563,71569,71593,71597,71633,71647,71663,71671,
71693,71699,71707,71711,71713,71719,71741,71761,71777,71789,71807,71809,
71821,71837,71843,71849,71861,71867,71879,71881,71887,71899,71909,71917,
71933,71941,71947,71963,71971,71983,71987,71993,71999,72019,72031,72043,
72047,72053,72073,72077,72089,72091,72101,72103,72109,72139,72161,72167,
72169,72173,72211,72221,72223,72227,72229,72251,72253,72269,72271,72277,
72287,72307,72313,72337,72341,72353,72367,72379,72383,72421,72431,72461,
72467,72469,72481,72493,72497,72503,72533,72547,72551,72559,72577,72613,
72617,72623,72643,72647,72649,72661,72671,72673,72679,72689,72701,72707,
72719,72727,72733,72739,72763,72767,72797,72817,72823,72859,72869,72871,
72883,72889,72893,72901,72907,72911,72923,72931,72937,72949,72953,72959,
72973,72977,72997,73009,73013,73019,73037,73039,73043,73061,73063,73079,
73091,73121,73127,73133,73141,73181,73189,73237,73243,73259,73277,73291,
73303,73309,73327,73331,73351,73361,73363,73369,73379,73387,73417,73421,
73433,73453,73459,73471,73477,73483,73517,73523,73529,73547,73553,73561,
73571,73583,73589,73597,73607,73609,73613,73637,73643,73651,73673,73679,
73681,73693,73699,73709,73721,73727,73751,73757,73771,73783,73819,73823,
73847,73849,73859,73867,73877,73883,73897,73907,73939,73943,73951,73961,
73973,73999,74017,74021,74027,74047,74051,74071,74077,74093,74099,74101,
74131,74143,74149,74159,74161,74167,74177,74189,74197,74201,74203,74209,
74219,74231,74257,74279,74287,74293,74297,74311,74317,74323,74353,74357,
74363,74377,74381,74383,74411,74413,74419,74441,74449,74453,74471,74489,
74507,74509,74521,74527,74531,74551,74561,74567,74573,74587,74597,74609,
74611,74623,74653,74687,74699,74707,74713,74717,74719,74729,74731,74747,
74759,74761,74771,74779,74797,74821,74827,74831,74843,74857,74861,74869,
74873,74887,74891,74897,74903,74923,74929,74933,74941,74959,75011,75013,
75017,75029,75037,75041,75079,75083,75109,75133,75149,75161,75167,75169,
75181,75193,75209,75211,75217,75223,75227,75239,75253,75269,75277,75289,
75307,75323,75329,75337,75347,75353,75367,75377,75389,75391,75401,75403,
75407,75431,75437,75479,75503,75511,75521,75527,75533,75539,75541,75553,
75557,75571,75577,75583,75611,75617,75619,75629,75641,75653,75659,75679,
75683,75689,75703,75707,75709,75721,75731,75743,75767,75773,75781,75787,
75793,75797,75821,75833,75853,75869,75883,75913,75931,75937,75941,75967,
75979,75983,75989,75991,75997,76001,76003,76031,76039,76079,76081,76091,
76099,76103,76123,76129,76147,76157,76159,76163,76207,76213,76231,76243,
76249,76253,76259,76261,76283,76289,76303,76333,76343,76367,76369,76379,
76387,76403,76421,76423,76441,76463,76471,76481,76487,76493,76507,76511,
76519,76537,76541,76543,76561,76579,76597,76603,76607,76631,76649,76651,76667,76673,76679,76697,76717,76733,76753,76757,76771,76777,76781,76801,
76819,76829,76831,76837,76847,76871,76873,76883,76907,76913,76919,76943,
76949,76961,76963,76991,77003,77017,77023,77029,77041,77047,77069,77081,
77093,77101,77137,77141,77153,77167,77171,77191,77201,77213,77237,77239,
77243,77249,77261,77263,77267,77269,77279,77291,77317,77323,77339,77347,
77351,77359,77369,77377,77383,77417,77419,77431,77447,77471,77477,77479,
77489,77491,77509,77513,77521,77527,77543,77549,77551,77557,77563,77569,
77573,77587,77591,77611,77617,77621,77641,77647,77659,77681,77687,77689,
77699,77711,77713,77719,77723,77731,77743,77747,77761,77773,77783,77797,
77801,77813,77839,77849,77863,77867,77893,77899,77929,77933,77951,77969,
77977,77983,77999,78007,78017,78031,78041,78049,78059,78079,78101,78121,
78137,78139,78157,78163,78167,78173,78179,78191,78193,78203,78229,78233,
78241,78259,78277,78283,78301,78307,78311,78317,78341,78347,78367,78401,
78427,78437,78439,78467,78479,78487,78497,78509,78511,78517,78539,78541,
78553,78569,78571,78577,78583,78593,78607,78623,78643,78649,78653,78691,
78697,78707,78713,78721,78737,78779,78781,78787,78791,78797,78803,78809,
78823,78839,78853,78857,78877,78887,78889,78893,78901,78919,78929,78941,
78977,78979,78989,79031,79039,79043,79063,79087,79103,79111,79133,79139,
79147,79151,79153,79159,79181,79187,79193,79201,79229,79231,79241,79259,
79273,79279,79283,79301,79309,79319,79333,79337,79349,79357,79367,79379,
79393,79397,79399,79411,79423,79427,79433,79451,79481,79493,79531,79537,
79549,79559,79561,79579,79589,79601,79609,79613,79621,79627,79631,79633,
79657,79669,79687,79691,79693,79697,79699,79757,79769,79777,79801,79811,
79813,79817,79823,79829,79841,79843,79847,79861,79867,79873,79889,79901,
79903,79907,79939,79943,79967,79973,79979,79987,79997,79999,80021,80039,
80051,80071,80077,80107,80111,80141,80147,80149,80153,80167,80173,80177,
80191,80207,80209,80221,80231,80233,80239,80251,80263,80273,80279,80287,
80309,80317,80329,80341,80347,80363,80369,80387,80407,80429,80447,80449,
80471,80473,80489,80491,80513,80527,80537,80557,80567,80599,80603,80611,
80621,80627,80629,80651,80657,80669,80671,80677,80681,80683,80687,80701,
80713,80737,80747,80749,80761,80777,80779,80783,80789,80803,80809,80819,
80831,80833,80849,80863,80897,80909,80911,80917,80923,80929,80933,80953]
b=[]
for m in a:
x=a.index(m)
if(x in a):
b.append(m)
s=[]
s.append(b[0])
for i in range(1,1000):
s.append((b[i]+s[i-1])%1000000007)
for j in range(int(input())):
n=int(input())
print(s[n-1])
|
train
|
APPS_structured
|
A function receives a certain numbers of integers ```n1, n2, n3 ..., np```(all positive and different from 0) and a factor ```k, k > 0```
The function rearranges the numbers ```n1, n2, ..., np``` in such order that generates the minimum number concatenating the digits and this number should be divisible by ```k```.
The order that the function receives their arguments is:
```python
rearranger(k, n1, n2, n3,....,np)
```
## Examples
```python
rearranger(4, 32, 3, 34, 7, 12) == "Rearrangement: 12, 3, 34, 7, 32 generates: 12334732 divisible by 4"
rearranger(10, 32, 3, 34, 7, 12) == "There is no possible rearrangement"
```
If there are more than one possible arrengement for the same minimum number, your code should be able to handle those cases:
```python
rearranger(6, 19, 32, 2, 124, 20, 22) == "Rearrangements: 124, 19, 20, 2, 22, 32 and 124, 19, 20, 22, 2, 32 generates: 124192022232 divisible by 6"
```
The arrangements should be in sorted order, as you see: `124, 19, 20, 2, 22, 32` comes first than `124, 19, 20, 22, 2, 32`.
Have an enjoyable time!
(Thanks to `ChristianE.Cooper` for his contribution to this kata)
|
from itertools import permutations
def rearranger(k, *args):
arguments = []
numbers = []
min_num = 10 ** 50
for arg in [*args]:
arguments.append(str(arg))
for perm in permutations(arguments):
num = int("".join(perm))
if num % k == 0:
if num == min_num and not perm in numbers:
numbers.append(perm)
if num < min_num:
min_num = num
numbers = [perm]
if len(numbers) == 0:
answer = "There is no possible rearrangement"
elif len(numbers) == 1:
nums = ', '.join(numbers[0])
answer = f"Rearrangement: {nums} generates: {min_num} divisible by {k}"
elif len(numbers) == 2:
nums1 = ', '.join(numbers[0])
nums2 = ', '.join(numbers[1])
answer = f"Rearrangements: {nums1} and {nums2} generates: {min_num} divisible by {k}"
return answer
|
from itertools import permutations
def rearranger(k, *args):
arguments = []
numbers = []
min_num = 10 ** 50
for arg in [*args]:
arguments.append(str(arg))
for perm in permutations(arguments):
num = int("".join(perm))
if num % k == 0:
if num == min_num and not perm in numbers:
numbers.append(perm)
if num < min_num:
min_num = num
numbers = [perm]
if len(numbers) == 0:
answer = "There is no possible rearrangement"
elif len(numbers) == 1:
nums = ', '.join(numbers[0])
answer = f"Rearrangement: {nums} generates: {min_num} divisible by {k}"
elif len(numbers) == 2:
nums1 = ', '.join(numbers[0])
nums2 = ', '.join(numbers[1])
answer = f"Rearrangements: {nums1} and {nums2} generates: {min_num} divisible by {k}"
return answer
|
train
|
APPS_structured
|
Implement a class/function, which should parse time expressed as `HH:MM:SS`, or `null/nil/None` otherwise.
Any extra characters, or minutes/seconds higher than 59 make the input invalid, and so should return `null/nil/None`.
|
import re
def to_seconds(time):
match=re.fullmatch(r'(\d{2}):([0-5][0-9]):([0-5][0-9])',time)
return int(match.group(1))*3600+int(match.group(2))*60+int(match.group(3)) if match else None
|
import re
def to_seconds(time):
match=re.fullmatch(r'(\d{2}):([0-5][0-9]):([0-5][0-9])',time)
return int(match.group(1))*3600+int(match.group(2))*60+int(match.group(3)) if match else None
|
train
|
APPS_structured
|
-----Problem Statement-----
Sereja has a sequence of n integers a[1], a[2], ..., a[n]. Sereja can do following transformation of the array:
- create a new sequence of n integers b[1], b[2], ..., b[n]in this way: (1 ≤ i ≤ n)
- Replace the sequence a by b, i.e., a[i] = b[i] for all i in [1, n]
Sereja decided to use his transformation k times. Then he computed the value of , where r — the sequence obtained after k transformations of sequence a, as described above.
Sereja lost sequence a, but he was left with the numbers q(r) and k. Now Sereja is interested in the question : what is the number of the sequences of the integers с[1], с[2], ..., с[n], such that 1 ≤ c[i] ≤ m and q(d) = q(r), where d — the sequence obtained after k transformations of sequence c, as described above.
-----Input-----
The first lines contains a single integer T, denoting the number of test cases. Each test case consist of four integers : n, m, q(r), k.
-----Output-----
In a single line print the remainder of division the answer of the problem on number 10^9 + 7.
-----Constraints-----
- 1 ≤ T ≤ 10000
- 1 ≤ n, m, q(r), k ≤ 10^9
-----Example-----
Input:
3
1 1 1 1
2 2 1 1
2 3 1 1
Output:
0
2
4
|
def mod(a,b,c):
x = 1
y = a
while(b>0):
if(b%2==1):
x = (x*y)%c
y = (y*y)%c
b /=2
return x%c
t = int(input())
num = 10**9+7
for i in range(t):
n,m,q,k = list(map(int,input().split()))
if m<=q:
print(0)
else:
a1 = m-q
a2 = mod(q+1,n,num)
a3 = mod(q-1,n,num)
a4 = mod(q,n,num)
a5 = a2-2*a4+a3
ans = a1*a5
print(ans%num)
|
def mod(a,b,c):
x = 1
y = a
while(b>0):
if(b%2==1):
x = (x*y)%c
y = (y*y)%c
b /=2
return x%c
t = int(input())
num = 10**9+7
for i in range(t):
n,m,q,k = list(map(int,input().split()))
if m<=q:
print(0)
else:
a1 = m-q
a2 = mod(q+1,n,num)
a3 = mod(q-1,n,num)
a4 = mod(q,n,num)
a5 = a2-2*a4+a3
ans = a1*a5
print(ans%num)
|
train
|
APPS_structured
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
q = int(input())
for rwere in range(q):
a = input()
b = input()
c = input()
n = len(a)
dasie = True
for i in range(n):
if c[i] == a[i] or c[i] == b[i]:
continue
else:
dasie = False
if dasie:
print("YES")
else:
print("NO")
|
q = int(input())
for rwere in range(q):
a = input()
b = input()
c = input()
n = len(a)
dasie = True
for i in range(n):
if c[i] == a[i] or c[i] == b[i]:
continue
else:
dasie = False
if dasie:
print("YES")
else:
print("NO")
|
train
|
APPS_structured
|
This kata is based on a [variation](https://www.codewars.com/kata/happy-numbers-5) of *Happy Numbers* by TySlothrop. It is advisable to complete it first to grasp the idea and then move on to this one.
___
Hello, my dear friend, and welcome to another *Happy Numbers* kata! What? You're not interested in them anymore? They are all the same? But what if I say that this one is a *performance version*...
___
# Your task:
Write a function `performant_numbers` which takes a number `n` as an argument and returns a list of all *happy numbers* from `1` to `n` inclusive. For example:
```
performant_numbers(10) => [1, 7, 10]
performant_numbers(50) => [1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49]
performant_numbers(100) => [1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100]
```
# Test suite:
* `5000` tests with number `n` being up to `300000`
* The reference solution takes around `4.9` seconds to calculate the result
* you are not allowed to hardcode the sequence: you'll have to compute it (max length of the code: 1700 characters)
___
Will you take up the challenge?
|
from bisect import bisect
def happy_numbers(n):
happy_list = []
happy = {1}
unhappy = set()
for k in range(1, n+1):
i = k
seen = set()
while True:
if i in happy:
happy_list.append(k)
happy |= seen
break
elif i in unhappy or i in seen:
unhappy |= seen
break
seen.add(i)
i = sum(int(d)**2 for d in str(i))
return happy_list
happy_list = happy_numbers(300000)
def performant_numbers(n):
i = bisect(happy_list, n)
return happy_list[:i]
|
from bisect import bisect
def happy_numbers(n):
happy_list = []
happy = {1}
unhappy = set()
for k in range(1, n+1):
i = k
seen = set()
while True:
if i in happy:
happy_list.append(k)
happy |= seen
break
elif i in unhappy or i in seen:
unhappy |= seen
break
seen.add(i)
i = sum(int(d)**2 for d in str(i))
return happy_list
happy_list = happy_numbers(300000)
def performant_numbers(n):
i = bisect(happy_list, n)
return happy_list[:i]
|
train
|
APPS_structured
|
Description
In English we often use "neutral vowel sounds" such as "umm", "err", "ahh" as fillers in conversations to help them run smoothly.
Bob always finds himself saying "err". Infact he adds an "err" to every single word he says that ends in a consonant! Because Bob is odd, he likes to stick to this habit even when emailing.
Task
Bob is begging you to write a function that adds "err" to the end of every word whose last letter is a consonant (not a vowel, y counts as a consonant).
The input is a string that can contain upper and lowercase characters, some punctuation but no numbers. The solution should be returned as a string.
NOTE: If the word ends with an uppercase consonant, the following "err" will be uppercase --> "ERR".
eg:
```
"Hello, I am Mr Bob" --> "Hello, I amerr Mrerr Boberr"
"THIS IS CRAZY!" --> "THISERR ISERR CRAZYERR!"
```
Good luck!
|
import re
def err_bob(string):
return re.sub(r'(?:[bcdfghjklmnpqrstvwxyz])\b',lambda m:m.group(0)+'ERR' if m.group(0).isupper() else m.group(0)+'err',string,flags=re.I)
|
import re
def err_bob(string):
return re.sub(r'(?:[bcdfghjklmnpqrstvwxyz])\b',lambda m:m.group(0)+'ERR' if m.group(0).isupper() else m.group(0)+'err',string,flags=re.I)
|
train
|
APPS_structured
|
##Task:
You have to write a function `add` which takes two binary numbers as strings and returns their sum as a string.
##Note:
* You are `not allowed to convert binary to decimal & vice versa`.
* The sum should contain `No leading zeroes`.
##Examples:
```
add('111','10'); => '1001'
add('1101','101'); => '10010'
add('1101','10111') => '100100'
```
|
def b2i(b):
return sum(1 << i for i, x in enumerate(b[::-1]) if x == '1')
def add(a, b):
return format(b2i(a) + b2i(b), 'b')
|
def b2i(b):
return sum(1 << i for i, x in enumerate(b[::-1]) if x == '1')
def add(a, b):
return format(b2i(a) + b2i(b), 'b')
|
train
|
APPS_structured
|
Vision has finally made it to Wakanda to get his MindStone extracted. The MindStone was linked to his brain in a highly sophisticated manner and Shuri had to solve a complex problem to extract the stone. The MindStone had $n$ integers inscribed in it and Shuri needs to apply the prefix sum operation on the array $k$ times to extract the stone.
Formally, given $n$ integers $A[1], A[2] ..... A[n]$ and a number $k$, apply the operation
$A[i] = \sum_{j=1}^{i} A[j]$
on the array $k$ times.
Finally Shuri needs to apply $modulo$ $(10^9 + 7)$ operation to each element of the array. Can you help Shuri accomplish this task before Thanos gets to them?
-----Input:-----
- First line of the input consists of two space separated integers $n$ and $k$.
- Second line contains $n$ space separated integers $A[1] .. A[n]$.
-----Output:-----
In a single line print $n$ space separated integers, the values of the resultant array after applying all the operations.
-----Constraints-----
- $1 \leq n \leq 1000$
- $1 \leq k \leq 10^{12}$
- $1 \leq A[i] \leq 10^9$
-----Subtasks-----
- 20 Points: $1 \leq k \leq 1000$
- 30 Points: $1 \leq k \leq 1000000$
- 50 Points: Original Constraints
-----Sample Input:-----
$4$ $2$
$3$ $4$ $1$ $5$
-----Sample Output:-----
$3$ $10$ $18$ $31$
-----EXPLANATION:-----
After applying the prefix sum operation once the array becomes -> $3$ $7$ $8$ $13$
After applying the prefix sum operation for the second time, the array becomes -> $3$ $10$ $18$ $31$
After applying $modulo$ $(10^9 +7)$ operation, array becomes -> $3$ $10$ $18$ $31$
|
# cook your dish here
try:
from itertools import accumulate
n, k = map(int, input().split())
lst = list(map(int, input().split()))
temp = (10**9)+7
for i in range(k):
lst = list(accumulate(lst))
for i in lst:
print(i%(temp), end = ' ')
except EOFError as e:
pass
|
# cook your dish here
try:
from itertools import accumulate
n, k = map(int, input().split())
lst = list(map(int, input().split()))
temp = (10**9)+7
for i in range(k):
lst = list(accumulate(lst))
for i in lst:
print(i%(temp), end = ' ')
except EOFError as e:
pass
|
train
|
APPS_structured
|
We all know how great ABD aka AB-DE-VILLIERS is. However his team mates were jealous of him and posed a problem for him to solve.The problem description is as follows :
Given an array of integers,find the length of the largest subarray(contiguous) of the given array with the maximum possible GCD (Greatest Common Divisor).
For info on GCD ,see this link: https://en.wikipedia.org/wiki/Greatest_common_divisor
GCD of the subarray is defined as the GCD of all the elements of the subarray.
As ABD is not aware of competitive programming he asks your help. Help him!
-----Input-----
First line will contain integer N denoting the size of array.
Second line will contain N integers denoting array elements.
-----Output-----
The answer as specified in the problem statement .
-----Constraints-----
1 <= N <= 1000000
1 <= array[i] <=100000000000
-----Example-----
Input:
4
2 4 8 3
Output:
1
Explanation
GCD of all possible subarrays of the given array are : 2 , 2 , 2 , 1 , 4 , 4, 1 , 8 , 1 , 3
Largest GCD possible : 8
Length of the largest subarray with GCD as 8 is 1
Hence answer is 1 .
|
n = int(input())
a = list(map(int,input().split()))
m = max(a)
ans = 0
i = 0
while (i<n):
temp = 0
if (a[i]==m):
while (i<n and a[i]==m):
i+=1
temp+=1
ans = max(ans,temp)
else:
i+=1
print(ans)
|
n = int(input())
a = list(map(int,input().split()))
m = max(a)
ans = 0
i = 0
while (i<n):
temp = 0
if (a[i]==m):
while (i<n and a[i]==m):
i+=1
temp+=1
ans = max(ans,temp)
else:
i+=1
print(ans)
|
train
|
APPS_structured
|
The statement of this problem is the same as the statement of problem C1. The only difference is that, in problem C1, $n$ is always even, and in C2, $n$ is always odd.
You are given a regular polygon with $2 \cdot n$ vertices (it's convex and has equal sides and equal angles) and all its sides have length $1$. Let's name it as $2n$-gon.
Your task is to find the square of the minimum size such that you can embed $2n$-gon in the square. Embedding $2n$-gon in the square means that you need to place $2n$-gon in the square in such way that each point which lies inside or on a border of $2n$-gon should also lie inside or on a border of the square.
You can rotate $2n$-gon and/or the square.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 200$) — the number of test cases.
Next $T$ lines contain descriptions of test cases — one per line. Each line contains single odd integer $n$ ($3 \le n \le 199$). Don't forget you need to embed $2n$-gon, not an $n$-gon.
-----Output-----
Print $T$ real numbers — one per test case. For each test case, print the minimum length of a side of the square $2n$-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn't exceed $10^{-6}$.
-----Example-----
Input
3
3
5
199
Output
1.931851653
3.196226611
126.687663595
|
# cook your dish here
# import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
import math
import collections
from sys import stdin,stdout,setrecursionlimit
import bisect as bs
T = int(stdin.readline())
for _ in range(T):
n = int(stdin.readline())
# a,b,c,d = list(map(int,stdin.readline().split()))
# h = list(map(int,stdin.readline().split()))
# b = list(map(int,stdin.readline().split()))
# a = stdin.readline().strip('\n')
t = 2*n
x = math.pi/(2*t)
h = 0.5 / (math.sin(x))
print(round(h,7))
|
# cook your dish here
# import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
import math
import collections
from sys import stdin,stdout,setrecursionlimit
import bisect as bs
T = int(stdin.readline())
for _ in range(T):
n = int(stdin.readline())
# a,b,c,d = list(map(int,stdin.readline().split()))
# h = list(map(int,stdin.readline().split()))
# b = list(map(int,stdin.readline().split()))
# a = stdin.readline().strip('\n')
t = 2*n
x = math.pi/(2*t)
h = 0.5 / (math.sin(x))
print(round(h,7))
|
train
|
APPS_structured
|
Complete the function/method so that it returns the url with anything after the anchor (`#`) removed.
## Examples
```python
# returns 'www.codewars.com'
remove_url_anchor('www.codewars.com#about')
# returns 'www.codewars.com?page=1'
remove_url_anchor('www.codewars.com?page=1')
```
|
def remove_url_anchor(url):
if '#' not in url:
return url
else:
get_index = list(url).index('#')
return url[:get_index]
|
def remove_url_anchor(url):
if '#' not in url:
return url
else:
get_index = list(url).index('#')
return url[:get_index]
|
train
|
APPS_structured
|
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
Example:
Input: 4
Output: false
Explanation: If there are 4 stones in the heap, then you will never win the game;
No matter 1, 2, or 3 stones you remove, the last stone will always be
removed by your friend.
|
class Solution:
def canWinNim(self, n):
"""
:type n: int
:rtype: bool
"""
if n % 4 == 0:
return False
return True
|
class Solution:
def canWinNim(self, n):
"""
:type n: int
:rtype: bool
"""
if n % 4 == 0:
return False
return True
|
train
|
APPS_structured
|
Ram and Shyam are sitting next to each other, hoping to cheat on an exam. However, the examination board has prepared $p$ different sets of questions (numbered $0$ through $p-1$), which will be distributed to the students in the following way:
- The students are assigned roll numbers — pairwise distinct positive integers.
- If a student's roll number is $r$, this student gets the $((r-1)\%p)$-th set of questions.
Obviously, Ram and Shyam can cheat only if they get the same set of questions.
You are given the roll numbers of Ram and Shyam: $A$ and $B$ respectively. Find the number of values of $p$ for which they can cheat, or determine that there is an infinite number of such values.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $A$ and $B$.
-----Output-----
For each test case, print a single line — the number of values of $p$ for which Ram and Shyam can cheat, or $-1$ if there is an infinite number of such values.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le A, B \le 10^8$
-----Example Input-----
1
2 6
-----Example Output-----
3
-----Explanation-----
Example case 1: They can cheat for $p = 1$, $p = 2$ or $p = 4$.
|
# cook your dish here
#import sys
#input=sys.stdin.readline
#m=int(input())
for nt in range(int(input())):
a,b=list(map(int,input().split()))
n=abs(a-b)
cnt=0
for i in range(1,int(n**(1/2))+1):
if n%i==0:
if n/i==i:
cnt+=1
else:
cnt+=2
print(cnt)
|
# cook your dish here
#import sys
#input=sys.stdin.readline
#m=int(input())
for nt in range(int(input())):
a,b=list(map(int,input().split()))
n=abs(a-b)
cnt=0
for i in range(1,int(n**(1/2))+1):
if n%i==0:
if n/i==i:
cnt+=1
else:
cnt+=2
print(cnt)
|
train
|
APPS_structured
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
res = [float('inf')] * len(arr)
ans = float('inf')
currMin = float('inf')
i = 0
s = 0
for j in range(len(arr)):
s += arr[j]
while s > target:
s -= arr[i]
i += 1
if s == target:
currMin = min(currMin, j - i + 1)
ans = min(ans, j - i + 1 + res[i-1])
res[j] = currMin
return ans if ans < float('inf') else -1
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
res = [float('inf')] * len(arr)
ans = float('inf')
currMin = float('inf')
i = 0
s = 0
for j in range(len(arr)):
s += arr[j]
while s > target:
s -= arr[i]
i += 1
if s == target:
currMin = min(currMin, j - i + 1)
ans = min(ans, j - i + 1 + res[i-1])
res[j] = currMin
return ans if ans < float('inf') else -1
|
train
|
APPS_structured
|
Polycarpus has a sequence, consisting of n non-negative integers: a_1, a_2, ..., a_{n}.
Let's define function f(l, r) (l, r are integer, 1 ≤ l ≤ r ≤ n) for sequence a as an operation of bitwise OR of all the sequence elements with indexes from l to r. Formally: f(l, r) = a_{l} | a_{l} + 1 | ... | a_{r}.
Polycarpus took a piece of paper and wrote out the values of function f(l, r) for all l, r (l, r are integer, 1 ≤ l ≤ r ≤ n). Now he wants to know, how many distinct values he's got in the end.
Help Polycarpus, count the number of distinct values of function f(l, r) for the given sequence a.
Expression x | y means applying the operation of bitwise OR to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is marked as "|", in Pascal — as "or".
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5) — the number of elements of sequence a. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^6) — the elements of sequence a.
-----Output-----
Print a single integer — the number of distinct values of function f(l, r) for the given sequence a.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
-----Examples-----
Input
3
1 2 0
Output
4
Input
10
1 2 3 4 5 6 1 2 9 10
Output
11
-----Note-----
In the first test case Polycarpus will have 6 numbers written on the paper: f(1, 1) = 1, f(1, 2) = 3, f(1, 3) = 3, f(2, 2) = 2, f(2, 3) = 2, f(3, 3) = 0. There are exactly 4 distinct numbers among them: 0, 1, 2, 3.
|
import sys, math,os
from io import BytesIO, IOBase
#from bisect import bisect_left as bl, bisect_right as br, insort
#from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7
def main():
n=int(data())
A=mdata()
s=set()
ans=set()
for i in A:
s=set(i|j for j in s)
s.add(i)
ans.update(s)
print(len(ans))
def __starting_point():
main()
__starting_point()
|
import sys, math,os
from io import BytesIO, IOBase
#from bisect import bisect_left as bl, bisect_right as br, insort
#from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7
def main():
n=int(data())
A=mdata()
s=set()
ans=set()
for i in A:
s=set(i|j for j in s)
s.add(i)
ans.update(s)
print(len(ans))
def __starting_point():
main()
__starting_point()
|
train
|
APPS_structured
|
For an integer ```k``` rearrange all the elements of the given array in such way, that:
all elements that are less than ```k``` are placed before elements that are not less than ```k```;
all elements that are less than ```k``` remain in the same order with respect to each other;
all elements that are not less than ```k``` remain in the same order with respect to each other.
For ```k = 6``` and ```elements = [6, 4, 10, 10, 6]```, the output should be
```splitByValue(k, elements) = [4, 6, 10, 10, 6]```.
For ```k``` = 5 and ```elements = [1, 3, 5, 7, 6, 4, 2]```, the output should be
```splitByValue(k, elements) = [1, 3, 4, 2, 5, 7, 6]```.
S: codefights.com
|
def split_by_value(k, elements):
return sorted(elements, key=k.__le__)
|
def split_by_value(k, elements):
return sorted(elements, key=k.__le__)
|
train
|
APPS_structured
|
Help Johnny!
He can't make his code work!
Easy Code
Johnny is trying to make a function that adds the sum of two encoded strings, but he can't find the error in his code! Help him!
|
def add(s1, s2):
s1 = s1.encode()
s2 = s2.encode()
return sum(s1+s2)
|
def add(s1, s2):
s1 = s1.encode()
s2 = s2.encode()
return sum(s1+s2)
|
train
|
APPS_structured
|
Given the list of numbers, you are to sort them in non decreasing order.
-----Input-----
t – the number of numbers in list, then t lines follow [t <= 10^6].
Each line contains one integer: N [0 <= N <= 10^6]
-----Output-----
Output given numbers in non decreasing order.
-----Example-----
Input:
5
5
3
6
7
1
Output:
1
3
5
6
7
|
t = int(input())
lst = []
for i in range(t):
lst.append(int(input()))
lst.sort()
for i in range(t):
print((lst[i]));
|
t = int(input())
lst = []
for i in range(t):
lst.append(int(input()))
lst.sort()
for i in range(t):
print((lst[i]));
|
train
|
APPS_structured
|
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
The same word in the dictionary may be reused multiple times in the segmentation.
You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
|
class Solution:
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
w = [0 for _ in range(len(s)+1)]
for i in range(1,len(s)+1):
if w[i] == 0 and s[:i] in wordDict:
w[i] = 1
if w[i]:
if i == len(s):
return True
for j in range(i+1,len(s)+1):
if w[j] == 0 and s[i:j] in wordDict:
w[j] = 1
if j == len(s) and w[j] == 1:
return True
return False
|
class Solution:
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
w = [0 for _ in range(len(s)+1)]
for i in range(1,len(s)+1):
if w[i] == 0 and s[:i] in wordDict:
w[i] = 1
if w[i]:
if i == len(s):
return True
for j in range(i+1,len(s)+1):
if w[j] == 0 and s[i:j] in wordDict:
w[j] = 1
if j == len(s) and w[j] == 1:
return True
return False
|
train
|
APPS_structured
|
The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of $n$ haybale piles on the farm. The $i$-th pile contains $a_i$ haybales.
However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices $i$ and $j$ ($1 \le i, j \le n$) such that $|i-j|=1$ and $a_i>0$ and apply $a_i = a_i - 1$, $a_j = a_j + 1$. She may also decide to not do anything on some days because she is lazy.
Bessie wants to maximize the number of haybales in pile $1$ (i.e. to maximize $a_1$), and she only has $d$ days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile $1$ if she acts optimally!
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $2t$ lines contain a description of test cases — two lines per test case.
The first line of each test case contains integers $n$ and $d$ ($1 \le n,d \le 100$) — the number of haybale piles and the number of days, respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 100$) — the number of haybales in each pile.
-----Output-----
For each test case, output one integer: the maximum number of haybales that may be in pile $1$ after $d$ days if Bessie acts optimally.
-----Example-----
Input
3
4 5
1 0 3 2
2 2
100 1
1 8
0
Output
3
101
0
-----Note-----
In the first test case of the sample, this is one possible way Bessie can end up with $3$ haybales in pile $1$: On day one, move a haybale from pile $3$ to pile $2$ On day two, move a haybale from pile $3$ to pile $2$ On day three, move a haybale from pile $2$ to pile $1$ On day four, move a haybale from pile $2$ to pile $1$ On day five, do nothing
In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile $2$ to pile $1$ on the second day.
|
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n').split(' ')]
def li3():return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
tot = 0
n,d = li()
l = li()
tot = l[0]
for i in range(1,n):
while d >= i and l[i]:
l[i] -= 1
tot += 1
d-=i
print(tot)
|
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n').split(' ')]
def li3():return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
tot = 0
n,d = li()
l = li()
tot = l[0]
for i in range(1,n):
while d >= i and l[i]:
l[i] -= 1
tot += 1
d-=i
print(tot)
|
train
|
APPS_structured
|
Sometimes, I want to quickly be able to convert miles per imperial gallon into kilometers per liter.
Create an application that will display the number of kilometers per liter (output) based on the number of miles per imperial gallon (input).
Make sure to round off the result to two decimal points. If the answer ends with a 0, it should be rounded off without the 0. So instead of 5.50, we should get 5.5.
Some useful associations relevant to this kata:
1 Imperial Gallon = 4.54609188 litres
1 Mile = 1.609344 kilometres
|
def converter(mpg):
return round(mpg / 2.8248105, 2)
|
def converter(mpg):
return round(mpg / 2.8248105, 2)
|
train
|
APPS_structured
|
# Introduction
Fish are an integral part of any ecosystem. Unfortunately, fish are often seen as high maintenance. Contrary to popular belief, fish actually reduce pond maintenance as they graze on string algae and bottom feed from the pond floor. They also make very enjoyable pets, providing hours of natural entertainment.
# Task
In this Kata you are fish in a pond that needs to survive by eating other fish. You can only eat fish that are the same size or smaller than yourself.
You must create a function called fish that takes a shoal of fish as an input string. From this you must work out how many fish you can eat and ultimately the size you will grow to.
# Rules
1. Your size starts at 1
2. The shoal string will contain fish integers between 0-9
3. 0 = algae and wont help you feed.
4. The fish integer represents the size of the fish (1-9).
5. You can only eat fish the same size or less than yourself.
6. You can eat the fish in any order you choose to maximize your size.
7 You can and only eat each fish once.
8. The bigger fish you eat, the faster you grow. A size 2 fish equals two size 1 fish, size 3 fish equals three size 1 fish, and so on.
9. Your size increments by one each time you reach the amounts below.
# Increase your size
Your size will increase depending how many fish you eat and on the size of the fish.
This chart shows the amount of size 1 fish you have to eat in order to increase your size.
Current size
Amount extra needed for next size
Total size 1 fish
Increase to size
1
4
4
2
2
8
12
3
3
12
24
4
4
16
40
5
5
20
60
6
6
24
84
7
Please note: The chart represents fish of size 1
# Returns
Return an integer of the maximum size you could be.
# Example 1
You eat the 4 fish of size 1 (4 * 1 = 4) which increases your size to 2
Now that you are size 2 you can eat the fish that are sized 1 or 2.
You then eat the 4 fish of size 2 (4 * 2 = 8) which increases your size to 3
```python
fish("11112222") => 3
```
# Example 2
You eat the 4 fish of size 1 (4 * 1 = 4) which increases your size to 2
You then eat the remainding 8 fish of size 1 (8 * 1 = 8) which increases your size to 3
```python
fish("111111111111") => 3
```
Good luck and enjoy!
# Kata Series
If you enjoyed this, then please try one of my other Katas. Any feedback, translations and grading of beta Katas are greatly appreciated. Thank you.
Maze Runner
Scooby Doo Puzzle
Driving License
Connect 4
Vending Machine
Snakes and Ladders
Mastermind
Guess Who?
Am I safe to drive?
Mexican Wave
Pigs in a Pen
Hungry Hippos
Plenty of Fish in the Pond
Fruit Machine
Car Park Escape
|
def fish(shoal):
eaten, size, target = 0, 1, 4
for f in sorted(map(int, shoal)):
if f > size: break
eaten += f
if eaten >= target:
size += 1
target += 4 * size
return size
|
def fish(shoal):
eaten, size, target = 0, 1, 4
for f in sorted(map(int, shoal)):
if f > size: break
eaten += f
if eaten >= target:
size += 1
target += 4 * size
return size
|
train
|
APPS_structured
|
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:
Input: 1->1->1->2->3
Output: 2->3
|
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
dummy = ListNode(0)
dummy.next = head
node = dummy
while node.next:
next = node.next
val = next.val
if next.next and next.next.val == val:
next = next.next.next
while next and next.val == val:
next = next.next
node.next = next
else:
node = node.next
return dummy.next
|
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
dummy = ListNode(0)
dummy.next = head
node = dummy
while node.next:
next = node.next
val = next.val
if next.next and next.next.val == val:
next = next.next.next
while next and next.val == val:
next = next.next
node.next = next
else:
node = node.next
return dummy.next
|
train
|
APPS_structured
|
As we all know, Chef is cooking string for long days, his new discovery on string is the longest common pattern length. The longest common pattern length between two strings is the maximum number of characters that both strings have in common. Characters are case sensitive, that is, lower case and upper case characters are considered as different. Note that characters can repeat in a string and a character might have one or more occurrence in common between two strings. For example, if Chef has two strings A = "Codechef" and B = "elfedcc", then the longest common pattern length of A and B is 5 (common characters are c, d, e, e, f).
Chef wants to test you with the problem described above. He will give you two strings of Latin alphabets and digits, return him the longest common pattern length.
-----Input-----
The first line of the input contains an integer T, denoting the number of test cases. Then the description of T test cases follows.
The first line of each test case contains a string A. The next line contains another character string B.
-----Output-----
For each test case, output a single line containing a single integer, the longest common pattern length between A and B.
-----Constraints-----
- 1 ≤ T ≤ 100
- 1 ≤ |A|, |B| ≤ 10000 (104), where |S| denotes the length of the string S
- Both of A and B can contain only alphabet characters (both lower and upper case) and digits
-----Example-----
Input:
4
abcd
xyz
abcd
bcda
aabc
acaa
Codechef
elfedcc
Output:
0
4
3
5
-----Explanation-----
Example case 1. There is no common character.
Example case 2. All the characters are same.
Example case 3. Three characters (a, a and c) are same.
Example case 4. This sample is mentioned by the statement.
|
from collections import Counter
def solve(A,B):
a = Counter(A)
b = Counter(B)
ans = 0
for i in a:
if i in b:
ans += min(a[i],b[i])
return ans
t = int(input())
for _ in range(t):
A = input()
B = input()
print(solve(A,B))
|
from collections import Counter
def solve(A,B):
a = Counter(A)
b = Counter(B)
ans = 0
for i in a:
if i in b:
ans += min(a[i],b[i])
return ans
t = int(input())
for _ in range(t):
A = input()
B = input()
print(solve(A,B))
|
train
|
APPS_structured
|
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax.
You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
|
def calculate_total(subtotal, tax, tip):
return round((1 + (tip + tax) / 100.0) * subtotal, 2)
|
def calculate_total(subtotal, tax, tip):
return round((1 + (tip + tax) / 100.0) * subtotal, 2)
|
train
|
APPS_structured
|
The chef is trying to decode some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem.
-----Input:-----
- First-line will contain $T$, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input, one integer $K$.
-----Output:-----
For each test case, output as the pattern.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq K \leq 100$
-----Sample Input:-----
4
1
2
3
4
-----Sample Output:-----
0
01
10
012
101
210
0123
1012
2101
3210
-----EXPLANATION:-----
No need, else pattern can be decode easily.
|
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
#______________________________________________________________________________________________________
# from math import *
# from bisect import *
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(2*(10**5)+100) this is must for dfs
mod = 10**9+7; md = 998244353
# ______________________________________________________________________________________________________
# segment tree for range minimum query
# sys.setrecursionlimit(10**5)
# n = int(input())
# a = list(map(int,input().split()))
# st = [float('inf') for i in range(4*len(a))]
# def build(a,ind,start,end):
# if start == end:
# st[ind] = a[start]
# else:
# mid = (start+end)//2
# build(a,2*ind+1,start,mid)
# build(a,2*ind+2,mid+1,end)
# st[ind] = min(st[2*ind+1],st[2*ind+2])
# build(a,0,0,n-1)
# def query(ind,l,r,start,end):
# if start>r or end<l:
# return float('inf')
# if l<=start<=end<=r:
# return st[ind]
# mid = (start+end)//2
# return min(query(2*ind+1,l,r,start,mid),query(2*ind+2,l,r,mid+1,end))
# ______________________________________________________________________________________________________
# Checking prime in O(root(N))
# def isprime(n):
# if (n % 2 == 0 and n > 2) or n == 1: return 0
# else:
# s = int(n**(0.5)) + 1
# for i in range(3, s, 2):
# if n % i == 0:
# return 0
# return 1
# def lcm(a,b):
# return (a*b)//gcd(a,b)
# ______________________________________________________________________________________________________
# nCr under mod
# def C(n,r,mod):
# if r>n:
# return 0
# num = den = 1
# for i in range(r):
# num = (num*(n-i))%mod
# den = (den*(i+1))%mod
# return (num*pow(den,mod-2,mod))%mod
# M = 10**5 +10
# ______________________________________________________________________________________________________
# For smallest prime factor of a number
# M = 1000010
# pfc = [i for i in range(M)]
# def pfcs(M):
# for i in range(2,M):
# if pfc[i]==i:
# for j in range(i+i,M,i):
# if pfc[j]==j:
# pfc[j] = i
# return
# pfcs(M)
# ______________________________________________________________________________________________________
tc = 1
tc, = inp()
for _ in range(tc):
n, = inp()
a = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
for j in range(i+1,n):
a[i][j] = a[i][j-1]+1
for j in range(i-1,-1,-1):
a[i][j] = a[i][j+1]+1
for i in a:
print(*i,sep = "")
|
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
#______________________________________________________________________________________________________
# from math import *
# from bisect import *
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(2*(10**5)+100) this is must for dfs
mod = 10**9+7; md = 998244353
# ______________________________________________________________________________________________________
# segment tree for range minimum query
# sys.setrecursionlimit(10**5)
# n = int(input())
# a = list(map(int,input().split()))
# st = [float('inf') for i in range(4*len(a))]
# def build(a,ind,start,end):
# if start == end:
# st[ind] = a[start]
# else:
# mid = (start+end)//2
# build(a,2*ind+1,start,mid)
# build(a,2*ind+2,mid+1,end)
# st[ind] = min(st[2*ind+1],st[2*ind+2])
# build(a,0,0,n-1)
# def query(ind,l,r,start,end):
# if start>r or end<l:
# return float('inf')
# if l<=start<=end<=r:
# return st[ind]
# mid = (start+end)//2
# return min(query(2*ind+1,l,r,start,mid),query(2*ind+2,l,r,mid+1,end))
# ______________________________________________________________________________________________________
# Checking prime in O(root(N))
# def isprime(n):
# if (n % 2 == 0 and n > 2) or n == 1: return 0
# else:
# s = int(n**(0.5)) + 1
# for i in range(3, s, 2):
# if n % i == 0:
# return 0
# return 1
# def lcm(a,b):
# return (a*b)//gcd(a,b)
# ______________________________________________________________________________________________________
# nCr under mod
# def C(n,r,mod):
# if r>n:
# return 0
# num = den = 1
# for i in range(r):
# num = (num*(n-i))%mod
# den = (den*(i+1))%mod
# return (num*pow(den,mod-2,mod))%mod
# M = 10**5 +10
# ______________________________________________________________________________________________________
# For smallest prime factor of a number
# M = 1000010
# pfc = [i for i in range(M)]
# def pfcs(M):
# for i in range(2,M):
# if pfc[i]==i:
# for j in range(i+i,M,i):
# if pfc[j]==j:
# pfc[j] = i
# return
# pfcs(M)
# ______________________________________________________________________________________________________
tc = 1
tc, = inp()
for _ in range(tc):
n, = inp()
a = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
for j in range(i+1,n):
a[i][j] = a[i][j-1]+1
for j in range(i-1,-1,-1):
a[i][j] = a[i][j+1]+1
for i in a:
print(*i,sep = "")
|
train
|
APPS_structured
|
## Description
You've been working with a lot of different file types recently as your interests have broadened.
But what file types are you using the most? With this question in mind we look at the following problem.
Given a `List/Array` of Filenames (strings) `files` return a `List/Array of string(s)` contatining the most common extension(s). If there is a tie, return a sorted list of all extensions.
### Important Info:
* Don't forget, you've been working with a lot of different file types, so expect some interesting extensions/file names/lengths in the random tests.
* Filenames and extensions will only contain letters (case sensitive), and numbers.
* If a file has multiple extensions (ie: `mysong.mp3.als`) only count the the last extension (`.als` in this case)
## Examples
```
files = ['Lakey - Better days.mp3',
'Wheathan - Superlove.wav',
'groovy jam.als',
'#4 final mixdown.als',
'album cover.ps',
'good nights.mp3']
```
would return: `['.als', '.mp3']`, as both of the extensions appear two times in files.
```
files = ['Lakey - Better days.mp3',
'Fisher - Stop it.mp3',
'Fisher - Losing it.mp3',
'#4 final mixdown.als',
'album cover.ps',
'good nights.mp3']
```
would return `['.mp3']`, because it appears more times then any other extension, and no other extension has an equal amount of appearences.
|
from collections import Counter
def solve(files):
count = Counter(f.rsplit(".")[-1] for f in files)
m = max(count.values(), default=0)
return sorted(f".{ext}" for ext, c in count.items() if c == m)
|
from collections import Counter
def solve(files):
count = Counter(f.rsplit(".")[-1] for f in files)
m = max(count.values(), default=0)
return sorted(f".{ext}" for ext, c in count.items() if c == m)
|
train
|
APPS_structured
|
A `bouncy number` is a positive integer whose digits neither increase nor decrease. For example, 1235 is an increasing number, 5321 is a decreasing number, and 2351 is a bouncy number. By definition, all numbers under 100 are non-bouncy, and 101 is the first bouncy number.
Determining if a number is bouncy is easy, but counting all bouncy numbers with N digits can be challenging for large values of N. To complete this kata, you must write a function that takes a number N and return the count of bouncy numbers with N digits. For example, a "4 digit" number includes zero-padded, smaller numbers, such as 0001, 0002, up to 9999.
For clarification, the bouncy numbers between 100 and 125 are: 101, 102, 103, 104, 105, 106, 107, 108, 109, 120, and 121.
|
from math import factorial as f
def nCk(n,k): return f(n)//f(k)//f(n-k)
def bouncy_count(x):
return 10**x - (nCk(10+x,10) + nCk(9+x,9) - 10*x - 1)
|
from math import factorial as f
def nCk(n,k): return f(n)//f(k)//f(n-k)
def bouncy_count(x):
return 10**x - (nCk(10+x,10) + nCk(9+x,9) - 10*x - 1)
|
train
|
APPS_structured
|
Simple interest on a loan is calculated by simply taking the initial amount (the principal, p) and multiplying it by a rate of interest (r) and the number of time periods (n).
Compound interest is calculated by adding the interest after each time period to the amount owed, then calculating the next interest payment based on the principal PLUS the interest from all previous periods.
Given a principal *p*, interest rate *r*, and a number of periods *n*, return an array [total owed under simple interest, total owed under compound interest].
```
EXAMPLES:
interest(100,0.1,1) = [110,110]
interest(100,0.1,2) = [120,121]
interest(100,0.1,10) = [200,259]
```
Round all answers to the nearest integer. Principal will always be an integer between 0 and 9999; interest rate will be a decimal between 0 and 1; number of time periods will be an integer between 0 and 49.
---
More on [Simple interest, compound interest and continuous interest](https://betterexplained.com/articles/a-visual-guide-to-simple-compound-and-continuous-interest-rates/)
|
def interest(p, r, n):
return [round(p * i) for i in ((1 + (r * n)), (1 + r)**n)]
|
def interest(p, r, n):
return [round(p * i) for i in ((1 + (r * n)), (1 + r)**n)]
|
train
|
APPS_structured
|
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $s$. The second line of the test case contains $n$ characters '0' and '1' — the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) — the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4
|
import sys
input = sys.stdin.readline
from collections import deque
t = int(input())
for _ in range(t):
n = int(input())
a = input()
ls = []
st0 = deque()
st1 = deque()
for i in range(n):
if a[i] == "0":
if st1:
x = st1.popleft()
st0.append(x)
elif st0:
x = st0[-1]+1
st0.append(x)
else:
x = 1
st0.append(x)
else:
if st0:
x = st0.pop()
st1.appendleft(x)
elif st1:
x = st1[-1]+1
st1.append(x)
else:
x = 1
st1.append(x)
ls.append(x)
print(max(ls))
print(*ls)
|
import sys
input = sys.stdin.readline
from collections import deque
t = int(input())
for _ in range(t):
n = int(input())
a = input()
ls = []
st0 = deque()
st1 = deque()
for i in range(n):
if a[i] == "0":
if st1:
x = st1.popleft()
st0.append(x)
elif st0:
x = st0[-1]+1
st0.append(x)
else:
x = 1
st0.append(x)
else:
if st0:
x = st0.pop()
st1.appendleft(x)
elif st1:
x = st1[-1]+1
st1.append(x)
else:
x = 1
st1.append(x)
ls.append(x)
print(max(ls))
print(*ls)
|
train
|
APPS_structured
|
Based on [this kata, Connect Four.](https://www.codewars.com/kata/connect-four-1)
In this kata we play a modified game of connect four. It's connect X, and there can be multiple players.
Write the function ```whoIsWinner(moves,connect,size)```.
```2 <= connect <= 10```
```2 <= size <= 52```
Each column is identified by a character, A-Z a-z:
``` ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ```
Moves come in the form:
```
['C_R','p_Y','s_S','I_R','Z_Y','d_S']
```
* Player R puts on C
* Player Y puts on p
* Player S puts on s
* Player R puts on I
* ...
The moves are in the order that they are played.
The first player who connect ``` connect ``` items in same color is the winner.
Note that a player can win before all moves are done. You should return the first winner.
If no winner is found, return "Draw".
A board with size 7, where yellow has connected 4:
All inputs are valid, no illegal moves are made.

|
columns = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def parseMove(move):
(loc, player) = move.split("_")
return (columns.index(loc), player)
def placeMove(move, board):
(loc, player) = parseMove(move)
board[loc].append(player)
def isConnect(loc, dir, connect, board):
count = 1
(x,y) = loc
player = board[x][y]
s = 1
isCheckForward = True
isCheckBack = True
while count < connect and (isCheckForward or isCheckBack):
(sx,sy) = tuple(e * s for e in dir)
if (isCheckForward and
x+sx < len(board) and x+sx >= 0 and
y+sy < len(board[x+sx]) and y+sy >= 0 and
board[x+sx][y+sy] == player):
count += 1
else:
isCheckForward = False
if (isCheckBack and
x-sx >= 0 and x-sx < len(board) and
y-sy >= 0 and y-sy < len(board[x-sx]) and
board[x-sx][y-sy] == player):
count += 1
else:
isCheckBack = False
s += 1
return count >= connect
def findWinner(move, connect, board):
(x, player) = parseMove(move)
y = len(board[x]) - 1
loc = (x,y)
for d in [(1,0), (0,1), (1,1), (1, -1)]:
if isConnect(loc, d, connect, board):
return player
return None
def whoIsWinner(moves,connect,size):
board = [[] for _ in range(size)]
winner = None
while not winner and len(moves) > 0:
move = moves.pop(0)
placeMove(move, board)
winner = findWinner(move, connect, board)
return winner if winner else "Draw"
|
columns = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def parseMove(move):
(loc, player) = move.split("_")
return (columns.index(loc), player)
def placeMove(move, board):
(loc, player) = parseMove(move)
board[loc].append(player)
def isConnect(loc, dir, connect, board):
count = 1
(x,y) = loc
player = board[x][y]
s = 1
isCheckForward = True
isCheckBack = True
while count < connect and (isCheckForward or isCheckBack):
(sx,sy) = tuple(e * s for e in dir)
if (isCheckForward and
x+sx < len(board) and x+sx >= 0 and
y+sy < len(board[x+sx]) and y+sy >= 0 and
board[x+sx][y+sy] == player):
count += 1
else:
isCheckForward = False
if (isCheckBack and
x-sx >= 0 and x-sx < len(board) and
y-sy >= 0 and y-sy < len(board[x-sx]) and
board[x-sx][y-sy] == player):
count += 1
else:
isCheckBack = False
s += 1
return count >= connect
def findWinner(move, connect, board):
(x, player) = parseMove(move)
y = len(board[x]) - 1
loc = (x,y)
for d in [(1,0), (0,1), (1,1), (1, -1)]:
if isConnect(loc, d, connect, board):
return player
return None
def whoIsWinner(moves,connect,size):
board = [[] for _ in range(size)]
winner = None
while not winner and len(moves) > 0:
move = moves.pop(0)
placeMove(move, board)
winner = findWinner(move, connect, board)
return winner if winner else "Draw"
|
train
|
APPS_structured
|
The numbers 12, 63 and 119 have something in common related with their divisors and their prime factors, let's see it.
```
Numbers PrimeFactorsSum(pfs) DivisorsSum(ds) Is ds divisible by pfs
12 2 + 2 + 3 = 7 1 + 2 + 3 + 4 + 6 + 12 = 28 28 / 7 = 4, Yes
63 3 + 3 + 7 = 13 1 + 3 + 7 + 9 + 21 + 63 = 104 104 / 13 = 8, Yes
119 7 + 17 = 24 1 + 7 + 17 + 119 = 144 144 / 24 = 6, Yes
```
There is an obvius property you can see: the sum of the divisors of a number is divisible by the sum of its prime factors.
We need the function ```ds_multof_pfs()``` that receives two arguments: ```nMin``` and ```nMax```, as a lower and upper limit (inclusives), respectively, and outputs a sorted list with the numbers that fulfill the property described above.
We represent the features of the described function:
```python
ds_multof_pfs(nMin, nMax) -----> [n1, n2, ....., nl] # nMin ≤ n1 < n2 < ..< nl ≤ nMax
```
Let's see some cases:
```python
ds_multof_pfs(10, 100) == [12, 15, 35, 42, 60, 63, 66, 68, 84, 90, 95]
ds_multof_pfs(20, 120) == [35, 42, 60, 63, 66, 68, 84, 90, 95, 110, 114, 119]
```
Enjoy it!!
|
from bisect import bisect_left
a = [12, 15, 35, 42, 60, 63, 66, 68, 84, 90, 95, 110, 114, 119, 140, 143, 152, 168, 189, 195, 204, 209, 216, 234, 245, 258, 264, 270, 280, 287, 290, 294, 297, 319, 322, 323, 352, 368, 377, 380, 384, 396, 470, 476, 480, 506, 510, 527, 531, 544, 552, 558, 559, 572, 588, 616, 621, 693, 702, 741, 744, 756, 760, 779, 812, 819, 825, 837, 855, 880, 899, 902, 923, 940, 950, 952, 989, 990, 1007, 1010, 1026, 1044, 1056, 1064, 1078, 1080, 1102, 1144, 1170, 1188, 1189, 1197, 1199, 1280, 1288, 1292, 1298, 1334, 1343, 1349, 1365, 1372, 1375, 1386, 1392, 1440, 1456, 1470, 1494, 1566, 1595, 1620, 1625, 1638, 1652, 1672, 1696, 1700, 1704, 1750, 1763, 1768, 1785, 1804, 1836, 1840, 1845, 1887, 1908, 1914, 1917, 1919, 1944, 1950, 1980, 1989, 1998, 2024, 2052, 2060, 2070, 2075, 2080, 2107, 2130, 2145, 2158, 2159, 2162, 2208, 2240, 2242, 2272, 2340, 2392, 2448, 2464, 2496, 2507, 2520, 2541, 2632, 2660, 2668, 2673, 2688, 2691, 2728, 2759, 2772, 2784, 2805, 2808, 2828, 2835, 2842, 2882, 2911, 2970, 2992, 3000, 3002, 3015, 3026, 3069, 3072, 3078, 3096, 3132, 3159, 3160, 3168, 3239, 3266, 3300, 3304, 3366, 3375, 3402, 3422, 3471, 3485, 3496, 3500, 3560, 3572, 3596, 3599, 3624, 3652, 3690, 3705, 3720, 3752, 3773, 3784, 3816, 3818, 3827, 3840, 3852, 3933, 3936, 3952, 3990, 4018, 4031, 4060, 4077, 4116, 4128, 4136, 4165, 4182, 4216, 4256, 4264, 4292, 4305, 4320, 4368, 4389, 4410, 4437, 4446, 4522, 4524, 4557, 4592, 4607, 4625, 4644, 4648, 4653, 4655, 4662, 4680, 4704, 4706, 4719, 4720, 4731, 4736, 4750, 4785, 4806, 4810, 4860, 4864, 4872, 4992, 4998, 5005, 5015, 5032, 5040, 5070, 5076, 5125, 5166, 5175, 5183, 5200, 5207, 5225, 5229, 5249, 5264, 5307, 5310, 5346, 5400, 5424, 5434, 5452, 5459, 5460, 5472, 5508, 5543, 5544, 5546, 5560, 5586, 5590, 5664, 5698, 5704, 5720, 5728, 5775, 5800, 5848, 5850, 5865, 5886, 5950, 5992, 6000, 6006, 6018, 6039, 6095, 6150, 6156, 6160, 6171, 6250, 6256, 6270, 6424, 6439, 6460, 6510, 6517, 6528, 6565, 6579, 6580, 6600, 6624, 6656, 6660, 6688, 6725, 6750, 6776, 6802, 6804, 6806, 6816, 6837, 6840, 6860, 6887, 6903, 6909, 6944, 6952, 6960, 7007, 7038, 7040, 7050, 7052, 7067, 7140, 7144, 7150, 7176, 7210, 7236, 7254, 7279, 7314, 7336, 7384, 7395, 7410, 7425, 7426, 7462, 7506, 7524, 7532, 7544, 7568, 7581, 7616, 7668, 7682, 7700, 7701, 7722, 7735, 7739, 7742, 7750, 7752, 7821, 7830, 7872, 7878, 7888, 7904, 7912, 7964, 8140, 8159, 8225, 8232, 8280, 8330, 8349, 8352, 8379, 8385, 8397, 8415, 8470, 8500, 8528, 8568, 8575, 8580, 8639, 8642, 8673, 8692, 8721, 8745, 8786, 8800, 8829, 8832, 8856, 8874, 8960, 8964, 8991, 8993, 9063, 9064, 9088, 9112, 9164, 9179, 9180, 9218, 9240, 9256, 9282, 9308, 9310, 9328, 9352, 9375, 9432, 9460, 9468, 9504, 9537, 9593, 9633, 9639, 9660, 9701, 9720, 9768, 9794, 9799, 9856, 9869, 9870, 9900, 9911, 9912, 9920, 9933, 9936, 9947, 9956, 9963, 9996, 10005, 10064, 10080, 10120, 10150, 10185, 10200, 10207, 10240, 10283, 10296, 10395, 10403, 10465, 10494, 10502, 10508, 10528, 10545, 10582, 10647, 10660, 10664, 10672, 10763, 10792, 10848, 10864, 10877, 10880, 10989, 11050, 11088, 11109, 11125, 11128, 11132, 11151, 11160, 11172, 11176, 11193, 11214, 11223, 11224, 11253, 11266, 11275, 11305, 11340, 11342, 11408, 11417, 11439, 11468, 11475, 11500, 11505, 11556, 11560, 11613, 11648, 11659, 11662, 11663, 11682, 11750, 11774, 11800, 11844, 11865, 11904, 11970, 11979, 11985, 12000, 12006, 12095, 12098, 12136, 12141, 12180, 12208, 12222, 12240, 12276, 12319, 12328, 12360, 12366, 12397, 12412, 12441, 12460, 12474, 12519, 12524, 12540, 12555, 12558, 12561, 12576, 12580, 12628, 12638, 12712, 12740, 12784, 12792, 12851, 12903, 12960, 12975, 12992, 13056, 13068, 13144, 13199, 13209, 13230, 13260, 13280, 13300, 13332, 13439, 13464, 13500, 13509, 13529, 13536, 13566, 13572, 13585, 13608, 13629, 13653, 13662, 13677, 13702, 13716, 13720, 13750, 13761, 13770, 13826, 13840, 13862, 13869, 13912, 13919, 14098, 14100, 14104, 14105, 14144, 14145, 14190, 14195, 14250, 14256, 14259, 14260, 14299, 14326, 14344, 14382, 14396, 14402, 14504, 14514, 14520, 14616, 14632, 14645, 14685, 14688, 14690, 14700, 14732, 14749, 14824, 14850, 14875, 14940, 14950, 14972, 14973, 14994, 15008, 15050, 15066, 15088, 15105, 15210, 15249, 15250, 15272, 15288, 15435, 15480, 15503, 15539, 15540, 15544, 15582, 15602, 15631, 15664, 15698, 15708, 15730, 15732, 15785, 15870, 15873, 15878, 15912, 15930, 15960]
def ds_multof_pfs(n, m):
return a[bisect_left(a, n):bisect_left(a, m + 1)]
|
from bisect import bisect_left
a = [12, 15, 35, 42, 60, 63, 66, 68, 84, 90, 95, 110, 114, 119, 140, 143, 152, 168, 189, 195, 204, 209, 216, 234, 245, 258, 264, 270, 280, 287, 290, 294, 297, 319, 322, 323, 352, 368, 377, 380, 384, 396, 470, 476, 480, 506, 510, 527, 531, 544, 552, 558, 559, 572, 588, 616, 621, 693, 702, 741, 744, 756, 760, 779, 812, 819, 825, 837, 855, 880, 899, 902, 923, 940, 950, 952, 989, 990, 1007, 1010, 1026, 1044, 1056, 1064, 1078, 1080, 1102, 1144, 1170, 1188, 1189, 1197, 1199, 1280, 1288, 1292, 1298, 1334, 1343, 1349, 1365, 1372, 1375, 1386, 1392, 1440, 1456, 1470, 1494, 1566, 1595, 1620, 1625, 1638, 1652, 1672, 1696, 1700, 1704, 1750, 1763, 1768, 1785, 1804, 1836, 1840, 1845, 1887, 1908, 1914, 1917, 1919, 1944, 1950, 1980, 1989, 1998, 2024, 2052, 2060, 2070, 2075, 2080, 2107, 2130, 2145, 2158, 2159, 2162, 2208, 2240, 2242, 2272, 2340, 2392, 2448, 2464, 2496, 2507, 2520, 2541, 2632, 2660, 2668, 2673, 2688, 2691, 2728, 2759, 2772, 2784, 2805, 2808, 2828, 2835, 2842, 2882, 2911, 2970, 2992, 3000, 3002, 3015, 3026, 3069, 3072, 3078, 3096, 3132, 3159, 3160, 3168, 3239, 3266, 3300, 3304, 3366, 3375, 3402, 3422, 3471, 3485, 3496, 3500, 3560, 3572, 3596, 3599, 3624, 3652, 3690, 3705, 3720, 3752, 3773, 3784, 3816, 3818, 3827, 3840, 3852, 3933, 3936, 3952, 3990, 4018, 4031, 4060, 4077, 4116, 4128, 4136, 4165, 4182, 4216, 4256, 4264, 4292, 4305, 4320, 4368, 4389, 4410, 4437, 4446, 4522, 4524, 4557, 4592, 4607, 4625, 4644, 4648, 4653, 4655, 4662, 4680, 4704, 4706, 4719, 4720, 4731, 4736, 4750, 4785, 4806, 4810, 4860, 4864, 4872, 4992, 4998, 5005, 5015, 5032, 5040, 5070, 5076, 5125, 5166, 5175, 5183, 5200, 5207, 5225, 5229, 5249, 5264, 5307, 5310, 5346, 5400, 5424, 5434, 5452, 5459, 5460, 5472, 5508, 5543, 5544, 5546, 5560, 5586, 5590, 5664, 5698, 5704, 5720, 5728, 5775, 5800, 5848, 5850, 5865, 5886, 5950, 5992, 6000, 6006, 6018, 6039, 6095, 6150, 6156, 6160, 6171, 6250, 6256, 6270, 6424, 6439, 6460, 6510, 6517, 6528, 6565, 6579, 6580, 6600, 6624, 6656, 6660, 6688, 6725, 6750, 6776, 6802, 6804, 6806, 6816, 6837, 6840, 6860, 6887, 6903, 6909, 6944, 6952, 6960, 7007, 7038, 7040, 7050, 7052, 7067, 7140, 7144, 7150, 7176, 7210, 7236, 7254, 7279, 7314, 7336, 7384, 7395, 7410, 7425, 7426, 7462, 7506, 7524, 7532, 7544, 7568, 7581, 7616, 7668, 7682, 7700, 7701, 7722, 7735, 7739, 7742, 7750, 7752, 7821, 7830, 7872, 7878, 7888, 7904, 7912, 7964, 8140, 8159, 8225, 8232, 8280, 8330, 8349, 8352, 8379, 8385, 8397, 8415, 8470, 8500, 8528, 8568, 8575, 8580, 8639, 8642, 8673, 8692, 8721, 8745, 8786, 8800, 8829, 8832, 8856, 8874, 8960, 8964, 8991, 8993, 9063, 9064, 9088, 9112, 9164, 9179, 9180, 9218, 9240, 9256, 9282, 9308, 9310, 9328, 9352, 9375, 9432, 9460, 9468, 9504, 9537, 9593, 9633, 9639, 9660, 9701, 9720, 9768, 9794, 9799, 9856, 9869, 9870, 9900, 9911, 9912, 9920, 9933, 9936, 9947, 9956, 9963, 9996, 10005, 10064, 10080, 10120, 10150, 10185, 10200, 10207, 10240, 10283, 10296, 10395, 10403, 10465, 10494, 10502, 10508, 10528, 10545, 10582, 10647, 10660, 10664, 10672, 10763, 10792, 10848, 10864, 10877, 10880, 10989, 11050, 11088, 11109, 11125, 11128, 11132, 11151, 11160, 11172, 11176, 11193, 11214, 11223, 11224, 11253, 11266, 11275, 11305, 11340, 11342, 11408, 11417, 11439, 11468, 11475, 11500, 11505, 11556, 11560, 11613, 11648, 11659, 11662, 11663, 11682, 11750, 11774, 11800, 11844, 11865, 11904, 11970, 11979, 11985, 12000, 12006, 12095, 12098, 12136, 12141, 12180, 12208, 12222, 12240, 12276, 12319, 12328, 12360, 12366, 12397, 12412, 12441, 12460, 12474, 12519, 12524, 12540, 12555, 12558, 12561, 12576, 12580, 12628, 12638, 12712, 12740, 12784, 12792, 12851, 12903, 12960, 12975, 12992, 13056, 13068, 13144, 13199, 13209, 13230, 13260, 13280, 13300, 13332, 13439, 13464, 13500, 13509, 13529, 13536, 13566, 13572, 13585, 13608, 13629, 13653, 13662, 13677, 13702, 13716, 13720, 13750, 13761, 13770, 13826, 13840, 13862, 13869, 13912, 13919, 14098, 14100, 14104, 14105, 14144, 14145, 14190, 14195, 14250, 14256, 14259, 14260, 14299, 14326, 14344, 14382, 14396, 14402, 14504, 14514, 14520, 14616, 14632, 14645, 14685, 14688, 14690, 14700, 14732, 14749, 14824, 14850, 14875, 14940, 14950, 14972, 14973, 14994, 15008, 15050, 15066, 15088, 15105, 15210, 15249, 15250, 15272, 15288, 15435, 15480, 15503, 15539, 15540, 15544, 15582, 15602, 15631, 15664, 15698, 15708, 15730, 15732, 15785, 15870, 15873, 15878, 15912, 15930, 15960]
def ds_multof_pfs(n, m):
return a[bisect_left(a, n):bisect_left(a, m + 1)]
|
train
|
APPS_structured
|
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.
Since the answer may not fit in an integer data type, return the answer as a string.
If there is no answer return an empty string.
Example 1:
Input: digits = [8,1,9]
Output: "981"
Example 2:
Input: digits = [8,6,7,1,0]
Output: "8760"
Example 3:
Input: digits = [1]
Output: ""
Example 4:
Input: digits = [0,0,0,0,0,0]
Output: "0"
Constraints:
1 <= digits.length <= 10^4
0 <= digits[i] <= 9
The returning answer must not contain unnecessary leading zeros.
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
mod_gap = sum(digits) % 3
if mod_gap == 0:
answer = ''.join([str(i) for i in sorted(digits, reverse=True)])
mult3 = [i for i in digits if i % 3==0]
non3 = [i for i in digits if i % 3!=0]
non3 = sorted(non3, reverse=True)
if mod_gap == 1:
last_odd_idx = [i for i in range(len(non3)) if non3[i] & 1]
if len(last_odd_idx) > 0:
last_odd_idx = last_odd_idx[-1]
non3 = non3[:last_odd_idx] + non3[last_odd_idx+1:]
elif mod_gap==2:
last_even_idx = [i for i in range(len(non3)) if non3[i] % 2==0]
if len(last_even_idx) > 0:
last_even_idx = last_even_idx[-1]
non3 = non3[:last_even_idx] + non3[last_even_idx+1:]
if sum(non3) % 3 != 0:
non3 = []
answer = ''.join([str(int(i)) for i in sorted(mult3+non3, reverse=True)])
while answer.startswith('0') and len(answer)>1:
answer = answer[1:]
return answer
|
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
mod_gap = sum(digits) % 3
if mod_gap == 0:
answer = ''.join([str(i) for i in sorted(digits, reverse=True)])
mult3 = [i for i in digits if i % 3==0]
non3 = [i for i in digits if i % 3!=0]
non3 = sorted(non3, reverse=True)
if mod_gap == 1:
last_odd_idx = [i for i in range(len(non3)) if non3[i] & 1]
if len(last_odd_idx) > 0:
last_odd_idx = last_odd_idx[-1]
non3 = non3[:last_odd_idx] + non3[last_odd_idx+1:]
elif mod_gap==2:
last_even_idx = [i for i in range(len(non3)) if non3[i] % 2==0]
if len(last_even_idx) > 0:
last_even_idx = last_even_idx[-1]
non3 = non3[:last_even_idx] + non3[last_even_idx+1:]
if sum(non3) % 3 != 0:
non3 = []
answer = ''.join([str(int(i)) for i in sorted(mult3+non3, reverse=True)])
while answer.startswith('0') and len(answer)>1:
answer = answer[1:]
return answer
|
train
|
APPS_structured
|
Rachel has some candies and she decided to distribute them among $N$ kids. The ith kid receives $A_i$ candies. The kids are happy iff the difference between the highest and lowest number of candies received is less than $X$.
Find out if the children are happy or not.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- The first line contains $N$ and $X$.
- The second line contains $N$ integers $A_1,A_2,...,A_N$.
-----Output:-----
For each test case print either "YES"(without quotes) if the kids are happy else "NO"(without quotes)
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq N, X \leq 10^5$
- $1 \leq A_i \leq 10^5$
-----Sample Input:-----
2
5 6
3 5 6 8 1
3 10
5 2 9
-----Sample Output:-----
NO
YES
-----EXPLANATION:-----
- Example 1: Difference between maximum and minimum candies received is 8-1=7. 7 is greater than 6, therefore, the kids are not happy.
|
# cook your dish here
# cook your dish here
from sys import stdout, stdin
def main():
numOfKids, difference = list(map(str, input().split()))
numbers = [int(x) for x in stdin.readline().split()]
numOfKids = int(numOfKids)
difference = int(difference)
smallest = largest = numbers[0]
for j in range(1, len(numbers)):
if (smallest > numbers[j]):
smallest = numbers[j]
min_position = j
if (largest < numbers[j]):
largest = numbers[j]
max_position = j
listDiff = largest - smallest
#print(largest)
#print(smallest)
#print(difference)
if(listDiff > difference):
print("NO")
elif(difference > listDiff):
print("YES")
elif(difference == listDiff):
print("NO")
def __starting_point():
testCases = int(stdin.readline())
if 0 <= testCases <= 100:
for _ in range(testCases):
main()
__starting_point()
|
# cook your dish here
# cook your dish here
from sys import stdout, stdin
def main():
numOfKids, difference = list(map(str, input().split()))
numbers = [int(x) for x in stdin.readline().split()]
numOfKids = int(numOfKids)
difference = int(difference)
smallest = largest = numbers[0]
for j in range(1, len(numbers)):
if (smallest > numbers[j]):
smallest = numbers[j]
min_position = j
if (largest < numbers[j]):
largest = numbers[j]
max_position = j
listDiff = largest - smallest
#print(largest)
#print(smallest)
#print(difference)
if(listDiff > difference):
print("NO")
elif(difference > listDiff):
print("YES")
elif(difference == listDiff):
print("NO")
def __starting_point():
testCases = int(stdin.readline())
if 0 <= testCases <= 100:
for _ in range(testCases):
main()
__starting_point()
|
train
|
APPS_structured
|
Chef recently took a course in linear algebra and learned about linear combinations of vectors. Therefore, in order to test his intelligence, Raj gave him a "fuzzy" problem to solve.
A sequence of integers $B_1, B_2, \ldots, B_M$ generates an integer $K$ if it is possible to find a sequence of integers $C_1, C_2, \ldots, C_M$ such that $C_1 \cdot B_1 + C_2 \cdot B_2 + \ldots + C_M \cdot B_M = K$.
In this problem, Chef has a sequence $A_1, A_2, \ldots, A_N$ and he should answer $Q$ queries. In each query, he is given an integer $K$; the answer to this query is the number of pairs $(l, r)$ such that $1 \le l \le r \le N$ and the subsequence $(A_l, A_{l+1}, \ldots, A_r)$ generates $K$.
Chef has no idea how to solve this problem ― can you help him find the answers to all queries?
-----Input-----
- The first line of the input contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.
- The third line contains a single integer $Q$.
- The following $Q$ lines describe queries. Each of these lines contains a single integer $K$.
-----Output-----
For each query, print a single line containing one integer ― the number of contiguous subsequences that generate $K$.
-----Constraints-----
- $1 \le N, Q \le 10^5$
- $1 \le A_i \le 10^9$ for each valid $i$
- $1 \le K \le 10^6$
-----Subtasks-----
Subtask #1 (10 points): $1 \le N \le 1,000$
Subtask #2 (90 points): original constraints
-----Example Input-----
2
2 4
3
1
2
8
-----Example Output-----
0
2
3
-----Explanation-----
The sequence $(2, 4)$ has three contiguous subsequences: $(2)$, $(4)$ and $(2, 4)$.
- In the first query, $1$ cannot be generated by any subsequence.
- In the second query, $2$ is generated by subsequences $(2)$ and $(2, 4)$. For example, for the subsequence $(2, 4)$, we can choose $C_1 = 1$ and $C_2 = 0$.
- In the third query, $8$ is generated by all three subsequences.
|
"""
Url: https://www.codechef.com/problems/FUZZYLIN
"""
__author__ = "Ronald Kaiser"
__email__ = "raios dot catodicos at gmail dot com"
from collections import defaultdict
from functools import reduce
from math import gcd
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
K = [int(input()) for _ in range(Q)]
MAX_K = max(K)
D = defaultdict(int)
gcd_all = reduce(gcd, A)
for i in range(N):
v = A[i]
for j in range(i, N):
v = gcd(v, A[j])
if v == 1 or v//gcd_all == 1:
D[v] += N - j
break
D[v] += 1
s = D[1] if 1 in D else 0
T = [s for _ in range(MAX_K + 1)]
for k, v in list(D.items()):
if k == 1: continue
for i in range(k, MAX_K + 1, k):
T[i] += v
for k in K: print(T[k])
|
"""
Url: https://www.codechef.com/problems/FUZZYLIN
"""
__author__ = "Ronald Kaiser"
__email__ = "raios dot catodicos at gmail dot com"
from collections import defaultdict
from functools import reduce
from math import gcd
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
K = [int(input()) for _ in range(Q)]
MAX_K = max(K)
D = defaultdict(int)
gcd_all = reduce(gcd, A)
for i in range(N):
v = A[i]
for j in range(i, N):
v = gcd(v, A[j])
if v == 1 or v//gcd_all == 1:
D[v] += N - j
break
D[v] += 1
s = D[1] if 1 in D else 0
T = [s for _ in range(MAX_K + 1)]
for k, v in list(D.items()):
if k == 1: continue
for i in range(k, MAX_K + 1, k):
T[i] += v
for k in K: print(T[k])
|
train
|
APPS_structured
|
A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns.
The graph is given as follows: graph[a] is a list of all nodes b such that ab is an edge of the graph.
Mouse starts at node 1 and goes first, Cat starts at node 2 and goes second, and there is a Hole at node 0.
During each player's turn, they must travel along one edge of the graph that meets where they are. For example, if the Mouse is at node 1, it must travel to any node in graph[1].
Additionally, it is not allowed for the Cat to travel to the Hole (node 0.)
Then, the game can end in 3 ways:
If ever the Cat occupies the same node as the Mouse, the Cat wins.
If ever the Mouse reaches the Hole, the Mouse wins.
If ever a position is repeated (ie. the players are in the same position as a previous turn, and it is the same player's turn to move), the game is a draw.
Given a graph, and assuming both players play optimally, return 1 if the game is won by Mouse, 2 if the game is won by Cat, and 0 if the game is a draw.
Example 1:
Input: [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
Output: 0
Explanation:
4---3---1
| |
2---5
\ /
0
Note:
3 <= graph.length <= 50
It is guaranteed that graph[1] is non-empty.
It is guaranteed that graph[2] contains a non-zero element.
|
class Solution:
def catMouseGame(self, graph: List[List[int]]) -> int:
N = len(graph)
def parents(m, c, t):
if t == 2:
for m2 in graph[m]:
yield m2, c, 3-t
else:
for c2 in graph[c]:
if c2:
yield m, c2, 3-t
DRAW, MOUSE, CAT=0,1,2
color = collections.defaultdict(int)
degree = {}
for m in range(N):
for c in range(N):
degree[m,c,1] = len(graph[m])
degree[m,c,2] = len(graph[c]) - (0 in graph[c])
queue = collections.deque([])
for i in range(N):
for t in range(1, 3):
color[0,i,t] = MOUSE
queue.append((0,i,t,MOUSE))
if i > 0:
color[i,i,t] = CAT
queue.append((i,i,t,CAT))
while queue:
i, j, t, c = queue.popleft()
for i2, j2, t2 in parents(i, j, t):
# if this parent node is not colored
if color[i2,j2,t2] is DRAW:
if t2 == c:#winning move
color[i2,j2,t2] = c
queue.append((i2,j2,t2,c))
else:
degree[i2,j2,t2] -= 1
if degree[i2,j2,t2] == 0:
color[i2,j2,t2] = 3-t2
queue.append((i2,j2,t2,3-t2))
return color[1,2,1]
|
class Solution:
def catMouseGame(self, graph: List[List[int]]) -> int:
N = len(graph)
def parents(m, c, t):
if t == 2:
for m2 in graph[m]:
yield m2, c, 3-t
else:
for c2 in graph[c]:
if c2:
yield m, c2, 3-t
DRAW, MOUSE, CAT=0,1,2
color = collections.defaultdict(int)
degree = {}
for m in range(N):
for c in range(N):
degree[m,c,1] = len(graph[m])
degree[m,c,2] = len(graph[c]) - (0 in graph[c])
queue = collections.deque([])
for i in range(N):
for t in range(1, 3):
color[0,i,t] = MOUSE
queue.append((0,i,t,MOUSE))
if i > 0:
color[i,i,t] = CAT
queue.append((i,i,t,CAT))
while queue:
i, j, t, c = queue.popleft()
for i2, j2, t2 in parents(i, j, t):
# if this parent node is not colored
if color[i2,j2,t2] is DRAW:
if t2 == c:#winning move
color[i2,j2,t2] = c
queue.append((i2,j2,t2,c))
else:
degree[i2,j2,t2] -= 1
if degree[i2,j2,t2] == 0:
color[i2,j2,t2] = 3-t2
queue.append((i2,j2,t2,3-t2))
return color[1,2,1]
|
train
|
APPS_structured
|
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) — the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$.
|
t = int(input())
for _ in range(t):
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
control_forward = min(k, m-1)
for i in range(control_forward+1):
l = i
r = n-1-control_forward+i
non_control_forward = max(m-1-control_forward, 0)
anstemp = 10**10
for j in range(non_control_forward+1):
l += j
r += -non_control_forward + j
anstemp = min(anstemp, max(a[l], a[r]))
l -= j
r -= -non_control_forward + j
ans = max(anstemp, ans)
print(ans)
|
t = int(input())
for _ in range(t):
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
control_forward = min(k, m-1)
for i in range(control_forward+1):
l = i
r = n-1-control_forward+i
non_control_forward = max(m-1-control_forward, 0)
anstemp = 10**10
for j in range(non_control_forward+1):
l += j
r += -non_control_forward + j
anstemp = min(anstemp, max(a[l], a[r]))
l -= j
r -= -non_control_forward + j
ans = max(anstemp, ans)
print(ans)
|
train
|
APPS_structured
|
Rule 30 is a one-dimensional binary cellular automaton. You can have some information here:
* [https://en.wikipedia.org/wiki/Rule_30](https://en.wikipedia.org/wiki/Rule_30)
You have to write a function that takes as input an array of 0 and 1 and a positive integer that represents the number of iterations. This function has to performe the nth iteration of the **Rule 30** with the given input.
The rule to derive a cell from itself and its neigbour is:
Current cell | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111
:-------------|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:
**New cell** | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0
As you can see the new state of a certain cell depends on his neighborhood. In *Current cells* you have the *nth* cell with his left and right neighbor, for example the first configuration is **000**:
* left neighbor = 0
* current cell = 0
* right neighbor = 0
The result for the current cell is **0**, as reported in **New cell** row.
You also have to pay attention to the following things:
* the borders of the list are always 0
* values different from 0 and 1 must be considered as 0
* a negative number of iteration never changes the initial sequence
* you have to return an array of 0 and 1
Here a small example step by step, starting from the list **[1]** and iterating for 5 times:
* We have only one element so first of all we have to follow the rules adding the border, so the result will be **[0, 1, 0]**
* Now we can apply the rule 30 to all the elements and the result will be **[1, 1, 1]** (first iteration)
* Then, after continuing this way for 4 times, the result will be **[1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1]**
In Python, you can also use a support function to print the sequence named printRule30. This function takes as parameters the current list of 0 and 1 to print, the max level that you can reach (number of iterations) and the length of initial array.
```python
def printRule30(list_, maxLvl, startLen):
...
```
The last two parameters are optional and are useful if you are printing each line of the iteration to center the result like this:
░░▓░░ -> step 1
░▓▓▓░ -> step 2
▓▓░░▓ -> step 3
If you pass only the array of 0 and 1 the previous result for each line will be like this:
▓ -> step 1
▓▓▓ -> step 2
▓▓░░▓ -> step 3
**Note:** the function can print only the current list that you pass to it, so you have to use it in the proper way during the interactions of the rule 30.
|
def cell30(l, c, r):
if l == 1: return 1 if c == 0 and r == 0 else 0
return 0 if c == 0 and r == 0 else 1
def rule30(row, n):
return pure30([c if c == 1 else 0 for c in row], n)
def pure30(row, n):
for i in range(n):
row.append(0)
row.insert(0, 0)
ref = row[:]
row = [cell30(0 if i == 0 else ref[i - 1], v, ref[i + 1] if i + 1 < len(ref) else 0) for i, v in enumerate(row)]
return row
|
def cell30(l, c, r):
if l == 1: return 1 if c == 0 and r == 0 else 0
return 0 if c == 0 and r == 0 else 1
def rule30(row, n):
return pure30([c if c == 1 else 0 for c in row], n)
def pure30(row, n):
for i in range(n):
row.append(0)
row.insert(0, 0)
ref = row[:]
row = [cell30(0 if i == 0 else ref[i - 1], v, ref[i + 1] if i + 1 < len(ref) else 0) for i, v in enumerate(row)]
return row
|
train
|
APPS_structured
|
Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types: Fold the sheet of paper at position p_{i}. After this query the leftmost part of the paper with dimensions 1 × p_{i} must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - p_{i}). Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance l_{i} from the left border of the current sheet of paper and the other at distance r_{i} from the left border of the current sheet of paper.
Please look at the explanation of the first test example for better understanding of the problem.
-----Input-----
The first line contains two integers: n and q (1 ≤ n ≤ 10^5; 1 ≤ q ≤ 10^5) — the width of the paper and the number of queries.
Each of the following q lines contains one of the described queries in the following format: "1 p_{i}" (1 ≤ p_{i} < [current width of sheet]) — the first type query. "2 l_{i} r_{i}" (0 ≤ l_{i} < r_{i} ≤ [current width of sheet]) — the second type query.
-----Output-----
For each query of the second type, output the answer.
-----Examples-----
Input
7 4
1 3
1 2
2 0 1
2 1 2
Output
4
3
Input
10 9
2 2 9
1 1
2 0 1
1 8
2 0 8
1 2
2 1 3
1 4
2 2 4
Output
7
2
10
4
5
-----Note-----
The pictures below show the shapes of the paper during the queries of the first example: [Image]
After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2.
|
from itertools import starmap
def main():
n, q = list(map(int, input().split()))
a = list(range(n + 1))
flipped = False
start = 0
end = n
for _ in range(q):
cmd, *args = list(map(int, input().split()))
if cmd == 1:
p = args[0]
if p > end-start-p:
flipped = not flipped
p = end-start-p
if flipped:
a[end-p:end-2*p:-1] = starmap(
lambda a, b: a+n-b,
list(zip(a[end-p:end-2*p:-1], a[end-p:end]))
)
end -= p
else:
start += p
a[start:start+p] = starmap(
lambda a, b: a-b,
list(zip(a[start:start+p], a[start:start-p:-1]))
)
else:
l, r = args
if flipped:
l, r = end-start-r, end-start-l
print(a[start + r] - a[start + l])
def __starting_point():
main()
__starting_point()
|
from itertools import starmap
def main():
n, q = list(map(int, input().split()))
a = list(range(n + 1))
flipped = False
start = 0
end = n
for _ in range(q):
cmd, *args = list(map(int, input().split()))
if cmd == 1:
p = args[0]
if p > end-start-p:
flipped = not flipped
p = end-start-p
if flipped:
a[end-p:end-2*p:-1] = starmap(
lambda a, b: a+n-b,
list(zip(a[end-p:end-2*p:-1], a[end-p:end]))
)
end -= p
else:
start += p
a[start:start+p] = starmap(
lambda a, b: a-b,
list(zip(a[start:start+p], a[start:start-p:-1]))
)
else:
l, r = args
if flipped:
l, r = end-start-r, end-start-l
print(a[start + r] - a[start + l])
def __starting_point():
main()
__starting_point()
|
train
|
APPS_structured
|
# Definition
An **_element is leader_** *if it is greater than The Sum all the elements to its right side*.
____
# Task
**_Given_** an *array/list [] of integers* , **_Find_** *all the **_LEADERS_** in the array*.
___
# Notes
* **_Array/list_** size is *at least 3* .
* **_Array/list's numbers_** Will be **_mixture of positives , negatives and zeros_**
* **_Repetition_** of numbers in *the array/list could occur*.
* **_Returned Array/list_** *should store the leading numbers **_in the same order_** in the original array/list* .
___
# Input >> Output Examples
```
arrayLeaders ({1, 2, 3, 4, 0}) ==> return {4}
```
## **_Explanation_**:
* `4` *is greater than the sum all the elements to its right side*
* **_Note_** : **_The last element_** `0` *is equal to right sum of its elements (abstract zero)*.
____
```
arrayLeaders ({16, 17, 4, 3, 5, 2}) ==> return {17, 5, 2}
```
## **_Explanation_**:
* `17` *is greater than the sum all the elements to its right side*
* `5` *is greater than the sum all the elements to its right side*
* **_Note_** : **_The last element_** `2` *is greater than the sum of its right elements (abstract zero)*.
___
```
arrayLeaders ({5, 2, -1}) ==> return {5, 2}
```
## **_Explanation_**:
* `5` *is greater than the sum all the elements to its right side*
* `2` *is greater than the sum all the elements to its right side*
* **_Note_** : **_The last element_** `-1` *is less than the sum of its right elements (abstract zero)*.
___
```
arrayLeaders ({0, -1, -29, 3, 2}) ==> return {0, -1, 3, 2}
```
## **_Explanation_**:
* `0` *is greater than the sum all the elements to its right side*
* `-1` *is greater than the sum all the elements to its right side*
* `3` *is greater than the sum all the elements to its right side*
* **_Note_** : **_The last element_** `2` *is greater than the sum of its right elements (abstract zero)*.
|
def array_leaders(numbers):
arr = []
for i in range(len(numbers)-1):
if numbers[i]>sum(numbers[i+1:]):
arr.append(numbers[i])
return arr+[numbers[-1]] if not numbers[-1]<=0 else arr
|
def array_leaders(numbers):
arr = []
for i in range(len(numbers)-1):
if numbers[i]>sum(numbers[i+1:]):
arr.append(numbers[i])
return arr+[numbers[-1]] if not numbers[-1]<=0 else arr
|
train
|
APPS_structured
|
Once, a genius guy Cristo visited NASA where he met many scientists. A young intern Mark at NASA asked Cristo to observe the strange behaviour of two
independent particles (say Alpha and Beta) moving in the free space.Cristo was astonished to see the movement of Alpha and Beta. However, he formulated a procedure
to evaluate the distance covered by the particles in given time.
The procedure calculates the distance covered by Alpha and Beta for a given time. Mark, however struggles
to evaluate the procedure manually and asks you to help him.
Cristo's Procedure :-
alpha = 0
beta = 0
Procedure CristoSutra( Ti ) :
if Ti <= 0 :
alpha = alpha + 1
else if Ti == 1 :
beta = beta + 1
else :
CristoSutra(Ti-1)
CristoSutra(Ti-2)
CristoSutra(Ti-3)
end procedure
Note: Print the answer by taking mod from 109+7 .
-----Constraints:-----
- 1<=T<=105
- 1<=Ti<=105
-----Input Format:-----
First line consists an integer t, number of Test cases.For each test case, there is an integer denoting time Ti.
-----Output Format:-----
For each test case, a single output line contains two space seperated numbers ,distance covered by alpha and beta in the given time.
-----Subtasks:-----
Subtask 1 (30 points)
- 1<=T<=10
- 1<=Ti<=1000
Subtask 2 (70 points)
original contraints
Sample Input:
2
1
2
Sample Output:
0 1
2 1
|
for _ in range(eval(input())):
n=eval(input())
mod=1000000007
f1,f2=[0]*101000,[0]*101000
f1[1]=0
f1[2]=2
f1[3]=3
f2[1]=1
f2[2]=1
f2[3]=2;
for i in range(4,100001):
f1[i]=f1[i-1]%mod+f1[i-2]%mod+f1[i-3]%mod
f2[i]=f2[i-1]%mod+f2[i-2]%mod+f2[i-3]%mod
print(f1[n]%mod,f2[n]%mod)
|
for _ in range(eval(input())):
n=eval(input())
mod=1000000007
f1,f2=[0]*101000,[0]*101000
f1[1]=0
f1[2]=2
f1[3]=3
f2[1]=1
f2[2]=1
f2[3]=2;
for i in range(4,100001):
f1[i]=f1[i-1]%mod+f1[i-2]%mod+f1[i-3]%mod
f2[i]=f2[i-1]%mod+f2[i-2]%mod+f2[i-3]%mod
print(f1[n]%mod,f2[n]%mod)
|
train
|
APPS_structured
|
The Tribonacci sequence Tn is defined as follows:
T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
Given n, return the value of Tn.
Example 1:
Input: n = 4
Output: 4
Explanation:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
Example 2:
Input: n = 25
Output: 1389537
Constraints:
0 <= n <= 37
The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.
|
class Solution:
DP: List[int] = [0, 1, 1]
def tribonacci(self, n: int) -> int:
if n >= 0:
if n < len(self.DP):
return self.DP[n]
else:
offset: int = len(self.DP)
self.DP.extend([0] * (n - offset + 1))
for i in range(offset, n + 1):
self.DP[i] = self.DP[i - 3] + self.DP[i - 2] + self.DP[i - 1]
return self.DP[n]
else:
raise ValueError
|
class Solution:
DP: List[int] = [0, 1, 1]
def tribonacci(self, n: int) -> int:
if n >= 0:
if n < len(self.DP):
return self.DP[n]
else:
offset: int = len(self.DP)
self.DP.extend([0] * (n - offset + 1))
for i in range(offset, n + 1):
self.DP[i] = self.DP[i - 3] + self.DP[i - 2] + self.DP[i - 1]
return self.DP[n]
else:
raise ValueError
|
train
|
APPS_structured
|
Chef and Chefina are best friends. Chefina wants to test the Problem Solving skills of chef so she provides Chef 2 integer number $X$ and $Y$
The task of chef is to take the two numbers $X$ and $Y$ and return their SUM.
Identify whether Chef can solve the problem or not?
-----Input:-----
- First line will contain the two integers $X$ and $Y$.
-----Output:-----
For each testcase, output in a single line the SUM of these two numbers $X$ and $Y$.
-----Constraints-----
- $1 \leq X \leq 100$
- $1 \leq Y \leq 100$
-----Sample Input:-----
6 70
-----Sample Output:-----
76
|
# cook your dish here
l = list(map(int,input().split()))
print(sum(l))
|
# cook your dish here
l = list(map(int,input().split()))
print(sum(l))
|
train
|
APPS_structured
|
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"
Output:
"eert"
Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input:
"cccaaa"
Output:
"cccaaa"
Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"
Output:
"bbAa"
Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.
|
class Solution:
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
charToFreq = {}
freqToChar = {}
for c in s:
if c not in charToFreq:
charToFreq[c] = 0
charToFreq[c] += 1
print(charToFreq)
for key, value in list(charToFreq.items()):
if value not in freqToChar:
freqToChar[value] = []
freqToChar[value].append(key)
print(freqToChar)
result = []
for key in sorted(freqToChar, reverse = True):
for char in freqToChar[key]:
result += [char] * key
return "".join(result)
|
class Solution:
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
charToFreq = {}
freqToChar = {}
for c in s:
if c not in charToFreq:
charToFreq[c] = 0
charToFreq[c] += 1
print(charToFreq)
for key, value in list(charToFreq.items()):
if value not in freqToChar:
freqToChar[value] = []
freqToChar[value].append(key)
print(freqToChar)
result = []
for key in sorted(freqToChar, reverse = True):
for char in freqToChar[key]:
result += [char] * key
return "".join(result)
|
train
|
APPS_structured
|
Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length $k$ have the same sum. A subarray of an array is any sequence of consecutive elements.
Phoenix currently has an array $a$ of length $n$. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between $1$ and $n$ inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 50$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 100$).
The second line of each test case contains $n$ space-separated integers ($1 \le a_i \le n$) — the array that Phoenix currently has. This array may or may not be already beautiful.
-----Output-----
For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines.
The first line should contain the length of the beautiful array $m$ ($n \le m \le 10^4$). You don't need to minimize $m$.
The second line should contain $m$ space-separated integers ($1 \le b_i \le n$) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array $a$. You may print integers that weren't originally in array $a$.
If there are multiple solutions, print any. It's guaranteed that if we can make array $a$ beautiful, we can always make it with resulting length no more than $10^4$.
-----Example-----
Input
4
4 2
1 2 2 1
4 3
1 2 2 1
3 2
1 2 3
4 4
4 3 4 2
Output
5
1 2 1 2 1
4
1 2 2 1
-1
7
4 3 2 1 4 3 2
-----Note-----
In the first test case, we can make array $a$ beautiful by inserting the integer $1$ at index $3$ (in between the two existing $2$s). Now, all subarrays of length $k=2$ have the same sum $3$. There exists many other possible solutions, for example: $2, 1, 2, 1, 2, 1$ $1, 2, 1, 2, 1, 2$
In the second test case, the array is already beautiful: all subarrays of length $k=3$ have the same sum $5$.
In the third test case, it can be shown that we cannot insert numbers to make array $a$ beautiful.
In the fourth test case, the array $b$ shown is beautiful and all subarrays of length $k=4$ have the same sum $10$. There exist other solutions also.
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
if len(set(a)) > k:
print(-1)
continue
a = list(set(a))
a += [1] * (k - len(a))
print(k * n)
print(*(a * n))
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
if len(set(a)) > k:
print(-1)
continue
a = list(set(a))
a += [1] * (k - len(a))
print(k * n)
print(*(a * n))
|
train
|
APPS_structured
|
As a part of this Kata, you need to create a function that when provided with a triplet, returns the index of the numerical element that lies between the other two elements.
The input to the function will be an array of three distinct numbers (Haskell: a tuple).
For example:
gimme([2, 3, 1]) => 0
*2* is the number that fits between *1* and *3* and the index of *2* in the input array is *0*.
Another example (just to make sure it is clear):
gimme([5, 10, 14]) => 1
*10* is the number that fits between *5* and *14* and the index of *10* in the input array is *1*.
|
def gimme(input_array):
# Implement this function
array=sorted(input_array);
mid_num=array[int((len(array)-1)/2)]
return(input_array.index(mid_num))
|
def gimme(input_array):
# Implement this function
array=sorted(input_array);
mid_num=array[int((len(array)-1)/2)]
return(input_array.index(mid_num))
|
train
|
APPS_structured
|
# A wolf in sheep's clothing
Wolves have been reintroduced to Great Britain. You are a sheep farmer, and are now plagued by wolves which pretend to be sheep. Fortunately, you are good at spotting them.
Warn the sheep in front of the wolf that it is about to be eaten. Remember that you are standing **at the front of the queue** which is at the end of the array:
```
[sheep, sheep, sheep, sheep, sheep, wolf, sheep, sheep] (YOU ARE HERE AT THE FRONT OF THE QUEUE)
7 6 5 4 3 2 1
```
If the wolf is the closest animal to you, return `"Pls go away and stop eating my sheep"`. Otherwise, return `"Oi! Sheep number N! You are about to be eaten by a wolf!"` where `N` is the sheep's position in the queue.
**Note:** there will always be exactly one wolf in the array.
## Examples
```python
warn_the_sheep(["sheep", "sheep", "sheep", "wolf", "sheep"]) == 'Oi! Sheep number 1! You are about to be eaten by a wolf!'
warn_the_sheep(['sheep', 'sheep', 'wolf']) == 'Pls go away and stop eating my sheep'
```
|
def warn_the_sheep(queue):
w = queue.index('wolf') +1
l = len(queue)
if w == l:
return 'Pls go away and stop eating my sheep'
else:
return f'Oi! Sheep number {l-w}! You are about to be eaten by a wolf!'
|
def warn_the_sheep(queue):
w = queue.index('wolf') +1
l = len(queue)
if w == l:
return 'Pls go away and stop eating my sheep'
else:
return f'Oi! Sheep number {l-w}! You are about to be eaten by a wolf!'
|
train
|
APPS_structured
|
You are the judge at a competitive eating competition and you need to choose a winner!
There are three foods at the competition and each type of food is worth a different amount of points.
Points are as follows:
- Chickenwings: 5 points
- Hamburgers: 3 points
- Hotdogs: 2 points
Write a function that helps you create a scoreboard.
It takes as a parameter a list of objects representing the participants, for example:
```
[
{name: "Habanero Hillary", chickenwings: 5 , hamburgers: 17, hotdogs: 11},
{name: "Big Bob" , chickenwings: 20, hamburgers: 4, hotdogs: 11}
]
```
It should return
"name" and "score" properties sorted by score; if scores are equals, sort alphabetically by name.
```
[
{name: "Big Bob", score: 134},
{name: "Habanero Hillary", score: 98}
]
```
Happy judging!
|
def scoreboard(who_ate_what):
score_board = []
for person in who_ate_what:
score = person.get('chickenwings',0)*5 + person.get('hamburgers',0)*3 + person.get('hotdogs',0)*2
contestant = {x:person[x] for x in ['name']}
contestant.update({'score':score})
score_board.append(contestant )
return sorted(score_board, key=lambda k: (-k['score'], k['name']))
|
def scoreboard(who_ate_what):
score_board = []
for person in who_ate_what:
score = person.get('chickenwings',0)*5 + person.get('hamburgers',0)*3 + person.get('hotdogs',0)*2
contestant = {x:person[x] for x in ['name']}
contestant.update({'score':score})
score_board.append(contestant )
return sorted(score_board, key=lambda k: (-k['score'], k['name']))
|
train
|
APPS_structured
|
Master Shifu is training Po to become The Dragon Warrior and as a final assignment he must obtain maximum deliciousness from dumplings. There are $N$ plates of dumplings in front of him with deliciousness $A_1, A_2, \ldots, A_N$, Po can choose any number of continuous plates of dumplings. The total deliciousness is the sum of deliciousness of all the chosen dumplings.
What is the minimum number of plates he must choose so that total deliciousness is maximum possible?
Note: Po must choose atleast one plate.
-----Input:-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.
-----Output:-----
For each test case, print a single line containing one integer.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N \le 2 \cdot 10^5$
- $0 \le A_i \le 10^9$
-----Sample Input:-----
2
4
1 2 3 4
5
3 2 0 3 0
-----Sample Output:-----
4
4
|
for i in range(int(input())):
n=int(input())
a=[int(k) for k in input().split()]
ind1=0
ind2=0
for j in range(len(a)):
if(a[j]==0):continue
else:
ind1=j
break
for k in range(len(a)-1,-1,-1):
if(a[k]==0):continue
else:
ind2=k
break
#print(ind1,ind2)
#print(a[ind1:ind2+1])
print(len(a[ind1:ind2+1]))
|
for i in range(int(input())):
n=int(input())
a=[int(k) for k in input().split()]
ind1=0
ind2=0
for j in range(len(a)):
if(a[j]==0):continue
else:
ind1=j
break
for k in range(len(a)-1,-1,-1):
if(a[k]==0):continue
else:
ind2=k
break
#print(ind1,ind2)
#print(a[ind1:ind2+1])
print(len(a[ind1:ind2+1]))
|
train
|
APPS_structured
|
Given a list of folders, remove all sub-folders in those folders and return in any order the folders after removing.
If a folder[i] is located within another folder[j], it is called a sub-folder of it.
The format of a path is one or more concatenated strings of the form: / followed by one or more lowercase English letters. For example, /leetcode and /leetcode/problems are valid paths while an empty string and / are not.
Example 1:
Input: folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]
Output: ["/a","/c/d","/c/f"]
Explanation: Folders "/a/b/" is a subfolder of "/a" and "/c/d/e" is inside of folder "/c/d" in our filesystem.
Example 2:
Input: folder = ["/a","/a/b/c","/a/b/d"]
Output: ["/a"]
Explanation: Folders "/a/b/c" and "/a/b/d/" will be removed because they are subfolders of "/a".
Example 3:
Input: folder = ["/a/b/c","/a/b/ca","/a/b/d"]
Output: ["/a/b/c","/a/b/ca","/a/b/d"]
Constraints:
1 <= folder.length <= 4 * 10^4
2 <= folder[i].length <= 100
folder[i] contains only lowercase letters and '/'
folder[i] always starts with character '/'
Each folder name is unique.
|
class Solution:
def removeSubfolders(self, folder: List[str]) -> List[str]:
folder.sort()
n = len(folder)
prev = folder[0]
i = 1
while i < n:
curr = folder[i]
if curr.startswith(prev + '/'):
folder.remove(curr)
curr = prev
i-= 1
n-= 1
prev = curr
i+= 1
return folder
|
class Solution:
def removeSubfolders(self, folder: List[str]) -> List[str]:
folder.sort()
n = len(folder)
prev = folder[0]
i = 1
while i < n:
curr = folder[i]
if curr.startswith(prev + '/'):
folder.remove(curr)
curr = prev
i-= 1
n-= 1
prev = curr
i+= 1
return folder
|
train
|
APPS_structured
|
Your task it to return ```true``` if the fractional part (rounded to 1 digit) of the result (```a``` / ```b```) exists, more than ```0``` and is multiple of ```n```.
Otherwise return ```false```. (For Python return True or False)
All arguments are positive digital numbers.
Rounding works like toFixed() method. (if more than...5 rounds up)
Find exapmles below:
```
isMultiple(5, 2, 3) -> false // 2.5 -> 5 is not multiple of 3
isMultiple(5, 3, 4) -> false // 1.7 -> 7 is not multiple of 4
isMultiple(5, 4, 3) -> true // 1.3 -> 3 is multiple of 3
isMultiple(666, 665, 2) -> false // 1.0 -> return false
```
|
def isMultiple(a, b, n):
x=int((a/b-a//b)*10+0.5)
return 0<x<10 and x%n==0
|
def isMultiple(a, b, n):
x=int((a/b-a//b)*10+0.5)
return 0<x<10 and x%n==0
|
train
|
APPS_structured
|
Chef and his friends are playing the game AMONG US. They all have chosen their names as numbers. There are N people in Chef’s group including him, and each swears that he is not the imposter. However, it turns out there were N+1 people in the game. Now all that Chef needs to know is the name of the imposter, which is a number. Also remember that numbers can be duplicate. Can you help out Chef in finding the imposter?
Input :
First line contains the value of N. Second line contains the N numbers that Chef’s friends used as their names. Third line contains the N+1 numbers that people in the game have used as their names.
Output :
Print the extra number in new line.
Constraints :
1 ≤ Numbers used as names ≤ 1,000
1 ≤ N ≤ 1,000,000
Sample Input :
3
4 2 5
4 2 3 5
Sample Output :
3
|
p = int(input())
l = list(map(int,input().split()))
m = list(map(int,input().split()))
b = sum(l)
c = sum(m)
print(abs(b-c))
|
p = int(input())
l = list(map(int,input().split()))
m = list(map(int,input().split()))
b = sum(l)
c = sum(m)
print(abs(b-c))
|
train
|
APPS_structured
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
#OM GANESHAY NAMH
#GANPATI BAPPA MORYA
import math,queue,heapq
import sys
sys.setrecursionlimit(10**6)
fastinput=sys.stdin.readline
fastout=sys.stdout.write
t=int(fastinput())
while t:
t-=1
n=int(fastinput())
a=list(map(int,fastinput().split()))
c=a.count(1)
if c==0 or c==n:
print(n)
print(*a)
elif c<=n//2:
print(n-c)
print('0 '*(n-c))
else:
if c%2==0:
print(c)
print('1 '*c)
else:
c-=1
print(c)
print('1 '*c)
|
#OM GANESHAY NAMH
#GANPATI BAPPA MORYA
import math,queue,heapq
import sys
sys.setrecursionlimit(10**6)
fastinput=sys.stdin.readline
fastout=sys.stdout.write
t=int(fastinput())
while t:
t-=1
n=int(fastinput())
a=list(map(int,fastinput().split()))
c=a.count(1)
if c==0 or c==n:
print(n)
print(*a)
elif c<=n//2:
print(n-c)
print('0 '*(n-c))
else:
if c%2==0:
print(c)
print('1 '*c)
else:
c-=1
print(c)
print('1 '*c)
|
train
|
APPS_structured
|
Timmy & Sarah think they are in love, but around where they live, they will only know once they pick a flower each. If one of the flowers has an even number of petals and the other has an odd number of petals it means they are in love.
Write a function that will take the number of petals of each flower and return true if they are in love and false if they aren't.
|
def lovefunc( flower1, flower2 ):
odd = False
even = False
if flower1%2 == 0:
even = True
elif flower1%2 != 0:
odd = True
if flower2%2 == 0:
even = True
elif flower2%2 != 0:
odd = True
if odd is True and even is True:
return True
return False
|
def lovefunc( flower1, flower2 ):
odd = False
even = False
if flower1%2 == 0:
even = True
elif flower1%2 != 0:
odd = True
if flower2%2 == 0:
even = True
elif flower2%2 != 0:
odd = True
if odd is True and even is True:
return True
return False
|
train
|
APPS_structured
|
You have the `radius` of a circle with the center in point `(0,0)`.
Write a function that calculates the number of points in the circle where `(x,y)` - the cartesian coordinates of the points - are `integers`.
Example: for `radius = 2` the result should be `13`.
`0 <= radius <= 1000`

|
def points(radius):
cnt = radius * 4 + 1
r2 = radius * radius
for i in range(1, radius + 1):
for j in range(1, radius + 1):
if (i * i + j * j <= r2):
cnt += 4
return cnt
|
def points(radius):
cnt = radius * 4 + 1
r2 = radius * radius
for i in range(1, radius + 1):
for j in range(1, radius + 1):
if (i * i + j * j <= r2):
cnt += 4
return cnt
|
train
|
APPS_structured
|
The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem.
-----Input:-----
- First-line will contain $T$, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input, one integer $K$.
-----Output:-----
For each test case, output as the pattern.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq K \leq 100$
-----Sample Input:-----
5
1
2
3
4
5
-----Sample Output:-----
1
1
32
1
32
654
1
32
654
10987
1
32
654
10987
1514131211
-----EXPLANATION:-----
No need, else pattern can be decode easily.
|
# cook your dish here
for i in range(int(input())):
p=int(input())
k=1
print(1,end="")
print()
s=0
for i in range(1,p):
k+=i+1
x=k
for j in range(i+1):
print(x,end="")
x-=1
print()
|
# cook your dish here
for i in range(int(input())):
p=int(input())
k=1
print(1,end="")
print()
s=0
for i in range(1,p):
k+=i+1
x=k
for j in range(i+1):
print(x,end="")
x-=1
print()
|
train
|
APPS_structured
|
For every good kata idea there seem to be quite a few bad ones!
In this kata you need to check the provided array (x) for good ideas 'good' and bad ideas 'bad'. If there are one or two good ideas, return 'Publish!', if there are more than 2 return 'I smell a series!'. If there are no good ideas, as is often the case, return 'Fail!'.
~~~if:c
For C: do not dynamically allocate memory,
instead return a string literal
~~~
|
def well(x):
y=0
for c in x:
if c=="good":
y=y+1
if y==0:
return 'Fail!'
if y > 2:
return 'I smell a series!'
return 'Publish!'
# 'Publish!', if there are more than 2 return 'I smell a series!'. If there are no good ideas, as is often the case, return 'Fail!'.
|
def well(x):
y=0
for c in x:
if c=="good":
y=y+1
if y==0:
return 'Fail!'
if y > 2:
return 'I smell a series!'
return 'Publish!'
# 'Publish!', if there are more than 2 return 'I smell a series!'. If there are no good ideas, as is often the case, return 'Fail!'.
|
train
|
APPS_structured
|
Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store.
Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group.
Jzzhu wonders how to get the maximum possible number of groups. Can you help him?
-----Input-----
A single integer n (1 ≤ n ≤ 10^5), the number of the apples.
-----Output-----
The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers — the numbers of apples in the current group.
If there are several optimal answers you can print any of them.
-----Examples-----
Input
6
Output
2
6 3
2 4
Input
9
Output
3
9 3
2 4
6 8
Input
2
Output
0
|
"""
Codeforces Round 257 Div 1 Problem C
Author : chaotic_iak
Language: Python 3.3.4
"""
def read(mode=2):
# 0: String
# 1: List of strings
# 2: List of integers
inputs = input().strip()
if mode == 0:
return inputs
if mode == 1:
return inputs.split()
if mode == 2:
return [int(x) for x in inputs.split()]
def write(s="\n"):
if isinstance(s, list): s = " ".join(map(str,s))
s = str(s)
print(s, end="")
################################################### SOLUTION
# croft algorithm to generate primes
# from pyprimes library, not built-in, just google it
from itertools import compress
import itertools
def croft():
"""Yield prime integers using the Croft Spiral sieve.
This is a variant of wheel factorisation modulo 30.
"""
# Implementation is based on erat3 from here:
# http://stackoverflow.com/q/2211990
# and this website:
# http://www.primesdemystified.com/
# Memory usage increases roughly linearly with the number of primes seen.
# dict ``roots`` stores an entry x:p for every prime p.
for p in (2, 3, 5):
yield p
roots = {9: 3, 25: 5} # Map d**2 -> d.
primeroots = frozenset((1, 7, 11, 13, 17, 19, 23, 29))
selectors = (1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0)
for q in compress(
# Iterate over prime candidates 7, 9, 11, 13, ...
itertools.islice(itertools.count(7), 0, None, 2),
# Mask out those that can't possibly be prime.
itertools.cycle(selectors)
):
# Using dict membership testing instead of pop gives a
# 5-10% speedup over the first three million primes.
if q in roots:
p = roots[q]
del roots[q]
x = q + 2*p
while x in roots or (x % 30) not in primeroots:
x += 2*p
roots[x] = p
else:
roots[q*q] = q
yield q
n, = read()
cr = croft()
primes = []
for i in cr:
if i < n:
primes.append(i)
else:
break
primes.reverse()
used = [0] * (n+1)
res = []
for p in primes:
k = n//p
tmp = []
while k:
if not used[k*p]:
tmp.append(k*p)
used[k*p] = 1
if len(tmp) == 2:
res.append(tmp)
tmp = []
k -= 1
if tmp == [p] and p > 2 and p*2 <= n and len(res) and res[-1][1] == p*2:
res[-1][1] = p
used[p*2] = 0
used[p] = 1
print(len(res))
for i in res:
print(" ".join(map(str, i)))
|
"""
Codeforces Round 257 Div 1 Problem C
Author : chaotic_iak
Language: Python 3.3.4
"""
def read(mode=2):
# 0: String
# 1: List of strings
# 2: List of integers
inputs = input().strip()
if mode == 0:
return inputs
if mode == 1:
return inputs.split()
if mode == 2:
return [int(x) for x in inputs.split()]
def write(s="\n"):
if isinstance(s, list): s = " ".join(map(str,s))
s = str(s)
print(s, end="")
################################################### SOLUTION
# croft algorithm to generate primes
# from pyprimes library, not built-in, just google it
from itertools import compress
import itertools
def croft():
"""Yield prime integers using the Croft Spiral sieve.
This is a variant of wheel factorisation modulo 30.
"""
# Implementation is based on erat3 from here:
# http://stackoverflow.com/q/2211990
# and this website:
# http://www.primesdemystified.com/
# Memory usage increases roughly linearly with the number of primes seen.
# dict ``roots`` stores an entry x:p for every prime p.
for p in (2, 3, 5):
yield p
roots = {9: 3, 25: 5} # Map d**2 -> d.
primeroots = frozenset((1, 7, 11, 13, 17, 19, 23, 29))
selectors = (1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0)
for q in compress(
# Iterate over prime candidates 7, 9, 11, 13, ...
itertools.islice(itertools.count(7), 0, None, 2),
# Mask out those that can't possibly be prime.
itertools.cycle(selectors)
):
# Using dict membership testing instead of pop gives a
# 5-10% speedup over the first three million primes.
if q in roots:
p = roots[q]
del roots[q]
x = q + 2*p
while x in roots or (x % 30) not in primeroots:
x += 2*p
roots[x] = p
else:
roots[q*q] = q
yield q
n, = read()
cr = croft()
primes = []
for i in cr:
if i < n:
primes.append(i)
else:
break
primes.reverse()
used = [0] * (n+1)
res = []
for p in primes:
k = n//p
tmp = []
while k:
if not used[k*p]:
tmp.append(k*p)
used[k*p] = 1
if len(tmp) == 2:
res.append(tmp)
tmp = []
k -= 1
if tmp == [p] and p > 2 and p*2 <= n and len(res) and res[-1][1] == p*2:
res[-1][1] = p
used[p*2] = 0
used[p] = 1
print(len(res))
for i in res:
print(" ".join(map(str, i)))
|
train
|
APPS_structured
|
Create a function that takes 2 positive integers in form of a string as an input, and outputs the sum (also as a string):
If either input is an empty string, consider it as zero.
|
sum_str = lambda *args: str(sum(int(x) for x in args if x))
|
sum_str = lambda *args: str(sum(int(x) for x in args if x))
|
train
|
APPS_structured
|
# Task
Below we will define what and n-interesting polygon is and your task is to find its area for a given n.
A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim side by side. You can see the 1-, 2- and 3-interesting polygons in the picture below.

# Example
For `n = 1`, the output should be `1`;
For `n = 2`, the output should be `5`;
For `n = 3`, the output should be `13`.
# Input/Output
- `[input]` integer `n`
Constraints: `1 ≤ n < 10000.`
- `[output]` an integer
The area of the n-interesting polygon.
|
def shape_area(n):
p = 0
area = 1
for i in range (1, n):
p = n*4-4
n -= 1
area += p
return area
|
def shape_area(n):
p = 0
area = 1
for i in range (1, n):
p = n*4-4
n -= 1
area += p
return area
|
train
|
APPS_structured
|
You are given an array with several `"even"` words, one `"odd"` word, and some numbers mixed in.
Determine if any of the numbers in the array is the index of the `"odd"` word. If so, return `true`, otherwise `false`.
|
def odd_ball(arr):
index = arr.index('odd')
return index in arr
|
def odd_ball(arr):
index = arr.index('odd')
return index in arr
|
train
|
APPS_structured
|
Given a Sudoku data structure with size `NxN, N > 0 and √N == integer`, write a method to validate if it has been filled out correctly.
The data structure is a multi-dimensional Array, i.e:
```
[
[7,8,4, 1,5,9, 3,2,6],
[5,3,9, 6,7,2, 8,4,1],
[6,1,2, 4,3,8, 7,5,9],
[9,2,8, 7,1,5, 4,6,3],
[3,5,7, 8,4,6, 1,9,2],
[4,6,1, 9,2,3, 5,8,7],
[8,7,6, 3,9,4, 2,1,5],
[2,4,3, 5,6,1, 9,7,8],
[1,9,5, 2,8,7, 6,3,4]
]
```
**Rules for validation**
- Data structure dimension: `NxN` where `N > 0` and `√N == integer`
- Rows may only contain integers: `1..N (N included)`
- Columns may only contain integers: `1..N (N included)`
- *'Little squares'* (`3x3` in example above) may also only contain integers: `1..N (N included)`
|
import numpy as np
class Sudoku(object):
def __init__(self, theArray):
self.grid = np.array(theArray)
self.listgrid = theArray
self.N = len(theArray)
self.M = len(theArray[0])
def has_valid_size(self):
if isinstance(self.listgrid[0][0], bool): return False
if self.grid.shape == (1,1):
return True
for i in self.listgrid:
if len(i) != self.N: return False
return True
#your code here
def is_valid(self):
if not self.has_valid_size():
return False
seqs2check = [self.getRowsIterator(), self.getColsIterator(), self.getSquaresIterator()]
for it in seqs2check:
for seq in it:
if self.checkSeq(seq) == False:
return False
return True
def getRowsIterator(self):
for i in range(self.N):
yield self.grid[i,:]
def getColsIterator(self):
for i in range(self.N):
yield self.grid[:,i]
def getSquaresIterator(self):
squareSize = int(np.sqrt(self.N))
for i in range(squareSize):
for j in range(squareSize):
ix1 = i*squareSize
ix2 = j*squareSize
yield self.grid[ix1:ix1+squareSize, ix2:ix2+squareSize].flatten()
def checkSeq(self, seq):
return sorted(seq) == list(range(1,self.N+1))
|
import numpy as np
class Sudoku(object):
def __init__(self, theArray):
self.grid = np.array(theArray)
self.listgrid = theArray
self.N = len(theArray)
self.M = len(theArray[0])
def has_valid_size(self):
if isinstance(self.listgrid[0][0], bool): return False
if self.grid.shape == (1,1):
return True
for i in self.listgrid:
if len(i) != self.N: return False
return True
#your code here
def is_valid(self):
if not self.has_valid_size():
return False
seqs2check = [self.getRowsIterator(), self.getColsIterator(), self.getSquaresIterator()]
for it in seqs2check:
for seq in it:
if self.checkSeq(seq) == False:
return False
return True
def getRowsIterator(self):
for i in range(self.N):
yield self.grid[i,:]
def getColsIterator(self):
for i in range(self.N):
yield self.grid[:,i]
def getSquaresIterator(self):
squareSize = int(np.sqrt(self.N))
for i in range(squareSize):
for j in range(squareSize):
ix1 = i*squareSize
ix2 = j*squareSize
yield self.grid[ix1:ix1+squareSize, ix2:ix2+squareSize].flatten()
def checkSeq(self, seq):
return sorted(seq) == list(range(1,self.N+1))
|
train
|
APPS_structured
|
You are given a grid with $R$ rows (numbered $1$ through $R$) and $C$ columns (numbered $1$ through $C$). Initially, each cell of this grid is either empty, contains an ant or an anteater. Each ant is moving in a fixed direction: up, down, left or right. The anteaters do not move.
The movement of ants happens in discrete steps. For example, when an ant is in the cell in the $i$-th row and $j$-th column at some point in time (in some step) and it is moving down, then in the next step, it enters the cell in the $(i+1)$-th row and $j$-th column. Two ants meet each other when they enter the same cell at the same point in time (in the same step). When ants meet, they do not interact in any way and keep moving in their fixed directions.
If an ant reaches an anteater, that anteater eats the ant, so the ant completely disappears. If an ant attempts to leave the grid, it also disappears. When two ants enter a cell containing an anteater at the same time, they are eaten before they could meet.
Calculate the total number of pairs of ants that meet each other.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $R$ and $C$.
- Each of the following $R$ lines contains a single string with length $C$. For each valid $i, j$, the $j$-th character in the $i$-th string is:
- '#' if the cell in the $i$-th row and $j$-th column of the grid contains an anteater
- 'U', 'D', 'L' or 'R' if this cell contains an ant moving up, down, left or right respectively
- '-' if this cell is empty
-----Output-----
For each test case, print a single line containing one integer — the number of pairs of ants that meet.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le R, C \le 50$
- each string contains only characters 'U', 'D', 'L', 'R', '#' and '-'
-----Example Input-----
10
3 3
R--
---
--U
1 4
R--R
2 2
--
--
1 4
R--L
1 4
-R-L
1 4
-R#L
3 3
R-D
-#-
R-U
3 3
R-D
---
R#U
3 3
-D-
R-L
-U-
1 7
RLLLLLL
-----Example Output-----
1
0
0
0
1
0
3
2
6
3
|
for t in range(int(input())):
r,c = list(map(int,input().split()))
grid = []
cpy = []
for i in range(r):
grid.append(list(input()))
change = True
ans = 0
while(change):
cpy = [["" for i in range(c)] for j in range(r)]
change = False
for i in range(r):
for j in range(c):
for ele in grid[i][j]:
if(ele=="D" and i<r-1):
change = True
if(grid[i+1][j]!="#"):
cpy[i+1][j] += "D"
elif(ele=="U" and i>0):
change = True
if(grid[i-1][j]!='#'):
cpy[i-1][j] += "U"
elif(ele=="R" and j<c-1):
change = True
if(grid[i][j+1]!="#"):
cpy[i][j+1] += "R"
elif(ele=="L" and j>0):
change = True
if(grid[i][j-1]!='#'):
cpy[i][j-1] += "L"
elif(ele=="#"):
cpy[i][j] = "#"
for i in range(r):
for j in range(c):
s = cpy[i][j]
ans += len(s)*(len(s)-1)/2
grid[i][j] = s
print(int(ans))
|
for t in range(int(input())):
r,c = list(map(int,input().split()))
grid = []
cpy = []
for i in range(r):
grid.append(list(input()))
change = True
ans = 0
while(change):
cpy = [["" for i in range(c)] for j in range(r)]
change = False
for i in range(r):
for j in range(c):
for ele in grid[i][j]:
if(ele=="D" and i<r-1):
change = True
if(grid[i+1][j]!="#"):
cpy[i+1][j] += "D"
elif(ele=="U" and i>0):
change = True
if(grid[i-1][j]!='#'):
cpy[i-1][j] += "U"
elif(ele=="R" and j<c-1):
change = True
if(grid[i][j+1]!="#"):
cpy[i][j+1] += "R"
elif(ele=="L" and j>0):
change = True
if(grid[i][j-1]!='#'):
cpy[i][j-1] += "L"
elif(ele=="#"):
cpy[i][j] = "#"
for i in range(r):
for j in range(c):
s = cpy[i][j]
ans += len(s)*(len(s)-1)/2
grid[i][j] = s
print(int(ans))
|
train
|
APPS_structured
|
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z.
|
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
def lcp(str1, str2):
i = 0
while (i < len(str1)) and (i < len(str2)):
print((str1[i], str2[i]))
if str1[i] == str2[i]:
i += 1
else:
if i == 0:
return ""
else:
return str1[:i]
if i == 0:
return ""
else:
return str1[:i]
if not strs:
return ""
if len(strs) == 1:
if not strs[0]:
return ""
else:
return strs[0]
str = lcp(strs[0], strs[1])
print(str)
if len(strs) == 2:
if not str:
return ""
else:
return str
for i in range(1, len(strs)):
str = lcp(strs[i], str)
if not str:
return ""
else:
return str
|
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
def lcp(str1, str2):
i = 0
while (i < len(str1)) and (i < len(str2)):
print((str1[i], str2[i]))
if str1[i] == str2[i]:
i += 1
else:
if i == 0:
return ""
else:
return str1[:i]
if i == 0:
return ""
else:
return str1[:i]
if not strs:
return ""
if len(strs) == 1:
if not strs[0]:
return ""
else:
return strs[0]
str = lcp(strs[0], strs[1])
print(str)
if len(strs) == 2:
if not str:
return ""
else:
return str
for i in range(1, len(strs)):
str = lcp(strs[i], str)
if not str:
return ""
else:
return str
|
train
|
APPS_structured
|
Given a natural number n, we want to know in how many ways we may express these numbers as product of other numbers.
For example the number
```python
18 = 2 x 9 = 3 x 6 = 2 x 3 x 3 # (we do not consider the product 18 x 1), (3 ways)
```
See this example a bit more complicated,
```python
60 = 2 x 30 = 3 x 20 = 4 x 15 = 5 x 12 = 6 x 10 = 2 x 2 x 15 = 2 x 3 x 10 = 2 x 5 x 6 = 3 x 4 x 5 = 2 x 2 x 3 x 5 (10 ways)
```
We need the function ```prod_int_part()```, that receives a number n, and ouputs the amount of total different products with all the products of max length sorted in this way:
1) each product will be expressed in a list of its factors in incresing order from left to right
2) if there is more than one list-product, these lists should be ordered by the value of the first term, if two lists have the same term equal thay should be ordered by the value of the second term.
Let's see some cases:
```python
prod_int_part(18) == [3, [2, 3, 3]]
prod_int_part(60) == [10, [2, 2, 3, 5]
```
If we have only one list-product with the maximum length, there is no use to have it with two nested braces, so the result will be like this case:
```python
prod_int_part(54) == [6, [2, 3, 3, 3]]
```
Now, let's see examples when ```n``` cannot be partitioned:
```python
prod_int_part(37) == [0, []]
prod_int_part(61) == [0, []]
```
Enjoy it!!
|
def product_way(n, start=2):
result = 0
for i in range(start, n//2+1):
if n%i == 0 and i<=n/i:
result += 1 + product_way(n//i, i)
return result
def prod_int_part(n):
x = product_way(n, 2)
y = []
m = n
for i in range(2, n//2+1):
if n%i == 0:
while (m%i == 0):
m /= i
y.append(i)
return [x, y]
|
def product_way(n, start=2):
result = 0
for i in range(start, n//2+1):
if n%i == 0 and i<=n/i:
result += 1 + product_way(n//i, i)
return result
def prod_int_part(n):
x = product_way(n, 2)
y = []
m = n
for i in range(2, n//2+1):
if n%i == 0:
while (m%i == 0):
m /= i
y.append(i)
return [x, y]
|
train
|
APPS_structured
|
The number 45 is the first integer in having this interesting property:
the sum of the number with its reversed is divisible by the difference between them(absolute Value).
```
45 + 54 = 99
abs(45 - 54) = 9
99 is divisible by 9.
```
The first terms of this special sequence are :
```
n a(n)
1 45
2 54
3 495
4 594
```
Make the function ```sum_dif_rev()```(`sumDivRef` in JavaScript and CoffeeScript), that receives ```n```, the ordinal number of the term and may give us, the value of the term of the sequence.
```python
sum_dif_rev(n) -----> a(n)
```
Let's see some cases:
```python
sum_dif_rev(1) -----> 45
sum_dif_rev(3) -----> 495
```
"Important: Do not include numbers which, when reversed, have a leading zero, e.g.: 1890 reversed is 0981, so 1890 should not be included."
Your code will be tested up to the first 65 terms, so you should think to optimize it as much as you can.
(Hint: I memoize, you memoize, he memoizes, ......they (of course) memoize)
Happy coding!!
|
def sum_dif_rev(n):
k = 0
i = 1
while(i <= n) :
k += 1
if str(k)[-1] != '0' :
p = int((str(k))[::-1])
if abs(p - k) != 0 :
if (p + k) % abs(p - k) == 0 :
i += 1
return k
|
def sum_dif_rev(n):
k = 0
i = 1
while(i <= n) :
k += 1
if str(k)[-1] != '0' :
p = int((str(k))[::-1])
if abs(p - k) != 0 :
if (p + k) % abs(p - k) == 0 :
i += 1
return k
|
train
|
APPS_structured
|
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.
Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows.
Please note that both secret number and friend's guess may contain duplicate digits.
Example 1:
Input: secret = "1807", guess = "7810"
Output: "1A3B"
Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.
Example 2:
Input: secret = "1123", guess = "0111"
Output: "1A1B"
Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.
Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
|
class Solution:
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
d1 = {}
d2 = {}
for i,c in enumerate(secret):
if c not in d1:
d1[c] = []
d1[c].append(i)
for i,c in enumerate(guess):
if c not in d2:
d2[c] = []
d2[c].append(i)
bulls,cows = 0,0
for k in d2:
if k not in d1:
continue
curBulls = curCows = 0
posGuess = d2[k]
posSecret = d1[k]
for p in posGuess:
if p in posSecret:
curBulls += 1
#print(curBulls)
curCows = max(0,min(len(posGuess) - curBulls,len(posSecret) - curBulls))
#print(curCows)
bulls += curBulls
cows += curCows
return str(bulls) + 'A' + str(cows) + 'B'
|
class Solution:
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
d1 = {}
d2 = {}
for i,c in enumerate(secret):
if c not in d1:
d1[c] = []
d1[c].append(i)
for i,c in enumerate(guess):
if c not in d2:
d2[c] = []
d2[c].append(i)
bulls,cows = 0,0
for k in d2:
if k not in d1:
continue
curBulls = curCows = 0
posGuess = d2[k]
posSecret = d1[k]
for p in posGuess:
if p in posSecret:
curBulls += 1
#print(curBulls)
curCows = max(0,min(len(posGuess) - curBulls,len(posSecret) - curBulls))
#print(curCows)
bulls += curBulls
cows += curCows
return str(bulls) + 'A' + str(cows) + 'B'
|
train
|
APPS_structured
|
In this kata, the number 0 is infected. You are given a list. Every turn, any item in the list that is adjacent to a 0 becomes infected and transforms into a 0. How many turns will it take for the whole list to become infected?
```
[0,1,1,0] ==> [0,0,0,0]
All infected in 1 turn.
[1,1,0,1,1] --> [1,0,0,0,1] --> [0,0,0,0,0]
All infected in 2 turns
[0,1,1,1] --> [0,0,1,1] --> [0,0,0,1] --> [0,0,0,0]
All infected in 3 turns.
```
All lists will contain at least one item, and at least one zero, and the only items will be 0s and 1s. Lists may be very very long, so pure brute force approach will not work.
|
def infected_zeroes(a):
zeros = [i for i, n in enumerate(a) if not n]
return len(zeros) and max((y - x) // 2 for x, y in
zip([-1 - zeros[0]] + zeros, zeros + [2*len(a) -1 - zeros[-1]]))
|
def infected_zeroes(a):
zeros = [i for i, n in enumerate(a) if not n]
return len(zeros) and max((y - x) // 2 for x, y in
zip([-1 - zeros[0]] + zeros, zeros + [2*len(a) -1 - zeros[-1]]))
|
train
|
APPS_structured
|
The prime number sequence starts with: `2,3,5,7,11,13,17,19...`. Notice that `2` is in position `one`.
`3` occupies position `two`, which is a prime-numbered position. Similarly, `5`, `11` and `17` also occupy prime-numbered positions. We shall call primes such as `3,5,11,17` dominant primes because they occupy prime-numbered positions in the prime number sequence. Let's call this `listA`.
As you can see from listA, for the prime range `range(0,10)`, there are `only two` dominant primes (`3` and `5`) and the sum of these primes is: `3 + 5 = 8`.
Similarly, as shown in listA, in the `range (6,20)`, the dominant primes in this range are `11` and `17`, with a sum of `28`.
Given a `range (a,b)`, what is the sum of dominant primes within that range? Note that `a <= range <= b` and `b` will not exceed `500000`.
Good luck!
If you like this Kata, you will enjoy:
[Simple Prime Streaming](https://www.codewars.com/kata/5a908da30025e995880000e3)
[Sum of prime-indexed elements](https://www.codewars.com/kata/59f38b033640ce9fc700015b)
[Divisor harmony](https://www.codewars.com/kata/59bf97cd4f98a8b1cd00007e)
|
def solve(a,b):
range_n = b+1
is_prime = [True] * range_n
primes = [0]
result = 0
for i in range (2, range_n):
if is_prime[i]:
primes += i,
for j in range (2*i, range_n, i):
is_prime[j] = False
for i in range (2, len(primes)):
if primes[i] >= a and is_prime[i]: result += primes[i]
return result
|
def solve(a,b):
range_n = b+1
is_prime = [True] * range_n
primes = [0]
result = 0
for i in range (2, range_n):
if is_prime[i]:
primes += i,
for j in range (2*i, range_n, i):
is_prime[j] = False
for i in range (2, len(primes)):
if primes[i] >= a and is_prime[i]: result += primes[i]
return result
|
train
|
APPS_structured
|
You are to write a function to transpose a guitar tab up or down a number of semitones. The amount to transpose is a number, positive or negative. The tab is given as an array, with six elements for each guitar string (fittingly passed as strings). Output your tab in a similar form.
Guitar tablature (or 'tab') is an alternative to sheet music, where notes are replaced by fret numbers and the five lines of the staff are replaced by six lines to represent each of the guitar's strings. It is still read from left to right like sheet music, and notes written directly above each other are played at the same time.
For example, Led Zeppelin's Stairway to Heaven begins:
```
e|-------5-7-----7-|-8-----8-2-----2-|-0---------0-----|-----------------|
B|-----5-----5-----|---5-------3-----|---1---1-----1---|-0-1-1-----------|
G|---5---------5---|-----5-------2---|-----2---------2-|-0-2-2-----------|
D|-7-------6-------|-5-------4-------|-3---------------|-----------------|
A|-----------------|-----------------|-----------------|-2-0-0---0--/8-7-|
E|-----------------|-----------------|-----------------|-----------------|
```
Transposed up two semitones, it would look like this:
```
e|-------7-9-----9-|-10-----10-4-----4-|-2---------2-----|------------------|
B|-----7-----7-----|----7--------5-----|---3---3-----3---|-2-3-3------------|
G|---7---------7---|------7--------4---|-----4---------4-|-2-4-4------------|
D|-9-------8-------|-7---------6-------|-5---------------|------------------|
A|-----------------|-------------------|-----------------|-4-2-2---2--/10-9-|
E|-----------------|-------------------|-----------------|------------------|
```
Note how when the 8th fret note on the top string in bar 2 gets transposed to the 10th fret, extra '-' are added on the other strings below so as to retain the single '-' that originally separated that beat (i.e. column) from the following note – fret 7 on the B string.
Each beat must retain at least one '-' separator before the next, to keep the tab legible. The inputted test tabs all obey this convention.
Electric guitars usually have 22 frets, with the 0th fret being an open string. If your fret numbers transpose to either negative values or values over 22, you should return 'Out of frets!' (and probably detune your guitar).
Tests include some randomly generated guitar tabs, which come with no guarantee of musical quality and/or playability...!
|
def transpose(amount, tab):
resultTab = ['' for tabStr in tab]
numChars = set(char for char in '0123456789')
i = 0
while i < len(tab[0]):
for tabNum, tabStr in enumerate(tab):
try:
valStr = tabStr[i]
# Are we looking at the second character of a two character number
if not (i > 0 and valStr in numChars and tabStr[i-1] in numChars):
# If the next character is a number to complete a two character number
if valStr is not '-' and i+1 < len(tabStr) and tabStr[i+1] in numChars:
valStr += tabStr[i+1]
val = int(valStr)
transposedVal = val + amount
if transposedVal > 22 or transposedVal < 0:
return 'Out of frets!'
resultTab[tabNum] += str(transposedVal)
except ValueError:
resultTab[tabNum] += tabStr[i]
maxLineLength = max([len(s) for s in resultTab])
minLineLength = min([len(s) for s in resultTab])
shouldTrim = False
if maxLineLength != minLineLength:
# This happens if the input string had a two character number that went down to a one
# character number after the transpose
shouldTrim = all([ (len(s) == minLineLength or s[len(s)-1] == '-') for s in resultTab])
if shouldTrim:
i += 1
for resTabNum, resTabStr in enumerate(resultTab):
if shouldTrim:
resultTab[resTabNum] = (resTabStr)[0:minLineLength]
resultTab[resTabNum] = (resTabStr + '-')[0:maxLineLength]
i += 1
return resultTab
|
def transpose(amount, tab):
resultTab = ['' for tabStr in tab]
numChars = set(char for char in '0123456789')
i = 0
while i < len(tab[0]):
for tabNum, tabStr in enumerate(tab):
try:
valStr = tabStr[i]
# Are we looking at the second character of a two character number
if not (i > 0 and valStr in numChars and tabStr[i-1] in numChars):
# If the next character is a number to complete a two character number
if valStr is not '-' and i+1 < len(tabStr) and tabStr[i+1] in numChars:
valStr += tabStr[i+1]
val = int(valStr)
transposedVal = val + amount
if transposedVal > 22 or transposedVal < 0:
return 'Out of frets!'
resultTab[tabNum] += str(transposedVal)
except ValueError:
resultTab[tabNum] += tabStr[i]
maxLineLength = max([len(s) for s in resultTab])
minLineLength = min([len(s) for s in resultTab])
shouldTrim = False
if maxLineLength != minLineLength:
# This happens if the input string had a two character number that went down to a one
# character number after the transpose
shouldTrim = all([ (len(s) == minLineLength or s[len(s)-1] == '-') for s in resultTab])
if shouldTrim:
i += 1
for resTabNum, resTabStr in enumerate(resultTab):
if shouldTrim:
resultTab[resTabNum] = (resTabStr)[0:minLineLength]
resultTab[resTabNum] = (resTabStr + '-')[0:maxLineLength]
i += 1
return resultTab
|
train
|
APPS_structured
|
An integral:
can be approximated by the so-called Simpson’s rule:
Here `h = (b-a)/n`, `n` being an even integer and `a <= b`.
We want to try Simpson's rule with the function f:
The task is to write a function called `simpson` with parameter `n` which returns the value of the integral of f on the interval `[0, pi]` (pi being 3.14159265359...).
## Notes:
- Don't round or truncate your results. See in "RUN EXAMPLES" the function `assertFuzzyEquals` or `testing`.
- `n` will always be even.
- We know that the exact value of the integral of f on the given interval is `2`.
You can see:
about rectangle method and trapezoidal rule.
|
from math import sin
from math import pi
def f(x):
return 3/2*(sin(x)**3)
def simpson(n):
return pi/(3*n)*(f(0) + f(pi) + 4*sum(f((2*i - 1)*(pi/n)) for i in range(1,n//2+1)) + 2*sum(f(2*i*(pi/n)) for i in range(1,n//2)))
|
from math import sin
from math import pi
def f(x):
return 3/2*(sin(x)**3)
def simpson(n):
return pi/(3*n)*(f(0) + f(pi) + 4*sum(f((2*i - 1)*(pi/n)) for i in range(1,n//2+1)) + 2*sum(f(2*i*(pi/n)) for i in range(1,n//2)))
|
train
|
APPS_structured
|
Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can move up, down, left or right from and to an empty cell.
Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right corner (m-1, n-1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.
Example 1:
Input:
grid =
[[0,0,0],
[1,1,0],
[0,0,0],
[0,1,1],
[0,0,0]],
k = 1
Output: 6
Explanation:
The shortest path without eliminating any obstacle is 10.
The shortest path with one obstacle elimination at position (3,2) is 6. Such path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).
Example 2:
Input:
grid =
[[0,1,1],
[1,1,1],
[1,0,0]],
k = 1
Output: -1
Explanation:
We need to eliminate at least two obstacles to find such a walk.
Constraints:
grid.length == m
grid[0].length == n
1 <= m, n <= 40
1 <= k <= m*n
grid[i][j] == 0 or 1
grid[0][0] == grid[m-1][n-1] == 0
|
class Solution:
def shortestPath(self, grid: List[List[int]], k: int) -> int:
from collections import deque, defaultdict
moves = [ [-1, 0], [1, 0], [0, -1], [0, 1]]
rows = len(grid)
cols = len(grid[0])
seen = set() #defaultdict(lambda: float(\"inf\"))
queue = deque()
queue.append((0, 0, k, 0))
while len(queue) > 0:
row, col, eliminations_left, path_length = queue.popleft()
if row == rows-1 and col == cols-1:
return path_length
for move in moves:
next_row, next_col = row + move[0], col + move[1]
if not (0 <= next_row < rows) or not (0 <= next_col < cols):
continue
if (next_row, next_col, eliminations_left) in seen:
continue
if grid[next_row][next_col] == 1:
if eliminations_left > 0:
seen.add((next_row, next_col, eliminations_left))
queue.append((next_row, next_col, eliminations_left-1, path_length+1))
else:
seen.add((next_row, next_col, eliminations_left))
queue.append((next_row, next_col, eliminations_left, path_length+1))
return -1
|
class Solution:
def shortestPath(self, grid: List[List[int]], k: int) -> int:
from collections import deque, defaultdict
moves = [ [-1, 0], [1, 0], [0, -1], [0, 1]]
rows = len(grid)
cols = len(grid[0])
seen = set() #defaultdict(lambda: float(\"inf\"))
queue = deque()
queue.append((0, 0, k, 0))
while len(queue) > 0:
row, col, eliminations_left, path_length = queue.popleft()
if row == rows-1 and col == cols-1:
return path_length
for move in moves:
next_row, next_col = row + move[0], col + move[1]
if not (0 <= next_row < rows) or not (0 <= next_col < cols):
continue
if (next_row, next_col, eliminations_left) in seen:
continue
if grid[next_row][next_col] == 1:
if eliminations_left > 0:
seen.add((next_row, next_col, eliminations_left))
queue.append((next_row, next_col, eliminations_left-1, path_length+1))
else:
seen.add((next_row, next_col, eliminations_left))
queue.append((next_row, next_col, eliminations_left, path_length+1))
return -1
|
train
|
APPS_structured
|
Perhaps it would be convenient to solve first ```Probabilities for Sums in Rolling Cubic Dice``` see at: http://www.codewars.com/kata/probabilities-for-sums-in-rolling-cubic-dice
Suppose that we roll dice that we never used, a tetrahedral die that has only 4 sides(values 1 to 4), or perhaps better the dodecahedral one with 12 sides and 12 values (from 1 to 12), or look! an icosahedral die with 20 sides and 20 values (from 1 to 20).
Let's roll dice of same type and try to see the distribution of the probability for all the possible sum values.
Suppose that we roll 3 tetrahedral dice:
```
Sum Prob Amount of hits Combinations
3 0.015625 1 {1,1,1}
4 0.046875 3 {1,2,1},{2,1,1},{1,1,2}
5 0.09375 6 {3,1,1},{1,1,3},{1,2,2},{1,3,1},{2,2,1},{2,1,2}
6 0.15625 10 {2,2,2},{1,3,2},{3,2,1},{1,4,1},{2,1,3},{1,1,4},{4,1,1},{3,1,2},{1,2,3},{2,3,1}
7 0.1875 12 {2,3,2},{1,2,4},{2,1,4},{1,4,2},{3,2,2}, {4,2,1},{3,3,1},{3,1,3},{2,2,3},{4,1,2},{2,4,1},{1,3,3}
8 0.1875 12 {2,3,3},{3,3,2},{2,2,4},{4,3,1},{2,4,2},{4,1,3},{4,2,2},{3,4,1},{1,3,4},{3,2,3},{3,1,4},{1,4,3}
9 0.15625 10 {3,3,3},{4,4,1},{2,4,3},{4,2,3},{4,3,2},{1,4,4},{4,1,4},{3,4,2},{2,3,4},{3,2,4}
10 0.09375 6 {3,4,3},{4,4,2},{3,3,4},{4,2,4},(2,4,4),{4,3,3}
11 0.046875 3 {4,3,4},{3,4,4},{4,4,3}
12 0.015625 1 {4,4,4}
tot: 1.0000
```
Note that the total sum of all the probabilities for each end has to be ```1.0000```
The register of hits per each sum value will be as follows:
```
[[3, 1], [4, 3], [5, 6], [6, 10], [7, 12], [8, 12], [9, 10], [10, 6], [11, 3], [12, 1]]
```
Create the function ```reg_sum_hits()``` that receive two arguments:
- number of dice, ```n```
- number of sides of the die, ```s```
The function will output a list of pairs with the sum value and the corresponding hits for it.
For the example given above:
```python
reg_sum_hits(3, 4) == [[3, 1], [4, 3], [5, 6], [6, 10], [7, 12], [8, 12], [9, 10], [10, 6], [11, 3], [12, 1]]
```
You will find more examples in the Example Test Cases.
Assumption: If the die has n sides, the values of it are from ```1``` to ```n```
|
reg_sum_hits=lambda n,s:sorted(map(list,__import__('collections').Counter(map(sum,__import__('itertools').product(range(1,s+1),repeat=n))).items()))
|
reg_sum_hits=lambda n,s:sorted(map(list,__import__('collections').Counter(map(sum,__import__('itertools').product(range(1,s+1),repeat=n))).items()))
|
train
|
APPS_structured
|
Chef solved so many hard questions, now he wants to solve some easy problems for refreshment. Chef asks Cheffina for the new question. Cheffina challenges the chef to print the total number of 0's in the binary representation of N(natural number).
-----Input:-----
- First-line will contain $T$, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input, $N$.
-----Output:-----
For each test case, output in a single line answer.
-----Constraints-----
- $1 \leq T \leq 10^6$
- $1 \leq N \leq 10^6$
-----Sample Input:-----
2
2
4
-----Sample Output:-----
1
2
-----EXPLANATION:-----
For 1) Binary representation of 2 is 10. i.e. only one 0 present in it.
For 2) Binary representation of 4 is 100, i.e. two 0's present in it.
|
from sys import stdin, stdout
input = stdin.readline
from collections import defaultdict as dd
import math
def geti(): return list(map(int, input().strip().split()))
def getl(): return list(map(int, input().strip().split()))
def gets(): return input()
def geta(): return int(input())
def print_s(s): stdout.write(s+'\n')
def solve():
for _ in range(geta()):
n=geta()
n=bin(n).split('b')[1]
print(n.count('0'))
def __starting_point():
solve()
__starting_point()
|
from sys import stdin, stdout
input = stdin.readline
from collections import defaultdict as dd
import math
def geti(): return list(map(int, input().strip().split()))
def getl(): return list(map(int, input().strip().split()))
def gets(): return input()
def geta(): return int(input())
def print_s(s): stdout.write(s+'\n')
def solve():
for _ in range(geta()):
n=geta()
n=bin(n).split('b')[1]
print(n.count('0'))
def __starting_point():
solve()
__starting_point()
|
train
|
APPS_structured
|
There is a task in Among Us in which $N$ temperature scale with unique readings are given and you have to make all of them equal. In one second you can choose an odd number and add or subtract that number in any one temperature value. Find minimum time (in seconds) required to complete the task.
$Note$: Equal value may or may not belong to array.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- First line of each testcase contains single integer $N$, the number of temperature scales
- Next line contain $A_i$ for $1 \leq i \leq N$, reading of temperature scales
-----Output:-----
For each testcase, output a single line the time (in seconds) required to complete the task.
-----Constraints-----
- $1 \leq T \leq 10^3$
- $1 \leq N \leq 10^3$
- $0 \leq A_i \leq 10^9$
-----Sample Input:-----
3
5
1 2 3 4 5
4
5 2 3 8
2
50 53
-----Sample Output:-----
5
4
1
-----EXPLANATION:-----
- In the $2^{nd}$ test case we need $2$ seconds to change $2$ to $8$ , $1$ second to change $3$ and $5$ to $8$ . So total time required is $2+1+1=4$ seconds.
|
# cook your dish here
for _ in range(int(input())):
n=int(input())
ar=list(map(int,input().split()))
odd=0
even=0
if n==1:
print(0)
continue
for i in range(n):
if ar[i]%2==1:
odd+=1
else:
even+=1
if odd>0:
vo=(odd-1)*2+even
else:
vo=even
if even>0:
ve=(even-1)*2+odd
else:
ve=odd
print(min(vo,ve))
|
# cook your dish here
for _ in range(int(input())):
n=int(input())
ar=list(map(int,input().split()))
odd=0
even=0
if n==1:
print(0)
continue
for i in range(n):
if ar[i]%2==1:
odd+=1
else:
even+=1
if odd>0:
vo=(odd-1)*2+even
else:
vo=even
if even>0:
ve=(even-1)*2+odd
else:
ve=odd
print(min(vo,ve))
|
train
|
APPS_structured
|
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.
Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.
Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.
Examples:
Input: "WRRBBW", "RB"
Output: -1
Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW
Input: "WWRRBBWW", "WRBRW"
Output: 2
Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty
Input:"G", "GGGGG"
Output: 2
Explanation: G -> G[G] -> GG[G] -> empty
Input: "RBYYBBRRB", "YRBGB"
Output: 3
Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty
Note:
You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input.
The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input.
Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.
|
class Solution:
def findMinStep(self, board, hand):
"""
:type board: str
:type hand: str
:rtype: int
"""
return self.use_dfs(board, hand)
def use_dfs(self, board, hand):
counts = collections.Counter(hand)
return self.dfs(board, counts)
def dfs(self, board, hand):
if not board:
return 0
result = float('inf')
i, j = 0, 0
n = len(board)
while i < n:
j = i
while j < n and board[i] == board[j]:
j += 1
color = board[i]
remove = 3 - (j - i)
if color in hand and hand[color] >= remove:
new_board = self.shrink(board[:i] + board[j:])
hand[color] -= remove
dist = self.dfs(new_board, hand)
if dist >= 0:
result = min(result, dist + remove)
hand[color] += remove
i = j
return result if float('inf') != result else -1
def shrink(self, board):
i, j = 0, 0
while i < len(board):
j = i
while j < len(board) and board[i] == board[j]:
j += 1
if j - i >= 3:
board = board[:i] + board[j:]
i = 0
else:
i = j
return board
|
class Solution:
def findMinStep(self, board, hand):
"""
:type board: str
:type hand: str
:rtype: int
"""
return self.use_dfs(board, hand)
def use_dfs(self, board, hand):
counts = collections.Counter(hand)
return self.dfs(board, counts)
def dfs(self, board, hand):
if not board:
return 0
result = float('inf')
i, j = 0, 0
n = len(board)
while i < n:
j = i
while j < n and board[i] == board[j]:
j += 1
color = board[i]
remove = 3 - (j - i)
if color in hand and hand[color] >= remove:
new_board = self.shrink(board[:i] + board[j:])
hand[color] -= remove
dist = self.dfs(new_board, hand)
if dist >= 0:
result = min(result, dist + remove)
hand[color] += remove
i = j
return result if float('inf') != result else -1
def shrink(self, board):
i, j = 0, 0
while i < len(board):
j = i
while j < len(board) and board[i] == board[j]:
j += 1
if j - i >= 3:
board = board[:i] + board[j:]
i = 0
else:
i = j
return board
|
train
|
APPS_structured
|
Given alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits).
You have to find a permutation of the string where no letter is followed by another letter and no digit is followed by another digit. That is, no two adjacent characters have the same type.
Return the reformatted string or return an empty string if it is impossible to reformat the string.
Example 1:
Input: s = "a0b1c2"
Output: "0a1b2c"
Explanation: No two adjacent characters have the same type in "0a1b2c". "a0b1c2", "0a1b2c", "0c2a1b" are also valid permutations.
Example 2:
Input: s = "leetcode"
Output: ""
Explanation: "leetcode" has only characters so we cannot separate them by digits.
Example 3:
Input: s = "1229857369"
Output: ""
Explanation: "1229857369" has only digits so we cannot separate them by characters.
Example 4:
Input: s = "covid2019"
Output: "c2o0v1i9d"
Example 5:
Input: s = "ab123"
Output: "1a2b3"
Constraints:
1 <= s.length <= 500
s consists of only lowercase English letters and/or digits.
|
class Solution:
def reformat(self, s: str) -> str:
cA = []
cD = []
for i in s:
if(i.isalpha()):
cA.append(i)
else:
cD.append(i)
res = ''
if(len(s) % 2 == 0):
if(len(cA) == len(cD)):
for i in range(len(cA)):
res += (cA[i] + cD[i])
else:
if(abs(len(cA)-len(cD)) == 1):
if(len(cA) > len(cD)):
res = cA[0]
for i in range(len(cD)):
res += (cD[i] + cA[i+1])
else:
res = cD[0]
for i in range(len(cA)):
res += (cA[i] + cD[i+1])
return res
|
class Solution:
def reformat(self, s: str) -> str:
cA = []
cD = []
for i in s:
if(i.isalpha()):
cA.append(i)
else:
cD.append(i)
res = ''
if(len(s) % 2 == 0):
if(len(cA) == len(cD)):
for i in range(len(cA)):
res += (cA[i] + cD[i])
else:
if(abs(len(cA)-len(cD)) == 1):
if(len(cA) > len(cD)):
res = cA[0]
for i in range(len(cD)):
res += (cD[i] + cA[i+1])
else:
res = cD[0]
for i in range(len(cA)):
res += (cA[i] + cD[i+1])
return res
|
train
|
APPS_structured
|
Many years ago, Roman numbers were defined by only `4` digits: `I, V, X, L`, which represented `1, 5, 10, 50`. These were the only digits used. The value of a sequence was simply the sum of digits in it. For instance:
```
IV = VI = 6
IX = XI = 11
XXL = LXX = XLX = 70
```
It is easy to see that this system is ambiguous, and some numbers could be written in many different ways. Your goal is to determine how many distinct integers could be represented by exactly `n` Roman digits grouped together. For instance:
```Perl
solve(1) = 4, because groups of 1 are [I, V, X, L].
solve(2) = 10, because the groups of 2 are [II, VI, VV, XI, XV, XX, IL, VL, XL, LL] corresponding to [2,6,10,11,15,20,51,55,60,100].
solve(3) = 20, because groups of 3 start with [III, IIV, IVV, ...etc]
```
`n <= 10E7`
More examples in test cases. Good luck!
|
def solve(n):
m = {1:4,2:10, 3:20, 4:35, 5:56, 6:83, 7:116, 8:155, 9:198, 10:244, 11:292, 12:341}
if n <= 12:
return m[n]
else:
return m[12]+49*(n-12)
|
def solve(n):
m = {1:4,2:10, 3:20, 4:35, 5:56, 6:83, 7:116, 8:155, 9:198, 10:244, 11:292, 12:341}
if n <= 12:
return m[n]
else:
return m[12]+49*(n-12)
|
train
|
APPS_structured
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.