source
stringclasses
3 values
instruction
stringlengths
23
3.97k
input
stringclasses
1 value
output
stringlengths
1
3.75k
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: There are $N$ cars (numbered $1$ through $N$) on a circular track with length $N$. For each $i$ ($2 \le i \le N$), the $i$-th of them is at a distance $i-1$ clockwise from car $1$, i.e. car $1$ needs to travel a distance $i-1$ clockwise to reach...
```python for _ in range(int(input())): n=int(input()) f=list(map(int,input().split())) sum1=f[0] d=0 i=1 while sum1!=0 and i<n: sum1=sum1-1+f[i] d+=1 i+=1 print(d+sum1) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef belongs to a very rich family which owns many gold mines. Today, he brought N gold coins and decided to form a triangle using these coins. Isn't it strange? Chef has a unusual way of forming a triangle using gold coins, which is described a...
```python t = eval(input()) def moneda(m): h = 1 triange = [] while m >= h: triange.append(h) m -= h h += 1 return len(triange) triangulo = [] for i in range(t): n = eval(input()) triangulo.append(n) for i in triangulo: print(moneda(i)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: There is Chef and Chef’s Crush who are playing a game of numbers. Chef’s crush has a number $A$ and Chef has a number $B$. Now, Chef wants Chef’s crush to win the game always, since she is his crush. The game ends when the greatest value of A^...
```python def main(): t = int(input()) while (t): m, n = map(int, input().split()) a , b= bin(m)[2:],bin(n)[2:] #print(a,b) max = m^n if len(a)>len(b): diff =len(a)-len(b) b= ("0"*diff)+b #print(b) elif len(a)<len(b): diff =len(b)-len(a) a= ("0"*diff)+a #print(a) ll = len(b) count= ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Mandarin chinese , Russian and Vietnamese as well. Chef is organising a contest with $P$ problems (numbered $1$ through $P$). Each problem has $S$ subtasks (numbered $1$ through $S$). The difficulty of a problem can be calculated as follows: - L...
```python p,s = [int(i) for i in input().split()] scores = {} for j in range(1, p + 1): sc = [int(i) for i in input().split()] ns = [int(i) for i in input().split()] nsc = dict(list(zip(sc,ns))) ssc = sorted(sc) score = 0 for a,b in zip(ssc[:-1], ssc[1:]): if nsc[a] > nsc[b]: score += 1 if score in list(sc...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a rectangle in his backyard. He has set up coordinate axes, and he wants the sides of the rectangle to be parallel to them. Of course, the area of ...
```python n = int(input()) points = [[int(x) for x in input().split()] for _ in range(n)] if n <= 1: print(-1) return dx = [1e9, -1e9] dy = [1e9, -1e9] for x, y in points: dx[0] = min(dx[0], x) dx[1] = max(dx[1], x) dy[0] = min(dy[0], y) dy[1] = max(dy[1], y) area = (dx[1] - dx[0]) * (dy[1] - dy[0]) if area: pri...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Yesterday, Chef found $K$ empty boxes in the cooler and decided to fill them with apples. He ordered $N$ apples, where $N$ is a multiple of $K$. Now, he just needs to hire someone who will distribute the apples into the boxes with professional p...
```python # cook your dish here t=int(input()) for i in range(t,0,-1): x,y=map(int,input().split()) k=x//y if k%y==0: print("NO") else: print("YES") ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Consider the following operations on a triple of integers. In one operation, you should: - Choose an integer $d$ and an arithmetic operation ― either addition or multiplication. - Choose a subset of elements of the triple. - Apply the arithmetic...
```python def eq_solve(v0, v1, u0, u1): den = u0 - v0 num = u1 - v1 if den != 0: return num / den return 1 def solve(p, q, r, a, b, c, rs): if p == a and q == b and r == c: return rs if rs >= 2: return 3 res = 3 adds = [a - p, b - q, c - r] mul...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef has a sequence of $N$ integers, $A_1, A_2, ... , A_N$. He likes this sequence if it contains a subsequence of $M$ integers, $B_1, B_2, ... , B_M$ within it. A subsequence is a sequence that can be derived from another sequence by deleting ...
```python t=int(input()) i=0 while i<t: n=int(input()) A=[] A=input().split() m=int(input()) B=[] B=input().split() j=0 a=-1 while j<m: c=1 if B[j] in A: b=A.index(B[j]) A.remove(B[j]) if b>=a: a=b c=1 else: c=0 break else: c=0 break j+=1 if c==1: print("Yes") else...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given a string s consisting of |s| small english letters. In one move you can replace any character of this string to the next character in alphabetical order (a will be replaced with b, s will be replaced with t, etc.). You cannot repl...
```python s = list(input()) target = 'abcdefghijklmnopqrstuvwxyz' ind_t = 0 ind_s = 0 while ind_s < len(s) and ind_t < 26: if ord(s[ind_s]) <= ord(target[ind_t]): s[ind_s] = target[ind_t] ind_t += 1 ind_s += 1 else: ind_s += 1 if ind_t == 26: print(''.join(s)) else: print(-1) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: "Ring Ring!!" Sherlock's phone suddenly started ringing. And it was none other than Jim Moriarty.. "Long time no see ! You miss me right ? Anyway we'll talk about it later . Let me first tell you something. Dr.Watson is with me . And you've go...
```python import sys user_input = sys.stdin.readline().split() T = int(user_input[0]) for j in range(T) : var = sys.stdin.readline().split() N = int(var[0]) M = int(var[1]) if (N%M)%2 : print("ODD") else : print("EVEN") ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Consider a tree $T$ (that is, a connected graph without cycles) with $n$ vertices labelled $1$ through $n$. We start the following process with $T$: while $T$ has more than one vertex, do the following: choose a random edge of $T$ equiprobab...
```python maxn=50+10 g=[None]*maxn dp=[None]*maxn c=[None]*maxn size=[0]*maxn for i in range(0,maxn): c[i]=[0]*maxn c[i][0]=1 for j in range(1,i+1): c[i][j]=c[i-1][j-1]+c[i-1][j] n=int(input()) for i in range(1,n+1): g[i]=[] for i in range(1,n): u,v=input().split() u=int(u) v=int(v...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Given an array of size N$N$ and two integers K$K$ and S$S$, the special sum of a subarray is defined as follows: (Sum of all elements of the subarray) * (K$K$ - p$p$ * S$S$) Where p$p$ = number of distinct prime factors of “product of all elem...
```python # cook your dish here from math import floor, sqrt try:long except NameError:long = int def fac(n): step,maxq,d = lambda x: 1 + (x<<2) - ((x>>1)<<1),long(floor(sqrt(n))),1 q = n % 2 == 0 and 2 or 3 while q <= maxq and n % q != 0: q = step(d) d += 1 return q <= maxq and [q] + fac(n/...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You've got a list of program warning logs. Each record of a log stream is a string in this format: "2012-MM-DD HH:MM:SS:MESSAGE" (without the quotes). String "MESSAGE" consists of spaces, uppercase and lowercase English letters and characters...
```python # import atexit # import io # import sys # # _INPUT_LINES = sys.stdin.read().splitlines() # input = iter(_INPUT_LINES).__next__ # _OUTPUT_BUFFER = io.StringIO() # sys.stdout = _OUTPUT_BUFFER # # # @atexit.register # def write(): # sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) import bisect from datetim...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number. Petya can ask questions like: "Is the unknown number divisible by number y?". The game is played by the following rules:...
```python def main(): n = int(input()) result = [] for i in range(2, n + 1): j = 2 while j * j <= i: if i % j == 0: break j += 1 else: j = i while j <= n: result.append(j) j *= i ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef is again playing a game with his best friend Garry. As usual, the rules of this game are extremely strange and uncommon. First, they are given a stack of $N$ discs. Each disc has a distinct, non-negative integer written on it. The players e...
```python for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a = [-1] + a[::-1] mx = a.index(max(a)) dp = [0] * (n + 1) for i in range(1, n + 1): for x in b: if i - x < 0: continue if i - x < mx <= i: dp[i] = 1 el...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef found a strange string yesterday - a string of signs s, where each sign is either a '<', '=' or a '>'. Let N be the length of this string. Chef wants to insert N + 1 positive integers into this sequence and make it valid. A valid sequence i...
```python for _ in range(int(input())): st=input().replace("=","") if not len(st):print(1) else: cu=mx=1 for j in range(1,len(st)): if st[j]==st[j-1]:cu+=1 else:mx=max(mx,cu);cu=1 print(max(mx+1,cu+1)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: In a country called Chef Land, there was a lot of monetary fraud, so Chefu, the head of the country, decided to choose new denominations of the local currency ― all even-valued coins up to an integer $N$ should exist. After a few days, a citizen...
```python for i in range(int(input())): n,k=list(map(int,input().split())) t=0 if n%2!=0: n-=1 t+=1 t+=(n//k) if n%k!=0: t+=1 print(t) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Let $a$ and $b$ be two arrays of lengths $n$ and $m$, respectively, with no elements in common. We can define a new array $\mathrm{merge}(a,b)$ of length $n+m$ recursively as follows: If one of the arrays is empty, the result is the other array...
```python t = int(input()) for _ in range(t): n = int(input()) l = [int(x) for x in input().split()] cur = l[0] cll = 1 blocks = [] for x in l[1:]: if x > cur: blocks.append(cll) cur = x cll = 1 else: cll += 1 blocks.append(cll...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: -----Problem Statement----- A classroom has several students, half of whom are boys and half of whom are girls. You need to arrange all of them in a line for the morning assembly such that the following conditions are satisfied: - The students m...
```python for u in range(int(input())): n=int(input()) l=list(map(int,input().split())) d=list(map(int,input().split())) ka=[] k=[] l.sort() d.sort() for i in range(n): ka.append(d[i]) ka.append(l[i]) for i in range(n): k.append(l[i]) k.append(d[i]) ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Give me Biscuit Sunny wants to make slices of biscuit of size c * d into identical pieces. but each piece is a square having maximum possible side length with no left over piece of biscuit. Input Format The first line contains an integer N. ...
```python def __gcd(a, b): # Everything divides 0 if (a == 0 or b == 0): return 0; # base case if (a == b): return a; # a is greater if (a > b): return __gcd(a - b, b); return __gcd(a, b - a); # Function to find # number of squares def NumberOfSquares(x, y): # Here in built PHP...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: A sophomore Computer Science student is frustrated with boring college lectures. Professor X agreed to give him some questions; if the student answers all questions correctly, then minimum attendance criteria will not apply to him. Professor X c...
```python # cook your dish here from sys import stdin, stdout for _ in range(int(stdin.readline())): n, q = list(map(int, stdin.readline().split())) arr = list(map(int, stdin.readline().split()))[:n] od = ev = 0 for i in arr: if bin(i).count('1')%2==0: ev += 1 else: od += 1 for _ in range(q): p = int(...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Mohit(Ex GenSec ) is the most active member of the roasting club who loves giving tasks to other members. One day he observed that none of the members were paying attention to the online classes, so he decided to have some fun and overcome the b...
```python n, m = map(int, input().split()) l = n f = 1 s = ((n)*(n+1))//2 - l - f for _ in range(m): k = int(input()) if 2 <= k <= n-1 or k in [f, l]: l, f = f, l else: l = k print(s+l+f) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Our chef has recently opened a new restaurant with a unique style. The restaurant is divided into K compartments (numbered from 1 to K) and each compartment can be occupied by at most one customer. Each customer that visits the restaurant has a...
```python try: for _ in range(int(input())): N, K = map(int, input().split()) cell = [] count = 0 l = [] for __ in range(N): inserted = list(map(int, input().split())) cell.append(inserted) cell.sort(key=lambda x: x[1]) time = {} for number in cell: if number[2] not in time: tim...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Rick and Morty are playing their own version of Berzerk (which has nothing in common with the famous Berzerk game). This game needs a huge space, so they play it with a computer. In this game there are n objects numbered from 1 to n arranged in...
```python f = lambda: list(map(int, input().split()))[1:] n = int(input()) s, p, q = [], [], [] for x in [0, 1]: r = f() s.append(r) t = [len(r)] * n t[0] = 0 p.append(t) q.append((x, 0)) while q: x, i = q.pop() y = 1 - x for d in s[y]: j = (i - d) % n if p[y][j] < 1:...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Dante is engaged in a fight with "The Savior". Before he can fight it with his sword, he needs to break its shields. He has two guns, Ebony and Ivory, each of them is able to perform any non-negative number of shots. For every bullet that hits ...
```python a, b, c = list(map(int, input().split())) p = [0] * 100000 p[0] = 1 p[a] = 1 p[b] = 1 for i in range(c + 1): if p[i]: p[i + a] = 1 p[i + b] = 1 if p[c]: print('Yes') else: print('No') ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given an array of n integer numbers a_0, a_1, ..., a_{n} - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times. -----Input----- The first line conta...
```python n = int(input()) A = [int(x) for x in input().split()] mn = min(A) I = [i for i in range(len(A)) if A[i] == mn] mindiff = min(I[i]-I[i-1] for i in range(1,len(I))) print(mindiff) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef is a really nice and respectful person, in sharp contrast to his little brother, who is a very nasty and disrespectful person. Chef always sends messages to his friends in all small letters, whereas the little brother sends messages in all ...
```python t=int(input()) def do(): n,k=map(int,input().split()) s=input() upper=0 lower=0 for i in s: if i.isupper(): upper+=1 else: lower+=1 if lower>k and upper<=k: print('chef') elif(upper>k and lower<=k): print('brother') elif(upper<=k and lower<=k): print('both') else: print('none') ret...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given a sequence $a_1, a_2, \dots, a_n$, consisting of integers. You can apply the following operation to this sequence: choose some integer $x$ and move all elements equal to $x$ either to the beginning, or to the end of $a$. Note that...
```python def main(): from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) inp1 = [-1] * (n + 1) inp2 = [-1] * (n + 1) for i, ai in enumerate(map(int, stdin.readline().split())): if inp1[ai] < 0: inp1[ai] = i ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: The garden has a tree with too many leaves on it and gardner wants to cut the unwanted leaves. This is a rooted tree, where a node $v$ is called parent of another node $u$, if there exists a directed edge from $v$ to $u$. Leaf node is a node wit...
```python def dfs(node): nonlocal adj,leaf val=0 flag=0 for i in adj[node]: x= dfs(i) val+=x if x==0: flag=1 leaf+=val-val%3 if val%3==0 and flag==0: return 1 else: return 0 for _ in range(int(input())): n=int(input()) adj=[[] for i in range(n+2)] arr=[int(i) for i in input().split()] leaf=0 #p...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: The flag of Berland is such rectangular field n × m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each o...
```python n,m=list(map(int,input().split())) f=[input() for _ in range(n)] def clr(ss): cc = None for s in ss: for c in s: if cc is None: cc = c elif cc != c: return None return cc if n%3 == 0: s = set() for i in range(0,n,n//3): ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: A beautiful sequence is defined as a sequence that do not have any repeating elements in it. You will be given any random sequence of integers, and you have to tell whether it is a beautiful sequence or not. -----Input:----- - The first line o...
```python # cook your dish here for _ in range(int(input())): n = int(input()) arr = list(map(int,input().split())) l = [] for i in range(0, len(arr)): for j in range(i+1, len(arr)): if(arr[i] == arr[j]): l.append(arr[j]) if (len(l) ==0): print("...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the ar...
```python t = int(input()) for case in range(t): n = int(input()) arr = list(map(int, input().split())) if arr[-1] > arr[0]: print("YES") else: print("NO") ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Today is Chef's birthday. His mom has surprised him with truly fruity gifts: 2 fruit baskets. The first basket contains N apples, and the second one contains M oranges. Chef likes apples and oranges very much but he likes them equally, and there...
```python for _ in range(int(input())): a,o,g=map(int,input().split()) while g>0: if a<o: a+=1 g-=1 elif o<a: o+=1 g-=1 else: break print(abs(a-o)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Rodriguez is a happy and content farmer. He has got a square field of side length $x$. Miguel, his son has himself grown into a man and his father wants to gift him something out of which he can make a living. So he gift's his son a square piece...
```python from math import sqrt def isPrime(n): for i in range(2, int(sqrt(n))+1): if(n%i==0): return True return False ans = [] for _ in range(int(input())): x, y = map(int, input().split()) ans.append('NO' if(isPrime(x**2-y**2)) else 'YES') print('\n'.join(ans)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Akshay is interested in mathematics, one day he came across a problem of modulus operator.He has a list of M integers say arr[M] and has to find all integers K such that : - K > 1 - arr[1]%K = arr[2]%K = arr[3]%K = … = arr[M]%K where '%' is a m...
```python l = [] for _ in range(int(input())): l.append(int(input())) for i in range(2,max(l)): r = [x%i for x in l] if len(set([x%i for x in l])) == 1: print(i, end = ' ') ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Polycarp has created his own training plan to prepare for the programming contests. He will train for $n$ days, all days are numbered from $1$ to $n$, beginning from the first. On the $i$-th day Polycarp will necessarily solve $a_i$ problems. O...
```python def main(): n = int(input()) a = list(int(x) for x in input().split()) s = sum(a) t = 0 for i in range(n): t += a[i] if 2 * t >= s: print(i + 1) return main() ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity. Your favorite store sells lemonade in bottles of n different volumes at differe...
```python 3 # Copyright (C) 2017 Sayutin Dmitry. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; version 3 # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: N Soldiers are lined up for a memory test. They are numbered from 0 to N-1 from left to right. In the test, there are M rounds. In each round, Captain selects one position. Soldier at that position will be numbered 0. All the soldiers to the r...
```python test = int(input()) for _ in range(test): n, m = map(int, input().split()) indexArray = list(map(int, input().split())) mini = min(indexArray) maxi = max(indexArray) result = n*[0] for i in range(n): result[i] = max(maxi - i, i - mini) print(result[i], end=" ") print() ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: -----Problem description----- As a holiday gift, Tojo received a probability problem. The problem read as follows Consider an N by M grid. Rows are numbered 1 to N, from top to bottom. Columns are numbered 1 to M, from left to right. You are ini...
```python from math import factorial for _ in range(int(input())): N,M=[int(a) for a in input().split()] print(float(N+M-1)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chefland is a grid with N$N$ rows and M$M$ columns. Each cell of this grid is either empty or contains a house. The distance between a pair of houses is the Manhattan distance between the cells containing them. For each d$d$ between 1$1$ and N+M...
```python # cook your dish here for a in range(int(input())): N,M=map(int,input().split()) b=[] for o in range(N): b.append(input()) c=[] for d in b: f=[] for e in range(len(d)): if d[e]=='1': f.append(e) c.append(f) i=[] for g in range(len(c)): for h in...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: For an array $b$ of length $m$ we define the function $f$ as $ f(b) = \begin{cases} b[1] & \quad \text{if } m = 1 \\ f(b[1] \oplus b[2],b[2] \oplus b[3],\dots,b[m-1] \oplus b[m]) & \quad \text{otherwise,} \end{cases} $ where $\oplus$ is bitwi...
```python n = int(input()) *a, = map(int, input().split()) dp = [[0 for i in range(n + 1)] for j in range(n + 1)] for i in range(n): dp[0][i] = a[i] for i in range(1, n): for j in range(n - i + 1): dp[i][j] = dp[i - 1][j] ^ dp[i - 1][j + 1] for i in range(1, n): for j in range(n - i): dp[i][...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chang's new maths teacher is very enthusiastic about making sure that students understand the concept rather than remembering it. On her first day at teaching, she gives an assignment to all the students to test their potential. Chang wants to d...
```python from math import sqrt a, b = map(int, input().split()) pairs = 0 for i in range(1, a+1): for j in range(1, b+1): root = sqrt(i**2 + j) if not root - int(root): pairs += 1 print(pairs) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: A plot of land can be described by $M x N$ dots such that horizontal and vertical distance between any two dots is 10m. Mr. Wolf would like to build a house in the land such that all four sides of the house are equal. Help Mr. Wolf to find the t...
```python # cook your dish here from sys import stdin,stdout,setrecursionlimit from math import ceil mod = 1000000007 t = int(stdin.readline()) for _ in range(t): m,n = list(map(int,input().split())) if m < n: m,n = n,m y = n-1 s1 = ((y*(y+1)) //2)%mod s2 = ((y*(y+1)*(2*y+1)) //6)%mod ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants his dad to give him a ...
```python def main(): T = int(input()) for t in range(T): N,K = map(int, input().split()) W = list(map(int, input().split())) W.sort() if 2*K > N: K = N - K kid = sum(W[:K]) dad = sum(W[K:]) diff = dad - kid print(diff) def __starting_point(): main() __starting_point() ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: $Gogi$, $Tapu$ and $Sonu$ are the elite members of $Tapu$ $Sena$. $Gogi$ is always stoned and asks absurd questions, But this time he asked a question which seems to be very serious and interesting. $Tapu$ wants to solve this question to impress...
```python # cook your dish here try: t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) gcd = max(a[0], a[-1]) print(gcd) except EOFError:pass ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given an array A, consisting of N integers and an array B, consisting of M integers. The subsequence of A is the array that can be obtained by picking the elements at the arbitrary sorted set of positions from A. Your task is to count th...
```python mod =(10**9)+7 n,m = list(map(int,input().split())) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] dp = [] for i in range(n): dp += [[0]*m] dp[-1][-1]=1 for i in range(n-2,-1,-1): dp[i][-1]=1 for j in range(m-1): x = (a[i]+b[j])-(b[j+1]) temp = 0 ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given two binary strings $a$ and $b$ of the same length. You can perform the following two operations on the string $a$: Swap any two bits at indices $i$ and $j$ respectively ($1 \le i, j \le n$), the cost of this operation is $|i - j...
```python # import collections, atexit, math, sys, bisect sys.setrecursionlimit(1000000) def getIntList(): return list(map(int, input().split())) try : #raise ModuleNotFoundError import numpy def dprint(*args, **kwargs): print(*args, **kwargs, file=sys.stderr) dprint('debug mode...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Shivam is the youngest programmer in the world, he is just 12 years old. Shivam is learning programming and today he is writing his first program. Program is very simple, Given two integers A and B, write a program to add these two numbers. -...
```python #Note that it's python3 Code. Here, we are using input() instead of raw_input(). #You can check on your local machine the version of python by typing "python --version" in the terminal. #Read the number of test cases. T = int(input()) for tc in range(T): # Read integers a and b. (a, b) = list(map(int, inpu...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Sergey Semyonovich is a mayor of a county city N and he used to spend his days and nights in thoughts of further improvements of Nkers' lives. Unfortunately for him, anything and everything has been done already, and there are no more possible i...
```python def main(): def countchildren(graph,vert,memo,pard=None): dumi=0 for child in graph[vert]: if child!=pard: if len(graph[child])==1: memo[child]=0 else: memo[child]=countchildren(graph,child,memo,vert)[0] ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way... The string $s$ he found is a binary string of length $n$ (i. e. string consists only of 0...
```python for _ in range(int(input())): # a, b = map(int, input().split()) n = int(input()) # arr = list(map(int, input().split())) s = input() l = 0 r = n - 1 if s.count('0') == n: print(s) continue if s.count('1') == n: print(s) continue while s[l] =...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef has three baskets and two of them have multiple balls(Natural numbers written on them). The first basket has N balls, the second basket has M balls and the third basket is empty. Chef starts choosing all the unique balls(only occurring once...
```python t = int(input()) for i in range(t): n, m = map(int, input().split()) arr1 = list(map(int, input().split())) arr2 = list(map(int, input().split())) hmap = {} for i in range(n): if arr1[i] in hmap: hmap[arr1[i]] += 1 else: hmap[arr1[i]] = 1 fo...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: A printer – who still uses moveable type – is preparing to print a set of pages for a book. These pages are to be numbered, as usual. The printer needs to know how many instances of each decimal digit will be required to set up the page numbers ...
```python line = input() test = 0 while line != "0": test += 1 d = {'0':0,'1':0,'2':0,'3':0,'4':0,'5':0,'6':0,'7':0,'8':0,'9':0} a = list(map(int,line.split())) for i in range(min(a),max(a)+1): for c in str(i): d[c] += 1 pairs = list(d.items()) pairs.sort() print...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: The bustling town of Siruseri has just one sports stadium. There are a number of schools, colleges, sports associations, etc. that use this stadium as the venue for their sports events. Anyone interested in using the stadium has to apply to the ...
```python # cook your dish here n=(int(input())) x=[] for _ in range(n): a,b=map(int,input().split()) a=[a,a+b] x.append(a) x = sorted(x, key= lambda i:i[1]) y=-1 c=0 for i in range(len(x)): if x[i][0]>y: c+=1 y=x[i][1] print(c) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." Let's call a string balanced if all characters that occur in this string occur in it the same ...
```python from sys import stdin from collections import Counter def func(arr,n,l): count=0 k=l//n if n<len(arr): for ele in arr[0:n]: count+=max(0,k-ele) else: for ele in arr: count+=max(0,ele-k) return count for _ in range(int(stdin.readline())): s=stdin.readline().strip() d=Counter(s) arr=sorted(...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Indian National Olympiad in Informatics 2015 In this problem you are given two lists of N integers, a1, a2, ..., aN and b1, b2, ... bN. For any pair (i, j) with i, j ϵ {1, 2, ..., N} we define the segment from i to j, written as [i, j], to be i,...
```python #dt = {} for i in x: dt[i] = dt.get(i,0)+1 import sys;input = sys.stdin.readline #import io,os; input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline #for pypy inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()] from collections import deque def getmax(x,n,k): mx = [] ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: VK gave a problem to Chef, but Chef is too lazy, so he asked you to solve the problem for him. The statement of the problem follows. Consider an integer with $N$ digits (in decimal notation, without leading zeroes) $D_1, D_2, D_3, \dots, D_N$. H...
```python for _ in range(int(input())): n,w = map(int , input().split()) sigma = 1 #len(str(num)) == n and D[i] - D[i - 1] ... = w if(w > 9 or w < -9): print(0) continue sigma = pow(10,n - 2,1000000007) if(w >= 0): sigma *= (9 - w) else: sigma *= (w + 10) print(sigma % 1000000007) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: There are n student groups at the university. During the study day, each group can take no more than 7 classes. Seven time slots numbered from 1 to 7 are allocated for the classes. The schedule on Monday is known for each group, i. e. time slot...
```python strings = int(input()) count = [0 for x in range(7)] for k in range(strings): s = input() for index in range(7): if s[index] == '1': count[index] += 1 print(max(count)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef Vivek is good in mathematics and likes solving problems on prime numbers. One day his friend Jatin told him about Victory numbers. Victory number can be defined as a number formed after summing up all the prime numbers till given number n. ...
```python from math import sqrt test = int(input()) for i in range(test): sum = 0 max = int(input()) if max==1: sum = 0 elif max==2: sum += 2 else: sum = sum + 2 for x in range(3,max+1): half = int(sqrt(x)) + 1 if all(x%y!=0 for y in range(2,half)): sum = sum + x print(sum) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell $(0, 0)$ on an infinite grid. You also have the sequence of instructions of this robot. It is written as the string $s$ consisting of characters 'L',...
```python n = int(input()) for _ in range(n): s = input() l,r,u,d = [s.count(i) for i in 'LRUD'] lr = min(l, r) ud = min(u, d) res = "" if lr == 0 and ud == 0: res = "" elif lr == 0: res = "UD" elif ud == 0: res = 'LR' else: res = 'R' * lr + 'U' * ud +...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: ----- Statement ----- You need to find a string which has exactly K positions in it such that the character at that position comes alphabetically later than the character immediately after it. If there are many such strings, print the one which...
```python for i in range(int(input())): N = int(input()) s = 'zyxwvutsrqponmlkjihgfedcba' r = '' while True: r = s[-N-1:] + r if N < 26: break N -= 25 print(r) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chandler has a list of non zero positive integers with him. He made a very interesting observation about the list. He noticed that the number of unique integers in an array of size $N$ is in the range $L$ to $R$ (both inclusive) and every elemen...
```python # cook your dish here for _ in range(int(input())): n,l,h=list(map(int,input().split())) print(n-l+1+2**(l)-2,1+2**(h)-2+2**(h-1)*(n-h)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed th...
```python 3 import sys from functools import lru_cache MOD = 1000000007 cnk = [[1 for i in range(1001)] for j in range(1001)] for i in range(1, 1001): for j in range(1, i): cnk[i][j] = cnk[i - 1][j - 1] + cnk[i - 1][j] k = int(input()) cs = [int(input()) for i in range(k)] ans = 1 sm = 0 for c in cs: ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Harry is a bright student. To prepare thoroughly for exams, he completes all the exercises in his book! Now that the exams are approaching fast, he is doing book exercises day and night. He writes down and keeps updating the remaining number of ...
```python t=eval(input()) a=[] b=[] top=-1 for __ in range(0,t): x=input().split() if(x[0]!="-1" and x[0]!="0"): add=int(x[0]) if top!=-1 and add>a[top][0] : b[top]+=1 else: a.append((add,x[1])) b.append(0) top+=1 elif (x[0]=="-1"): #print("%s %s" %(b[t...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Let's call a string "s-palindrome" if it is symmetric about the middle of the string. For example, the string "oHo" is "s-palindrome", but the string "aa" is not. The string "aa" is not "s-palindrome", because the second half of it is not a mirr...
```python import sys, math s=input() pal='AHIMOoTUVvWwXxY' n=len(s) l=0 r=n-1 flag=True fir='pq' sec='bd' while l<=r: if s[l]==s[r] and s[l] in pal: l+=1 r-=1 continue elif s[l]==s[r]: flag=False break elif (s[l] in fir) and (s[r] in fir): l+=1 r-=1 ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Teddy and Tracy like to play a game based on strings. The game is as follows. Initially, Tracy writes a long random string on a whiteboard. Then, each player starting with Teddy makes turn alternately. Each turn, the player must erase a contiguo...
```python import sys def mex(S,W,C,start,end): """Returns Nim-number of S[start:end]""" key=(start,end) try: return C[key] except KeyError: pass A=set() for s in range(start,end): for e in range(start+1,end+1): if S[s:e] not in W: continue A.add(m...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Mr. Wire Less is not that good at implementing circuit in a breadboard. In his Digital Logic Design course, he has to implement several boolean functions using the breadboard. In a breadboard, inputs are given through the switches and outputs ar...
```python # cook your dish here mod=8589934592 list1=[] for i in range(int(input())): x=int(input()) ans=(pow(2,x,mod)-1)%mod list1.append((i+1,ans)) for i in list1: print(f'Case {i[0]}: {i[1]}') ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Evlampiy has found one more cool application to process photos. However the application has certain limitations. Each photo i has a contrast v_{i}. In order for the processing to be truly of high quality, the application must receive at least k...
```python def f(m): nonlocal dp, sdp l = 0 for i in range(n): while l < n and v[l] < v[i] - m: l += 1 if l - 1 > i - k: dp[i] = False else: dp[i] = (sdp[i - k + 1] != sdp[l - 1]) sdp[i + 1] = sdp[i] + (1 if dp[i] else 0) return dp[n - 1...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of th...
```python def solve(l): m = l.index(max(l)) if m == 0 or m == len(l) - 1: return 1 return 1 + min(solve(l[0:m]), solve(l[m+1:])) tc = int(input()) for test in range(tc): n = int(input()) l = list(map(int, input().split())) print(solve(l)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot 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 example...
```python import sys input = sys.stdin.readline for _ in range(int(input())): a, k = list(map(int, input().split())) for _ in range(k - 1): if '0' in str(a): break a += int(min(list(str(a)))) * int(max(list(str(a)))) print(a) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Consider the following $4 \times 4$ pattern: 1 2 4 7 3 5 8 11 6 9 12 14 10 13 15 16 You are given an integer $N$. Print the $N \times N$ pattern of the same kind (containing integers $1$ through $N^2$). -----Input----- - The first line o...
```python for i in range(int(input())): t=int(input()) n=0 for i in range(1,t+1): n=n+i x=[n] y=n for j in range(i,t+i-1): if j<t: z=y+j else: z=y+(2*t-j-1) x.append(z) y=z print(*x) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the fo...
```python x = int(input()) y = list(map(int, input().split(' '))) y[0] = 1 y[x-1] = 1 z = y[:] for i in range(1, x): z[i] = min(z[i], z[i-1] + 1) w = y[:] for i in range(x-2, -1, -1): w[i] = min(w[i], w[i+1]+1) ans = 0 for i in range(x): ans = max(ans, min(z[i], w[i])) print(ans) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: k kids seem to have visited your home for the festival. It seems like the kids had all been fighting with each other, so you decided to keep them as far as possible from each other. You had placed n chairs on the positive number line, each at po...
```python #dt = {} for i in x: dt[i] = dt.get(i,0)+1 import sys;input = sys.stdin.readline inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()] def check(mid): pos = x[0] ct = 1 for i in range(1,n): if x[i]-pos >= mid: pos = x[i] ct += 1 ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given a integer $n$ ($n > 0$). Find any integer $s$ which satisfies these conditions, or report that there are no such numbers: In the decimal representation of $s$: $s > 0$, $s$ consists of $n$ digits, no digit in $s$ equals $0$, ...
```python #JMD #Nagendra Jha-4096 import sys import math #import fractions #import numpy ###File Operations### fileoperation=0 if(fileoperation): orig_stdout = sys.stdout orig_stdin = sys.stdin inputfile = open('W:/Competitive Programming/input.txt', 'r') outputfile = open('W:/Competitive Programm...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given a sequence D_1, D_2, ..., D_N of length N. The values of D_i are all distinct. Does a tree with N vertices that satisfies the following conditions exist? - The vertices are numbered 1,2,..., N. - The edges are numbered 1,2,..., N...
```python from collections import defaultdict N = int(input()) C = defaultdict(int) for i in range(N): D = int(input()) C[D] = i + 1 E = [] H = [1] * (N + 1) DD = sorted([[k, v] for k, v in C.items()], reverse=True) Adj = [[] for i in range(N)] for D, n in DD[:-1]: try: p = C[D - N + 2 * H[n]] if n == p: ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: The following graph G is called a Petersen graph and its vertices have been numbered from 0 to 9. Some letters have also been assigned to vertices of G, as can be seen from the following picture: Let's consider a walk W in graph G, which cons...
```python let_to_num = {'A':[0,5], 'B':[1,6], 'C':[2,7], 'D':[3,8], 'E':[4,9]} num_to_let = {0:'A', 1:'B', 2:'C', 3:'D', 4:'E', 5:'A', 6:'B', 7:'C', 8:'D', 9:'E'} connections = {0:(1,4,5), 1:(0,2,6), 2:(1,3,7), 3:(2,4,8), 4:(0,3,9), 5:(0,7,8), 6:(1,8,9), 7:(2,5,9), 8:(3,5,6), 9:(4,6,7)} ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Every character in the string “IITMANDI” is given a certain number of points. You are given a scrabble board with only one row. The input contains the positions of score modifiers such as: Double Letter, Triple Letter, Double Word and Triple Wor...
```python # cook your dish here try: T = int(input()) for i in range(T): n = int(input()) s = input() arr = [int(i) for i in input().strip().split(" ")] res = 1 result = 0 for j in range(n-7): res = 1 res1= 0 s1 = s[j:j+8] for i in range(8): if s1[i] == 'D': res = res*2 res1 ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef has decided to retire and settle near a peaceful beach. He had always been interested in literature & linguistics. Now when he has leisure time, he plans to read a lot of novels and understand structure of languages. Today he has decided to...
```python # cook your dish here from collections import Counter from math import factorial for _ in range(int(input())): s=input() c=Counter(s) k=factorial(len(s)) for value in c.values(): if value>1: k=k//factorial(value) print(k%(10**9+7)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: There are N towns located in a line, conveniently numbered 1 through N. Takahashi the merchant is going on a travel from town 1 to town N, buying and selling apples. Takahashi will begin the travel at town 1, with no apple in his possession. The...
```python N,T = list(map(int,input().split())) A = list(map(int,input().split())) cummax = [A[-1]] for a in reversed(A[:-1]): cummax.append(max(cummax[-1], a)) cummax.reverse() maxgain = n = 0 for buy,sell in zip(A,cummax): gain = sell - buy if gain > maxgain: maxgain = gain n = 1 elif...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n flights that must depart today, the i-th of them is planned to depart at the i-th minute of the day. Metropolis airport is the main transport h...
```python from heapq import heappush,heappop,heapify n,k=map(int,input().split()) *l,=map(int,input().split()) q=[(-l[i],i)for i in range(k)];heapify(q) a=[0]*n s=0 for i in range(k,n): heappush(q,(-l[i],i)) x,j=heappop(q) s-=x*(i-j) a[j]=i+1 for i in range(n,n+k): x,j=heappop(q) s-=x*(i-j) ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Bohan loves milk tea so much and he drinks one cup of milk tea every day. The local shop sells milk tea in two sizes: a Medium cup for $3 and a Large cup for $4. For every cup of milk tea purchased Bohan receives a promotional stamp. Bohan may r...
```python t = int(input()) for tc in range(t): seq = input() dollar = 0 stamp = 0 for ct in seq: if stamp >= 6: stamp -= 6 continue elif ct == 'M': dollar += 3 elif ct == 'L': dollar += 4 stamp += 1 print(dollar) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef wants to teach a lesson of sharing to the students. There are $N$ students (numbered from $1$ to $N$ from left to right) who are asked to stand in a row. Initially Chef gave $A$$i$ candies to the $i$$th$ child. In one operation any child ca...
```python from math import ceil for _ in range(int(input())): n = int(input()) arr = [int(x) for x in input().split()] sarr = sum(arr) mavg = sarr/n while n>1: sarr -= arr.pop() n-=1 mavg = max(mavg, sarr/n) print(int(ceil(mavg))) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Kabir Singh is playing a game on the non-negative side of x-axis. It takes him $1 second$ to reach from Pth position to (P−1)th position or (P+1)th position. Kabir never goes to the negative side and also doesn't stop at any moment of time. Th...
```python # cook your dish here T=int(input()) MOD=int(1e9+7) for t in range(T): N,K=[int(a) for a in input().split()] M=K//2 # ans= ((K%2)?( (N+M)*(N+M) + M ):( (N+M)*(N+M) - M) ) ans=(N+M)*(N+M) -M if(K%2): ans+=2*M if(N==0): ans=K*(K-1) print(ans%MOD) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: 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. -----In...
```python a = list(input()) b = int(input()) a.sort() a = a[::-1] prefix = "" while(len(a) > 0): for i in range(len(a)): num = prefix + a[i] + "".join(sorted(a[:i] + a[i + 1:])) if (int(num) <= b): prefix += a[i] a = a[:i] + a[i+1:] break print(prefix) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: There are $n$ cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from $1$ to $n$. Two fairs are currently taking p...
```python import sys input = sys.stdin.readline from collections import deque t=int(input()) for testcaess in range(t): n,m,a,b=list(map(int,input().split())) E=[[] for i in range(n+1)] for i in range(m): x,y=list(map(int,input().split())) E[x].append(y) E[y].append(x) USE1=[...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given an $array$ of size $N$ and an integer $K$ ( $N > 1 , K > 0$ ). Each element in the array can be incremented by $K$ or decremented by $K$ $at$ $most$ $once$. So there will be $3^n$ possible combinations of final array. (As there ar...
```python # cook your dish here testcases=int(input()) for _ in range(testcases): (N,K)=list(map(int,input().split())) array=list(map(int,input().split())) max=array[0] min=array[0] for i in array: if i>max: max=i if i<min: min=i ma...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Devu has n weird friends. Its his birthday today, so they thought that this is the best occasion for testing their friendship with him. They put up conditions before Devu that they will break the friendship unless he gives them a grand party on ...
```python # cook your dish here test = int(input()) for _ in range(0,test): n = int(input()) lister = set(map(int,input().split())) print(len(lister)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Write a program that reads two numbers $X$ and $K$. The program first finds the factors of $X$ and then gives the sum of $K$th power of every factor. The program also finds the factor of $k$ and outputs the sum of $X$ times of every factor. --...
```python try: for _ in range(int(input())): s,s1=0,0 x,k=[int(i) for i in input().split()] for i in range(2,x+1): if(x%i==0): s=s+i**k for i in range(2,k+1): if(k%i==0): s1+=i*x print(s,s1) except EOFError as...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaroo can go into another kangaroo's pocket if and only if the size of kangaroo who hold the kangaroo is at least twice as large as the size of kangaroo who is h...
```python # -*- coding: utf-8 -*- from time import perf_counter from sys import stdin def run(n, s): m = 0 small = n // 2 for big in range(n-1, (n+1)//2-1, -1): while small >= 0 and s[small] > s[big] / 2: small -= 1 if small == -1: break #print(small, big) ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: The chef was playing with numbers and he found that natural number N can be obtained by sum various unique natural numbers, For challenging himself chef wrote one problem statement, which he decided to solve in future. Problem statement: N can b...
```python for _ in range(int(input())): x,n = map(int,input().split()) reach = [0]*(x+1) reach[0] = 1 i=1 while i**n<=x: j = 1 while j+i**n<=x: j+=1 j-=1 while j>=0: if reach[j]>0: reach[j+i**n]+=reach[j] j-=1 ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given a permutation $p_1, p_2, \ldots, p_n$. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment $1,2,\ldots, k$, in other words in the end the...
```python import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ class Binary_Indexed_Tree(): def __init__(self, n): self.n = n self.data = [0]*(n+1) def add(self, i, x): while i <= self.n: self.data[i] += x i += i & -i def get(self, i)...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given a binary string $s$ consisting of $n$ zeros and ones. Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subs...
```python import sys input=sys.stdin.readline #t=1 t=int(input()) for _ in range(t): n=int(input()) s=input().rstrip() s=[s[-i-1] for i in range(n)] ans=[] zero=[] one=[] res=[-1]*n pos=0 while s: b=s.pop() if b=="0": if not one: new=1 ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence ...
```python n = input() read = input() p = [] for x in read.split(): p.append((float)(x)) v = 0.0 l = 0.0 for item in p: v = v*(1-item) + item*(v + 2*l + 1) l = (l + 1)*item print(v) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: The statement of this problem is the same as the statement of problem C1. The only difference is that, in problem C1, $n$ is always even, and in C2, $n$ is always odd. You are given a regular polygon with $2 \cdot n$ vertices (it's convex and h...
```python import math T = int(input()) for _ in range(T): n = int(input()) diags = 1/math.sin(math.pi/2/n) print(diags * math.cos(math.pi/4/n)) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those ...
```python #!/usr/bin/env python #-*-coding:utf-8 -*- import sys,collections n,q=list(map(int,input().split())) M=collections.defaultdict(collections.deque) Q=collections.deque() L=[] s=n=m=0 for _ in range(q): y,x=list(map(int,input().split())) if 2>y: s+=1 Q.append(x) M[x].append(n) n+=1 elif 3>y: y=M.get...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Chef is solving mathematics problems. He is preparing for Engineering Entrance exam. He's stuck in a problem. $f(n)=1^n*2^{n-1}*3^{n-2} * \ldots * n^{1} $ Help Chef to find the value of $f(n)$.Since this number could be very large, compute it m...
```python T=int(input()) t=[] for _ in range(T): N=int(input()) t.append(N) N=max(t)+1 l=[0 for i in range(N)] p=1 a=1 for i in range(1,N): a=(a*i)%1000000007 p=p*a%1000000007 l[i]=p for i in t: print(l[i]) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You're given an array $a$ of $n$ integers, such that $a_1 + a_2 + \cdots + a_n = 0$. In one operation, you can choose two different indices $i$ and $j$ ($1 \le i, j \le n$), decrement $a_i$ by one and increment $a_j$ by one. If $i < j$ this ope...
```python t=int(input()) while t>0 : n=int(input()) a=list(map(int,input().split())) an=0 s=0 for i in a : if s+i>=0 : s+=i else : s+=i an-=s s=0 print(an) t-=1 ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given a colored permutation $p_1, p_2, \dots, p_n$. The $i$-th element of the permutation has color $c_i$. Let's define an infinite path as infinite sequence $i, p[i], p[p[i]], p[p[p[i]]] \dots$ where all elements have same color ($c[i]...
```python from sys import stdin input = stdin.readline q = int(input()) for rwerew in range(q): n = int(input()) p = list(map(int,input().split())) c = list(map(int,input().split())) for i in range(n): p[i] -= 1 przyn = [0] * n grupa = [] i = 0 while i < n: if przyn[i] == 1: i += 1 else: nowa_grupa ...
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Bharat was given a problem to solve, by his brother, Lord Ram. The problem was like, given integers, $N$ and $K$, Bharat has to find the number (possibilities) of non-increasing arrays of length $K$, where each element of the array is between $1...
```python import math p=7+10**9 n,k=list(map(int,input().split())) c=math.factorial(n+k-1)//((math.factorial(k))*(math.factorial(n-1))) print(c%p) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: Once, a genius guy Cristo visited NASA where he met many scientists. A young intern Mark at NASA asked Cristo to observe the strange behaviour of two independent particles (say Alpha and Beta) moving in the free space.Cristo was astonished to s...
```python for _ in range(eval(input())): n=eval(input()) mod=1000000007 f1,f2=[0]*101000,[0]*101000 f1[1]=0 f1[2]=2 f1[3]=3 f2[1]=1 f2[2]=1 f2[3]=2; for i in range(4,100001): f1[i]=f1[i-1]%mod+f1[i-2]%mod+f1[i-3]%mod f2[i]=f2[i-1]%mod+f2[i-2]%mod+f2[i-3]%mod print(f1[n]%mod,f2[n]%mod) ```
open-r1/verifiable-coding-problems-python-10k
Solve the following coding problem using the programming language python: You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be stri...
```python t=int(input()) for _ in range(t): n=int(input()) grid=[] for _ in range(n): temp=[] temp=list(map(int,input().strip().split())) temp.sort() grid.append(temp) curr=max(grid[n-1]) total=curr for i in range(n-2,0-1,-1): flag=0 for j in range(n-1,0-1,-1): if grid[i][j]<curr: flag=1...