index int64 0 5.16k | difficulty int64 7 12 | question stringlengths 126 7.12k | solution stringlengths 30 18.6k | test_cases dict |
|---|---|---|---|---|
5,100 | 7 | Let's define the following recurrence: $$$a_{n+1} = a_{n} + minDigit(a_{n}) β
maxDigit(a_{n}).$$$
Here minDigit(x) and maxDigit(x) are the minimal and maximal digits in the decimal representation of x without leading zeroes. For examples refer to notes.
Your task is calculate a_{K} for given a_{1} and K.
Input
The ... | def f(x):
return str(int(x)+int(min(x))*int(max(x)))
for _ in range(int(input())):
n,k=input().split()
for i in range(1,int(k)):
n = f(n)
if(min(n)=='0'):
break
print(n) | {
"input": [
"8\n1 4\n487 1\n487 2\n487 3\n487 4\n487 5\n487 6\n487 7\n"
],
"output": [
"42\n487\n519\n528\n544\n564\n588\n628\n"
]
} |
5,101 | 12 | You are given an array a consisting of n integers.
In one move, you can choose some index i (1 β€ i β€ n - 2) and shift the segment [a_i, a_{i + 1}, a_{i + 2}] cyclically to the right (i.e. replace the segment [a_i, a_{i + 1}, a_{i + 2}] with [a_{i + 2}, a_i, a_{i + 1}]).
Your task is to sort the initial array by no m... | res, a = [], []
def rot(i: int) -> None:
global res, a
res.append(i + 1)
a[i], a[i+1], a[i+2] = a[i+2], a[i], a[i+1]
#print(f'Rotation on {i}: {a}')
def solve():
global res, a
res.clear()
input()
a = [int(i) for i in input().split()]
s = sorted(a)
for i in range(len(a) - 2):
... | {
"input": [
"5\n5\n1 2 3 4 5\n5\n5 4 3 2 1\n8\n8 4 5 2 3 6 7 3\n7\n5 2 1 6 4 7 3\n6\n1 2 3 3 6 4\n"
],
"output": [
"0\n\n6\n3 1 3 2 2 3 \n13\n2 1 1 6 4 2 4 3 3 4 4 6 6 \n-1\n4\n3 3 4 4 \n"
]
} |
5,102 | 9 | You are given an array a_1, a_2, ... , a_n consisting of integers from 0 to 9. A subarray a_l, a_{l+1}, a_{l+2}, ... , a_{r-1}, a_r is good if the sum of elements of this subarray is equal to the length of this subarray (β_{i=l}^{r} a_i = r - l + 1).
For example, if a = [1, 2, 0], then there are 3 good subarrays: a_{1... | def good(arr):
d={0:1}
s=0
ans=0
for i in range(0,len(arr)):
s+=arr[i]
x=s-i-1
d[x]=d.get(x,0)+1
ans+=d[x]-1
return ans
for i in range(int(input())):
a=input()
lst=[int(i) for i in input()]
print(good(lst)) | {
"input": [
"3\n3\n120\n5\n11011\n6\n600005\n"
],
"output": [
"3\n6\n1\n"
]
} |
5,103 | 10 | Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked... | def main():
for _ in range(int(input())):
x,y=map(int,input().split())
c=list(map(int,input().split()))
r1=abs(x)*c[5 if x >= 0 else 2] +abs(y)*c[1 if y >= 0 else 4]
r2=abs(x)*c[0 if x >=0 else 3]+abs(y-x)*c[1 if y >= x else 4]
r3=abs(y)*c[0 if y >=0 else 3]+abs(y-x)*c[5 if x... | {
"input": [
"2\n-3 1\n1 3 5 7 9 11\n1000000000 1000000000\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n"
],
"output": [
"18\n1000000000000000000\n"
]
} |
5,104 | 9 | Once upon a time in the Kingdom of Far Far Away lived Sam the Farmer. Sam had a cow named Dawn and he was deeply attached to her. Sam would spend the whole summer stocking hay to feed Dawn in winter. Sam scythed hay and put it into haystack. As Sam was a bright farmer, he tried to make the process of storing hay simple... | from math import sqrt
p, n = [], int(input())
def f(x, y): return (x + 2) * (y + 2) + (2 * (x + y + 2) * n) // (x * y)
for x in range(2, int(sqrt(n)) + 1):
if n % x == 0: p.append(x)
p += [n // x for x in reversed(p)]
p.append(n)
u = v = f(1, 1)
for m in p:
for x in range(1, int(sqrt(m)) + 1):
... | {
"input": [
"7\n",
"4\n",
"12\n"
],
"output": [
"47 65\n",
"28 41\n",
"48 105\n"
]
} |
5,105 | 8 | There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach ... | import collections
def solveTestCase():
n, u, v = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
mx = 0
for i in range(n-1):
mx = max(mx, abs(a[i]-a[i+1]))
if mx >=2:
print(0)
elif mx == 1:
print(min(u, v))
else:
print(v+min(u, v))
if __name__ == "__main__":
t = int(input())
... | {
"input": [
"3\n2 3 4\n2 2\n2 3 4\n3 2\n2 4 3\n3 2\n"
],
"output": [
"\n7\n3\n3\n"
]
} |
5,106 | 11 | An array is called beautiful if all the elements in the array are equal.
You can transform an array using the following steps any number of times:
1. Choose two indices i and j (1 β€ i,j β€ n), and an integer x (1 β€ x β€ a_i). Let i be the source index and j be the sink index.
2. Decrease the i-th element by x, an... | def pow(a, n):
if n == 0:
return 1
if n % 2 == 0:
return pow(a ** 2 % MOD, n // 2)
return a * pow(a, n - 1) % MOD
def C(n, k):
return fact[n] * pow(fact[n - k], MOD - 2) * pow(fact[k], MOD - 2) % MOD
MOD = 10 ** 9 + 7
n = int(input())
a = list(map(int, input().split()))
fact = [1] ... | {
"input": [
"5\n0 11 12 13 14\n",
"3\n1 2 3\n",
"4\n0 4 0 4\n"
],
"output": [
"\n120",
"\n6",
"\n2"
]
} |
5,107 | 9 | In some country live wizards. They love to ride trolleybuses.
A city in this country has a trolleybus depot with n trolleybuses. Every day the trolleybuses leave the depot, one by one and go to the final station. The final station is at a distance of d meters from the depot. We know for the i-th trolleybus that it lea... | '''input
1 2 26
28 29
'''
import math
def get_time(t, v):
total = t
t1 = math.sqrt((2 * d)/ a)
if a* t1 < v:
total += t1
else:
t2 = v/a
total += t2
r_d = d - 0.5 *(a * t2 * t2)
total += r_d/v
return total
from sys import stdin, stdout
# main starts
n,a , d = list(map(int, stdin.readline().split(... | {
"input": [
"3 10 10000\n0 10\n5 11\n1000 1\n",
"1 2 26\n28 29\n"
],
"output": [
"1000.500000000\n1000.500000000\n11000.050000000\n",
"33.099019514\n"
]
} |
5,108 | 7 | Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:
* After the cutting each ribbon piece should have length a, b or c.
* After the cutting the number of ribbon pieces should be maximum.
Help Polycarpus and find the number of ribbon pieces ... | def fun(n,a,b,c):
d=[0]+[-1e9]*5000
for i in range(1,n+1):
d[i]=1+max(d[i-a],d[i-b],d[i-c])
return d[n]
n,a,b,c=map(int,input().split())
print(fun(n,a,b,c)) | {
"input": [
"5 5 3 2\n",
"7 5 5 2\n"
],
"output": [
"2\n",
"2\n"
]
} |
5,109 | 7 | Those days, many boys use beautiful girls' photos as avatars in forums. So it is pretty hard to tell the gender of a user at the first glance. Last year, our hero went to a forum and had a nice chat with a beauty (he thought so). After that they talked very often and eventually they became a couple in the network.
Bu... | def sw(s):
gst={1:'IGNORE HIM!',0:'CHAT WITH HER!'}
return gst[len(set(s))%2]
print(sw(input())) | {
"input": [
"xiaodao\n",
"sevenkplus\n",
"wjmzbmr\n"
],
"output": [
"IGNORE HIM!\n",
"CHAT WITH HER!\n",
"CHAT WITH HER!\n"
]
} |
5,110 | 8 | Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.
This time mr. Boosch plans to sign 2k l... | def f(x):
return pref[x + k] - pref[x]
n, k = map(int, input().split())
*a, = map(int, input().split())
pref = [0]
for i in range(1, n + 1):
pref.append(pref[i - 1] + a[i - 1])
a, ma, mb = 0, 0, k
for b in range(k, n - k + 1):
if f(a) < f(b - k):
a = b - k
if f(b) + f(a) > f(ma) + f(mb):
... | {
"input": [
"6 2\n1 1 1 1 1 1\n",
"5 2\n3 6 1 1 6\n"
],
"output": [
"1 3\n",
"1 4\n"
]
} |
5,111 | 8 | Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m.
Public transport is not free. There are 4 types of tickets:
1. A ticket for one r... | def readln(): return tuple(map(int, input().split()))
c = readln()
n, m = readln()
a = readln()
b = readln()
va = min(sum([min(c[1], c[0] * _) for _ in a]), c[2])
vb = min(sum([min(c[1], c[0] * _) for _ in b]), c[2])
print(min(va + vb, c[3]))
| {
"input": [
"4 3 2 1\n1 3\n798\n1 2 3\n",
"100 100 8 100\n3 5\n7 94 12\n100 1 47 0 42\n",
"1 3 7 19\n2 3\n2 5\n4 4 4\n"
],
"output": [
"1",
"16",
"12"
]
} |
5,112 | 9 | One very well-known internet resource site (let's call it X) has come up with a New Year adventure. Specifically, they decided to give ratings to all visitors.
There are n users on the site, for each user we know the rating value he wants to get as a New Year Present. We know that user i wants to get at least ai ratin... | def main():
n = int(input())
aa = list(map(int, input().split()))
idx = sorted(range(n), key=aa.__getitem__)
b = 0
for i in idx:
aa[i] = b = max(b + 1, aa[i])
print(' '.join(map(str, aa)))
if __name__ == '__main__':
main()
| {
"input": [
"1\n1000000000\n",
"3\n5 1 1\n"
],
"output": [
"1000000000 \n",
"5 \n1 \n2 \n"
]
} |
5,113 | 7 | Sereja showed an interesting game to his friends. The game goes like that. Initially, there is a table with an empty cup and n water mugs on it. Then all players take turns to move. During a move, a player takes a non-empty mug of water and pours all water from it into the cup. If the cup overfills, then we assume that... | def readln(): return tuple(map(int, input().split()))
n, s = readln()
a = readln()
print('YES' if sum(a) - max(a) <= s else 'NO') | {
"input": [
"3 4\n1 1 1\n",
"3 4\n3 1 3\n",
"3 4\n4 4 4\n"
],
"output": [
"YES\n",
"YES\n",
"NO\n"
]
} |
5,114 | 7 | Malek lives in an apartment block with 100 floors numbered from 0 to 99. The apartment has an elevator with a digital counter showing the floor that the elevator is currently on. The elevator shows each digit of a number with 7 light sticks by turning them on or off. The picture below shows how the elevator shows each ... | def indicate(m):
a = [2, 7, 2, 3, 3, 4, 2, 5, 1, 2]
return a[m // 10] * a[m % 10]
print(indicate(int(input())))
| {
"input": [
"00\n",
"89\n",
"73\n"
],
"output": [
"4\n",
"2\n",
"15\n"
]
} |
5,115 | 10 | A and B are preparing themselves for programming contests.
After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes.
A likes lowercase letters of the Latin alphabet. He has assigned to each letter a ... | def main():
xlat = list(map(int, input().split()))
dd = [{} for _ in range(26)]
m = res = 0
s = input()
for c in s:
cx = ord(c) - 97
d = dd[cx]
res += d.get(m, 0)
m += xlat[cx]
d[m] = d.get(m, 0) + 1
print(res)
if __name__ == '__main__':
main() | {
"input": [
"1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa\n",
"1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab\n"
],
"output": [
"2\n",
"2\n"
]
} |
5,116 | 11 | Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.
Let's assume that we are given a c... | from collections import defaultdict,deque,Counter,OrderedDict
from heapq import heappop,heappush
def main():
n,m = map(int,input().split())
adj = [[] for i in range(n+1)]
for i in range(m):
a,b,c = map(int,input().split())
adj[a].append((b, c, i))
adj[b].append((a, c, i))
v = ... | {
"input": [
"3 3\n1 2 1\n2 3 1\n1 3 2\n3\n",
"4 4\n1 2 1\n2 3 1\n3 4 1\n4 1 2\n4\n"
],
"output": [
"2\n1 2 ",
"4\n4 2 3 "
]
} |
5,117 | 8 | In this task you need to process a set of stock exchange orders and use them to create order book.
An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di β buy or sell, and integer qi. This means that the participant is ready to buy or sell... | def main():
n, s = map(int, input().split())
tot = {"S": {}, "B": {}}
for _ in range(n):
tmp = input().split()
t, p, q = tot[tmp[0]], int(tmp[1]), int(tmp[2])
t[p] = t.get(p, 0) + q
for c in "S", "B":
t = tot[c]
pp = sorted(t.keys(), reverse=True)
for p in... | {
"input": [
"6 2\nB 10 3\nS 50 2\nS 40 1\nS 50 6\nB 20 4\nB 25 10\n"
],
"output": [
"S 50 8\nS 40 1\nB 25 10\nB 20 4\n"
]
} |
5,118 | 9 | A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the tra... | def find_max_substr(t, s):
l, r = 0, len(t)
while l != r:
m = (l + r) // 2
if t[:m + 1] in s:
l = m + 1
else:
r = m
l1 = l
rs = s[::-1]
l, r = 0, len(t)
while l != r:
m = (l + r) // 2
if t[:m + 1] in rs:
l = m + 1
... | {
"input": [
"ami\nno\n",
"abc\ncbaabc\n",
"aaabrytaaa\nayrat\n"
],
"output": [
"-1\n",
"2\n3 1\n1 3\n",
"3\n1 1\n6 5\n8 7\n"
]
} |
5,119 | 11 | For his computer science class, Jacob builds a model tree with sticks and balls containing n nodes in the shape of a tree. Jacob has spent ai minutes building the i-th ball in the tree.
Jacob's teacher will evaluate his model and grade Jacob based on the effort he has put in. However, she does not have enough time to ... | import sys
input = sys.stdin.readline
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
g = [[] for _ in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
g[u-1].append(v-1)
g[v-1].append(u-1)
stack = [0]
done = [False] * n
par = [0] * n
order = []
while len(stack) > 0:
x = sta... | {
"input": [
"4 2\n1 5 5 5\n1 2\n1 3\n1 4\n",
"5 3\n3 6 1 4 2\n1 2\n2 4\n2 5\n1 3\n"
],
"output": [
"1",
"3"
]
} |
5,120 | 7 | Arya has n opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the school, then Arya... | def I(): return map(int, input().split())
_, d = I()
tmp, ans = 0, 0
for i in range(d):
if "0" not in input():
tmp = 0
else:
tmp += 1
ans = max(tmp, ans)
print(ans)
| {
"input": [
"4 5\n1101\n1111\n0110\n1011\n1111\n",
"2 2\n10\n00\n",
"4 1\n0100\n"
],
"output": [
"2",
"2",
"1"
]
} |
5,121 | 8 | PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses.
You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, i... | from sys import stdin
def main():
n, m = map(int, input().split())
l = stdin.read().splitlines()
le = len(set(l[:n]) & set(l[n:]))
print(('NO', 'YES')[m < n + le % 2])
if __name__ == '__main__':
main()
| {
"input": [
"2 2\nkremowka\nwadowicka\nkremowka\nwiedenska\n",
"5 1\npolandball\nis\na\ncool\ncharacter\nnope\n",
"1 2\na\na\nb\n"
],
"output": [
"YES\n",
"YES\n",
"NO\n"
]
} |
5,122 | 9 | Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value ai.
Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affectio... | from sys import stdin
input = stdin.readline
def f(a, k):
pref = 0
ans = 0
d = {0: 1}
t = 1
s = sum(a)
fac = set([1])
for i in range(50):
t *= k
fac.add(t)
for i in a:
pref += i
for num in fac:
need = pref - num
ans += d.get(need, 0)
d[pref] = d.get(pref, 0) + 1
return ans
n, k = map(int, inp... | {
"input": [
"4 2\n2 2 2 2\n",
"4 -3\n3 -6 -3 12\n"
],
"output": [
" 8\n",
"3\n"
]
} |
5,123 | 7 | Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In o... | def r():
return list(map(int, input().split()))
n = int(input())
matr = [r() for i in range(n)]
ans = True
columns = list(map(set,zip(*matr)))
for row in matr:
ans &= all(x == 1 or any(x - a in row for a in col) for x, col in zip(row, columns))
print("Yes" if ans else "No") | {
"input": [
"3\n1 1 2\n2 3 1\n6 4 1\n",
"3\n1 5 2\n1 1 1\n1 2 3\n"
],
"output": [
"Yes\n",
"No\n"
]
} |
5,124 | 7 | Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each eleme... | def run(i):
global l
d[i]=1
if(l[i][1]==0):
return i
return run(l[i][1])
n=int(input())
l=[]
for u in range(n):
l.append(list(map(int,input().split())))
l.insert(0,0)
d={}
ans=0
start=0
for i in range(1,len(l)):
if(l[i][0] == 0):
ans = run(i)
start = i
break
for i in range(start+1,len(l)):
if(l[i][0... | {
"input": [
"7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0\n"
],
"output": [
"4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0\n"
]
} |
5,125 | 9 | Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems... | def flip(x):
return any(bin(i)[2:] in s for i in range(2 << k) if not (i & x))
n, k = map(int, input().split())
s = set()
for _ in range(n):
x = input().replace(' ', '')
if flip(int(x, 2)) or x == '0' * k:
exit(print('YES'))
s.add(x.lstrip('0'))
print('NO') | {
"input": [
"3 2\n1 0\n1 1\n0 1\n",
"5 3\n1 0 1\n1 1 0\n1 0 0\n1 0 0\n1 0 0\n"
],
"output": [
"YES\n",
"NO\n"
]
} |
5,126 | 9 | You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.
It is allowed to leave a as it is.
Input
The first line contains integer a (1 β€ a β€ 1018). The second line contains i... | def main():
a = sorted(input(), reverse=True)
b = int(input())
k = ""
while len(a) > 0:
for i in range(len(a)):
num = k + a[i] + "".join(sorted(a[:i] + a[i + 1:]))
if int(num) <= b:
k += a[i]
a = a[:i] + a[i + 1:]
break
... | {
"input": [
"4940\n5000\n",
"3921\n10000\n",
"123\n222\n"
],
"output": [
"4940\n",
"9321\n",
"213\n"
]
} |
5,127 | 10 | Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tour. Of course, they will visit Berland as well.
There are n cities in Berland. People can travel between cities using two-directional train routes; there are exactly m routes, i-th route can be used to go from c... | from __future__ import division, print_function
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import BytesIO, IOBase
# FastIO for PyPy2 and PyPy3 by Pajenegod
class FastI(object):
def __init__(self, fd=0, buffersize=2**14):
... | {
"input": [
"4 2\n1 2 4\n2 3 7\n6 20 1 25\n",
"3 3\n1 2 1\n2 3 1\n1 3 1\n30 10 20\n"
],
"output": [
"6 14 1 25 ",
"12 10 12 "
]
} |
5,128 | 10 | In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal... | def y(x):
return -(a*x+c)/b
def x(y):
return -(b*y+c)/a
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
taxi_path = abs(x1-x2) + abs(y1-y2)
if a == 0 or b == 0:
print(taxi_path)
else:
av_1 = abs(x1-x(y1)) + pow(abs(x(y1) - x(y2))**2 + abs(y1-y2)**2, 0.5) + abs(x(... | {
"input": [
"3 1 -9\n0 3 3 -1\n",
"1 1 -3\n0 3 3 0\n"
],
"output": [
"6.16227766016838\n",
"4.242640687119285\n"
]
} |
5,129 | 8 | Alice's hair is growing by leaps and bounds. Maybe the cause of it is the excess of vitamins, or maybe it is some black magic...
To prevent this, Alice decided to go to the hairdresser. She wants for her hair length to be at most l centimeters after haircut, where l is her favorite number. Suppose, that the Alice's he... | from sys import stdin, stdout
def main(input=stdin.readlines, print=stdout.write, map=map, int=int, str=str):
lines, answer = input(), []
n, m, l = map(int, lines[0].split())
a = [-1] + list(map(int, lines[1].split())) + [-1]
x = sum(1 for q in range(n + 1) if a[q] > l >= a[q - 1])
for q1 in lines... | {
"input": [
"4 7 3\n1 2 3 4\n0\n1 2 3\n0\n1 1 3\n0\n1 3 1\n0\n"
],
"output": [
"1\n2\n2\n1\n"
]
} |
5,130 | 9 | Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this a... | import sys
def fi():
return sys.stdin.readline()
if __name__ == '__main__':
d = dict()
n = int(fi())
l = list(map(int, fi().split()))
s = sum(l)
for i in range (n):
d.setdefault(l[i],[])
d[l[i]].append(i+1)
c = 0
ans = []
for q,v in d.items():
a = (s-q)/2
b = len(v)
b -= 1
if (d.get(a) and q!=a) or... | {
"input": [
"5\n2 5 1 2 2\n",
"4\n8 3 5 2\n",
"5\n2 1 2 4 3\n"
],
"output": [
"3\n1 4 5\n",
"2\n1 4\n",
"0\n\n"
]
} |
5,131 | 9 | Ramesses came to university to algorithms practice, and his professor, who is a fairly known programmer, gave him the following task.
You are given two matrices A and B of size n Γ m, each of which consists of 0 and 1 only. You can apply the following operation to the matrix A arbitrary number of times: take any subma... | def g(f):
a=b=0;
for _ in range(f):i=input();a,b=a^int(i.replace(' ',''),2),b*2+i.count('1')%2
return a,b
n=int(input().split()[0]);print("Yes"if g(n)==g(n)else"No") | {
"input": [
"3 4\n0 1 0 1\n1 0 1 0\n0 1 0 1\n1 1 1 1\n1 1 1 1\n1 1 1 1\n",
"6 7\n0 0 1 1 0 0 1\n0 1 0 0 1 0 1\n0 0 0 1 0 0 1\n1 0 1 0 1 0 0\n0 1 0 0 1 0 1\n0 1 0 1 0 0 1\n1 1 0 1 0 1 1\n0 1 1 0 1 0 0\n1 1 0 1 0 0 1\n1 0 1 0 0 1 0\n0 1 1 0 1 0 0\n0 1 1 1 1 0 1\n",
"3 3\n0 1 0\n0 1 0\n1 0 0\n1 0 0\n1 0 0\n... |
5,132 | 9 | The only difference between easy and hard versions is constraints.
If you write a solution in Python, then prefer to send it in PyPy to speed up execution time.
A session has begun at Beland State University. Many students are taking exams.
Polygraph Poligrafovich is going to examine a group of n students. Students ... | import sys
def nline():
return sys.stdin.readline()[:-1]
def ni():
return int(sys.stdin.readline())
def na():
return [int(v) for v in sys.stdin.readline().split()]
n,m=na()
arr=na()
sm=0
count=[0]*101
for i in range(0,n):
diff,k=sm+arr[i]-m,0
if(diff>0):
for j in range(100,-1,-1):
tmp=count[j]*j... | {
"input": [
"7 15\n1 2 3 4 5 6 7\n",
"5 100\n80 40 40 40 60\n"
],
"output": [
"0 0 0 0 0 2 3 \n",
"0 1 1 2 3 \n"
]
} |
5,133 | 7 | In the city of Saint Petersburg, a day lasts for 2^{100} minutes. From the main station of Saint Petersburg, a train departs after 1 minute, 4 minutes, 16 minutes, and so on; in other words, the train departs at time 4^k for each integer k β₯ 0. Team BowWow has arrived at the station at the time s and it is trying to co... | def solution(x):
if x <= 1:
return 0
return (len(str(x - 1)) + 1) // 2
x = int(input())
print(solution(x)) | {
"input": [
"100000000\n",
"101\n",
"10100\n"
],
"output": [
"4\n",
"2\n",
"3\n"
]
} |
5,134 | 8 | You are given n positive integers a_1, β¦, a_n, and an integer k β₯ 2. Count the number of pairs i, j such that 1 β€ i < j β€ n, and there exists an integer x such that a_i β
a_j = x^k.
Input
The first line contains two integers n and k (2 β€ n β€ 10^5, 2 β€ k β€ 100).
The second line contains n integers a_1, β¦, a_n (1 β€ a_... | from collections import Counter as C
NN = 100000
X = [-1] * (NN+1)
L = [[] for _ in range(NN+1)]
k = 2
while k <= NN:
X[k] = 1
L[k].append(k)
for i in range(k*2, NN+1, k):
X[i] = 0
L[i].append(k)
d = 2
while k**d <= NN:
for i in range(k**d, NN+1, k**d):
L[i].appen... | {
"input": [
"6 3\n1 3 9 8 24 1\n"
],
"output": [
"5"
]
} |
5,135 | 8 | Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every... | def main():
n, k = map(int, input().split())
res, bbb = 0, set()
#
for _ in range(n):
aa = bytes((ord(c) - 3) & 3 for c in input())
for bb in bbb:
res += bytes(((0, 2, 1), (2, 1, 0), (1, 0, 2))[a][b] for a, b in zip(aa, bb)) in bbb
bbb.add(aa)
print(res // 2)
if... | {
"input": [
"3 3\nSET\nETS\nTSE\n",
"3 4\nSETE\nETSE\nTSES\n",
"5 4\nSETT\nTEST\nEEET\nESTE\nSTES\n"
],
"output": [
"1\n",
"0\n",
"2\n"
]
} |
5,136 | 8 | Bessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play!
More specifically, he wants to get from (0,0) to (x,0) by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance betw... | def solve():
n, x = map(int, input().split())
a = list(map(int, input().split()))
if x in a:
print(1)
else:
m = max(a)
print(max(2, (x + m - 1) // m))
t = int(input())
for _ in range(t):
solve() | {
"input": [
"4\n2 4\n1 3\n3 12\n3 4 5\n1 5\n5\n2 10\n15 4\n"
],
"output": [
"2\n3\n1\n2\n"
]
} |
5,137 | 8 | The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length... | I = input
pr = print
def main():
for _ in range(int(I())):
n = int(I())
ar = list(map(int,I().split()))
i = max(ar);d=[]
for c in range(2):
if set(ar[:i])==set(range(1,i+1)) and set(ar[i:])==set(range(1,n-i+1)): d.append(i);
i = n-i
d=set(d)
pr(len(d))
for x in d:pr(x,n-x)
main() | {
"input": [
"6\n5\n1 4 3 2 1\n6\n2 4 1 3 2 1\n4\n2 1 1 3\n4\n1 3 3 1\n12\n2 1 3 4 5 6 7 8 9 1 10 2\n3\n1 1 1\n"
],
"output": [
"2\n1 4\n4 1\n1\n4 2\n0\n0\n1\n2 10\n0\n"
]
} |
5,138 | 8 | There are n models in the shop numbered from 1 to n, with sizes s_1, s_2, β¦, s_n.
Orac will buy some of the models and will arrange them in the order of increasing numbers (i.e. indices, but not sizes).
Orac thinks that the obtained arrangement is beatiful, if for any two adjacent models with indices i_j and i_{j+1} ... | def solv():
x=int(input())
s=list(map(int,input().split()))
dp=[1]*(x+1)
for n in range(1,x+1):
for k in range(2*n,x+1,n):
if s[n-1]<s[k-1]:
dp[k]=max(dp[k],dp[n]+1)
print(max(dp))
for _ in range(int(input())):solv() | {
"input": [
"4\n4\n5 3 4 6\n7\n1 4 2 3 6 4 9\n5\n5 4 3 2 1\n1\n9\n"
],
"output": [
"2\n3\n1\n1\n"
]
} |
5,139 | 11 | Naman has two binary strings s and t of length n (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert s into t using the following operation as few times as possible.
In one operation, he can choose any subsequence of s and rotate it clockwise once.
For example, if s = ... | def solve(n,s,t):
maxi = 0
mini = 0
summ = 0
for i in range(n):
summ += int(s[i]) - int(t[i])
if summ > maxi: maxi = summ
if summ < mini: mini = summ
if summ != 0:
print(-1)
else:
print(maxi-mini)
if __name__ == '__main__':
n = int(input())
s... | {
"input": [
"10\n1111100000\n1111100001\n",
"6\n010000\n000001\n",
"10\n1111100000\n0000011111\n",
"8\n10101010\n01010101\n"
],
"output": [
"-1\n",
"1\n",
"5\n",
"1\n"
]
} |
5,140 | 7 | One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal.
The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with si... | def main():
for _ in range(int(input())):
print(int(input())//2+1)
main() | {
"input": [
"2\n3\n4\n"
],
"output": [
"2\n3\n"
]
} |
5,141 | 8 | Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya loves long lucky numbers very much. He is interested in the minimum lucky number d that meet... | a1,a2,a3,a4=map(int,input().split())
L=[]
def Solve(a1,a2,a3,a4):
if(a3-a4<-1 or a3-a4>1 or a1<a3 or a1<a4 or a2<a3 or a2<a4):
return -1
elif(a3-a4==0):
Ans="47"*a3
Ans+="4"
if(a1-a3==0 and a2-a4==0):
return -1
elif(a1-a3==0):
return "74"*a3+"7"*... | {
"input": [
"4 7 3 1\n",
"2 2 1 1\n"
],
"output": [
"-1",
"4774"
]
} |
5,142 | 10 | You are given a matrix a consisting of positive integers. It has n rows and m columns.
Construct a matrix b consisting of positive integers. It should have the same size as a, and the following conditions should be met:
* 1 β€ b_{i,j} β€ 10^6;
* b_{i,j} is a multiple of a_{i,j};
* the absolute value of the dif... | def inputs():
return map(int, input().split())
n, m = inputs()
lcm = 720720
for i in range(n):
a = list(inputs())
for j in range(m):
if (i ^ j) & 1: print(lcm, end=" ")
else: print(lcm - a[j] ** 4, end=" ")
print() | {
"input": [
"2 3\n16 16 16\n16 16 16\n",
"2 2\n1 2\n2 3\n",
"2 2\n3 11\n12 8\n"
],
"output": [
"\n16 32 48\n32 48 64\n",
"\n1 2\n2 3\n",
"\n327 583\n408 664\n"
]
} |
5,143 | 8 | Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to ... | def solve():
mod = int(1e9+7)
n, m, k = map(int, input().split())
if k==1 or k>n:
print(pow(m, n, mod))
elif k==n:
print(pow(m, (n+1)//2, mod))
elif k%2==1:
print((m*m)%mod)
elif k%2==0:
print(m)
solve() | {
"input": [
"1 1 1\n",
"5 2 4\n"
],
"output": [
"1\n",
"2\n"
]
} |
5,144 | 7 | Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito'... | #!/usr/bin/python3
def readln(): return tuple(map(int, input().split()))
s, n = readln()
for x, y in sorted([readln() for _ in range(n)]):
if s <= x:
s = -1
break
s += y
print('YES' if s >= 0 else 'NO')
| {
"input": [
"2 2\n1 99\n100 0\n",
"10 1\n100 100\n"
],
"output": [
"YES\n",
"NO\n"
]
} |
5,145 | 8 | Do you remember how Kai constructed the word "eternity" using pieces of ice as components?
Little Sheldon plays with pieces of ice, each piece has exactly one digit between 0 and 9. He wants to construct his favourite number t. He realized that digits 6 and 9 are very similar, so he can rotate piece of ice with 6 to u... | def c(a, b):
a = a.replace('6', '9')
a = a.replace('2', '5')
b = b.replace('6', '9')
b = b.replace('2', '5')
n = 10000
for i in '01345789':
t = a.count(i)
if t != 0:
n = min(n, b.count(i)//t)
return n
a = input()
b = input()
print(c(a, b)) | {
"input": [
"42\n23454\n",
"169\n12118999\n"
],
"output": [
"2\n",
"1\n"
]
} |
5,146 | 9 | Β«BersoftΒ» company is working on a new version of its most popular text editor β Bord 2010. Bord, like many other text editors, should be able to print out multipage documents. A user keys a sequence of the document page numbers that he wants to print out (separates them with a comma, without spaces).
Your task is to w... | a=sorted(list(set(map(int,input().split(',')))))
b=[[a[0],a[0]]]
for i in a[1:]:
if i==b[-1][1]+1:b[-1][1]+=1
else:b+=[[i,i]]
def f(x):
if x[0]==x[1]:return str(x[0])
return str(x[0])+'-'+str(x[1])
print(','.join(map(f,b)))
| {
"input": [
"30,20,10\n",
"3,2,1\n",
"1,2,3,1,1,2,6,6,2\n"
],
"output": [
"10,20,30",
"1-3",
"1-3,6"
]
} |
5,147 | 7 | You are given an integer m as a product of integers a1, a2, ... an <image>. Your task is to find the number of distinct decompositions of number m into the product of n ordered positive integers.
Decomposition into n products, given in the input, must also be considered in the answer. As the answer can be very large, ... | from math import factorial as f
def primes(n):
sieve = [True] * n
for i in range(3,int(n**0.5)+1,2):
if sieve[i]:
sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1)
return [2] + [i for i in range(3,n,2) if sieve[i]]
p = primes(31627)
s = [0]*(31623)
s1={}
def factorize(n):
for i in p:
if... | {
"input": [
"1\n15\n",
"3\n1 1 2\n",
"2\n5 7\n"
],
"output": [
"1\n",
"3\n",
"4\n"
]
} |
5,148 | 11 | DZY loves planting, and he enjoys solving tree problems.
DZY has a weighted tree (connected undirected graph without cycles) containing n nodes (they are numbered from 1 to n). He defines the function g(x, y) (1 β€ x, y β€ n) as the longest edge in the shortest path between nodes x and y. Specially g(z, z) = 0 for every... | n = int(input())
edges = [[int(x) for x in input().split()] for i in range(n-1)]
edges = sorted(edges)
use_count = [0]+[int(input()) for i in range(n)]
lo,hi = 0,10000
def getpar(par,u):
if par[par[u]] == par[u]:
return par[u]
par[u] = getpar(par,par[u])
return par[u]
def unite(par,sz,use,u,v):
... | {
"input": [
"4\n1 2 1\n2 3 2\n3 4 3\n1\n1\n1\n1\n",
"4\n1 2 1\n2 3 2\n3 4 3\n4\n4\n4\n4\n"
],
"output": [
"2\n",
"3\n"
]
} |
5,149 | 7 | George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory.
George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living... | def cn(v):
return len([x for x in v if x[1]-x[0]>1])
n=int(input())
v=[]
for _ in range(n):
v.append([int(x) for x in input().split()])
print(cn(v)) | {
"input": [
"3\n1 1\n2 2\n3 3\n",
"3\n1 10\n0 10\n10 10\n"
],
"output": [
"0\n",
"2\n"
]
} |
5,150 | 10 | A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took... | def fail():
print(-1)
import sys
sys.exit()
n = int(input())
count = (n + 1) * [ 0 ]
assign = n * [ None ]
for i, x in enumerate(map(int, input().split())):
if x > n:
fail()
count[x] += 1
assign[i] = count[x]
for i in range(2, n):
if count[i - 1] < count[i]:
fail()
print(count[1])
print(' '.joi... | {
"input": [
"9\n1 2 3 1 2 1 4 2 5\n",
"4\n4 3 2 1\n",
"4\n1 2 2 3\n"
],
"output": [
"3\n1 1 1 2 2 3 1 3 1 ",
"1\n1 1 1 1 ",
"-1"
]
} |
5,151 | 8 | One popular blog site edits the uploaded photos like this. It cuts a rectangular area out of them so that the ratio of height to width (i.e. the height / width quotient) can vary from 0.8 to 1.25 inclusively. Besides, at least one side of the cut area should have a size, equal to some power of number 2 (2x for some int... | import sys
import math
def input(): return sys.stdin.readline().strip()
def iinput(): return int(input())
def minput(): return map(int, input().split())
def listinput(): return list(map(int, input().split()))
h,w=minput()
x=2**int(math.log2(h))
y=2**int(math.log2(w))
if x>y:
x=int(min(x,y*1.25))
else:
y... | {
"input": [
"2 1\n",
"2 2\n",
"5 5\n"
],
"output": [
"1 1",
"2 2",
"5 4"
]
} |
5,152 | 10 | Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 Γ n table).
At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 Γ a rectangle (that is, it occupies... | import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def solve():
n, k, a = mints()
a += 1
m = mint()
x = list(mints())
l = 0
r = m + 1
while r - l > 1:
c = (l + r) // 2
b = x[:c]
b.sort()
last = 0
cnt = 0
for i ... | {
"input": [
"11 3 3\n5\n4 8 6 1 11\n",
"5 1 3\n1\n3\n",
"5 1 3\n2\n1 5\n"
],
"output": [
"3\n",
"1\n",
"-1\n"
]
} |
5,153 | 9 | The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vec... | def minusv(L):
return [-x for x in L]
def adamar(M):
return [L*2 for L in M] + [L + minusv(L) for L in M]
k = int(input())
a = [[1]]
for i in range(k):
a = adamar(a)
for L in a:
print(''.join(['+' if c==1 else '*' for c in L]))
| {
"input": [
"2\n"
],
"output": [
"++++\n+*+*\n++**\n+**+\n"
]
} |
5,154 | 11 | Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road sy... | n,m=map(int,input().split())
g=[[] for i in range(n)]
for i in range(m):
a,b=map(int,input().split())
g[a-1].append(b-1)
g[b-1].append(a-1)
a=0
v=[-1]*n
def dfs(x):
global a
s=[x]
v[x]=-2
while s:
x=s.pop()
for j in g[x]:
if v[j]==-1:
s.append(j)
... | {
"input": [
"5 5\n2 1\n1 3\n2 3\n2 5\n4 3\n",
"4 3\n2 1\n1 3\n4 3\n",
"6 5\n1 2\n2 3\n4 5\n4 6\n5 6\n"
],
"output": [
"0",
"1",
"1"
]
} |
5,155 | 8 | Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not ... | import sys
def minp():
return sys.stdin.readline().strip()
n = int(minp())
k = 0
for i in sorted(map(int,minp().split())):
if i >= k+1:
k += 1
print(k+1) | {
"input": [
"5\n1 3 3 3 6\n",
"2\n2 1\n"
],
"output": [
"5\n",
"3\n"
]
} |
5,156 | 7 | Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings.
Hulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is hate, seco... | th='that '
hf='I hate that I love '.split(th)
def fl(n):
v=[hf[x%2] for x in range(n)]
return th.join(v)+'it'
print(fl(int(input()))) | {
"input": [
"2\n",
"1\n",
"3\n"
],
"output": [
"I hate that I love it\n",
"I hate it\n",
"I hate that I love that I hate it\n"
]
} |
5,157 | 10 | The organizers of a programming contest have decided to present t-shirts to participants. There are six different t-shirts sizes in this problem: S, M, L, XL, XXL, XXXL (sizes are listed in increasing order). The t-shirts are already prepared. For each size from S to XXXL you are given the number of t-shirts of this si... | import sys
def gg():
print('NO')
sys.exit()
s,m,l,xl,xxl,xxxl=map(int,input().split())
r={'S':s,'M':m,'L':l,'XL':xl,'XXL':xxl,'XXXL':xxxl}
t={'S,M':[],'M,L':[],'L,XL':[],'XL,XXL':[],'XXL,XXXL':[]}
n=int(input())
z=['']*n
for i in range(n):
s=input()
if ',' not in s:
if r[s]<=0: gg()
r[s]... | {
"input": [
"0 1 0 1 1 0\n3\nXL\nS,M\nXL,XXL\n",
"1 1 2 0 1 1\n5\nS\nM\nS,M\nXXL,XXXL\nXL,XXL\n"
],
"output": [
"YES\nXL\nM\nXXL\n",
"NO\n"
]
} |
5,158 | 10 | Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible.
Limak has a string s t... | # http://codeforces.com/contest/771/problem/D
"""
DP-solution.
For each state (v, k, x, v_is_last_letter) we trial a step along the v, k and x
axes and check that
dp[future_state] = min(dp[future_state], dp[state] + cost_of_move)
Hence this implicitly reults in the one with least cost.
V, K, X are arrays that contain... | {
"input": [
"4\nVKVK\n",
"7\nVVKEVKK\n",
"5\nLIMAK\n",
"20\nVKVKVVVKVOVKVQKKKVVK\n",
"5\nBVVKV\n"
],
"output": [
"3\n",
"3\n",
"0\n",
"8\n",
"2\n"
]
} |
5,159 | 10 | Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.
Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be pos... | import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache #use @lru_cache(None)
if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')
#
def input(): return sys.stdin.readline()
def mapi(arg=0): ret... | {
"input": [
"6 3 2\n1 5 6\n1 2\n1 3\n1 4\n1 5\n5 6\n",
"6 2 4\n1 6\n1 2\n2 3\n3 4\n4 5\n5 6\n"
],
"output": [
"2\n4 5 ",
"1\n3 "
]
} |
5,160 | 10 | A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array.
Let's call a permutation an almost identity permutation iff there exist at least n - k indices i (1 β€ i β€ n) such that pi = i.
Your task is to count the number of almost identity permutations for given numbers... | def bexpans(n,p):
if p == 0:
return 1
else:
return n*bexpans(n-1,p-1)//p
s = [1,0,1,2,9]
n,k = map(int,input().split())
res = sum(bexpans(n,i)*s[i] for i in range(k+1))
print(res) | {
"input": [
"4 2\n",
"4 1\n",
"5 4\n",
"5 3\n"
],
"output": [
"7\n",
"1\n",
"76\n",
"31\n"
]
} |
5,161 | 8 | It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one β into b pieces.
Ivan knows that there will be n people at the celebration (including himself), so Ivan has set n plat... | def fn(Cake,X):
if Cake<X:
return -1
return Cake//X
n,a,b=list(map(int,input().split()))
print(max([min(fn(a,i),fn(b,n-i)) for i in range(1,n)])) | {
"input": [
"4 7 10\n",
"5 2 3\n"
],
"output": [
"3\n",
"1\n"
]
} |
5,162 | 8 | Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. Fo... | n=int(input())
a=[]
def foo(x):
if x>=n and str(x).count('4')==str(x).count('7'):
a.append(x)
if x<1e12+1:
foo(10*x+4)
foo(10*x+7)
foo(0)
a.sort()
print(a[0])
| {
"input": [
"47\n",
"4500\n"
],
"output": [
"47\n",
"4747\n"
]
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.