index
int64
0
5.16k
difficulty
int64
7
12
question
stringlengths
126
7.12k
solution
stringlengths
30
18.6k
test_cases
dict
600
10
The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it? The discotheque can be seen as an infinite xy-plane, in which there are a total of n dancers. Once someone starts moving around, they will move only inside their own movement range, which i...
n = int(input()) d = [1] * n p = [[] for i in range(n)] def f(): x, y, r = map(int, input().split()) return r * r, x, y t = sorted(f() for i in range(n)) for i in range(n): r, x, y = t[i] for j in range(i + 1, n): s, a, b = t[j] if (a - x) ** 2 + (b - y) ** 2 < s: p[j].a...
{ "input": [ "5\n2 1 6\n0 4 1\n2 -1 3\n1 -2 1\n4 -1 1\n", "8\n0 0 1\n0 0 2\n0 0 3\n0 0 4\n0 0 5\n0 0 6\n0 0 7\n0 0 8\n" ], "output": [ "138.230076757951\n", "289.026524130261\n" ] }
601
10
One quite ordinary day Valera went to school (there's nowhere else he should go on a week day). In a maths lesson his favorite teacher Ms. Evans told students about divisors. Despite the fact that Valera loved math, he didn't find this particular topic interesting. Even more, it seemed so boring that he fell asleep in ...
def pr(x): d = 2 while d * d <= x: if x % d == 0: return 0 d += 1 return 1 def cnt(n, k): if not pr(k) or n < k: return 0 n1 = n // k return n1 - sum(cnt(n1, i) for i in range(2, min(k, n1 + 1))) a, b, k = map(int, input().split()) ans = cnt(b, k) - cnt(a - 1, k...
{ "input": [ "12 23 3\n", "1 10 2\n", "6 19 5\n" ], "output": [ "2\n", "5\n", "0\n" ] }
602
11
You are given a program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular dependencies between tasks. A task can only be executed if all tasks it depen...
from collections import deque import sys input = sys.stdin.readline def put(): return map(int, input().split()) def bfs_(main, co, flag): curr = main if flag else co while curr: x = curr.popleft() for y in graph[x]: inedge[y]-=1 if inedge[y]==0: if is...
{ "input": [ "4 3\n1 1 1 0\n0 1\n0 2\n3 0\n", "4 3\n0 1 0 1\n0 1\n1 2\n2 3\n" ], "output": [ "1", "2" ] }
603
8
The last stage of Football World Cup is played using the play-off system. There are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the four...
import math def rec(a,b,count): if a//2 == b//2: return count else: return rec(a//2,b//2,count+1) n,a,b = input().split() res = rec(int(a)-1,int(b)-1,1) if res==math.log2(int(n)): print("Final!") else: print(res)
{ "input": [ "4 1 2\n", "8 7 5\n", "8 2 6\n" ], "output": [ "1", "2", "Final!" ] }
604
8
You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where ai, j = 1 if the i-th switch turns on the j-th lamp and ai, j = 0 if the i-th switch is not connected to the j-th lamp. Initially all m lamps are t...
def main(): n, m = map(int, input().split()) grid = [input() for _ in range(n)] counts = [sum(grid[i][j] == '1' for i in range(n)) for j in range(m)] no_skip = all(any(grid[i][j] == '1' and counts[j] == 1 for j in range(m)) for i in range(n)) print('NO' if no_skip else 'YES') main()
{ "input": [ "4 5\n10100\n01000\n00110\n00101\n", "4 5\n10101\n01000\n00111\n10000\n" ], "output": [ "NO\n", "YES\n" ] }
605
7
Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2, where (x_1, y_1) and (x...
def main(): n = int(input()) numbers = list(map(int, input().split(" "))) numbers.sort() r = [numbers[n-1+x]-numbers[x] for x in range(n+1)] #print(numbers) #print(r) print(min([r[0]*r[-1],(numbers[-1]-numbers[0])*min(r)])) main()
{ "input": [ "4\n4 1 3 2 3 2 1 3\n", "3\n5 8 5 5 7 5\n" ], "output": [ "1", "0" ] }
606
7
You have n coins, each of the same value of 1. Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets. Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, howeve...
def go(): n = int(input()) return len(bin(n)) - 2 print(go())
{ "input": [ "6\n", "2\n" ], "output": [ "3\n", "2\n" ] }
607
8
Dark Assembly is a governing body in the Netherworld. Here sit the senators who take the most important decisions for the player. For example, to expand the range of the shop or to improve certain characteristics of the character the Dark Assembly's approval is needed. The Dark Assembly consists of n senators. Each of...
#!/usr/bin/env python3 n, k, A = map(int, input().rstrip().split()) senators = [] mx_bribe = 0 for i in range(n): lvl, loy = map(int, input().rstrip().split()) senators.append((lvl, loy)) mx_bribe += (100 - loy) // 10 bribe = [0] * n def calc(votes): bsum, cnt, p = 0, 0, 1.0 for i, s in enumera...
{ "input": [ "5 3 100\n11 80\n14 90\n23 70\n80 30\n153 70\n", "5 6 100\n11 80\n14 90\n23 70\n80 30\n153 70\n", "1 3 20\n20 20\n" ], "output": [ "0.9628442962\n", "1.0000000000\n", "0.7500000000\n" ] }
608
9
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors. Having finished painting all bricks, Chouti was s...
def f(x): p=1; for i in range(1,x): p*=i return p n,m,k=map(int,input().split()) print(m*(m-1)**k*f(n)//f(k+1)//f(n-k)%998244353)
{ "input": [ "3 3 0\n", "3 2 1\n" ], "output": [ "3\n", "4\n" ] }
609
9
NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar pi...
import math def f(n, r): ct = math.sin(math.pi / n) return r * ct/(1 - ct) print(f(*map(int,input().split())))
{ "input": [ "3 1\n", "100 100\n", "6 1\n" ], "output": [ "6.464101615", "3.242939086", "1" ] }
610
7
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at...
n, m = map(int, input().split()) def dist(a, b): return (n + b - a) % n def main(): inp1 = [0] * (n + 1) inp2 = [n] * (n + 1) for _ in range(m): a, b = map(int, input().split()) inp1[a] += 1 inp2[a] = min(inp2[a], dist(a, b)) inp = tuple((((r1 - 1) * n + r2) for r1, r2 in...
{ "input": [ "5 7\n2 4\n5 1\n2 3\n3 4\n4 1\n5 3\n3 5\n", "2 3\n1 2\n1 2\n1 2\n" ], "output": [ "10 9 10 10 9\n", "5 6\n" ] }
611
7
We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in a...
def max_primes(n,l): l.sort(reverse=True) l.insert(1,l[-1]) l.pop() print(*l) n=int(input()) l=list(map(int,input().split())) max_primes(n,l)
{ "input": [ "9\n1 1 2 1 1 1 2 1 1\n", "5\n1 2 1 2 1\n" ], "output": [ "2 1 2 1 1 1 1 1 1\n", "2 1 2 1 1\n" ] }
612
9
You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1...
import math ch='abcdefghijklmnopqrstuvwxyz' def sieve(n): p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * 2, n + 1, p): prime[i] = False p += 1 prime[0]= False prime[1]= False s = ['#']+list(input()) lis=[0]*26 n = len(s)-1 prime = [Tru...
{ "input": [ "abcd\n", "abc\n", "xxxyxxx\n" ], "output": [ "NO\n", "YES\nabc", "YES\nxxxxxxy\n" ] }
613
7
Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. I...
def answer(): a = int(input()) print(9*a,8*a) answer()
{ "input": [ "512\n", "1\n" ], "output": [ "516 4\n", "9 8\n" ] }
614
7
Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clar...
def solve(s): res = ''.join(list(filter(lambda i: int(i) % 2 == 1, s)))[:2] return -1 if len(res) < 2 else res n = int(input()) for i in range(n): input() print(solve(input()))
{ "input": [ "4\n4\n1227\n1\n0\n6\n177013\n24\n222373204424185217171912\n" ], "output": [ "17\n-1\n17\n37\n" ] }
615
8
You are given an array a of length n. You are also given a set of distinct positions p_1, p_2, ..., p_m, where 1 ≤ p_i < n. The position p_i means that you can swap elements a[p_i] and a[p_i + 1]. You can apply this operation any number of times for each of the given positions. Your task is to determine if it is poss...
import math def rints(): return list(map(int,input().split())) t = int(input()) for _ in range(t): n,m = rints() a = rints() p = rints() for _ in range(100): for pi in p: if a[pi-1] > a[pi]: a[pi], a[pi-1] = a[pi-1], a[pi] print("YES" if sorted(a[:]) == ...
{ "input": [ "6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4\n" ], "output": [ "YES\nNO\nYES\nYES\nNO\nYES\n" ] }
616
12
Kate has a set S of n integers \{1, ..., n\} . She thinks that imperfection of a subset M ⊆ S is equal to the maximum of gcd(a, b) over all pairs (a, b) such that both a and b are in M and a ≠ b. Kate is a very neat girl and for each k ∈ \{2, ..., n\} she wants to find a subset that has the smallest imperfection am...
def f(n): d = [1]*(n+1) for i in range(2, int(n**0.5)+1): if d[i]>1: continue for j in range(2*i, n+1, i): d[j] = max(d[j], j//i) return d n = int(input()) d = f(n)[1:] d.sort() print(*d[1:])
{ "input": [ "3\n", "2\n" ], "output": [ "1 1", "1" ] }
617
9
The statement of this problem is the same as the statement of problem C2. 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 ⋅ 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...
import math as np def fun(): n = int(input()) print(1/np.tan(np.pi/(2*n))) T = int(input()) for _ in range(1,T+1): fun()
{ "input": [ "3\n2\n4\n200\n" ], "output": [ "1.0000000000000002\n2.414213562373095\n127.32133646887215\n" ] }
618
7
You are given three integers x, y and n. Your task is to find the maximum integer k such that 0 ≤ k ≤ n that k mod x = y, where mod is modulo operation. Many programming languages use percent operator % to implement it. In other words, with given x, y and n you need to find the maximum possible integer from 0 to n tha...
def main(): (x,y,n) = map(int, input().split(' ')) print (((n-y)//x)*x+y) for _ in range(int(input())): main()
{ "input": [ "7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999\n" ], "output": [ "12339\n0\n15\n54306\n999999995\n185\n999999998\n" ] }
619
8
Let's call a list of positive integers a_0, a_1, ..., a_{n-1} a power sequence if there is a positive integer c, so that for every 0 ≤ i ≤ n-1 then a_i = c^i. Given a list of n positive integers a_0, a_1, ..., a_{n-1}, you are allowed to: * Reorder the list (i.e. pick a permutation p of \{0,1,...,n - 1\} and change...
n,s=open(0) arr=sorted(map(int,s.split())) n=int(n) def mys(x): s=0 for i in range(n): s+=abs(arr[i]-x**i) return s mmax=mys(1) mmin=mys(1) t=2 while t**(n-1)<=(mmax+arr[n-1]): mmin=min(mmin,mys(t)) t+=1 print(mmin)
{ "input": [ "3\n1 3 2\n", "3\n1000000000 1000000000 1000000000\n" ], "output": [ "1\n", "1999982505\n" ] }
620
9
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R...
import sys import array input=sys.stdin.readline t=int(input()) for _ in range(t): n,q=map(int,input().split()) a=array.array("i",[-1]+list(map(int,input().split()))+[-1]) def f(i): if i<=0 or i>=n+1: return 0 if a[i-1]<a[i]>a[i+1]: return a[i] if a[i-1]>a[i]<...
{ "input": [ "3\n3 1\n1 3 2\n1 2\n2 2\n1 2\n1 2\n1 2\n7 5\n1 2 5 4 3 6 7\n1 2\n6 7\n3 4\n1 2\n2 3\n" ], "output": [ "3\n4\n2\n2\n2\n9\n10\n10\n10\n9\n11\n" ] }
621
8
Athenaeus has just finished creating his latest musical composition and will present it tomorrow to the people of Athens. Unfortunately, the melody is rather dull and highly likely won't be met with a warm reception. His song consists of n notes, which we will treat as positive integers. The diversity of a song is th...
def solve(): n=int(input()) A=[int(x) for x in input().split()] cnt = [0 for x in range(max(A)+3)] for x in A: cnt[x]=cnt[x]+1 ans=0 for i in range(max(A)+2): if cnt[i]>1: cnt[i+1]=cnt[i+1]+1 if cnt[i]:ans=ans+1 print(ans) import sys #sys.stdout=open("oup.txt","w") T=int(input()) for i in range(T): s...
{ "input": [ "5\n6\n1 2 2 2 5 6\n2\n4 4\n6\n1 1 3 4 4 5\n1\n1\n6\n1 1 1 2 2 2\n" ], "output": [ "\n5\n2\n6\n1\n3\n" ] }
622
9
You are given a positive integer x. Check whether the number x is representable as the sum of the cubes of two positive integers. Formally, you need to check if there are two integers a and b (1 ≤ a, b) such that a^3+b^3=x. For example, if x = 35, then the numbers a=2 and b=3 are suitable (2^3+3^3=8+27=35). If x=4, t...
def cube(n): i=1 while(i**3<n): if(round((n-i**3)**(1/3))**3==(n-i**3)): return 'Yes' i+=1 return 'No' t=int(input()) for _ in range(t): n=int(input()) print(cube(n))
{ "input": [ "7\n1\n2\n4\n34\n35\n16\n703657519796\n" ], "output": [ "\nNO\nYES\nNO\nNO\nYES\nYES\nYES\n" ] }
623
12
Polycarp is wondering about buying a new computer, which costs c tugriks. To do this, he wants to get a job as a programmer in a big company. There are n positions in Polycarp's company, numbered starting from one. An employee in position i earns a[i] tugriks every day. The higher the position number, the more tugriks...
from math import ceil def f(arr,b,c): b.append(0) currd=0 currm=0 ans=float("inf") j=0 for i in arr: ans=min(ans,max(ceil((c-currm)/i),0)+currd) t=max(ceil((b[j]-currm)/i),0) currd+=t+1 currm+=t*i-b[j] j += 1 return ans for i in range(int(input())): a,b=map(int,input().strip().split()) lst=list...
{ "input": [ "3\n4 15\n1 3 10 11\n1 2 7\n4 100\n1 5 10 50\n3 14 12\n2 1000000000\n1 1\n1\n" ], "output": [ "\n6\n13\n1000000000\n" ] }
624
7
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebr...
from queue import PriorityQueue from queue import Queue import math from collections import * import sys import operator as op from functools import reduce # sys.setrecursionlimit(10 ** 6) MOD = int(1e9 + 7) input = sys.stdin.readline def ii(): return list(map(int, input().strip().split())) def ist(): return input().s...
{ "input": [ "3\n", "4\n" ], "output": [ "6", "11" ] }
625
9
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length — an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance betw...
from collections import defaultdict def summ(x): s=0 for i in range(n): for j in range(i+1,n): s+=x[i][j] return s INF=10**15 n=int(input()) dis=[] for _ in range(1,n+1): dis.append(list(map(int,input().split()))) insum=summ(dis) k=int(input()) for i in range(k): a,b,cost=list(...
{ "input": [ "2\n0 5\n5 0\n1\n1 2 3\n", "3\n0 4 5\n4 0 9\n5 9 0\n2\n2 3 8\n1 2 1\n" ], "output": [ "3\n", "17 12\n" ] }
626
7
Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in such a way that the leftmost item has number 1, the rightmost item has number n. Each item has a weight, the i-th item weights wi kilograms. Vasya needs to collect all these items, however he won't do it by himself. He us...
from sys import stdin from itertools import accumulate def arr_sum(arr): return list(accumulate(arr, lambda x, y: x + y)) rints = lambda: [int(x) for x in stdin.readline().split()] n, l, r, ql, qr = rints() w, ans = [0] + rints(), float('inf') mem = arr_sum(w) for i in range(n + 1): s1, s2 = mem[i] - mem[...
{ "input": [ "3 4 4 19 1\n42 3 99\n", "4 7 2 3 9\n1 2 3 4\n" ], "output": [ "576\n", "34\n" ] }
627
8
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the peopl...
n = int(input()) a, b = zip(*(map(int, input().split()) for _ in range(n))) i = j = 0 while i < n and j < n and i + j < n: if a[i] < b[j]: i += 1 else: j += 1 def p(x): return ("1" * max(n // 2, x)).ljust(n, "0") print(p(i)) print(p(j))
{ "input": [ "4\n9840 9920\n9860 9980\n9930 10020\n10040 10090\n", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110\n" ], "output": [ "1110\n1100\n", "1100\n1100\n" ] }
628
10
You can find anything whatsoever in our Galaxy! A cubical planet goes round an icosahedral star. Let us introduce a system of axes so that the edges of the cubical planet are parallel to the coordinate axes and two opposite vertices lay in the points (0, 0, 0) and (1, 1, 1). Two flies live on the planet. At the moment ...
def f(l1,l2): return sum([l1[i]==l2[i] for i in range(len(l1))])>0 l1 = list(map(int,input().split())) l2 = list(map(int,input().split())) print('YES' if f(l1,l2) else 'NO')
{ "input": [ "0 0 0\n1 1 1\n", "0 0 0\n0 1 0\n", "1 1 0\n0 1 0\n" ], "output": [ "NO\n", "YES\n", "YES\n" ] }
629
9
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...
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from math import * def prm(x): s = int(sqrt(x+0.5)) for i in range(2, s+1): if not (x%i): return False return True def ap(a): while(len(a) >= 2): print(a[-1], a[-2]) a.pop() a.pop() n, cnt, ans, vis, ansl, ansr = int(input())...
{ "input": [ "6\n", "9\n", "2\n" ], "output": [ "2\n3 6\n2 4\n", "3\n3 9\n2 4\n6 8\n", "0\n" ] }
630
11
Vasya is studying in the last class of school and soon he will take exams. He decided to study polynomials. Polynomial is a function P(x) = a0 + a1x1 + ... + anxn. Numbers ai are called coefficients of a polynomial, non-negative integer n is called a degree of a polynomial. Vasya has made a bet with his friends that h...
def gnb(x, b): if b == 1: return [x] g = [] while x: g.append(x % b) x //= b return g ans = 0 t, a, b = map(int, input().split()) if t == 1 and a == 1 and b == 1: print("inf") raise SystemExit cf = gnb(b, a) a2 = sum([x * y for x, y in zip(cf, [t**n for n in range(len(cf))])]) if a2 == a: ans += 1 if len(cf)...
{ "input": [ "2 3 3\n", "2 2 2\n" ], "output": [ "1\n", "2\n" ] }
631
10
Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. Let's assume that n people stand in the queue for the escalator. At each second one of the two following possibilities takes place: either the first person in the queue ente...
def main(): tmp = input().split() n, p, t = int(tmp[0]), float(tmp[1]), int(tmp[2]) q = 1. - p res = [0.] * (n + 1) res[0] = 1. for _ in range(t): tmp = [x * q for x in res] tmp[-1] = res[-1] for i in range(n): tmp[i + 1] += res[i] * p res = tmp pr...
{ "input": [ "1 0.50 1\n", "4 0.20 2\n", "1 0.50 4\n" ], "output": [ "0.500000000000\n", "0.400000000000\n", "0.937500000000\n" ] }
632
10
In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a and b are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any ...
from itertools import combinations_with_replacement from collections import deque #sys.stdin = open("input_py.txt","r") n, m = map(int, input().split()) G = [ [] for i in range(n)] for i in range(m): x, y = map(int, input().split()) x-=1; y-=1 G[x].append(y) G[y].append(x) def BFS(s): dist = [-...
{ "input": [ "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n3 5 1\n", "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n3 5 2\n", "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n2 4 2\n" ], "output": [ "-1\n", "0\n", "1\n" ] }
633
7
You are given three sticks with positive integer lengths of a, b, and c centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters. In particular, it is allowed not to increase the l...
def f(a,b,c,l): if a<b+c: return 0 else: c=min(l,a-b-c) return (c+1)*(c+2)/2 a,b,c,l = map(int,input().split()) z=(l+1)*(l+2)*(l+3)/6 i=0 while i<=l: z-=f(a+i,b,c,l-i)+f(b+i,c,a,l-i)+f(c+i,a,b,l-i) i+=1 print(int(z))
{ "input": [ "1 1 1 2\n", "1 2 3 1\n", "10 2 1 7\n" ], "output": [ "4\n", "2\n", "0\n" ] }
634
10
Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ superhero. Byteforces is a country that consists of n cities, connected by n - 1 bidirectional roads. Every road connects exactly two distinct cities, and the whole road system is designed in a way that one is able to go ...
def f(k, h): print(k, h) exit() def g(p): i = p.pop() return i, t[i] import sys d = list(map(int, sys.stdin.read().split())) n, m = d[0], d[1] if n == 1: f(1, 0) k = 2 * n t = [[0, set(), i, 0] for i in range(n + 1)] for i in d[k:]: t[i][0] = 1 for a, b in zip(d[2:k:2], d[3:k:2]): t[a][1].add(...
{ "input": [ "7 2\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n2 7\n", "6 4\n1 2\n2 3\n2 4\n4 5\n4 6\n2 4 5 6\n" ], "output": [ "2\n3", "2\n4" ] }
635
8
It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not en...
n = int(input()) a = input().split() def get(l, r): if l == r: return int(a[l]) m = (l + r) // 2 return get(l, m) * get(m + 1, r) print(get(0, n - 1))
{ "input": [ "3\n5 10 1\n", "5\n0 3 1 100 1\n", "4\n1 1 10 11\n" ], "output": [ "50", "0\n", "110" ] }
636
8
You are given an undirected graph that consists of n vertices and m edges. Initially, each edge is colored either red or blue. Each turn a player picks a single vertex and switches the color of all edges incident to it. That is, all red edges with an endpoint in this vertex change the color to blue, while all blue edge...
from collections import deque n, m = map(int, input().split()) adj = [[] for i in range(n)] for i in range(m): u, v, c = input().split() u, v = int(u)-1, int(v)-1 adj[u].append((v, c)) adj[v].append((u, c)) visited = S = T = None def bfs(i, k): q = deque([(i, 0)]) while q: u, p = q...
{ "input": [ "4 5\n1 2 R\n1 3 R\n2 3 B\n3 4 B\n1 4 B\n", "6 5\n1 3 R\n2 3 R\n3 4 B\n4 5 R\n4 6 R\n", "3 3\n1 2 B\n3 1 R\n3 2 B\n" ], "output": [ "-1", "2\n3 4 ", "1\n2 " ] }
637
10
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matc...
root = lambda k : int((1 + (1 + 8*k) ** 0.5)/2) def check(k): n = root(k) return n*(n-1)/2 == k def solve(a00, a01, a10, a11): s = a00 + a01 + a10 + a11 if not check(a00) or not check(a11) or not check(s): return None x, y, z = root(a00), root(a11), root(s) if a00 == 0 or a11 == 0: ...
{ "input": [ "1 2 3 4\n", "1 2 2 1\n" ], "output": [ "Impossible", "0110" ] }
638
7
One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into several parts. This time Lesha decided to split the array A into several, possibly one, new arrays so that the sum of elements in each of the new arrays is not zero. One more condition is that if we place the new arrays one...
def Main(n): arr = list(map(int, input().split())) if any(arr): print('YES') if sum(arr): print(1) print(1,n) else: for i, v in enumerate(arr): if v: print(2) print(1,i + 1) print(i + 2, n) break else: print('NO') if __name__ == '_...
{ "input": [ "4\n1 2 3 -5\n", "1\n0\n", "8\n9 -12 3 4 -4 -10 7 3\n", "3\n1 2 -3\n" ], "output": [ "YES\n1\n1 4\n", "NO\n", "YES\n2\n1 1\n2 8\n", "YES\n2\n1 1\n2 3\n" ] }
639
9
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel fee...
s, n = input(), int(input()) t = [input() for i in range(n)] def f(i): global t for j in range(n): if i < j: if len(t[j]) < len(t[i]) and t[j] in t[i]: return False elif j < i and t[j] in t[i]: return False return True t = [t[i] for i in range(n) if f(i)] n = len(s) r = [0] * n...
{ "input": [ "unagioisii\n2\nioi\nunagi\n", "Go_straight_along_this_street\n5\nstr\nlong\ntree\nbiginteger\nellipse\n", "IhaveNoIdea\n9\nI\nh\na\nv\ne\nN\no\nI\nd\n" ], "output": [ "5 1", "12 4", "0 0" ] }
640
7
Hideo Kojima has just quit his job at Konami. Now he is going to find a new place to work. Despite being such a well-known person, he still needs a CV to apply for a job. During all his career Hideo has produced n games. Some of them were successful, some were not. Hideo wants to remove several of them (possibly zero)...
def main(): n = int(input()) a = list(map(int, input().split())) ans = max(sum(a), n - sum(a)) for i in range(n): ans = max(ans, sum(a[i:]) + i - sum(a[:i])) print(ans) main()
{ "input": [ "6\n0 1 0 0 1 0\n", "1\n0\n", "4\n1 1 0 1\n" ], "output": [ "4\n", "1\n", "3\n" ] }
641
9
It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly S slices. It is known that the i-th contestant will eat si slices of pizza, and gain ai ...
f = lambda: map(int, input().split()) n, s = f() u, v = [], [] for i in range(n): d, a, b = f() if a > b: u.append([a, b, d]) else: v.append([b, a, d]) def g(t): t.sort(key=lambda q: q[1] - q[0]) m = sum(d for a, b, d in t) k = s * (m // s) n = m - k x = y = z = ...
{ "input": [ "6 10\n7 4 7\n5 8 8\n12 5 8\n6 11 6\n3 3 7\n5 9 6\n", "3 12\n3 5 7\n4 6 7\n5 9 5\n" ], "output": [ "314\n", "84\n" ] }
642
11
You are given a tree (a connected acyclic undirected graph) of n vertices. Vertices are numbered from 1 to n and each vertex is assigned a character from a to t. A path in the tree is said to be palindromic if at least one permutation of the labels in the path is a palindrome. For each vertex, output the number of pa...
import sys, os def centroid_decomp(coupl): n = len(coupl) bfs = [n - 1] for node in bfs: bfs += coupl[node] for nei in coupl[node]: coupl[nei].remove(node) size = [0] * n for node in reversed(bfs): size[node] = 1 + sum(size[child] for child in coupl[nod...
{ "input": [ "7\n6 2\n4 3\n3 7\n5 2\n7 2\n1 4\nafefdfs\n", "5\n1 2\n2 3\n3 4\n3 5\nabcbb\n" ], "output": [ "1 4 1 1 2 4 2\n", "1 3 4 3 3\n" ] }
643
9
Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on. During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The s...
def solve(k, d, t): q, r = divmod(k, d) up = d - r if r != 0 else 0 perblock2 = k * 2 + up qt, rt = divmod(2 * t, perblock2) if rt <= k * 2: extra = rt / 2 else: extra = rt - k return (k + up) * qt + extra k, d, t = [int(v) for v in input().split()] print(solve(k, d, t))
{ "input": [ "4 2 20\n", "3 2 6\n" ], "output": [ "20.0\n", "6.5\n" ] }
644
11
The cities of Byteland and Berland are located on the axis Ox. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line Ox there are three types of cities: * the cities of Byteland, * the cities of Berland, * disputed cities. Recent...
def inpmap(): return map(int, input().split()) n = int(input()) b, r, p = None, None, None res = 0 mr = -1 mb = -1 for i in range(n): ix, t = input().split() ix = int(ix) if t != 'R': if b is not None: res += ix - b mb = max(mb, ix - b) b = ix if t != 'B': ...
{ "input": [ "5\n10 R\n14 B\n16 B\n21 R\n32 R\n", "4\n-5 R\n0 P\n3 P\n7 B\n" ], "output": [ "24\n", "12\n" ] }
645
7
Berland Football Cup starts really soon! Commentators from all over the world come to the event. Organizers have already built n commentary boxes. m regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be...
def i_ints(): return list(map(int, input().split())) n, m ,a, b = i_ints() rest = n % m print(min(rest * b, (m - rest) * a))
{ "input": [ "30 6 17 19\n", "2 7 3 7\n", "9 7 3 8\n" ], "output": [ "0\n", "14\n", "15\n" ] }
646
9
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that...
import sys import math def main(): s = next(sys.stdin).strip() s += 'b' res = [int(i != j) for i,j in zip(s,s[1:])] print(' '.join(map(str,res))) if __name__ == '__main__': main()
{ "input": [ "bbab\n", "aaaaa\n" ], "output": [ "0 1 1 0\n", "0 0 0 0 1\n" ] }
647
7
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute — at the point 2 and so on). There are lanterns on the...
def cal(lo,v): return lo//v q=int(input()) for _ in range(0,q): L,v,l,r=map(int,input().split()) print(cal(L,v)-(cal(r,v)-cal(l-1,v)))
{ "input": [ "4\n10 2 3 7\n100 51 51 51\n1234 1 100 199\n1000000000 1 1 1000000000\n" ], "output": [ "3\n0\n1134\n0\n" ] }
648
12
Vasya wants to buy himself a nice new car. Unfortunately, he lacks some money. Currently he has exactly 0 burles. However, the local bank has n credit offers. Each offer can be described with three numbers a_i, b_i and k_i. Offers are numbered from 1 to n. If Vasya takes the i-th offer, then the bank gives him a_i bur...
n = int(input()) a = [tuple(map(int, input().split())) for i in range(n)] a = [(y, x, k) for x, y, k in a] a.sort(reverse=True) dp = [[-1] * (n + 1) for i in range(n)] def f(i, j): if i < 0 or j < -1: return 0 if dp[i][j] == -1: y, x, k = a[i] dp[i][j] = f(i - 1, j) + max(0, x - k * y) ...
{ "input": [ "4\n10 9 2\n20 33 1\n30 115 1\n5 3 2\n", "3\n40 1 2\n1000 1100 5\n300 2 1\n" ], "output": [ "32\n", "1337\n" ] }
649
7
Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by n streets along the Eastern direction and m streets across the Southern direction. Naturally, this city has nm intersections. At any intersection of i-th Eastern street and j-th Southern street there is a monument...
n, m = list(map(int, input().split())) gor = [] ver = [] o = [] for i in range(n): gor.append(list(map(int, input().split()))) o.append([]) for i in range(m): ver.append([]) for i in range(m): for g in gor: ver[i].append(g[i]) ver = list(map(lambda x: sorted(list(set(x))), ver)) def f(vg, vr, ...
{ "input": [ "2 2\n1 2\n3 4\n", "2 3\n1 2 1\n2 1 2\n" ], "output": [ "2 3 \n3 2 \n", "2 2 2 \n2 2 2 \n" ] }
650
9
Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the i-th of them will start during the x_i-th minute. Ivan doesn't want to skip any of the events, so he has to set his alarm clock in such a way that it rings during minutes x_1, x_2, ..., x_n, so he will be awak...
import math read = lambda: map(int, input().split()) n,m = read(); a = list(read()); b = list(read()) gcd = 0 for i in a[1:]: gcd = math.gcd(gcd, i - a[0]) def solve(): for i in range(m): if gcd % b[i] == 0: print('YES\n', a[0],i+1) return print('NO') solve()
{ "input": [ "3 5\n3 12 18\n2 6 5 3 3\n", "4 2\n1 5 17 19\n2 1\n", "4 2\n1 5 17 19\n4 5\n" ], "output": [ "YES\n 3 4\n", "YES\n 1 1\n", "NO\n" ] }
651
8
Let's write all the positive integer numbers one after another from 1 without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536... Your task is to print the k-th digit of this sequence. Input The first and only lin...
def mp(): return map(int, input().split()) def f(i): return (10 ** i - 10 ** (i - 1)) * i n = int(input()) i = 1 sum = 0 while n - f(i) >= 0: n -= f(i) sum += f(i) // i i += 1 print(str(sum + (n + i - 1) // i)[n % i - 1])
{ "input": [ "21\n", "7\n" ], "output": [ "5\n", "7\n" ] }
652
8
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i. Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if ...
def main(): n=int(input()) arr = list(map(int,input().split())) min = arr[n-1] co = 0 for i in range(n-1,-1,-1): if min < arr[i]: co +=1 else: min = arr[i] print(co) for _ in range(int(input())): main()
{ "input": [ "5\n6\n3 9 4 6 7 5\n1\n1000000\n2\n2 1\n10\n31 41 59 26 53 58 97 93 23 84\n7\n3 2 1 2 3 4 5\n" ], "output": [ "3\n0\n1\n8\n2\n" ] }
653
9
In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transpor...
def create(n): res = [[0] * n for _ in range(n)] cur = 1 for j in range(n): for i in (range(n - 1, -1, -1) if j & 1 else range(n)): res[i][j] = cur cur += 1 return res n = int(input()) res = create(n) for l in res: print(*l)
{ "input": [ "3\n" ], "output": [ "1 6 7 \n2 5 8 \n3 4 9 \n" ] }
654
8
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes o...
from itertools import accumulate def divisors(n): tab = [] for i in range(2, int(n**.5) + 2): if n % i == 0: while n % i == 0: n //= i tab.append(i) if n > 1: tab.append(n) return tab n = int(input()) a = list(map(int, input().split())) s = sum(a) tab = divisors(s) if s == 1: print(-1) else: m...
{ "input": [ "1\n1\n", "3\n1 0 1\n" ], "output": [ "-1", "2\n" ] }
655
7
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems. Polycarp decided to store the hash of the password, generated by the following algorithm: 1. take the password p, consisting of lowercase Latin letters, and shuffle the l...
def ans(p, h): n = len(p) for i in range(len(h)-len(p)+1): if sorted(h[i:i+n]) == sorted(p): return "YES" return "NO" for u in range(int(input())): p=input() h = input() print(ans(p,h))
{ "input": [ "5\nabacaba\nzyxaabcaabkjh\nonetwothree\nthreetwoone\none\nzzonneyy\none\nnone\ntwenty\nten\n" ], "output": [ "YES\nYES\nNO\nYES\nNO\n" ] }
656
8
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n. Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to som...
def go(): n = int(input()) b = list(map(int, input().split())) d={} for i,bb in enumerate(b): d[bb-i] = d.get(bb-i,0)+bb return max(d.values()) print (go())
{ "input": [ "7\n8 9 26 11 12 29 14\n", "6\n10 7 1 9 10 15\n", "1\n400000\n" ], "output": [ "55\n", "26\n", "400000\n" ] }
657
7
Denis was very sad after Nastya rejected him. So he decided to walk through the gateways to have some fun. And luck smiled at him! When he entered the first courtyard, he met a strange man who was selling something. Denis bought a mysterious item and it was... Random permutation generator! Denis could not believed hi...
t = int(input()) def sol(): n = int(input()) a = list(map(int, input().split())) for i in range(n-1): if a[i] + 1 != a[i+1] and a[i] < a[i+1]: return False return True for i in range(t): print('Yes' if sol() else 'No')
{ "input": [ "5\n5\n2 3 4 5 1\n1\n1\n3\n1 3 2\n4\n4 2 3 1\n5\n1 5 2 4 3\n" ], "output": [ "Yes\nYes\nNo\nYes\nNo\n" ] }
658
7
Johnny has recently found an ancient, broken computer. The machine has only one register, which allows one to put in there one variable. Then in one operation, you can shift its bits left or right by at most three positions. The right shift is forbidden if it cuts off some ones. So, in fact, in one operation, you can m...
def main(): for _ in range(int(input())): a, b = map(bin, map(int, input().split())) print((abs(len(a) - len(b)) + 2) // 3 if a.rstrip('0') == b.rstrip('0') else -1) if __name__ == '__main__': main()
{ "input": [ "10\n10 5\n11 44\n17 21\n1 1\n96 3\n2 128\n1001 1100611139403776\n1000000000000000000 1000000000000000000\n7 1\n10 8\n" ], "output": [ "1\n1\n-1\n0\n2\n2\n14\n0\n-1\n-1\n" ] }
659
9
This is the easy version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In ...
def solve(n, a, b): cmds = [] for i in range(n): if a[i] != b[i]: cmds.extend([i + 1, 1, i + 1]) return cmds t = int(input()) for case in range(t): x = solve(int(input()), input(), input()) print(len(x), *x)
{ "input": [ "5\n2\n01\n10\n5\n01011\n11100\n2\n01\n01\n10\n0110011011\n1000110100\n1\n0\n1\n" ], "output": [ "3 1 2 1 \n3 1 5 2 \n0 \n7 1 10 8 7 1 2 1 \n1 1 \n" ] }
660
7
You are given an array a of length n, and an integer x. You can perform the following operation as many times as you would like (possibly zero): replace two adjacent elements of the array by their sum. For example, if the initial array was [3, 6, 9], in a single operation one can replace the last two elements by their ...
#fi = open("task1.txt") #def input(): # return fi.readline() T = int(input()) for _ in range(T): n, x = [int(y) for y in input().split()] b = [int(y) for y in input().split()] print(sum(b)//x+(1 if sum(b)%x>0 else 0),sum([(y//x)+(1 if y%x>0 else 0) for y in b]))
{ "input": [ "2\n3 3\n3 6 9\n3 3\n6 4 11\n" ], "output": [ "\n6 6\n7 8\n" ] }
661
7
The \text{gcdSum} of a positive integer is the gcd of that integer with its sum of digits. Formally, \text{gcdSum}(x) = gcd(x, sum of digits of x) for a positive integer x. gcd(a, b) denotes the greatest common divisor of a and b — the largest integer d such that both integers a and b are divisible by d. For example...
import math def f(n): return sum(int(i) for i in str(n)) for _ in range(int(input())): n = int(input()) while 1: if math.gcd(n, f(n)) > 1: print(n) break n += 1
{ "input": [ "3\n11\n31\n75\n" ], "output": [ "\n12\n33\n75\n" ] }
662
11
On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≤ a_i ≤ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to ...
def solve(): input() n, k = [int(i) for i in input().split()] a = [int(i) for i in input().split()] t = [int(i) for i in input().split()] dp = [10 ** 10 for i in range(n + 1)] for i in range(k): dp[a[i]] = t[i] for i in range(1, len(dp)): dp[i] = min(dp[i - 1] + 1, dp[i]) ...
{ "input": [ "5\n\n6 2\n2 5\n14 16\n\n10 1\n7\n30\n\n5 5\n3 1 4 2 5\n3 1 4 2 5\n\n7 1\n1\n1000000000\n\n6 3\n6 1 3\n5 5 5\n" ], "output": [ "15 14 15 16 16 17 \n36 35 34 33 32 31 30 31 32 33 \n1 2 3 4 5 \n1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 \n5 6 5 6 6 5 \n" ] }
663
8
Vasya has been playing Plane of Tanks with his friends the whole year. Now it is time to divide the participants into several categories depending on their results. A player is given a non-negative integer number of points in each round of the Plane of Tanks. Vasya wrote results for each round of the last year. He ha...
def t(g, n): for x, y in ((50, 'noob'), (20, 'random'), (10, 'average'), (1, 'hardcore')): if g > x * n // 100: return y return 'pro' p, st = {}, {} for i in range(int(input())): n, s = input().split() if n not in p or int(s) > p[n]: p[n] = int(s) for i, si in enumerate(sorte...
{ "input": [ "5\nvasya 100\nvasya 200\nartem 100\nkolya 200\nigor 250\n", "3\nvasya 200\nkolya 1000\nvasya 1000\n" ], "output": [ "4\nartem noob\nigor pro\nkolya random\nvasya random\n", "2\nkolya pro\nvasya pro\n" ] }
664
9
Vasya is developing his own programming language VPL (Vasya Programming Language). Right now he is busy making the system of exceptions. He thinks that the system of exceptions must function like that. The exceptions are processed by try-catch-blocks. There are two operators that work with the blocks: 1. The try op...
from sys import stdin, stdout n = int(stdin.readline()) label = '' cnt = 0 def first(s): f = '' for a in s: if 'z' >= a >= 'a': f += a else: break return f for i in range(n): s = stdin.readline().strip() if first(s) == 'throw': label...
{ "input": [ "8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")\n", "8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE, \"BE in line 5\") \ncatch(AE,\"AE somewhere\...
665
9
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells. Input The first input line contains two integ...
n, k = map(int, input().split()) s = list(input()) if k > 2: c = set(ord("A") + i for i in range(26)) r = 0 for i in range(1,len(s)): if s[i-1] == s[i]: r += 1 if i + 1 < n: s[i] = "A" if "A" not in {s[i-1], s[i+1]} else ('B' if "B" not in {s[i], s[i+1]} els...
{ "input": [ "3 2\nBBB\n", "6 3\nABBACC\n" ], "output": [ "1\nBAB", "2\nABCACA\n" ] }
666
9
Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: 1. The coordinates of each point in the set are integers. 2. For any two points from the set, the distance between them is a non-integer. Consider all poi...
def solve(n,m): a=min(n+1,m+1) print(a) for i in range(a): print(i,a-1-i) A=list(map(int,input().split())) solve(A[0],A[1])
{ "input": [ "2 2\n", "4 3\n" ], "output": [ "3\n0 2\n1 1\n2 0\n", "4\n0 3\n1 2\n2 1\n3 0\n" ] }
667
9
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes ...
import math from decimal import * getcontext().prec=30 x=input() m=int(input()) ans=[0]*1010 def dfs(curr,bal): if curr>m: return 1 for i in range(bal+1,11): if ans[curr-1]!=i and x[i-1]=='1': ans[curr]=i if dfs(curr+1,i-bal)==1: return 1 return 0 if d...
{ "input": [ "0000000101\n3\n", "1000000000\n2\n" ], "output": [ "YES\n8 10 8\n", "NO\n" ] }
668
9
Recently, the bear started studying data structures and faced the following problem. You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answe...
import math, sys input = sys.stdin.buffer.readline def ints(): return map(int, input().split()) n = int(input()) x = list(ints()) MAX = max(x) + 1 freq = [0] * MAX for i in x: freq[i] += 1 sieve = [False] * MAX f = [0] * MAX for i in range(2, MAX): if sieve[i]: continue for j in range(i, MAX...
{ "input": [ "7\n2 3 5 7 11 4 8\n2\n8 10\n2 123\n", "6\n5 5 7 10 14 15\n3\n2 11\n3 12\n4 4\n" ], "output": [ "0\n7\n", "9\n7\n0\n" ] }
669
8
Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris. There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them bac...
import sys def solve(): n = int(input()) s = 1000000 xset = set(map(int, input().split())) res = set() wantother = 0 for i in range(1, s + 1): opposite = s - i + 1 if i in xset: if opposite not in xset: res.add(opposite) else: ...
{ "input": [ "1\n1\n", "3\n1 4 5\n" ], "output": [ "1\n1000000 ", "3\n1000000 999997 999996 " ] }
670
9
Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also bo...
n, m = map(int, input().split()) arr = list(map(int, input().split())) price = sum(abs(a - b) for a, b in zip(arr, arr[1:])) from collections import defaultdict adj = defaultdict(list) for a,b in zip(arr, arr[1:]): if a != b: adj[a].append(b) adj[b].append(a) def median(xs): return sorted(xs)...
{ "input": [ "10 5\n9 4 3 8 8\n", "4 6\n1 2 3 4 3 2\n" ], "output": [ " 6\n", " 3\n" ] }
671
9
You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color? Your task is to write a program tha...
def get_answer(a, b, c): return min((a+b+c)//3, a+b+c-max(a,b,c)) r, g, b = map(int, input().split()) print(get_answer(r, g, b))
{ "input": [ "1 1 1\n", "5 4 3\n", "2 3 3\n" ], "output": [ "1\n", "4\n", "2\n" ] }
672
11
Celebrating the new year, many people post videos of falling dominoes; Here's a list of them: https://www.youtube.com/results?search_query=New+Years+Dominos User ainta, who lives in a 2D world, is going to post a video as well. There are n dominoes on a 2D Cartesian plane. i-th domino (1 ≤ i ≤ n) can be represented a...
''' from bisect import bisect,bisect_left from collections import * from math import gcd,ceil,sqrt,floor,inf from heapq import * from itertools import * from operator import add,mul,sub,xor,truediv,floordiv from functools import *''' #------------------------------------------------------------------------ import os ...
{ "input": [ "6\n1 5\n3 3\n4 4\n9 2\n10 1\n12 1\n4\n1 2\n2 4\n2 5\n2 6\n" ], "output": [ "0\n1\n1\n2\n" ] }
673
9
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? <image> One day, when he came to his friend Evan, Om Nom didn't find him at h...
def main(): c, hr, hb, wr, wb = map(int, input().split()) ans = 0 for i in range(10**6 + 1): if i * wr <= c: ans = max(ans, i * hr + ((c - i * wr) // wb) * hb) if i * wb <= c: ans = max(ans, i * hb + ((c - i * wb) // wr) * hr) print(ans) main()
{ "input": [ "10 3 5 2 3\n" ], "output": [ "16" ] }
674
10
We all know that GukiZ often plays with arrays. Now he is thinking about this problem: how many arrays a, of length n, with non-negative elements strictly less then 2l meet the following condition: <image>? Here operation <image> means bitwise AND (in Pascal it is equivalent to and, in C/C++/Java/Python it is equival...
n,k,l,m=map(int,input().split()) fc={1:1,2:1} def f(n): if n not in fc: k=n//2;fc[n]=(f(k+1)**2+f(k)**2 if n%2 else f(k)*(2*f(k+1)-f(k)))%m return fc[n] s=k<2**l for i in range(l): s*=pow(2,n,m)-f(n+2) if (k>>i)%2 else f(n+2) print(s%m)
{ "input": [ "2 1 2 10\n", "2 1 1 3\n", "3 3 2 10\n" ], "output": [ "3\n", "1\n", "9\n" ] }
675
9
This is yet another problem dealing with regular bracket sequences. We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not....
def solve(s): l=[] length=ref=0 count=1 pos=-1 for i in range(len(s)): if s[i]=="(": l.append(i) elif l: l.pop() ref=i-l[-1] if l else i-pos if ref>length: length=ref count=1 elif ref==length: count+=1 else: pos=i print(length,count) s=input() solve(s)
{ "input": [ ")((())))(()())\n", "))(\n" ], "output": [ "6 2\n", "0 1\n" ] }
676
7
Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5.... The sequence is built in the following way: at first the number 1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4 and so on. Note that the sequence contains numbers, not d...
def main(): n = int(input()) for i in range(1,10**8): if n-i <= 0: break n -= i print(n) if __name__ == "__main__": main()
{ "input": [ "56\n", "5\n", "55\n", "3\n", "10\n" ], "output": [ "1\n", "2\n", "10\n", "2\n", "4\n" ] }
677
10
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only. Waking up in the ...
def I(): return(list(map(int,input().split()))) n,k=I() a=I() b=I() l=0 # l=104 r=2*(10**9)+1 # r=106 # sumi=sum(a) # summ=sum(b) # print(sumi,summ) while(l<r-1): # print(l,r) m=(l+r)//2 s=0 for i in range(n): s+=max(m*a[i]-b[i],0) # print(s) if s>k: r=m else: l=m print(l)
{ "input": [ "4 3\n4 3 5 6\n11 12 14 20\n", "3 1\n2 1 4\n11 3 16\n" ], "output": [ "3\n", "4\n" ] }
678
9
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior. There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate ...
n, root = map(int, input().split()) a = list(map(int, input().split())) def push(d, x, val): if x not in d: d[x] = 0 d[x]+=val if d[x]==0: del d[x] d = {} for x in a: push(d, x, 1) min_ = 0 root -= 1 inf = 9999999 if a[root] != 0: min_+=1 push(d, a[root], -1) ...
{ "input": [ "5 3\n1 0 0 4 1\n", "3 2\n2 0 2\n" ], "output": [ "2\n", "1\n" ] }
679
8
Running with barriers on the circle track is very popular in the country where Dasha lives, so no wonder that on her way to classes she saw the following situation: The track is the circle with length L, in distinct points of which there are n barriers. Athlete always run the track in counterclockwise direction if you...
k, l = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) def solve(): for i in range(l): if a == sorted((x + i) % l for x in b): return "YES" return "NO" print(solve())
{ "input": [ "4 9\n2 3 5 8\n0 1 3 6\n", "2 4\n1 3\n1 2\n", "3 8\n2 4 6\n1 5 7\n" ], "output": [ "YES\n", "NO\n", "YES\n" ] }
680
10
Input The only line of the input contains a string of digits. The length of the string is between 1 and 10, inclusive. Output Output "Yes" or "No". Examples Input 373 Output Yes Input 121 Output No Input 436 Output Yes
def k(s): n = (8, -1, -1, 3, 6, 9, 4, 7, 0, 5) for i in range(len(s)): if n[int(s[i])] != int(s[len(s) - i - 1]): return False return True s = input() if k(s): print('Yes') else: print("No")
{ "input": [ "436\n", "121\n", "373\n" ], "output": [ "YES", "NO", "YES" ] }
681
9
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends. There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j co...
def find(n): return (n + 1) // 2 - 1 print(find(int(input())))
{ "input": [ "10\n", "2\n" ], "output": [ "4", "0" ] }
682
9
Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is ai meters high. Vladimir has just planted n bamboos in a row, each of...
import itertools unfold = itertools.chain.from_iterable def jumps(a): d = speedup while d < a - 1: c = (a + d - 1) // d d = (a + c - 2) // (c - 1) yield d def calc(d): return sum(d - 1 - (i - 1) % d for i in a) def ans(): for d, pd in zip(D, D[1:]): d -= 1 cd...
{ "input": [ "3 40\n10 30 50\n", "3 4\n1 3 5\n" ], "output": [ "32\n", "3\n" ] }
683
10
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds: * divide the number x by 3 (x must be divisible by 3); * multiply the number x by 2. After each operation, Polycarp writes down the result on the boar...
def f(x): i=0; while x%3==0: x//=3 i-=1 while x%2==0: x//=2 i+=1 return i input() a=list(map(int,input().split())) a= sorted(a,key=f) print(*a)
{ "input": [ "4\n42 28 84 126\n", "2\n1000000000000000000 3000000000000000000\n", "6\n4 8 6 3 12 9\n" ], "output": [ "126 42 84 28 \n", "3000000000000000000 1000000000000000000 \n", "9 3 6 12 4 8 \n" ] }
684
8
Let's introduce a number system which is based on a roman digits. There are digits I, V, X, L which correspond to the numbers 1, 5, 10 and 50 respectively. The use of other roman digits is not allowed. Numbers in this system are written as a sequence of one or more digits. We define the value of the sequence simply as...
def f(n): return (n+3)*(n+2)*(n+1)//6; n=int(input()); ans=f(n); if n>=6:ans-=f(n-6); if n>=9:ans-=f(n-9)*2; if n>=10:ans+=f(n-10); if n>=14:ans+=f(n-14); print(ans);
{ "input": [ "2\n", "10\n", "1\n" ], "output": [ "10", "244", "4" ] }
685
10
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from 1 to n, the junction 1 is called the root. A subtree of a junction v is a set of junc...
n = int(input()) tr = {} p = [int(s) for s in input().split()] for i in range(n-1): if not tr.get(p[i]-1): tr[p[i]-1] = [] tr[p[i]-1].append(i+1) # print(tr) lc = [-1 for i in range(n)] def get_lc(i): if lc[i] == -1: if tr.get(i): lc[i] = 0 for j in tr[i]: ...
{ "input": [ "5\n1 1 3 3\n", "3\n1 1\n" ], "output": [ "1 1 1 2 3 \n", "1 1 2 \n" ] }
686
11
Everyone knows that computers become faster and faster. Recently Berland scientists have built a machine that can move itself back in time! More specifically, it works as follows. It has an infinite grid and a robot which stands on one of the cells. Each cell of the grid can either be empty or contain 0 or 1. The mach...
def C(x, y): return x + '10' + y + 't' + y def CBF(x, y): return x + '01' + y + 't' + y Cr = C('r', 'l') Cl = C('l', 'r') Cu = C('u', 'd') Cd = C('d', 'u') CBFr = CBF('r', 'l') CBFl = CBF('l', 'r') CBFu = CBF('u', 'd') CBFd = CBF('d', 'u') def CE(x, y): return x+x+'0'+x+'1'+y+y+'10'+y+'t'+x+...
{ "input": [ "2\n123456789 987654321\n555555555 555555555\n" ], "output": [ "ds10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10utsusdtedslss10...
687
7
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent. Alice initially has a token on some cell on the line, and Bob tries to guess where it is. Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order....
def ck(p,q): return 1 if bpos[p]>fpos[q] else 0 n,k=map(int,input().split()) x=list(map(int,input().split())) fpos=[-1 for i in range(100001)] bpos=[k+5 for i in range(100001)] for i in range(k): fpos[x[i]]=i bpos[x[k-1-i]]=k-1-i ans=ck(n,n) for i in range(1,n): ans+=ck(i,i)+ck(i,i+1)+ck(i+1,i) print(an...
{ "input": [ "5 3\n5 1 4\n", "100000 1\n42\n", "4 8\n1 2 3 4 4 3 2 1\n" ], "output": [ "9\n", "299997\n", "0\n" ] }
688
8
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find th...
s=input() def pri(l,r): k=1 while r-2*k>=l: if s[r]!=s[r-k] or s[r-k]!=s[r-2*k]: k+=1 continue return False return True ans=0 for i in range(len(s)): j=i while j<len(s) and pri(i,j): j+=1 ans+=len(s)-j print(ans)
{ "input": [ "010101\n", "11001100\n" ], "output": [ "3\n", "0\n" ] }
689
11
Vus the Cossack has a field with dimensions n × m, which consists of "0" and "1". He is building an infinite field from this field. He is doing this in this way: 1. He takes the current field and finds a new inverted field. In other words, the new field will contain "1" only there, where "0" was in the current field...
from sys import stdin,stdout n,m,q = map(int, stdin.readline().split()) mat = [[0]*m for i in range(n)] for i in range(n): row = stdin.readline().strip() for j,c in enumerate(row): mat[i][j] = 1 if c == '1' else -1 #print(mat) def get(a,b): if a < 0 or b < 0: return 0 x = a^b ans...
{ "input": [ "2 3 7\n100\n101\n4 12 5 17\n5 4 9 4\n1 4 13 18\n12 1 14 9\n3 10 7 18\n3 15 12 17\n8 6 8 12\n", "2 2 5\n10\n11\n1 1 8 8\n2 4 5 6\n1 2 7 8\n3 3 6 8\n5 6 7 8\n" ], "output": [ "6\n3\n98\n13\n22\n15\n3\n", "32\n5\n25\n14\n4\n" ] }
690
10
You are given a tree with n nodes. You have to write non-negative integers on its edges so that the following condition would be satisfied: For every two nodes i, j, look at the path between them and count the sum of numbers on the edges of this path. Write all obtained sums on the blackboard. Then every integer from ...
import math n = int(input()) if n == 1: print() else: edge = [list(map(int, input().split())) for i in range(1, n) ] g = {} for x, y in edge: if x not in g: g[x] = [] if y not in g: g[y] = [] g[x].append(y) g[y].append(x) ...
{ "input": [ "3\n2 3\n2 1\n", "4\n2 4\n2 3\n2 1\n", "5\n1 2\n1 3\n1 4\n2 5\n" ], "output": [ "2 3 1\n2 1 2\n", "2 4 1\n2 3 2\n2 1 4\n", "1 2 1\n2 5 1\n1 3 3\n1 4 6\n" ] }
691
7
Let's denote correct match equation (we will denote it as CME) an equation a + b = c there all integers a, b and c are greater than zero. For example, equations 2 + 2 = 4 (||+||=||||) and 1 + 2 = 3 (|+||=|||) are CME but equations 1 + 2 = 4 (|+||=||||), 2 + 2 = 3 (||+||=|||), and 0 + 1 = 1 (+|=|) are not. Now, you ha...
def f(n): if n == 2: return 2 return n%2 for i in range(int(input())): print(f(int(input())))
{ "input": [ "4\n2\n5\n8\n11\n" ], "output": [ "2\n1\n0\n1\n" ] }
692
8
Balph is learning to play a game called Buma. In this game, he is given a row of colored balls. He has to choose the color of one new ball and the place to insert it (between two balls, or to the left of all the balls, or to the right of all the balls). When the ball is inserted the following happens repeatedly: if so...
def run(): s = input() i = 0 j = len(s)-1 count = 0 while i<=j: if s[i] != s[j]: return 0 c = s[i] count = 0 while i < len(s) and s[i] == c and i<=j: i += 1 count += 1 while j >= 0 and s[j] == c and i<=j: j -= 1 ...
{ "input": [ "OOOWWW\n", "BBWBB\n", "BBWWBB\n", "BWWB\n", "WWWOOOOOOWWW\n" ], "output": [ "0\n", "0\n", "3\n", "0\n", "7\n" ] }
693
10
You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ...
import math as mt import sys input=sys.stdin.readline I=lambda:list(map(int,input().split())) n,m=I() a=[I() for i in range(n)] ans=[] lo=0 hi=10**9 def vanguda(mid: int) -> bool: global ans f={} for i in range(n): bi=0 for j in range(m): if a[i][j]>=mid: bi+=1 ...
{ "input": [ "6 5\n5 0 3 1 2\n1 8 9 1 3\n1 2 3 4 5\n9 1 0 3 7\n2 3 0 6 3\n6 4 1 7 0\n" ], "output": [ "1 5\n" ] }
694
8
You are given two positive integers n (1 ≤ n ≤ 10^9) and k (1 ≤ k ≤ 100). Represent the number n as the sum of k positive integers of the same parity (have the same remainder when divided by 2). In other words, find a_1, a_2, …, a_k such that all a_i>0, n = a_1 + a_2 + … + a_k and either all a_i are even or all a_i ar...
def main(n,k): if (n - 2*(k-1)) %2==0 and n-2*(k-1)>0: print("YES") print( (str(2)+" ")*(k-1) + str(n- 2*(k-1)) ) return if (n - k + 1)%2==1 and n- k+1>0: print("YES") print( "1 "*(k-1) + str(n-k+1) ) return print("NO") t = int(input()) for i in range(t): n,k = [int(i) for i in input()...
{ "input": [ "8\n10 3\n100 4\n8 7\n97 2\n8 8\n3 10\n5 3\n1000000000 9\n" ], "output": [ "yes\n2 2 6\nyes\n1 1 1 97\nno\nno\nyes\n1 1 1 1 1 1 1 1\nno\nyes\n1 1 3\nyes\n2 2 2 2 2 2 2 2 999999984\n" ] }
695
8
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase ha...
def func(n): return (n*(n+1))//2 for _ in range(int(input())): x=int(input()) s=0 i=1 while x-func(i)>=0: x-=func(i) i=2*i+1 s+=1 print(s)
{ "input": [ "4\n1\n8\n6\n1000000000000000000\n" ], "output": [ "1\n2\n1\n30\n" ] }
696
8
You are given a string s of even length n. String s is binary, in other words, consists only of 0's and 1's. String s has exactly n/2 zeroes and n/2 ones (n is even). In one operation you can reverse any substring of s. A substring of a string is a contiguous subsequence of that string. What is the minimum number of...
def solve(): n=int(input()) s=input() answer=(n-s.count('10')-s.count('01'))//2 print(answer) t=int(input()) while t: solve() t-=1
{ "input": [ "3\n2\n10\n4\n0110\n8\n11101000\n" ], "output": [ "0\n1\n2\n" ] }
697
12
Barbara was late for her math class so as a punishment the teacher made her solve the task on a sheet of paper. Barbara looked at the sheet of paper and only saw n numbers a_1, a_2, …, a_n without any mathematical symbols. The teacher explained to Barbara that she has to place the available symbols between the numbers ...
#!/usr/bin/env python3 from itertools import accumulate, groupby from functools import reduce def prod(a): return reduce(lambda x,y: min(x*y,10**6),a,1) def solve_positive(a): if a == '': return '+' b = [''.join(v) for _,v in groupby(a, key=lambda x: x == '1')] if b[0][0] == '1': return '+' ...
{ "input": [ "4\n2 1 1 2\n+*\n", "3\n2 2 0\n+-*\n" ], "output": [ "\n2+1+1+2\n", "\n2*2-0\n" ] }
698
7
n heroes fight against each other in the Arena. Initially, the i-th hero has level a_i. Each minute, a fight between two different heroes occurs. These heroes can be chosen arbitrarily (it's even possible that it is the same two heroes that were fighting during the last minute). When two heroes of equal levels fight,...
def read_ints(): return map(int, input().split()) t_n, = read_ints() for i_t in range(t_n): a_n, = read_ints() a_seq = tuple(read_ints()) result = len(a_seq) - a_seq.count(min(a_seq)) print(result)
{ "input": [ "3\n3\n3 2 2\n2\n5 5\n4\n1 3 3 7\n" ], "output": [ "\n1\n0\n3\n" ] }
699
9
You are given an array a of n integers. Find the number of pairs (i, j) (1 ≤ i < j ≤ n) where the sum of a_i + a_j is greater than or equal to l and less than or equal to r (that is, l ≤ a_i + a_j ≤ r). For example, if n = 3, a = [5, 1, 2], l = 4 and r = 7, then two pairs are suitable: * i=1 and j=2 (4 ≤ 5 + 1 ≤ 7...
from bisect import bisect_right def solve(v,x): pcnt=0 for i,e in enumerate(v): j=bisect_right(v,x-e) if i<j: pcnt+=j-i-1 return pcnt for _ in range(int(input())): n,l,r=map(int,input().split()) v=list(map(int,input().split())) v=sorted(v) print(solve(v,r)-solve...
{ "input": [ "4\n3 4 7\n5 1 2\n5 5 8\n5 1 2 4 3\n4 100 1000\n1 1 1 1\n5 9 13\n2 5 5 1 1\n" ], "output": [ "\n2\n7\n0\n1\n" ] }