source
stringclasses
1 value
task_type
stringclasses
1 value
in_source_id
stringlengths
1
4
problem
stringlengths
488
6.07k
gold_standard_solution
stringlengths
19
30.1k
verification_info
dict
metadata
dict
problem_id
stringlengths
5
9
contaminated_aime_2024
bool
1 class
contaminated_aime_2025
bool
1 class
contaminated_math_500
bool
1 class
contaminated_gpqa
bool
1 class
contaminated_lcb
bool
2 classes
apps
verifiable_code
986
Solve the following coding problem using the programming language python: Problem Statement:Captain America and Iron Man are at WAR and the rage inside Iron Man is rising. But Iron Man faces a problem to identify the location of Captain America. There are N buildings situtaed adjacently to each other and Captain America can be at any building. Iron Man has to arrange the Buildings from 1 to N is such a way that Value(i.e abs(Building Number -Position of Building))=K for every building. Can You help Iron Man to Find The Arrangement of the Buildings? P.S- If no arrangement exist, then print “CAPTAIN AMERICA EVADES”. Input Format: The first line of input contains a single integer,T, denoting the number of test cases. Each of the T subsequent lines contains 2 space-separated integers describing the respective N and K values for a test case. Output Format: On a new line for each test case, Print the lexicographically smallest arrangement; If no absolute arrangement exists, print “CAPTAIN AMERICA EVADES”. Constraints: SubTask#1 1<=T<=10 1<=N<=10^5 0<=K<=N SubTask#2 Original Constraints.. SubTask#3 Original Constraints.. Sample Input: 3 2 1 3 0 3 2 Sample Output: 2 1 1 2 3 CAPTAIN AMERICA EVADES Explanation: Case 1: N=2 and K=1 Therefore the arrangement is [2,1]. Case 2: N=3 and K=0 Therefore arrangement is [1,2,3]. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) while t> 0: t =t -1 n,k = list(map(int,input().split())) a = [0]*n done = True def swap(z): for j in range(0,n): if a[j] == 0: a[j] = z done = True break else: if a[j] > z: swap(j) a[j] = z else: done = False break for i in range(0,n): for j in range(0,n): if abs(i-j) == k: if a[j] == 0: a[j] = i + 1 done = True break else: if a[j] > i + 1: swap(a[j]) a[j] = i + 1 else: done = False if 0 in a: print('CAPTAIN AMERICA EVADES') else: if done: for c in a: print(c, end=' ') print() else: print('CAPTAIN AMERICA EVADES') # for i in range(1,n+1): # if i - k >=0 : # if a[abs(i-k-1)] == 0: # a[abs(i-k-1)] = i # done = True # else: # done = False # break # else: # done = False # break # if done: # for c in a: # print c, # print # else: # print "CAPTAIN AMERICA EVADES" ```
{ "language": "python", "test_cases": [ { "input": "3\n2 1\n3 0\n3 2\n", "output": "2 1\n1 2 3\nCAPTAIN AMERICA EVADES\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COWS2016/problems/CVW" }
vfc_2786
false
false
false
false
false
apps
verifiable_code
987
Solve the following coding problem using the programming language python: Back in 2015, Usain Bolt announced that he'll be retiring after the 2017 World Championship. Though his final season did not end gloriously, we all know that he is a true legend and we witnessed his peak during 2008 - 2013. Post retirement, Usain Bolt is still leading an adventurous life. He's exploring the unexplored parts of the globe. But sometimes he gets bored, and reads questions asked about him on Quora. One such question he read was, "Who would win a race between Usain Bolt and a tiger if the race is on a straight line track and the tiger is $distancetoBolt$ meters behind Bolt? The finishing point is $finish$ meters away from Bolt's starting position. The tiger starts with an initial speed of $0$ $meter/second$, and will accelerate itself with $tigerAccelaration$ $m/s^2$. Bolt can run with a constant speed of $boltSpeed$ $m/s$ from start to finish. Given these values, find out who will win the race - Bolt or the tiger? " Note that Bolt will win the race if and only if he touches the finishing line before the tiger touches it. If both of them finish together, the tiger is announced as the winner since Bolt was given an initial advantage. See the figure below for more clarity. Since Bolt was busy practicing in the tracks during his Physics school classes, he is asking for your help to solve the question. Can you please help him? He just remembers two formulae from the class, and thinks that they will be useful to you: $Displacement (S) $ = $ut$ +$ (1/2)at^2$ where $u$ is the initial velocity , #$ $is the acceleration and $t$ is the time taken. $Velocity$ = $Displacement /Time$ -----Input:----- - The first line will contain $T$, the number of testcases. Then the description of each test case follow. - Each test case contains 4 integers $finish, distancetoBolt, tigerAccelaration, boltSpeed$. -----Output:----- For each testcase, output in a single line, the word "Bolt" or "Tiger" without quotes, depending on whether Bolt wins or the tiger wins. -----Constraints----- - $1 \leq T \leq 100000$ - $1 \leq finish\leq 10^5$ - $1 \leq distancetoBolt\leq 10^5$ - $1 \leq tigerAccelaration\leq 10^5$ - $1 \leq boltSpeed\leq 10^5$ -----Sample Input:----- 2 10 100 10 10 100 10 5 10 -----Sample Output:----- Bolt Tiger The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for i in range(int(input())): finish,distanetobolt,tigerAcceleration,boltspeed=map(int,input().split()) t1=((2*(finish+distanetobolt)/(tigerAcceleration))**0.5) t2=(finish/boltspeed) if t1>t2: print("Bolt") elif t1<t2: print("Tiger") else: print("Tiger") ```
{ "language": "python", "test_cases": [ { "input": "2\n10 100 10 10\n100 10 5 10\n", "output": "Bolt\nTiger\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/USANBOLT" }
vfc_2790
false
false
false
false
false
apps
verifiable_code
988
Solve the following coding problem using the programming language python: You are given a sequence of non-negative integers $A_1, A_2, \ldots, A_N$. At most once, you may choose a non-negative integer $X$ and for each valid $i$, change $A_i$ to $A_i \oplus X$ ($\oplus$ denotes bitwise XOR). Find the minimum possible value of the sum of all elements of the resulting sequence. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of the input contains a single integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the minimum possible sum. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 10^5$ - $1 \le A_i \le 10^9$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $10^6$ -----Subtasks----- Subtask #1 (50 points): - $1 \le N \le 10^3$ - $1 \le A_i \le 10^3$ for each valid $i$ Subtask #2 (50 points): original constraints -----Example Input----- 3 5 2 3 4 5 6 4 7 7 7 7 3 1 1 3 -----Example Output----- 14 0 2 -----Explanation----- Example case 1: If we choose $X = 6$, the sequence becomes $(4, 5, 2, 3, 0)$. Example case 2: We can choose $X = 7$ to make all elements of the resulting sequence equal to $0$. Example case 3: We can choose $X = 1$. The sequence becomes $(0, 0, 2)$, with sum $2$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here # cook your dish here test=int(input()) for _ in range(test): n=int(input()) a=[(bin(int(x))[2:][::-1]+("0")*32)for x in input().split()] res="" mysum=0 for i in range(32): mycount=0 for j in range(n): if(a[j][i]=="0"): mycount+=1 if(mycount==n): break if(mycount>(n//2)): res+="0" mysum+=(n-mycount)*int(pow(2,i)) else: res+="1" mysum+=mycount*int(pow(2,i)) print(mysum) ```
{ "language": "python", "test_cases": [ { "input": "3\n5\n2 3 4 5 6\n4\n7 7 7 7\n3\n1 1 3\n", "output": "14\n0\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/MINARRS" }
vfc_2794
false
false
false
false
false
apps
verifiable_code
989
Solve the following coding problem using the programming language python: Chef and Paja are bored, so they are playing an infinite game of ping pong. The rules of the game are as follows: - The players play an infinite number of games. At the end of each game, the player who won it scores a point. - In each game, one of the players serves. Chef serves in the first game. - After every $K$ points are scored (regardless of which players scored them), i.e. whenever $K$ games have been played since the last time the serving player changed, the player that serves in the subsequent games changes: if Chef served in the game that just finished, then Paja will serve in the next game and all subsequent games until the serving player changes again; if Paja served, then Chef will serve. The players got a little too caught up in the game and they forgot who is supposed to serve in the next game. Will you help them determine that? So far, Chef has scored $X$ points and Paja has scored $Y$ points. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains three space-separated integers $X$, $Y$ and $K$. -----Output----- For each test case, print a single line containing the string "Chef" if Chef is supposed to serve next or "Paja" otherwise (without quotes). -----Constraints----- - $1 \le T \le 50$ - $0 \le X, Y \le 10^9$ - $1 \le K \le 10^9$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 3 1 3 3 5 7 2 38657 76322 564 -----Example Output----- Paja Chef Paja -----Explanation----- Example case 1: Chef served for the first three games, after that Paja started serving. He only served in one game, so he is supposed to serve next. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) while(n>0): x,y,z=map(int,input().split()) t=(x+y)//z if t%2==0: print('Chef') else: print('Paja') n-=1 ```
{ "language": "python", "test_cases": [ { "input": "3\n1 3 3\n5 7 2\n38657 76322 564\n", "output": "Paja\nChef\nPaja\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/PAJAPONG" }
vfc_2798
false
false
false
false
false
apps
verifiable_code
990
Solve the following coding problem using the programming language python: Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a. What is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the sides of the Square. Input The input contains three positive integer numbers in the first line: n,  m and a (1 ≤  n, m, a ≤ 10^9). Output Print the needed number of flagstones in new line. Examples input 6 6 4 output 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python try: n,m,a=map(int,input().split()) if n%a!=0: number1=(n//a)+1 else: number1=(n//a) if m%a!=0: number2=(m//a)+1 else: number2=(m//a) print(number1*number2) except: pass ```
{ "language": "python", "test_cases": [ { "input": "s\ninput\n6 6 4\noutput\n4\n", "output": "", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/CSTR2020/problems/MALLTILE" }
vfc_2802
false
false
false
false
false
apps
verifiable_code
991
Solve the following coding problem using the programming language python: The Chef once decided to prepare some nice dishes on his birthday. There are N items kept on his shelf linearly from position 1 to N. Taste of the i-th item is denoted by a integer Ai. He wants to make Q dishes. A dish will be made using some ingredients in the continuous range AL, AL + 1, , , AR (1-base indexing). Quality of the dish will be determined by the ingredient with minimum taste. Chef wants help of his assistant Rupsa to find out sum and product of qualities of the dishes. As product of the qualities of the dishes could be very large, print it modulo 109 + 7. Also, you are given an integer K and you are assured that for each dish, the size of continuous range of the ingredients (i.e. R - L + 1) will always lie between K and 2 * K, both inclusive. Method of generation of Array A You are given non-negative integer parameters a, b, c, d, e, f, r, s, t, m, A[1] for x = 2 to N: if(t^x mod s <= r) // Here t^x signifies "t to the power of x" A[x] = (a*A[x-1]^2 + b*A[x-1] + c) mod m else A[x] = (d*A[x-1]^2 + e*A[x-1] + f) mod m Method of generation of range of ingredients for Q dishes You are given non-negative integer parameters L1, La, Lc, Lm, D1, Da, Dc, Dm for i = 1 to Q: L1 = (La * L1 + Lc) mod Lm; D1 = (Da * D1 + Dc) mod Dm; L = L1 + 1; R = min(L + K - 1 + D1, N); -----Input----- - The first line contains three integers N, K and Q. - The second line contains the integers a, b, c, d, e, f, r, s, t, m, and A[1]. - Then third line contains the integers L1, La, Lc, Lm, D1, Da, Dc, and Dm -----Output----- Output two space separated integers: - The sum of qualities of the dishes. - The product of qualities of the dishes modulo 109+7. -----Constraints----- - 1 ≤ N, Q ≤ 107 - 1 ≤ K ≤ N - 0 ≤ a, b, c, d, e, f, r, s, t, m, A[1] ≤ 109+7 - 1 ≤ Lm ≤ N - K + 1 - 1 ≤ Dm ≤ K + 1 - 1 ≤ La, Lc ≤ Lm - 1 ≤ Da, Dc ≤ Dm - 1 ≤ L1 ≤ N - 1 ≤ D1 ≤ K -----Sub tasks----- - Subtask #1: 1 ≤ N, Q ≤ 1000 (10 points) - Subtask #2: 1 ≤ Q ≤ 104 (20 points) - Subtask #3: original constraints (70 points) -----Example----- Input: 4 2 1 1 1 1 1 1 1 1 1 1 100 1 1 1 1 3 1 1 1 2 Output: 13 13 -----Explanation----- - The array A comes out to be {1, 3, 13, 83} and the first dish has L = 3 and R = 4. The minimum in this range is 13, thus the sum and product both are 13 and hence the answer. -----Note----- Multiplier for C# and Java have been reduced to 1.5 instead of 2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,k, q = list(map(int, input().split())) mod = 10 ** 9 + 7 A = [0] * n a, b, c, d, e, f, r, s, t, m, x = list(map(int, input().split())) A[0] = x for x in range(1, n): if pow(t, x+1, s) <= r: A[x] = (a*pow(A[x-1], 2, m) + b*A[x-1] + c) % m else: A[x] = (d*pow(A[x-1], 2, m) + e*A[x-1] + f) % m def SRMQ(arr, k): from collections import deque n = len(arr) ans = [None] * n deque = deque() for i in range(len(arr)): while deque and deque[-1] > arr[i]: deque.pop() deque.append(arr[i]) if i >= k and arr[i-k] == deque[0]: deque.popleft() if i >= k-1: ans[i-k+1] = deque[0] return ans v = SRMQ(A, k) L1, La, Lc, Lm, D1, Da, Dc, Dm = list(map(int, input().split())) s = 0 prod = 1 for _ in range(q): L1 = (La * L1 + Lc) % Lm; D1 = (Da * D1 + Dc) %Dm; L = L1 + 1; R = min(L + k - 1 + D1, n); z = min(v[L-1], v[R-k]) s += z prod = (prod * z) % mod print(s, prod) ```
{ "language": "python", "test_cases": [ { "input": "4 2 1\n1 1 1 1 1 1 1 1 1 100 1\n1 1 1 3 1 1 1 2\n", "output": "13 13\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CHEFCK" }
vfc_2806
false
false
false
false
true
apps
verifiable_code
992
Solve the following coding problem using the programming language python: January and February are usually very cold in ChefLand. The temperature may reach -20 and even -30 degrees Celsius. Because of that, many people seal up windows in their houses. Sergey also lives in ChefLand. He wants to seal the window in his house. The window has the shape of a simple convex polygon with N vertices. For the sealing, there are M kinds of sticky stripes, which are sold in the shops. The stripe of the ith type has the length of Li millimeters and the cost of Ci rubles. The sealing process consists in picking the stripe and sticking it on the border of the window. The stripe can't be cut (it is made of very lasting material) and can only be put straight, without foldings. It is not necessary to put the strip strictly on the window border, it can possibly extend outside the border side of window too (by any possible amount). The window is considered sealed up if every point on its' border is covered with at least one stripe. Now Sergey is curious about the stripes he needs to buy. He wonders about the cheapest cost, at which he can seal his window. Please help him. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the number of number of vertices in the polygon denoting Sergey's window. Each of the following N lines contains a pair of space-separated integer numbers Xi Yi, denoting the coordinates of the ith points. The following line contains a single integer M denoting the number of types of sticky stripe which is sold in the shop. Each of the following M lines contains a pair of space-separated integers Li Ci denoting the length and the cost of the sticky stripe of the ith type respectively. -----Output----- For each test case, output a single line containing the minimum cost of sealing up the window. -----Constraints----- - 1 ≤ T ≤ 10 - The coordinates of the window are given either in clockwise or in a counter-clockwise order. - No three or more vertices lie on the same line (i.e. are collinear). - 0 ≤ Xi, Yi ≤ 106 - 1 ≤ Li, Ci ≤ 106 -----Subtasks----- - Subtask #1 (17 points): 3 ≤ N ≤ 10, M = 1 - Subtask #2 (24 points): 3 ≤ N ≤ 42, M ≤ 2 - Subtask #3 (59 points): 3 ≤ N ≤ 2000, 1 ≤ M ≤ 10 -----Example----- Input:1 4 0 0 1000 0 1000 2000 0 2000 2 1000 10 2000 15 Output:50 -----Explanation----- Example case 1. In this case, Sergey's window is a rectangle with the side lengths of 1000 and 2000. There are two types of the sticky stripes in the shop - the one of the length 1000 with the cost of 10 rubles and with the length of 2000 and the cost of 15 rubles. The optimal solution would be to buy 2 stripes of the first type 2 stripes of the second type. The cost will be 2 × 15 + 2 × 10 = 50 rubles. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import sqrt import sys sys.setrecursionlimit(10**8) intMax = 10**18 def knapsack(rl,l,c,m): if m==0 and rl>0: return intMax if rl<=0: return 0 return min(c[m-1]+knapsack(rl-l[m-1],l,c,m),knapsack(rl,l,c,m-1)) for _ in range(int(input())): n= int(input()) cost=[] length=[] sides=[] for i in range(n): x,y = map(int,input().split()) if i == 0: x0 = x y0 = y prevx = x prevy = y elif i == n-1: sides.append(sqrt((prevx-x)**2 + (prevy-y)**2)) sides.append(sqrt((x0-x)**2 + (y0-y)**2)) else: sides.append(sqrt((prevx-x)**2 + (prevy-y)**2)) prevx = x prevy = y m = int(input()) for j in range(m): li,ci = map(int,input().split()) length.append(li) cost.append(ci) ans=0 #print(sides) for k in sides: ans= ans + knapsack(k,length,cost,m) print(int(ans)) ```
{ "language": "python", "test_cases": [ { "input": "1\n4\n0 0\n1000 0\n1000 2000\n0 2000\n2\n1000 10\n2000 15\n", "output": "50\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/SEALUP" }
vfc_2810
false
false
false
false
false
apps
verifiable_code
993
Solve the following coding problem using the programming language python: Supermarket Dilemma Chef is going to local supermarket but there appears a problem with chef as he is confused about which Supermarket he can choose to go as he is not able to decide whether he can park his car in that particular supermarket’s parking lot or not! There are N parking slots in each supermarket which are marked from 1,2,3,4…N. Chef will go to that supermarket in which he gets to know that there is exactly 1 empty parking slot having number K that exactly divides the total number of slots (N) available in that supermarket. The 1st and Nth parking slots are always occupied by the staff of every supermarket. Rest parking slots are empty as Chef is arriving early morning to the supermarket. Now Chef needs your help in determining whether he can park his car in a supermarket or not! Input The first line contains the single integer N showing how many supermarkets are there for the chef to choose. The next N lines contain a number ‘ai’ which represents the total parking slots available in ith supermarket. Output You need to output "YES" (without the quotes), if a supermarket can be reached by Chef, and "NO" (without the quotes), if it can't. Constraints 1<=N<=10^5 1<=ai<=10^12 Sample Input : 2 4 5 Sample Output : YES NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math def check(s): if s==2: return 1 for i in range(2,math.ceil(math.sqrt(s))+1): if s%i == 0: return 0 return 1 def Solve(slots): if slots<3: return 0 #check s = math.sqrt(slots) if math.floor(s) == math.ceil(s): return check(s) return 0 N = int(input()) for t in range(N): slots = int(input()) if (Solve(slots)): print("YES") else: print("NO") ```
{ "language": "python", "test_cases": [ { "input": "2\n4\n5\n", "output": "YES\nNO\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/NITJ2021/problems/CIN002" }
vfc_2814
false
false
false
false
false
apps
verifiable_code
994
Solve the following coding problem using the programming language python: You are given an integer sequence $A_1, A_2, \ldots, A_N$ and an integer $X$. Consider a $N \times N$ matrix $B$, where $B_{i,j} = A_i + A_j$ for each valid $i$ and $j$. You need to find the number of square submatrices of $B$ such that the sum of their elements is $X$. Formally, find the number of quartets $(x_1, y_1, x_2, y_2)$ such that $1 \le x_1 \le x_2 \le N$, $1 \le y_1 \le y_2 \le N$, $x_2-x_1 = y_2-y_1$ and $\sum_{i=x_1}^{x_2}\sum_{j=y_1}^{y_2} B_{i,j} = X$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $X$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the number of square submatrices with sum $X$. -----Constraints----- - $1 \le T \le 100$ - $1 \le X \le 10^6$ - $1 \le N \le 10^5$ - $1 \le A_i \le 10^6$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $10^6$ -----Subtasks----- Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints -----Example Input----- 2 5 36 1 2 3 1 12 4 54 3 3 3 3 -----Example Output----- 6 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here import sys input=sys.stdin.readline for _ in range(int(input())): n,x=list(map(int,input().split())) l=[0] pre=[0]*(n+1) sum=0 i=1 for m in input().split(): l.append(int(m)) sum+=int(m) pre[i]=sum i+=1 dict={} k=[] i=1 while (i*i)<=x: if x%i==0: k.append(i) if (i*i)!=x: k.append(x//i) else: break i+=1 ans=0 for a in k: if a>n: continue z=x//a for j in range(a,n+1): s=pre[j]-pre[j-a] if s>z: continue if s in dict: dict[s]+=1 else: dict[s]=1 for j in range(a,n+1): s=pre[j]-pre[j-a] if s>z: continue if (z-s) in dict: ans+=dict[z-s] for j in range(a,n+1): s=pre[j]-pre[j-a] if s>z: continue dict[s]=0 print(ans) ```
{ "language": "python", "test_cases": [ { "input": "2\n5 36\n1 2 3 1 12\n4 54\n3 3 3 3\n", "output": "6\n4\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/SQRSUBMA" }
vfc_2818
false
false
false
false
false
apps
verifiable_code
995
Solve the following coding problem using the programming language python: Mr. Krabs loves money. He enters a bank and sees that there are n rooms arranged in a row. The only rooms with doors are the two rooms on either corner. Initially, all the rooms are locked (including the corner ones). To enter a room, it must be unlocked with a key. Mr. Krabs is able to steal k keys from the sleeping night guard. Each key can be used to unlock only one room. To reach one of the inner rooms, he has to unlock an adjacent room first. Eg: If there are 5 rooms labelled: 1 2 3 4 5 Only Rooms 1 and 5 can be accessed initially. To unlock room 2, either room 1 or room 3 must be unlocked. Similarly to unlock Room 4, he must unlock room 5 or room 3 first. Each room has some money in it. The money present in room i is given by A[i]. Using the k keys, calculate what is the maximum possible amount of money obtainable. -----Input:----- - First line will contain n, number of rooms. - Second line will contain space separated integers, denoting A[i], money present in each room. - Third line will contain an integer, k, the number of keys available. -----Output:----- Maximum money obtained. -----Constraints----- - 1 <= Number of Rooms <= 10^5 - 1 <= A[i] <= 10^4 - 1 <= k <= Number of Rooms -----Subtasks----- - 40 points : 1 <= Number of Rooms <= 100 - 60 points : Number of Rooms > 100 -----Sample Input:----- 7 1 2 3 4 5 6 1 3 -----Sample Output:----- 12 -----Explanation:----- For the first step, Mr. Krabs can only unlock the room at the left corner or the right corner. If he unlocks the room in the right corner, he obtains 1 dollar. He can then go on to unlock the second last room and gain 6 dollars, for a total of 7 dollars. Finally he can unlock the third last room and get 5 dollars, for a total of 12 dollars. This is the maximum amount of money he can get with only 3 keys. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python rooms=int(input()) money=list(map(int,input().split())) keys=int(input()) rev=-1 a,b=0,-1 tot=[] #print(money[-3:-1]) x=0 for i in range(keys): #print(money[b:-1],money[0:a]) x=sum(money[b:])+sum(money[0:keys-abs(b)]) tot.append(x) b-=1 print(max(tot)) ```
{ "language": "python", "test_cases": [ { "input": "7\n1 2 3 4 5 6 1\n3\n", "output": "12\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COFJ2020/problems/JUN2" }
vfc_2822
false
false
false
false
false
apps
verifiable_code
996
Solve the following coding problem using the programming language python: The game of billiards involves two players knocking 3 balls around on a green baize table. Well, there is more to it, but for our purposes this is sufficient. The game consists of several rounds and in each round both players obtain a score, based on how well they played. Once all the rounds have been played, the total score of each player is determined by adding up the scores in all the rounds and the player with the higher total score is declared the winner. The Siruseri Sports Club organises an annual billiards game where the top two players of Siruseri play against each other. The Manager of Siruseri Sports Club decided to add his own twist to the game by changing the rules for determining the winner. In his version, at the end of each round, the cumulative score for each player is calculated, and the leader and her current lead are found. Once all the rounds are over the player who had the maximum lead at the end of any round in the game is declared the winner. Consider the following score sheet for a game with 5 rounds: RoundPlayer 1Player 2114082289134390110411210658890 The total scores of both players, the leader and the lead after each round for this game is given below:RoundPlayer 1Player 2LeaderLead114082Player 1582229216Player 1133319326Player 274431432Player 215519522Player 23 Note that the above table contains the cumulative scores. The winner of this game is Player 1 as he had the maximum lead (58 at the end of round 1) during the game. Your task is to help the Manager find the winner and the winning lead. You may assume that the scores will be such that there will always be a single winner. That is, there are no ties. Input The first line of the input will contain a single integer N (N ≤ 10000) indicating the number of rounds in the game. Lines 2,3,...,N+1 describe the scores of the two players in the N rounds. Line i+1 contains two integer Si and Ti, the scores of the Player 1 and 2 respectively, in round i. You may assume that 1 ≤ Si ≤ 1000 and 1 ≤ Ti ≤ 1000. Output Your output must consist of a single line containing two integers W and L, where W is 1 or 2 and indicates the winner and L is the maximum lead attained by the winner. Example Input: 5 140 82 89 134 90 110 112 106 88 90 Output: 1 58 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s1=s2=lead=0 mlead1=mlead2=0 for _ in range(int(input())): x, y= list(map(int, input().split())) s1, s2= s1+x, s2+y if(s1>s2): lead=(s1-s2) mlead1= max(mlead1, lead) else: lead=(s2-s1) mlead2= max(mlead2, lead) if(mlead1<(mlead2)): print('2', mlead2) else: print('1', mlead1) ```
{ "language": "python", "test_cases": [ { "input": "5\n140 82\n89 134\n90 110\n112 106\n88 90\n", "output": "1 58\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/TLG" }
vfc_2826
false
false
false
false
false
apps
verifiable_code
997
Solve the following coding problem using the programming language python: Dr. S. De teaches computer architecture in NIT Patna. Whenever he comes across any good question(with complexity $k$), he gives that question to students within roll number range $i$ and $j$ At the start of semester he assigns score of $10$ to every student in his class if a student submits a question of complexity $k$, his score gets multiplied by $k$ This month he gave $M$ questions and he is wondering what will be mean of maximum scores of all the student. He is busy in improving his finger print attendance module, can you help him? Input file may be large so try to use fast input output -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a First line of input, two integers $N, M$ i.e. Number of students in the class and number of questions given in this month. - Next $M$ lines contains 3 integers -$i, j, k$ i.e. starting roll number, end roll number and complexity of the question -----Output:----- For each testcase, output in a single line answer - $floor$ value of Mean of maximum possible score for all students. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq N, M \leq 10^5$ - $1 \leq i \leq j \leq N$ - $1 \leq k \leq 100$ -----Subtasks----- Subtask1 - $1 \leq T \leq 10$ - $1 \leq N, M \leq 10^4$ Subtask2 - Original Constraints -----Sample Input:----- 1 5 3 1 3 5 2 5 2 3 4 7 -----Sample Output:----- 202 -----EXPLANATION:----- Initial score of students will be : $[10, 10, 10, 10, 10]$ after solving question 1 scores will be: $[50, 50, 50, 10, 10]$ after solving question 2 scores will be: $[50, 100, 100, 20, 20]$ after solving question 1 scores will be: $[50, 100, 700, 140, 20]$ Hence after all questions mean of maximum scores will $(50+100+700+140+20)/5 = 202$ The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python try: t=int(input()) for a in range(t): l=input().split() n=int(l[0]) m=int(l[1]) d={} s=0 for b in range(m): l1=input().split() i=int(l1[0]) j=int(l1[1]) k=int(l1[2]) for c in range(i,j+1): if c not in d: d[c]=10 for c in range(i,j+1): d[c]=d[c]*k for i in d: s=s+d[i] print(s//n) except: pass ```
{ "language": "python", "test_cases": [ { "input": "1\n5 3\n1 3 5\n2 5 2\n3 4 7\n", "output": "202\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/STRT2020/problems/PROFDE" }
vfc_2830
false
false
false
false
false
apps
verifiable_code
998
Solve the following coding problem using the programming language python: You are given an N × N grid initially filled by zeros. Let the rows and columns of the grid be numbered from 1 to N, inclusive. There are two types of operations can be applied to the grid: - RowAdd R X: all numbers in the row R should be increased by X. - ColAdd C X: all numbers in the column C should be increased by X. Now after performing the sequence of such operations you need to find the maximum element in the grid. -----Input----- The first line of the input contains two space separated integers N and Q denoting the size of the grid and the number of performed operations respectively. Each of the following Q lines describe an operation in the format described above. -----Output----- Output a single line containing the maximum number at the grid after performing all the operations. -----Constraints----- - 1 ≤ N ≤ 314159 - 1 ≤ Q ≤ 314159 - 1 ≤ X ≤ 3141 - 1 ≤ R, C ≤ N -----Example----- Input: 2 4 RowAdd 1 3 ColAdd 2 1 ColAdd 1 4 RowAdd 2 1 Output: 7 -----Explanation----- The grid changes as follows: 00 33 34 74 74 00 00 01 41 52 The maximum number in the final grid is 7. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,q=map(int,input().split()) dr={} dc={} for i in range(1,n+1): dr[i]=0 dc[i]=0 mer=0 mec=0 for i in range(q): s,j,k=input().split() j=int(j) k=int(k) if s=="RowAdd": dr[j]+=k if dr[j]>mer: mer=dr[j] else: dc[j]+=k if mec<dc[j]: mec=dc[j] # m=max(list(dr.values()))+max(list(dc.values())) # for i in range(n): # for j in range(n): # ar[i][j]=dr[i+1]+dc[j+1] # if ar[i][j]>m: # m=ar[i][j] print(mer+mec) ```
{ "language": "python", "test_cases": [ { "input": "2 4\nRowAdd 1 3\nColAdd 2 1\nColAdd 1 4\nRowAdd 2 1\n", "output": "7\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/ROWCOLOP" }
vfc_2834
false
false
false
false
false
apps
verifiable_code
999
Solve the following coding problem using the programming language python: The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- -First-line will contain $T$, the number of test cases. Then the test cases follow. -Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 26$ - $1 \leq K \leq 26$ -----Sample Input:----- 2 2 4 -----Sample Output:----- A 12 A 12 ABC 1234 -----EXPLANATION:----- No need, else pattern can be decode easily. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python try: for _ in range(int(input())): k=int(input()) for i in range(1,k+1): print(" "*(k-i),end="") if i%2==1: for j in range(0,i): print(chr(65+j),end="") else: for j in range(0,i): print(j+1,end="") print() except: pass ```
{ "language": "python", "test_cases": [ { "input": "2\n2\n4\n", "output": "A\n12\nA\n12\nABC\n1234\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PBK12020/problems/ITGUY16" }
vfc_2838
false
false
false
false
false
apps
verifiable_code
1000
Solve the following coding problem using the programming language python: The activity of a panipuri seller is "making a panipuri and putting it on the palte of his customer". $N$ customers are eating panipuri, Given an array $A$ of length $N$, $i^{th}$ customer takes $A_i$ seconds to eat a panipuri. The Speed of Panipuri seller refers to the number of customers served per second. Determine the minimum speed of panipuri seller so that no customer has to wait for panipuri after getting his/her first panipuri. Assume that the plate can hold infinite panipuris, and customer starts eating next panipuri just after finishing the current one. You would be provided with the time taken by each customer to eat a panipuri. Panpuri seller serves panipuri in round robin manner (first to last and then again first). -----Input:----- - First line will contain $T$, number of testcases. Then the test cases follow. - For each test case, the first line contains $N$ number of customers. - Then the second line will contain $N$ space separated integers, $A_1$ to $A_N$, eating time taken by each customer(in seconds). -----Output:----- - For each test case, print a single line containing the minimum integral speed of panipuri seller so that no customer has to wait. -----Constraints----- - $0 < T \leq 100$ - $0 < N \leq 10^6$ - $0 < A_i \leq 10^9$ - Sum of $N$ over all test cases does not exceed $10^6$ -----Sample Input:----- 2 4 2 4 6 3 5 2 3 4 6 5 -----Sample Output:----- 2 3 -----EXPLANATION:----- $Test case$ $1$: Let's assume speed of panipuri seller be 1. So starting from $0^{th}$ second, in $0$ to $1$ second seller make a panipuri and put it into the plate of first customer, and first customer completely eat it in $1$ to $3$ second as he takes $2$ second to eat a panipuri. But in $3$ to $4$ second panipuri seller is serving to $4^{th}$ customer, and hence first customer have to wait, so 1 cannot be our answer. Next possible speed can be $2$ . Starting from $0^{th}$ second, at half second seller make a panipuri and put it into the plate of first customer, first customer completely eat it on $2.5$ second as he takes $2$ second to eat a panipuri. In $0.5$ to $1$ second, seller serve the second customer. similarly in $1$ to $2$ second $3^{rd}$ and $4^{th}$ customer will be serve, in $2$ to $2.5$ second, seller will again serve the first customer so at $2.5$ sec $1^{st}$ customer will have a panipuri to eat. Similarly proceeding further we will find that no customer have to wait when speed is $2$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here from math import ceil for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) ans=ceil(n/min(a)) print(int(ans)) ```
{ "language": "python", "test_cases": [ { "input": "2\n4\n2 4 6 3\n5\n2 3 4 6 5\n", "output": "2\n3\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ECPG2020/problems/ECPC10B" }
vfc_2842
false
false
false
false
false
apps
verifiable_code
1001
Solve the following coding problem using the programming language python: Chef wants to buy a new phone, but he is not willing to spend a lot of money. Instead, he checks the price of his chosen model everyday and waits for the price to drop to an acceptable value. So far, he has observed the price for $N$ days (numbere $1$ through $N$); for each valid $i$, the price on the $i$-th day was $P_i$ dollars. On each day, Chef considers the price of the phone to be good if it is strictly smaller than all the prices he has observed during the previous five days. If there is no record of the price on some of the previous five days (because Chef has not started checking the price on that day yet), then Chef simply ignores that previous day ― we could say that he considers the price on that day to be infinite. Now, Chef is wondering ― on how many days has he considered the price to be good? Find the number of these days. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N$ space-separated integers $P_1, P_2, \dots, P_N$. -----Output----- For each test case, print a single line containing one integer ― the number of days with a good price. -----Constraints----- - $1 \le T \le 100$ - $7 \le N \le 100$ - $350 \le P_i \le 750$ for each valid $i$ -----Subtasks----- Subtask #1 (30 points): $N = 7$ Subtask #2 (70 points): original constraints -----Example Input----- 1 7 375 750 723 662 647 656 619 -----Example Output----- 2 -----Explanation----- Example case 1: Chef considers the price to be good on day $1$, because he has not observed any prices on the previous days. The prices on days $2, 3, 4, 5, 6$ are not considered good because they are greater than the price on day $1$. Finally, the price on day $7$ is considered good because it is smaller than all of the prices on days $2, 3, 4, 5, 6$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) g=1 for j in range(1,n): if j-5<0: mi=min(a[0:j]) #print(a[0:j]) if mi>a[j]: g=g+1 else: mi=min(a[j-5:j]) #print(a[j-5:j]) if mi>a[j]: g=g+1 print(g) ```
{ "language": "python", "test_cases": [ { "input": "1\n7\n375 750 723 662 647 656 619\n", "output": "2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/S10E" }
vfc_2846
false
false
false
false
false
apps
verifiable_code
1002
Solve the following coding problem using the programming language python: Chef works in a similar way to a travelling salesman ― he always travels to new cities in order to sell his delicious dishes. Today, Chef is planning to visit $N$ cities (numbered $1$ through $N$). There is a direct way to travel between each pair of cities. Each city has a specific temperature; let's denote the temperature in the $i$-th city by $C_i$. Chef has a fixed temperature tolerance $D$ with the following meaning: for each pair of cities $a$ and $b$, he may travel from city $a$ directly to city $b$ only if $|C_a-C_b| \le D$, otherwise he would catch a heavy flu because of the sudden change in temperature. Chef starts from city $1$. Is he able to visit all $N$ cities in such a way that each city is visited exactly once? Notes: - Chef is not able to travel through a city without visiting it. - City $1$ is visited at the beginning. - It is not necessary to be able to travel directly to city $1$ from the last city Chef visits. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $D$. - The second line contains $N$ space-separated integers $C_1, C_2, \ldots, C_N$. -----Output----- For each test case, print a single line containing the string "YES" (without quotes) if Chef can visit all cities or "NO" (without quotes) if he cannot. -----Constraints----- - $1 \le T \le 1,000$ - $2 \le N \le 10^5$ - $0 \le D \le 10^9$ - $0 \le C_i \le 10^9$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $10^6$ -----Subtasks----- Subtask #1 (20 points): - $N \le 1,000$ - the sum of $N$ over all test cases does not exceed $10,000$ Subtask #2 (80 points): original constraints -----Example Input----- 2 5 3 3 2 1 4 5 5 4 10 1 3 2 9 -----Example Output----- YES NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here def solution(b,n1,d): first=b[0] b.sort() for j in range(n1-1): if(a[j+1]-a[j]>d): return "NO" for j in range(n1): if(b[j]==first): pos=j if(pos==0 or pos==n1-1): return "YES" rec=1 for j in range(pos-1,n1-2): if(a[j+2]-a[j]>d): rec=0 break if(rec): return "YES" rec=1 for j in range(pos+1,1,-1): if(a[j]-a[j-2]>d): rec=0 break if(rec): return "YES" else: return "NO" testcase=int(input()) for i in range(testcase): n,d=list(map(int,input().split())) a=list(map(int,input().split())) print(solution(a,n,d)) ```
{ "language": "python", "test_cases": [ { "input": "2\n5 3\n3 2 1 4 5\n5 4\n10 1 3 2 9\n", "output": "YES\nNO\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/TRVLCHEF" }
vfc_2850
false
false
false
false
false
apps
verifiable_code
1003
Solve the following coding problem using the programming language python: In the world of DragonBool there are fierce warriors called Soints. Also there are even fiercer warriors called Sofloats – the mortal enemies of Soints. The power of each warrior is determined by the amount of chakra he possesses which is some positive integer. Warriors with zero level of chakra are dead warriors :) When the fight between Soint with power CI and Sofloat with power CF occurs the warrior with lower power will die and the winner will lose the amount of chakra that his enemy have possessed before the fight. So three cases are possible: - CI > CF. Then Sofloat will die while the new power of Soint will be CI – CF. - CI < CF. Then Soint will die while the new power of Sofloat will be CF – CI. - CI = CF. In this special case both warriors die. Each warrior (Soint or Sofloat) has his level of skills which is denoted by some positive integer. The fight between two warriors can occur only when these warriors are Soint and Sofloat of the same level. In particual, friendly fights are not allowed, i.e., a Soint cannot fight with another Soint and the same holds for Sofloats. Lets follow the following convention to denote the warriors. A Soint of level L and power C will be denoted as (I, C, L), while Sofloat of level L and power C will be denoted as (F, C, L). Consider some examples. If A = (I, 50, 1) fights with B = (F, 20, 1), B dies and A becomes (I, 30, 1). On the other hand, (I, 50, 1) cannot fight with (F, 20, 2) as they have different levels. There is a battle between Soints and Sofloats. There are N Soints and M Sofloats in all. The battle will consist of series of fights. As was mentioned above in each fight one Soint and one Sofloat of the same level take part and after the fight the warrior with lower power will die (or both will die if they have the same power). The battle proceeds as long as there exists at least one pair of warriors who can fight. The distribution of warriors by levels satisfies the following condition: for every Soint of level L there exists at least one Sofloat of the same level L and vice-versa. So if for some level L we have at least one warrior of this level then there is at least one Soint of level L and at least one Sofloat of level L. There is a powerful wizard, whose name is SoChef, on the side of Soints. He can increase the amount of chakra of each Soint by any number. SoChef wants the army of Soints to win this battle. But increasing amount of chakra of any Soint by one costs him a lot of his magic power. Hence he wants to minimize the total amount of additional chakra he should give to Soints in order for them to win. Note, however, that the win here means that all Sofloats should be dead irregardless of whether any Soint is alive. Also note that the battle can proceed by different scenarios and the SoChef need to distribute additional chakra among the Soints in such a way that they will win for any possible battle scenario. Help SoChef and find the minimal amount of additional chakra he should give to Soints in order for them to win. -----Input----- The first line of the input contains an integer T, the number of test cases. T test cases follow. The first line of each test case contains two space separated integers N and M. Here N is the number of Soints participating in the battle and M is the number of Sofloats for the same. Each of the next N lines contains two space separated integers Ci and Li, the amount of chakra and level of i-th Soint correspondingly. The next M lines describe power and level of Sofloats participating in the battle in the same format. -----Output----- For each test case output a single integer on a single line, the minimum amount of chakra SoChef should give to Soints in order for them to win the battle. -----Constraints----- Each integer in the input file is positive and does not exceed 100. That is1 ≤ T ≤ 100 1 ≤ N ≤ 100 1 ≤ M ≤ 100 1 ≤ Ci ≤ 100 1 ≤ Li ≤ 100 For every Soint of level L there exists at least one Sofloat of the same level L and vice-versa. It is guaranteed that each official test file will satisfy all these constraints. You DON'T need to verify them in your program. -----Example----- Input: 2 2 3 10 1 20 2 5 2 5 2 18 1 5 5 73 87 69 13 36 36 77 46 43 93 49 46 74 93 78 87 99 13 59 36 Output: 8 89 -----Explanation----- Case 1. The warriors are I1 = (I, 10, 1), I2 = (I, 20, 2), F1 = (F, 5, 2), F2 = (F, 5, 2), F3 = (F, 18, 1). Without the SoChef help the battle can proceed as follows. - I2 fights with F1, F1 dies, I2 becomes (I, 15, 2). - I2 fights with F2, F2 dies, I2 becomes (I, 10, 2). - I1 fights with F3, I1 dies, F3 becomes (F, 8, 1). So if SoChef will give 8 additional units of chakra to I1 the Soints will win the battle and even one Soint (I2) will left alive. Hence the answer is 8. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=eval(input()) for i in range(0,t): x=input() nm=x.split(' ') nm[0]=int(nm[0]) nm[1]=int(nm[1]) csoint=[] lsoint=[] csofloat=[] lsofloat=[] for j in range(0,nm[0]): a=input() b=a.split(' ') b[0]=int(b[0]) b[1]=int(b[1]) csoint.append(b[0]) lsoint.append(b[1]) for k in range(0,nm[1]): a=input() b=a.split(' ') b[0]=int(b[0]) b[1]=int(b[1]) csofloat.append(b[0]) lsofloat.append(b[1]) chakra=0 ## for j in range(0,nm[0]): ## if csofloat==[0]*nm[1]: ## print chakra ## break ## for k in range(0,nm[1]): ## if csofloat==[0]*nm[1]: ## print chakra ## break ## if lsoint[j]==lsofloat[k]: ## if csoint[j]>csofloat[k]: ## csoint[j]-=csofloat[k] ## csofloat[k]=0 ## lsofloat[k]=0 ## elif csofloat[k]>csoint[j]: ## if lsoint.count(lsoint[j])==1==lsofloat.count(lsofloat[k]): ## chakra+=csofloat[k]-csoint[j] ## csoint[j]=csofloat[k] ## csoint[j]=0 ## csofloat[k]=0 ## lsoint[j]=0 ## lsofloat[k]=0 ## elif lsoint.count(lsoint[j])==1 and lsofloat.count(lsofloat[k])>lsoint.count(lsoint[j]): ## csoint[j]=csofloat[k]+1 ## chakra+=csofloat[k]-csoint[j]+1 ## csoint[j]=1 ## csofloat[k]=0 ## lsofloat[k]=0 ## else: ## csofloat[k]-=csoint[j] ## csoint[j]=0 ## lsoint[j]=0 ## break ## else: ## if lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])>=lsofloat.count(lsofloat[k]): ## csoint[j]=0 ## csofloat[k]=0 ## lsoint[j]=0 ## lsofloat[k]=0 ## break ## elif lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])<lsofloat.count(lsofloat[k]): ## csoint[j]=1 ## chakra+=1 ## csofloat[k]=0 ## lsofloat[k]=0 l=min(lsoint)-1 ci=[] for a in range(l+1,max(lsoint)+1): c=0 l+=1 if l not in lsoint: continue for j in range(0,nm[0]): if lsoint[j]==l: c+=csoint[j] ci.append(c) l=min(lsofloat)-1 cf=[] for a in range(l+1,max(lsofloat)+1): c=0 l+=1 if l not in lsofloat: continue for j in range(0,nm[1]): if lsofloat[j]==l: c+=csofloat[j] cf.append(c) for i in range(0,len(ci)): if ci[i]<cf[i]: chakra+=cf[i]-ci[i] print(chakra) ```
{ "language": "python", "test_cases": [ { "input": "2\n2 3\n10 1\n20 2\n5 2\n5 2\n18 1\n5 5\n73 87\n69 13\n36 36\n77 46\n43 93\n49 46\n74 93\n78 87\n99 13\n59 36\n\n\n", "output": "8\n89\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/OCT12/problems/DRGNBOOL" }
vfc_2854
false
false
false
false
false
apps
verifiable_code
1004
Solve the following coding problem using the programming language python: A group of rebels travelling on a square hoverboard is ambushed by Imperial Stormtroopers.Their big hoverboard is an easy target, so they decide to split the board into smaller square hoverboards so that they can bolt away easily.But they should also make sure they don't get too spread out.Help the rebels split the craft into minimum number of smaller crafts possible. -----Input----- A single integer N denoting the side length of the big hoverboard. -----Output----- In the first line, output the integer 'k' which is the minimum number of square boards into which the bigger board can be split up. In the second line, output k space separated integers which denote the sizes of the smaller square hoverboards.This must be in increasing order of sizes. -----Constraints----- N ranges from 2 to 50. -----Example----- Input: 3 Output: 6 1 1 1 1 1 2 -----Explanation----- A square of side length 3 can be split into smaller squares in two ways: Either into 9 squares of side1 or 5 squares of side 1 and 1 square of size 2.The second case is the favourable one. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a=int(input()) if(a%2==0): print("4") print(a/2,a/2,a/2,a/2) else: print("6") print((a-1)/2,(a-1)/2,(a-1)/2,(a-1)/2,(a-1)/2,(a+1)/2) ```
{ "language": "python", "test_cases": [ { "input": "3\n", "output": "6\n1 1 1 1 1 2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/SOPC2015/problems/SOPC1505" }
vfc_2858
false
false
false
false
false
apps
verifiable_code
1005
Solve the following coding problem using the programming language python: Takaki Tono is a Computer Programmer in Tokyo. His boss at work shows him an online puzzle, which if solved would earn the solver a full expense paid trip to Los Angeles, California. Takaki really wants to solve this, as the love of his life, Akari, lives in Los Angeles and he hasn't met her since four years. Upon reading the puzzle he realizes that it is a query based problem. The problem is as follows :- You are given a Tree T with N nodes numbered from 1 to N, with each node numbered z having a positive integer Az written on it. This integer denotes the value of the node. You have to process Q queries, of the following forms :- 1) C x y : Report the closest two values in the unique path from x to y i.e compute min(|Ap - Aq|) where p and q are two distinct nodes on the unique path from x to y. 2) F x y : Report the farthest two values in the unique path from x to y i.e. compute max(|Ap - Aq|) where p and q are two distinct nodes on the unique path from x to y. It is also mentioned that x is not equal to y in any query and that no two nodes have the same value printed on them. Also, |x| denotes the absolute value of x. Takaki is perplexed and requires your help to solve this task? Can you help him out? -----Input----- The first line of the input contains an integer N denoting the number of nodes in tree T. The second line comprises N space separated integers denoting A, where the i-th integer denotes Ai. The next N-1 lines each comprise two space separated integers u and v, denoting that node u and node v are connected by an edge. It is guaranteed that the final graph will be a connected tree. The next line contains a single integer Q, denoting number of queries. The next Q lines comprise the queries. Each such line is of the format C x y or F x y. -----Output----- For each query, print the required output as mentioned above. -----Constraints----- - 2 ≤ N ≤ 35000 - 1 ≤ Ai ≤ 109 - 1 ≤ Q ≤ 35000 - 1 ≤ u, v ≤ N - No two nodes have the same value printed on them. - x is not equal to y in any query. -----Subtasks----- -----Subtasks-----Subtask #1 (15 points) - N, Q ≤ 1000Subtask #2 (20 points) - Only Type F queries are present.Subtask #3 (65 points) - Original constraints -----Example----- Input:5 1 2 7 4 5 1 2 2 3 2 4 2 5 7 C 1 5 F 1 5 C 2 4 C 1 2 F 1 3 F 3 4 F 2 4 Output:1 4 2 1 6 5 2 -----Explanation----- Given below is the tree corresponding to the sample input. Each node has two numbers written in it. The first number represents the node index and the second number indicates node value. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python MAXX = 10**9+1 N = eval(input()) nodes = list(map(int, input().split(" "))) edges = [set() for _ in range(N)] for _ in range(N-1): a, b = list(map(int, input().split(" "))) edges[a-1].add(b-1) edges[b-1].add(a-1) path = [[] for _ in range(N)] visited, tovisit = set(), [(0, 0)] while tovisit: p, v = tovisit.pop() if v not in visited: path[v] = path[p] + [v] visited.add(v) news = edges[v] - visited tovisit.extend([(v, x) for x in news]) # print path Q = eval(input()) for _ in range(Q): q, a, b = input().split(" ") a, b = int(a)-1, int(b)-1 i = 1 while i < min(len(path[a]), len(path[b])): if path[a][i] != path[b][i]: break i += 1 s = path[a][i-1:] + path[b][i:] # print s if q == "C": s = sorted([nodes[i] for i in s]) d = s[-1] - s[0] for i in range(len(s)-1): d = min(d, s[i+1]-s[i]) print(d) else: M = 0 m = MAXX for i in range(len(s)): M = max(M, nodes[s[i]]) m = min(m, nodes[s[i]]) print(M - m) # print M[(s, l)] - m[(s, l)] ```
{ "language": "python", "test_cases": [ { "input": "5\n1 2 7 4 5\n1 2\n2 3\n2 4\n2 5\n7\nC 1 5\nF 1 5\nC 2 4\nC 1 2\nF 1 3\nF 3 4\nF 2 4\n", "output": "1\n4\n2\n1\n6\n5\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/LTIME38/problems/CLOSEFAR" }
vfc_2862
false
false
false
false
true
apps
verifiable_code
1006
Solve the following coding problem using the programming language python: Chef Leonardo has a decimal integer $N$ and a non-zero decimal digit $d$. $N$ does not contain the digit zero; specifically, $N$ should always be treated as a decimal integer without leading zeroes. Chef likes $d$ and does not like any other digit, so he decided to change $N$. He may apply the following operation any number of times (including zero): append the digit $d$ to the decimal representation of $N$ ($d$ becomes the least significant digit of $N$), then remove one occurrence of one digit from the decimal representation of $N$. Chef has already changed a lot of numbers and he got bored. Now, he wants to know the smallest possible value of $N$ which can be obtained by performing the operation described above. Can you help him? -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $N$ and $d$. -----Output----- For each test case, print a single line containing one integer - the minimum number Chef can obtain. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 10^{18}$ - $N$ does not contain the digit $0$ - $1 \le d \le 9$ -----Subtasks----- Subtask #1 (40 points): - $1 \le T \le 100$ - $1 \le N \le 10^9$ Subtask #2 (60 points): original constraints -----Example Input----- 3 35 4 42 4 24 9 -----Example Output----- 34 24 24 -----Explanation----- Example case 1: Chef can remove the digit $5$ and add $4$ to the end of $N$. Then, $N$ becomes $34$. Example case 2: Chef can remove the digit $4$ from the beginning of $N$, add $4$ to the end of $N$ and get $N = 24$. Example case 3: Chef cannot make his number any smaller. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for _ in range(int(input())): n,d=map(str,input().split()) k=list(n) dd,c,n=d,0,len(n) for x in range(n): if int(k[n-x-1])>int(d): k.pop(n-x-1) c+=1 else: d=k[n-x-1] print(''.join(k)+c*dd) ```
{ "language": "python", "test_cases": [ { "input": "3\n35 4\n42 4\n24 9\n", "output": "34\n24\n24\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CHDIGER" }
vfc_2866
false
false
false
false
false
apps
verifiable_code
1007
Solve the following coding problem using the programming language python: Given an array A1,A2...AN, you have to print the size of the largest contiguous subarray such that GCD of all integers in that subarray is 1. Formally, For a subarray Ai,Ai+1...Aj where 1 ≤ i < j ≤ N to be valid: GCD(Ai,Ai+1...Aj) should be 1. You have to print the size of the largest valid subarray. If no valid subarray exists, output -1. Note:A single element is not considered as a subarray according to the definition of this problem. -----Input----- First line contains T, the number of testcases. Each testcase consists of N in one line followed by N integers in the next line. -----Output----- For each testcase, print the required answer in one line. -----Constraints----- - 1 ≤ T ≤ 10 - 2 ≤ N ≤ 105 - 1 ≤ Ai ≤ 105 -----Example----- Input: 2 2 7 2 3 2 2 4 Output: 2 -1 -----Explanation----- Example case 1.GCD(2,7)=1. So the subarray [A1,A2] is valid. Example case 2.No subarray satisfies. Note: Use scanf/print instead of cin/cout. Large input files. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math for _ in range(int(input())): n=int(input()) ar=[int(x) for x in input().split()] # dp=[1]*n f=0 g=ar[0] for i in range(1,n): g=math.gcd(g,ar[i]) if g==1: f=1 print(n) break if f==0: print(-1) ```
{ "language": "python", "test_cases": [ { "input": "2\n2\n7 2\n3\n2 2 4\n", "output": "2\n-1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/SUBGCD" }
vfc_2870
false
false
false
false
false
apps
verifiable_code
1008
Solve the following coding problem using the programming language python: You are given an array A consisting of N integers. A group of disjoint subarrays in it will be a collection of subarrays of the array. Formally a group of subarrays consisting of K subarrays can be denoted by 2 * K indices, [i1, j1], [i2, j2] , ..., [iK, jK], such that i1 ≤ j1 < i2 ≤ j2 < ... < iK ≤ jK. For example, in array A = {5, 6, 7}. A group of subarrays could be the subarrays denoted by indices [1, 1], [2, 3]. The subarray corresponding to indices [1, 1] will be {5}. The subarray corresponding to indices [2, 3] will be {6, 7}. So, we have i1 = 1, j1 = 1, i2 = 2, j2 = 3 and K = 2. You can check that the indices satisfy the property i1 ≤ j1 < i2 ≤ j2. Note that the group of subarrays [1, 2] and [2, 3] won't be disjoint, as it does not satisfy the property j1 < i2. In other words, the index 2 is common in two subarrays, which should not happen. Let M denote the maximum value of K in a group of K disjoint subarrays of array A, such that there are not two elements (not indices) common in those subarrays. This means, that if the group contained subarrays [A[i1], A[j1], [A[i2], A[j2]] , ..., A[[iK, jK]], then there should not be an element which is present in more than one subarrays. You have to find maximum number of group of disjoint subarrays that number of subarrays in those groups are equal to M. As the answer could be large, output it modulo 109 + 7. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. First line of the each test case contains a single integer N denoting number of elements in array A. Second line of each test case contains N space separated integers denoting the elements of the array A -----Output----- For each test case, output a single line corresponding to maximum number of group of disjoint subarrays of array A. -----Constraints----- - 1 ≤ T, N ≤ 105 - 1 ≤ Ai ≤ n - Sum of N over all the test cases in a single file won't exceed 105 -----Subtasks----- Subtask #1 (30 points) - 1 ≤ T, N ≤ 103 - 1 ≤ Ai ≤ n - Sum of N over all the test cases in a single file won't exceed 103 Subtask #2 (70 points) - original constraints -----Example----- Input 3 2 3 4 3 1 2 2 5 1 1 2 2 2 Output: 1 3 18 -----Explanation----- Example case 1. M will be equal to 2. The subarrays will be {[1, 1], [2, 2]}. Example case 2. M will be equal to 3. The subarrays will be {[1, 1], [2, 2]}, {[1, 1], [3, 3]} and {[1, 1], [2, 3]}. Note that {[2, 2], [3, 3]} won't be a non-intersecting subarray as A[2] = 2 and A[3] = 2. So, 2 is common in both these subarrays. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=int(input()) for k in range(t): n=int(input()) l=[int(i) for i in input().split()] m={} count=1 for i in range(1,n): if l[i]==l[i-1]: count+=1 else: if l[i-1] not in m: m[l[i-1]]=(count*(count+1))/2 else: m[l[i-1]]+=(count*(count+1))/2 count=1 if(l[n-1]) not in m: m[l[n-1]]=(count*(count+1))/2 else: m[l[n-1]]+=(count*(count+1))/2 s=1 for x in m: s=(s*m[x])%(1000000007) print(s) ```
{ "language": "python", "test_cases": [ { "input": "3\n2\n3 4\n3\n1 2 2\n5\n1 1 2 2 2\n\n\n", "output": "1\n3\n18\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/MXSUBARR" }
vfc_2874
false
false
false
false
false
apps
verifiable_code
1009
Solve the following coding problem using the programming language python: Chef is playing a game on a sequence of $N$ positive integers, say $A_1, A_2, ... A_N$ The game is played as follows. - If all the numbers are equal, the game ends. - Otherwise - Select two numbers which are unequal - Subtract the smaller number from the larger number - Replace the larger number with the result from above Chef has already figured out that the game always terminates. He also knows, for a given sequence of integers, the game will always terminate on the same value, no matter how the game is played. Chef wants you to simulate the game for him and tell him if the game terminates on $1$. In fact, there may be many such games. Given a sequence of integers Chef wants to know the number of sub-sequences of the given sequence, for which, playing the above game on the subsuquence will terminate on $1$. A sub-sequence can be obtained from the original sequence by deleting $0$ or more integers from the original sequence. See the explanation section for clarity. -----Input----- - The first line of the input contains an integer $T$, the number of test cases. Then follow the description of $T$ test cases. - The first line of each test case contains a single integer $N$, the length of the sequence. - The second line contains $N$ positive integers, each separated by a single space. -----Output----- For each test case, output a single integer - the number of sub-sequences of the original sequence, such that, playing the game on the sub-sequence results in ending the game with all the values equal to $1$. -----Constraints----- - $1 \le T \le 100$ - $1 \le N \le 60$ - $1 \le A_i \le 10^4$ - All $A_i$ will be distinct. -----Sample Input----- 3 4 2 3 5 7 4 3 4 8 16 3 6 10 15 -----Sample Output----- 11 7 1 -----Explanation----- Test Case 1: The following $11$ sub-sequences are counted. - $\{ 2, 3 \}$ - $\{ 2, 5 \}$ - $\{ 2, 7 \}$ - $\{ 3, 5 \}$ - $\{ 3, 7 \}$ - $\{ 5, 7 \}$ - $\{ 2, 3, 5 \}$ - $\{ 2, 3, 7 \}$ - $\{ 2, 5, 7 \}$ - $\{ 3, 5, 7 \}$ - $\{ 2, 3, 5, 7 \}$ Test Case 2: The following $7$ sub-sequences are counted. - $\{ 3, 4 \}$ - $\{ 3, 8 \}$ - $\{ 3, 16 \}$ - $\{ 3, 4, 8 \}$ - $\{ 3, 4, 16 \}$ - $\{ 3, 8, 16 \}$ - $\{ 3, 4, 8, 16 \}$ Test Case 3: There are $8$ subsequences of $\{ 6, 10, 15 \}$ - $\{\}$ => The game cannot be played on this sub-sequence - $\{ 6 \}$ => The game cannot be played on this sub-sequence - $\{ 10 \}$ => The game cannot be played on this sub-sequence - $\{ 15 \}$ => The game cannot be played on this sub-sequence - $\{ 6, 10 \}$ => The game cannot end at $\{ 1, 1 \}$ - $\{ 6, 15 \}$ => The game cannot end at $\{ 1, 1 \}$ - $\{ 10, 15 \}$ => The game cannot end at $\{ 1, 1 \}$ - $\{ 6, 10, 15 \}$ => The game ends at $\{ 1, 1, 1 \}$. Hence this is the only sub-sequence that is counted in the result. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here from sys import stdin import functools def gcd(a, b): if (a == 0): return b return gcd(b % a, a) MAX=10001 def func(ind, g, dp, n, a): if (ind == n): if (g == 1): return 1 else: return 0 if (dp[ind][g] != -1): return dp[ind][g] ans = (func(ind + 1, g, dp, n, a) + func(ind + 1, gcd(a[ind], g), dp, n, a)) dp[ind][g] = ans return dp[ind][g] def countSubsequences(a, n): dp = [[-1 for i in range(MAX)] for i in range(n)] count = 0 for i in range(n): count += func(i + 1, a[i], dp, n, a) return count test=int(stdin.readline()) for _ in range(test): n=int(stdin.readline()) list1=list(map(int,stdin.readline().split())) print(countSubsequences(list1,n)) ```
{ "language": "python", "test_cases": [ { "input": "3\n4\n2 3 5 7\n4\n3 4 8 16\n3\n6 10 15\n", "output": "11\n7\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/AMSGAME2" }
vfc_2878
false
false
false
false
false
apps
verifiable_code
1010
Solve the following coding problem using the programming language python: Anmol gained a lot of weight last semester. So this semester, he decided to run everyday. There is a very long straight road starting at his hostel. There are N poles on the road - P1, P2, P3,..., PN on the road. All the poles lie on the same side of his hostel. The distance between Pi and his hostel is Di. For 1 ≤ i, j ≤ N, i < j implies Di < Dj Everyday, Anmol chooses a pole Pi to start running from. He keeps on running until he reaches Pi+K. Whenever he reaches a pole (other than the starting pole), he records the distance traveled since the last pole. You are given the distances recorded by him today. Your task is to find the number of distinct values of i such that i + K ≤ N and if he starts at Pi and end at Pi+K, he would end up having exactly the same record of distances (in the same order). -----Input----- - The first line of the input contains an integer T denoting the number of test cases. - The first line of each test case contains two space separated integers N and K. - The next line contains N space separated integers D1, D2,..., DN. - The next line contains K space separated integers representing the distances recorded by Anmol in the same order. . -----Output----- - For each test case, output a single line containing the answer for that test case. -----Constraints----- - 1 ≤ T ≤ 10 - 2 ≤ N ≤ 5 x 104 - 1 ≤ K < N - 1 ≤ Di ≤ 106 -----Subtasks----- -----Subtask #1 (20 points)----- - 1 ≤ N ≤ 1000 -----Subtask #2 (80 points)----- - Original constraints -----Example----- Input: 3 5 1 1 5 10 12 14 5 5 2 5 8 13 16 21 3 5 5 3 2 6 8 11 16 2 3 5 Output: 1 2 1 -----Explanation----- Example case 1. If he runs from P2 to P3, he will record (5) Example case 2. He can start at P1 or P3 Example case 3. He can start at P2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #code snippet reference:http://www.geeksforgeeks.org/searching-for-patterns-set    -2-kmp-algorithm/ def KMPMatch(pattern, string): M = len(pattern) N = len(string) nonlocal ans lps = [0]*M j = 0 LPSCompute(pattern, M, lps) i = 0 while i < N: if pattern[j] == string[i]: i+=1 j+=1 if j==M: ans+=1 j = lps[j-1] elif i < N and pattern[j] != string[i]: if j != 0: j = lps[j-1] else: i+=1 def LPSCompute(pattern, M, lps): len = 0 lps[0] i = 1 while i < M: if pattern[i]==pattern[len]: len+=1 lps[i] = len i+=1 else: if len!=0: len = lps[len-1] else: lps[i] = 0 i+=1 for _ in range(int(input())): n,k=list(map(int,input().split())) s=list(map(int,input().split())) pattern=list(map(int,input().split())) ans=0 string=[] for i in range(n-1): string.append(s[i+1]-s[i]) KMPMatch(pattern, string) print(ans) ```
{ "language": "python", "test_cases": [ { "input": "3\n5 1\n1 5 10 12 14\n5\n5 2\n5 8 13 16 21\n3 5\n5 3\n2 6 8 11 16\n2 3 5\n", "output": "1\n2\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/LOCAPR16/problems/RUNNING" }
vfc_2882
false
false
false
false
true
apps
verifiable_code
1011
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 capital letters. You just received a message given by a string s. You don't know whether this message is sent by Chef or his brother. Also, the communication channel through which you received the message is erroneous and hence can flip a letter from uppercase to lowercase or vice versa. However, you know that this channel can make at most K such flips. Determine whether the message could have been sent only by Chef, only by the little brother, by both or by none. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K denoting the length of the string s and the maximum number of flips that the erroneous channel can make. - The second line contains a single string s denoting the message you received. -----Output----- For each test case, output a single line containing one string — "chef", "brother", "both" or "none". -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 100 - 0 ≤ K ≤ N - s consists only of (lowercase and uppercase) English letters -----Example----- Input 4 5 1 frauD 5 1 FRAUD 4 4 Life 10 4 sTRAWBerry Output chef brother both none -----Explanation----- Example case 1: Only one flip is possible. So it is possible that Chef sent "fraud" and the channel flipped the last character to get "frauD". However, it is not possible for the brother to have sent "FRAUD", because then it would need 4 flips. Hence the answer is "chef". Example case 2: Only one flip is possible. So it is possible that the brother sent "FRAUD" and the channel didn't flip anything. However, it is not possible for Chef to have sent "fraud", because then it would need 5 flips. Hence the answer is "brother". Example case 3: Four flips are allowed. It is possible that Chef sent "life" and the channel flipped the first character to get "Life". It is also possible that the brother sent "LIFE" and the channel flipped the last three characters to get "Life". Hence the answer is "both". Example case 4: Four flips are allowed. It is not possible that Chef sent "strawberry", because it would need five flips to get "sTRAWBerry". It is also not possible that the brother sent "STRAWBERRY", because that would also need five flips. Hence the answer is "none". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```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') return for i in range(t): do() ```
{ "language": "python", "test_cases": [ { "input": "4\n5 1\nfrauD\n5 1\nFRAUD\n4 4\nLife\n10 4\nsTRAWBerry\n", "output": "chef\nbrother\nboth\nnone\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/GOODBAD" }
vfc_2886
false
false
false
false
false
apps
verifiable_code
1012
Solve the following coding problem using the programming language python: Alice likes prime numbers. According to Alice, only those strings are nice whose sum of character values at a prime position is prime. She has a string $S$. Now, she has to count the number of nice strings which come before string $S$( including $S$) in the dictionary and are of the same length as $S$. Strings are zero-indexed from left to right. To find the character value she uses the mapping {'a': 0, 'b':1, 'c':2 ……. 'y': 24, 'z':25} . For example, for string $abcde$ Characters at prime positions are $'c'$ and $'d'$. c + d = 2 + 3 = 5. Since, 5 is a prime number, the string is $nice$. Since there could be many nice strings print the answer modulo $10^{9}+7$. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, the string $S$. -----Output:----- For each testcase, output in a single line number of nice strings modulo $10^{9}+7$. -----Constraints----- - $1 \leq T \leq 10$ - $2 \leq |S| \leq 10^2$ String $S$ contains only lowercase letters. -----Sample Input:----- 1 abc -----Sample Output:----- 10 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys from collections import defaultdict from copy import copy MOD = 10**9 + 7 R = lambda t = int: t(input()) RL = lambda t = int: [t(x) for x in input().split()] RLL = lambda n, t = int: [RL(t) for _ in range(n)] # primes up to n def primes(n): P = [] n = int(n) U = [1] * (n+1) p = 2 while p <= n: if U[p]: P += [p] x = p while x <= n: U[x] = 0 x += p p += 1 return P def solve(): S = R(str).strip() X = [ord(c)-ord('a') for c in S] P = primes(10000) L = defaultdict(lambda : 0) s = 0 for i in range(len(S)): p = i in P NL = defaultdict(lambda : 0) for a in range(26): for l in L: NL[l + a * p] += L[l] for a in range(X[i]): NL[s + a * p] += 1 s += X[i] * p L = NL L[s] += 1 r = 0 for p in P: r += L[p] print(r % MOD) T = R() for t in range(1, T + 1): solve() ```
{ "language": "python", "test_cases": [ { "input": "1\nabc\n", "output": "10\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ENJU2020/problems/ECJN207" }
vfc_2890
false
false
false
false
false
apps
verifiable_code
1013
Solve the following coding problem using the programming language python: Navnit is a college student and there are $N$ students in his college .Students are numbered from $1$ to $N$. You are given $M$ facts that "Student $A_i$ and $B_i$".The same fact can be given multiple times .If $A_i$ is a friend of $B_i$ ,then $B_i$ is also a friend of $A_i$ . If $A_i$ is a friend of $B_i$ and $B_i$ is a friend of $C_i$ then $A_i$ is also a friend of $C_i$. Find number of ways in which two students can be selected in such a way that they are not friends. -----Input:----- - First line will contain two integers $N$ and $M$. - Then $M$ lines follow. Each line contains two integers $A_i$ and $B_i$ denoting the students who are friends. -----Output:----- For each testcase, output the number of ways in which two students can be selected in such a way that they are friends. -----Constraints----- - $2 \leq N \leq 200000$ - $0 \leq M \leq 200000$ - $1 \leq A_i,B_i \leq N$ -----Sample Input:----- 5 3 1 2 3 4 1 5 -----Sample Output:----- 6 -----EXPLANATION:----- Groups of friend are $[1,2,5]$ and $[3,4]$.Hence the answer is 3 X 2 =6. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here from collections import defaultdict d=defaultdict(list) def dfs(i): p=0 nonlocal v e=[i] while(e!=[]): p+=1 x=e.pop(0) v[x]=1 for i in d[x]: if v[i]==-1: v[i]=1 e.append(i) return p n,m=list(map(int,input().split())) for i in range(n+1): d[i]=[] for _ in range(m): a,b=list(map(int,input().split())) d[a].append(b) d[b].append(a) v=[] for i in range(n+1): v.append(-1) c=0 p=[] for i in range(1,n+1): if v[i]==-1: c+=1 p.append(dfs(i)) an=0 s=0 for i in range(c): s+=p[i] an+=p[i]*(n-s) print(an) ```
{ "language": "python", "test_cases": [ { "input": "5 3\n1 2\n3 4\n1 5\n", "output": "6\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/INDC2021/problems/NAV28" }
vfc_2894
false
false
false
false
false
apps
verifiable_code
1014
Solve the following coding problem using the programming language python: A number K$K$ is said to be magical if it can be represented as a power of 2 only.That is K$K$=2x$2^{x}$ for some natural number x$x$. Given a string of digits S$S$ of length N$N$, Let P be a valid arrangement of S. By valid arrangement we mean that it should not have any leading zeroes. Find the sum of all such distinct Pi's, which, when treated as a number is magical. Two arrangements are said to be distinct, if there is atleast one index where they differ. As the sum can be very large, print the sum modulo 109+7$10^{9}+7$. -----Input:----- -The first line of the input contains a single integer T$T$ denoting the number of test cases. -Only line of each test case contains a string S$S$ of length N$N$, consisting only of digits between 0 to 9. -----Output:----- For each test case, print a single integer denoting the sum of all such magical Pi′s$Pi's$ modulo 109+7$10^{9}+7$. If no such Pi$Pi$ exists print "-1". -----Constraints----- - 1≤T≤1000$1 \leq T \leq 1000$ - 2≤N≤1000$2 \leq N \leq 1000$ - String only consists of digits between 0 to 9, both inclusive. -----Subtasks----- - 10 points : 1≤N≤5$1 \leq N \leq 5$ - 40 points : 1≤N≤100$1 \leq N \leq 100$ - 50 points : Original Constraints -----Sample Input:----- 2 35566 31 -----Sample Output:----- 65536 -1 -----EXPLANATION:----- Only arrangement is 65536. No arrangement of 31 gives us a power of two. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import log2, ceil MOD = int(1e9 + 7) srt = lambda s: ''.join(sorted(s)) for _ in range(int(input())): s = srt(input()) res = -1 for p in range(ceil(log2(int(s))), int(log2(int(s[::-1]))) + 1): if int(srt(str(pow(2, p)))) == int(s): if res == -1: res = 0 res = (res + pow(2, p, MOD)) % MOD print(res) ```
{ "language": "python", "test_cases": [ { "input": "2\n35566\n31\n", "output": "65536\n-1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ALQU2018/problems/POWTWO" }
vfc_2898
false
false
false
false
false
apps
verifiable_code
1015
Solve the following coding problem using the programming language python: The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 4 1 2 3 4 -----Sample Output:----- 2 24 68 246 81012 141618 2468 10121416 18202224 26283032 -----EXPLANATION:----- No need, else pattern can be decode easily. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here try: for _ in range(int(input())): k = int(input()) num = 1 for i in range(1,k+1,1): for j in range(1,k+1,1): print(num*2,end="") num = num +1 print("") except: pass ```
{ "language": "python", "test_cases": [ { "input": "4\n1\n2\n3\n4\n", "output": "2\n24\n68\n246\n81012\n141618\n2468\n10121416\n18202224\n26283032\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PTRN2021/problems/ITGUY50" }
vfc_2902
false
false
false
false
false
apps
verifiable_code
1016
Solve the following coding problem using the programming language python: You have been recently hired as a developer in CodeChef. Your first mission is to implement a feature that will determine the number of submissions that were judged late in a contest. There are $N$ submissions, numbered $1$ through $N$. For each valid $i$, the $i$-th submission was submitted at time $S_i$ and judged at time $J_i$ (in minutes). Submitting and judging both take zero time. Please determine how many submissions received their verdicts after a delay of more than $5$ minutes. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of the input contains a single integer $N$. - $N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $S_i$ and $J_i$. -----Output----- For each test case, print a single line containing one integer — the number of submissions for which the judging was delayed by more than 5 minutes. -----Constraints----- - $1 \le T \le 100$ - $1 \le N \le 100$ - $1 \le S_i \le J_i \le 300$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 1 5 1 3 4 4 4 10 1 11 2 7 -----Example Output----- 2 -----Explanation----- Example case 1: The delays of the respective submissions are $2$ minutes, $0$ minutes, $6$ minutes, $10$ minutes and $5$ minutes. Only submissions $3$ and $4$ are delayed by more than $5$ minutes, hence the answer is $2$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for t in range(int(input())): c=0 for i in range(int(input())): s,j=list(map(int,input().split())) if (j-s)>5: c+=1 print(c) ```
{ "language": "python", "test_cases": [ { "input": "1\n5\n1 3\n4 4\n4 10\n1 11\n2 7\n", "output": "2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/JDELAY" }
vfc_2906
false
false
false
false
false
apps
verifiable_code
1017
Solve the following coding problem using the programming language python: Chef recently started working at ABC corporation. Let's number weekdays (Monday through Friday) by integers $1$ through $5$. For each valid $i$, the number of hours Chef spent working at the office on weekday $i$ was $A_i$. Unfortunately, due to the COVID-19 pandemic, Chef started working from home and his productivity decreased by a considerable amount. As per Chef's analysis, $1$ hour of work done at the office is equivalent to $P$ hours of work done at home. Now, in order to complete his work properly, Chef has to spend more hours working from home, possibly at the cost of other things like sleep. However, he does not have to do the same work on each day as he would have in the office ― for each weekday, he can start the work for this day on an earlier day and/or complete it on a later day. The only requirement is that his work does not pile up indefinitely, i.e. he can complete his work for each week during the same week. One day has $24$ hours. If Chef is unable to complete his work for a week during those five weekdays, then he has to work during the weekend too. Chef wishes to know whether he has to work on weekends or if he can complete his work by working only on weekdays. Help him answer that question. (It is possible that Chef would be unable to finish his work even if he worked all the time, but he does not want to know about that.) -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains six space-separated integers $A_1$, $A_2$, $A_3$, $A_4$, $A_5$ and $P$. -----Output----- For each test case, print a single line containing the string "Yes" if Chef has to work on weekends or "No" otherwise (without quotes). -----Constraints----- - $1 \le T \le 1,000$ - $0 \le A_i \le 24$ for each valid $i$ - $1 \le P \le 24$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 14 10 12 6 18 2 10 10 10 10 10 3 -----Example Output----- No Yes -----Explanation----- Example case 1: Here, $P=2$, so the number of hours Chef has to work from home to handle his workload for days $1$ through $5$ is $[28,20,24,12,36]$. If he works for full $24$ hours on each of the five weekdays, he finishes all the work, so he does not have to work on weekends. Example case 2: No matter what Chef does, he will have to work on weekends. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for t in range(int(input())): a1,a2,a3,a4,a5,p=[int(x)for x in input().rstrip().split()] if (a1+a2+a3+a4+a5)*p >120: print("Yes") else: print("No") ```
{ "language": "python", "test_cases": [ { "input": "2\n14 10 12 6 18 2\n10 10 10 10 10 3\n", "output": "No\nYes\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/LOSTWKND" }
vfc_2910
false
false
false
false
false
apps
verifiable_code
1018
Solve the following coding problem using the programming language python: Chef has a garden with $N$ plants arranged in a line in decreasing order of height. Initially the height of the plants are $A_1, A_2, ..., A_N$. The plants are growing, after each hour the height of the $i$-th plant increases by $i$ millimeters. Find the minimum number of integer hours that Chef must wait to have two plants of the same height. -----Input:----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N$ space separated integers $A_1,A_2,..A_N$. -----Output:----- For each test case print a single line containing one integer, the minimum number of integer hours that Chef must wait to have two plants of the same height. -----Constraints----- - $1 \leq T \leq 1000$ - $2 \leq N \leq 10^5$ - $0\leq A_i \leq 10^{18}$ - $A_i >A_{i+1}$, for each valid $i$ - The Sum of $N$ over all test cases does not exceed $10^6$ -----Sample Input:----- 1 3 8 4 2 -----Sample Output:----- 2 -----EXPLANATION:----- After $2$ hours there are two plants with the same height. $[8,4,2] \rightarrow [9,6,5] \rightarrow [10,8,8]$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) hrs = arr[0] - arr[1] for i in range(1, n-1): if hrs > arr[i] - arr[i+1]: hrs = arr[i] - arr[i+1] print(hrs) ```
{ "language": "python", "test_cases": [ { "input": "1\n3\n8 4 2\n", "output": "2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/INFTINCR" }
vfc_2914
false
false
false
false
false
apps
verifiable_code
1019
Solve the following coding problem using the programming language python: The snakes want to build a temple for Lord Cobra. There are multiple strips of land that they are looking at, but not all of them are suitable. They need the strip of land to resemble a coiled Cobra. You need to find out which strips do so. Formally, every strip of land, has a length. Suppose the length of the i-th strip is is Ni, then there will be Ni integers, Hi1, Hi2, .. HiNi, which represent the heights of the ground at various parts of the strip, in sequential order. That is, the strip has been divided into Ni parts and the height of each part is given. This strip is valid, if and only if all these conditions are satisfied: - There should be an unique 'centre' part. This is where the actual temple will be built. By centre, we mean that there should be an equal number of parts to the left of this part, and to the right of this part. - Hi1 = 1 - The heights keep increasing by exactly 1, as you move from the leftmost part, to the centre part. - The heights should keep decreasing by exactly 1, as you move from the centre part to the rightmost part. Note that this means that HiNi should also be 1. Your job is to look at every strip and find if it's valid or not. -----Input----- - The first line contains a single integer, S, which is the number of strips you need to look at. The description of each of the S strips follows - The first line of the i-th strip's description will contain a single integer: Ni, which is the length and number of parts into which it has been divided. - The next line contains Ni integers: Hi1, Hi2, .., HiNi. These represent the heights of the various parts in the i-th strip. -----Output----- - For each strip, in a new line, output "yes" if is a valid strip, and "no", if it isn't. -----Constraints----- - 1 ≤ S ≤ 100 - 3 ≤ Ni ≤ 100 - 1 ≤ Hij ≤ 100 -----Example----- Input: 7 5 1 2 3 2 1 7 2 3 4 5 4 3 2 5 1 2 3 4 3 5 1 3 5 3 1 7 1 2 3 4 3 2 1 4 1 2 3 2 4 1 2 2 1 Output: yes no no no yes no no -----Explanation----- In the first strip, all the conditions are satisfied, hence it is valid. In the second strip, it does not start with a 1, and hence is invalid. In the third strip, it keeps increasing even past the centre, instead of decreasing. Hence invalid. The fourth strip does not increase and decrease by exactly 1. Hence invalid. The fifth satisfies all conditions and hence is valid. The sixth and seventh strip do not have a 'centre' part. Because for every part, there are either more parts to its right than its left, or more parts on its left than its right. Hence both the strips are invalid. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for i in range(int(input())): N=int(input()) L=list(map(int,input().split())) l,h=0,N-1 flag=1 if L[l]!=1 and L[h]!=1: flag=0 else: while(l<h): if (L[l]!=L[h]) or (L[l+1]-L[l]!=1 and L[h-1]-L[h]!=1): flag=0 break l+=1 h-=1 if flag: print("yes") else: print("no") ```
{ "language": "python", "test_cases": [ { "input": "7\n5\n1 2 3 2 1\n7\n2 3 4 5 4 3 2\n5\n1 2 3 4 3\n5\n1 3 5 3 1\n7\n1 2 3 4 3 2 1\n4\n1 2 3 2\n4\n1 2 2 1\n", "output": "yes\nno\nno\nno\nyes\nno\nno\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/TEMPLELA" }
vfc_2918
false
false
false
false
false
apps
verifiable_code
1020
Solve the following coding problem using the programming language python: Vanja and Miksi have already finished everything for this year at their university, so they decided to spend their free time playing a game with a binary sequence $A_1, A_2, \dots, A_N$ (i.e. a sequence containing only integers $0$ and $1$). At the beginning of the game, Vanja and Miksi write down the expression $* A_1 * A_2 * A_3 * \ldots * A_N$ (note below that each $*$ is a placeholder and does not denote multiplication). The players take turns alternately, starting with Vanja. The game lasts $N$ turns. In each turn, the current player must replace the first (leftmost) remaining $*$ by the sign $+$ or $-$ (or, equivalently, by the operation of addition or subtraction). After the last turn, the boys calculate the resulting value $V$ of the expression. If $|V| \ge K$, the winner is Vanja; otherwise, the winner is Miksi. Please predict the winner of the game if both players play optimally. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $K$. - The second line contains $N$ space-separated integers $A_1, A_2, \dots, A_N$. -----Output----- For each test case, print a single line containing one integer — $1$ if Vanja (the first player) is the winner or $2$ if Miksi (the second player) is the winner. -----Constraints----- - $1 \le T \le 50$ - $1 \le N \le 3 \cdot 10^4$ - $0 \le K \le 3 \cdot 10^4$ - $0 \le A_i \le 1$ for each valid $i$ -----Example Input----- 2 2 1 1 0 3 5 0 1 0 -----Example Output----- 1 2 -----Explanation----- Example case 1: $K = 1$, so if Vanja replaces the first $*$ by $+$, the value of the final expression will be $V = 1$; then, $K \le |V|$, so the winner is Vanja. Example case 2: $K = 5$, but the absolute value of the final expression cannot be greater than $1$. The winner is Miksi. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for _ in range(int(input())): n,k=list(map(int,input().split())) a=list(map(int,input().split())) m=0 for i in range(n): if i%2==0: if m<0: m-=a[i] else: m+=a[i] else: if m<0: m+=a[i] else: m-=a[i] if abs(m)>=k: print(1) else: print(2) ```
{ "language": "python", "test_cases": [ { "input": "2\n2 1\n1 0\n3 5\n0 1 0\n", "output": "1\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/MIXGA" }
vfc_2922
false
false
false
false
false
apps
verifiable_code
1021
Solve the following coding problem using the programming language python: The chef likes to play with numbers. He takes some integer number x, writes it down on his iPad, 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 iteration, Chef writes down the result on his iPad and replaces x with the result. So there will be n numbers on the iPad after all. You are given a sequence of length n — the numbers that Chef wrote down. This sequence is given in the order of the sequence can mismatch the order of the numbers written on the iPad. Your problem is to rearrange elements of this sequence in such a way that it can match a possible Chef's game in the order of the numbers written on the board. I.e. each next number will be exactly two times the previous number or exactly one-third of the previous number. I can give a guarantee that the answer exists. -----Input:----- - The first line of the input contains an integer number N i.e the number of the elements in the sequence. - The second line of the input contains n integer numbers a1,a2,…, an i.e rearranged (reordered) sequence that Chef can write down on the iPad. -----Output:----- Print N integer numbers — rearranged (reordered) input sequence that can be the sequence that Chef could write down on the iPad. It is guaranteed that the answer exists -----Constraints----- - $2 \leq N \leq 100$ - $1 \leq A[i] \leq 3* 10^{18} $ -----Sample Input:----- 6 4 8 6 3 12 9 -----Sample Output:----- 9 3 6 12 4 8 -----EXPLANATION:----- In the first example, the given sequence can be rearranged in the following way: [9,3,6,12,4,8]. It can match possible Polycarp's game which started with x=9. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python class Node: def __init__(self,x): self.x=x self.next=None self.prev=None self.flag=True for t in range(1): n=int(input()) arr=list(map(int,input().split())) for i in range(n): arr[i]=Node(arr[i]) for i in arr: d=[i.x%3==0,i.x,i.x//3,i.x*2] if d[0]: for j in arr: if j.x==d[2]: i.next=j j.prev=i break else: for j in arr: if j.x == d[3]: i.next = j j.prev = i break else: for j in arr: if j.x==d[3]: i.next=j j.prev=i break f,l=None,None for i in arr: if i.prev==None: f=i elif i.next==None: l=i while f!=l and l!=None: print(f.x,end=" ") f=f.next print(f.x) ```
{ "language": "python", "test_cases": [ { "input": "6\n4 8 6 3 12 9\n", "output": "9 3 6 12 4 8\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/BRBG2020/problems/THTOG" }
vfc_2926
false
false
false
false
false
apps
verifiable_code
1022
Solve the following coding problem using the programming language python: There are $N$ cities on a circle, numbered $1$ through $N$. For each $i$ ($1 \le i \le N-1$), cities $i$ and $i+1$ are directly connected by a bidirectional road with length $A_i$, and cities $N$ and $1$ are also directly connected by a bidirectional road with length $A_N$. However, we do not know the lengths of some roads. For each city $i$, we do know that it has an opposite city — formally, there is a city $j \neq i$ such that the clockwise distance between cities $i$ and $j$ is equal to the counterclockwise distance between these cities. Please find the lengths of all roads in such a way that the above condition is satisfied and the sum of lengths of all roads is minimised. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of the input contains a single integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \dots, A_N$. For each valid $i$, $A_i = -1$ denotes that the length of road $i$ is unknown. -----Output----- For each test case, print a line containing the string "NO" if there is no solution or "YES" otherwise. If a solution exists, print a second line containing $N$ space-separated positive integers — the lengths of all roads in your solution. Each of these integers should be $\le 10^9$. If there are multiple solutions, you may print any one. -----Constraints----- - $1 \le T \le 100$ - $3 \le N \le 10^5$ - $1 \le A_i \le 10^9$ or $A_i = -1$ for each valid $i$ - the sum of $N$ for all test cases does not exceed $3\cdot 10^5$ -----Subtasks----- Subtask #1 (10 points): $N \le 4$ Subtask #2 (20 points): $A_i = \pm 1$ for each valid $i$ Subtask #3 (70 points): original constraints -----Example Input----- 4 4 1 1 1 1 4 1 1 1 2 4 1 -1 -1 4 4 1 -1 2 -1 -----Example Output----- YES 1 1 1 1 NO YES 1 4 1 4 NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here # cook your dish here import math test=int(input()) for _ in range(test): n=int(input()) l=list(map(int,input().split())) f=0 for i in range(math.ceil(n//2)): if n%2==1: f=1 break else: if l[i]!=l[i+n//2]: if min(l[i],l[i+n//2])==-1: l[i]=max(l[i],l[i+n//2]) l[i+n//2]=max(l[i],l[i+n//2]) else: f=1 break else: if l[i]==-1: l[i]=1 l[i+n//2]=1 if f==1: print("NO") else: print("YES") print(*l) ```
{ "language": "python", "test_cases": [ { "input": "4\n4\n1 1 1 1\n4\n1 1 1 2\n4\n1 -1 -1 4\n4\n1 -1 2 -1\n", "output": "YES\n1 1 1 1\nNO\nYES\n1 4 1 4\nNO\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/OPPOSITE" }
vfc_2930
false
false
false
false
true
apps
verifiable_code
1023
Solve the following coding problem using the programming language python: The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 50$ - $1 \leq K \leq 50$ -----Sample Input:----- 5 1 2 3 4 5 -----Sample Output:----- 1 1 23 1 23 456 1 23 4 5 6789 1 23 4 5 6 7 89101112 -----EXPLANATION:----- No need, else pattern can be decode easily. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for _ in range(int(input())): n = int(input()) count = 1 l = 3*(n-1) i = 0 if n==1: print(1) continue while count<=l-n: for j in range(i+1): if j==i: print(count) count += 1 elif j==0: print(count,end="") count += 1 else: print(" ",end="") i+=1 while count<=l: print(count,end="") count += 1 print() ```
{ "language": "python", "test_cases": [ { "input": "5\n1\n2\n3\n4\n5\n", "output": "1\n1\n23\n1\n23\n456\n1\n23\n4 5\n6789\n1\n23\n4 5\n6 7\n89101112\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PSTR2020/problems/ITGUY05" }
vfc_2934
false
false
false
false
false
apps
verifiable_code
1024
Solve the following coding problem using the programming language python: Note : This question carries $100$ $points$ CodeLand is celebrating a festival by baking cakes! In order to avoid wastage, families follow a unique way of distributing cakes. For $T$ families in the locality, $i$-th family (1 <= $i$ <= $T$) has $N$ members. They baked $S$ slices of cakes. The smallest member of the family gets $K$ slices of cakes. Each family has a lucky number of $R$ and they agree to distribute the slices such that the member gets $R$ times more slices than the member just smaller than them. Since the family is busy in festival preparations, find out if the number of slices would be sufficient for the family or not. Also, find how many extra slices they have or how many slices are they short of. Also, the locality is kind and believes in sharing. So, you also need to determine if each family would have sufficient slices if families shared their cakes among each other! -----Input :----- - First line of input will have a single integer $T$ i.e. the number of families in the locality - For next $T$ lines, each line will describe one family through 4 integers i.e. $S$, $N$, $K$, $R$ separated by spaces -----Output----- - First $T$ lines of output will show if slices are enough for the family or not, followed by extra or required slices. For example, if slices are sufficient, the output would be $POSSIBLE$ $10$ which implies 10 slices are extra. If slices are insufficient, the output would be $IMPOSSIBLE$ $15$ which implies 15 slices are required to fulfill the requirement. - Last line of output would be $IMPOSSIBLE$ or $POSSIBLE$ depending on whether each family would get enough slices after sharing. -----Constraints:----- - 1 ≤ $T$ ≤ 50 - 1 ≤ $N$ ≤ 15 - 1 ≤ $S$ ≤ 10^9 - 1 ≤ $K$ ≤ 5 - 1 ≤ $R$ ≤ 5 -----Sample Input:----- 5 100 4 2 2 100 4 3 2 100 4 3 3 200 4 4 2 10 3 2 2 -----Sample Output:----- POSSIBLE 70 POSSIBLE 55 IMPOSSIBLE 20 POSSIBLE 140 IMPOSSIBLE 4 POSSIBLE -----Explanation----- For the first home, the youngest member has got 2 slices, the one older than them gets 2 times more slices, and so forth. So, first home needs 2 + 4 + 8 + 16 = 30 slices. But since they made 100, they have 70 extra, hence we print 'POSSIBLE 70'. Doing the same for other houses, we find 20 + 4 = 24 slices extra are required so that all families have enough slices. In this case we have 70 + 55 + 140 = 265 extra slices which is > 24, so we print 'POSSIBLE' in last line. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here extra, less = 0,0 for _ in range(int(input())): sli,mem,sma,luc = list(map(int, input().split())) total = sma t = sma while mem > 1: t *= luc total += t mem -= 1 if total <= sli: extra += sli-total print('POSSIBLE',sli-total) else: less += total-sli print('IMPOSSIBLE',total-sli) if extra >= less: print('POSSIBLE') else: print('IMPOSSIBLE') ```
{ "language": "python", "test_cases": [ { "input": "5\n100 4 2 2\n100 4 3 2\n100 4 3 3\n200 4 4 2\n10 3 2 2\n", "output": "POSSIBLE 70\nPOSSIBLE 55\nIMPOSSIBLE 20\nPOSSIBLE 140\nIMPOSSIBLE 4\nPOSSIBLE\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PCO12020/problems/MORECAKE" }
vfc_2938
false
false
false
false
false
apps
verifiable_code
1025
Solve the following coding problem using the programming language python: You are given a tree rooted at node $1$ with $N$ vertices. The $i$$th$ vertex initially has value $A_i (1 \leq i \leq N)$. You are also given $Q$ queries. In each query you are given a vertex $V$. Let $S = \{ S_1 , S_2 , ... S_x \} $ denote the set of vertices such that $S_i$ is in the subtree of $V$, distance between $S_i$ and $V$ is even and $S_i \neq V$ for all $i$. For all $S_i$ , add $A$$S_i$ to $A_V$ and change the value of $A$$S_i$ to zero. Find the values of all the vertices after all queries are performed. Note-The distance between two vertices is defined as the number of edges traversed on the shortest path from one vertex to the other. -----Input:----- - The first line contains an integer $T$ denoting the number of test cases. - The first line of each test case contain two integers $N$ and $Q$. - The second line contains $N$ space separated integers, $A_1, A_2, ..., A_n$ denoting the initial values of the vertices. - The next $N-1$ lines contain two integers $u$ and $v$ denoting an edge between $u$and $v$. - The next $Q$ lines contain a single integer which is the query. -----Output:----- - Print a single line containing $N$ integers for each test case which is the final values of the vertices. -----Constraints:----- - $1\leq T \leq 10$ - $1 \leq N \leq 200000$ - $1 \leq Q \leq 200000$ - $0 \leq A_i \leq 10^9$ - The sum of $N$ over all test cases does not exceed $200000$. - The sum of $Q$ over all test cases does not exceed $200000$. -----Sample Input----- 1 4 3 6 2 7 3 1 2 2 3 3 4 3 2 1 -----Sample Output----- 13 5 0 0 -----Explanation----- Node $3$ has no child in its subtree which is at an even distance so there is no change in the values. Values of nodes after $1st$ query: $6, 2, 7, 3$. Node $4$ is at an even distance in the subtree of node $2$ so $A_4$ gets added to $A_2$ and $A_4$ becomes 0. Values of nodes after $2nd$ query: $6, 5, 7, 0$. Node $3$ is at an even distance in the subtree of node $1$ so $A_3$ gets added to $A_1$ and $A_3$ becomes 0. Values of nodes after $3rd$ query: $13, 5, 0, 0$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from collections import defaultdict as dd,deque as dq def opbfs(u,vis,ll,parr): q=dq([(u,0)]) uu=u su=0 while q: u,lol=q.pop() par=parr[u] if(lol%2==0): vis[u]=1 su+=ll[u-1] ll[u-1]=0 for j in d[u]: if(j!=par): q.appendleft((j,lol+1)) ll[uu-1]=su def bfs(height,d,parr): q=dq([1]) while q: u=q.pop() height[u]=height[parr[u]]+1 for i in d[u]: if(i!=parr[u]): q.appendleft(i) parr[i]=u t=int(input()) while t: n,q=map(int,input().split()) ll=list(map(int,input().split())) d=dd(list) for i in range(n-1): u,v=map(int,input().split()) d[u].append(v) d[v].append(u) vis=[0]*(n+1) l=[] height=[0]*(n+1) parr=[0]*(n+1) bfs(height,d,parr) for i in range(q): u=int(input()) l.append((height[u],u,i)) l.sort() vis=[0]*(n+1) #print(l) for i in l: he,u,ind=i if(vis[u]==0): #print(u) opbfs(u,vis,ll,parr) print(*ll) t-=1 ```
{ "language": "python", "test_cases": [ { "input": "1\n4 3\n6 2 7 3\n1 2\n2 3\n3 4\n3\n2\n1\n", "output": "13 5 0 0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CENS20F" }
vfc_2942
false
false
false
false
true
apps
verifiable_code
1026
Solve the following coding problem using the programming language python: This is probably the simplest problem ever. You just need to count the number of ordered triples of different numbers (X1, X2, X3), where Xi could be any positive integer from 1 to Ni, inclusive (i = 1, 2, 3). No, wait. I forgot to mention that numbers N1, N2, N3 could be up to 1018. Well, in any case it is still quite simple :) By the way, because of this the answer could be quite large. Hence you should output it modulo 109 + 7. That is you need to find the remainder of the division of the number of required triples by 109 + 7. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains three space-separated integers N1, N2, N3. -----Output----- For each test case, output a single line containing the number of required triples modulo 109 + 7. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ Ni ≤ 1018 -----Example----- Input: 5 3 3 3 2 4 2 1 2 3 25 12 2012 1 1 2013 Output: 6 4 1 578880 0 -----Explanation----- Example case 1. We have the following triples composed of different numbers up to 3: (1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1) Example case 2. Here the triples are: (1, 3, 2) (1, 4, 2) (2, 3, 1) (2, 4, 1) Example case 3. Here the only triple is (1, 2, 3). Example case 4. Merry Christmas! Example case 5. ... and Happy New Year! By the way here the answer is zero since the only choice for X1 and for is X2 is 1, so any such triple will have equal numbers. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python d=1000000007 for _ in range(int(input())): l=sorted(list(map(int,input().split()))) ans=(l[0]%d)*((l[1]-1)%d)*((l[2]-2)%d) print(ans%d) ```
{ "language": "python", "test_cases": [ { "input": "5\n3 3 3\n2 4 2\n1 2 3\n25 12 2012\n1 1 2013\n", "output": "6\n4\n1\n578880\n0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/THREEDIF" }
vfc_2946
false
false
false
false
false
apps
verifiable_code
1027
Solve the following coding problem using the programming language python: Chef recently learned about concept of periodicity of strings. A string is said to have a period P, if P divides N and for each i, the i-th of character of the string is same as i-Pth character (provided it exists), e.g. "abab" has a period P = 2, It also has a period of P = 4, but it doesn't have a period of 1 or 3. Chef wants to construct a string of length N that is a palindrome and has a period P. It's guaranteed that N is divisible by P. This string can only contain character 'a' or 'b'. Chef doesn't like the strings that contain all a's or all b's. Given the values of N, P, can you construct one such palindromic string that Chef likes? If it's impossible to do so, output "impossible" (without quotes) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The only line of each test case contains two space separated integers N, P. -----Output----- For each test case, output a single line containing the answer of the problem, i.e. the valid string if it exists otherwise "impossible" (without quotes). If there are more than possible answers, you can output any. -----Constraints----- - 1 ≤ T ≤ 20 - 1 ≤ P, N ≤ 105 -----Subtasks----- - Subtask #1 (25 points) : P = N - Subtask #2 (75 points) : No additional constraints -----Example----- Input 5 3 1 2 2 3 3 4 4 6 3 Output impossible impossible aba abba abaaba -----Explanation----- Example 1: The only strings possible are either aaa or bbb, which Chef doesn't like. So, the answer is impossible. Example 2: There are four possible strings, aa, ab, ba, bb. Only aa and bb are palindromic, but Chef doesn't like these strings. Hence, the answer is impossible. Example 4: The string abba is a palindrome and has a period of 4. Example 5: The string abaaba is a palindrome and has a period of length 3. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python T=int(input()) for i in range(T): n,m=list(map(int,input().split())) if(m<=2): print("impossible") else: l=[0]*m if(m%2==0): a=m//2 else: a=(m//2)+1 for j in range(a): if(j%2==0): l[j]="a" l[m-j-1]="a" else: l[j]="b" l[m-j-1]="b" r="" s=n//m for e in l: r=r+e print(r*s) ```
{ "language": "python", "test_cases": [ { "input": "5\n3 1\n2 2\n3 3\n4 4\n6 3\n", "output": "impossible\nimpossible\naba\nabba\nabaaba\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/PERPALIN" }
vfc_2950
false
false
false
false
true
apps
verifiable_code
1028
Solve the following coding problem using the programming language python: Chef has some numbers. His girlfriend Chefina feels good when chef gives her a particular pattern number also called as Armstrong number. Armstrong number is a number whose sum of its all individual digit raise to the power of the number of digit in that number is equal to that number itself eg.. 153 = 1^3 + 5^3 + 3^3 (153 is an Armstrong number) 1634 = 1^4 + 6^4 + 3^4 + 4^4 (1634 is an Armstrong number) As a love guru of chef you have to help chef to find Armstrong numbers Among the numbers which chef has initially so that Chefina feels good -----Input:----- First line will contain a positive Integer $T$ which is the number of testcases Next $T$ lines follows an Integer $N$. -----Output:----- For Every n You have to print "FEELS GOOD" without qoutes if it is an armstrong number otherwise Print "FEELS BAD" without quotes -----Constraints----- - $1 \leq T \leq 10$ - $2 \leq N \leq 10^6$ -----Sample Input:----- 3 153 11 1634 -----Sample Output:----- FEELS GOOD FEELS BAD FEELS GOOD -----EXPLANATION:----- For test case 1 --> 153 = 1^3 + 5^3 + 3^3 (153 is an armstrong number) The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def power(x, y): if y == 0: return 1 if y % 2 == 0: return power(x, y // 2) * power(x, y // 2) return x * power(x, y // 2) * power(x, y // 2) # Function to calculate order of the number def order(x): # Variable to store of the number n = 0 while (x != 0): n = n + 1 x = x // 10 return n # Function to check whether the given # number is Armstrong number or not def isArmstrong(x): n = order(x) temp = x sum1 = 0 while (temp != 0): r = temp % 10 sum1 = sum1 + power(r, n) temp = temp // 10 # If condition satisfies return (sum1 == x) # Driver code for _ in range(int(input())): num = int(input()) if isArmstrong(num): print("FEELS GOOD") else: print("FEELS BAD") ```
{ "language": "python", "test_cases": [ { "input": "3\n153\n11\n1634\n", "output": "FEELS GOOD\nFEELS BAD\nFEELS GOOD\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/SPTC2020/problems/CPCEJC5" }
vfc_2954
false
false
false
false
false
apps
verifiable_code
1029
Solve the following coding problem using the programming language python: After a long and successful day of preparing food for the banquet, it is time to clean up. There is a list of n jobs to do before the kitchen can be closed for the night. These jobs are indexed from 1 to n. Most of the cooks have already left and only the Chef and his assistant are left to clean up. Thankfully, some of the cooks took care of some of the jobs before they left so only a subset of the n jobs remain. The Chef and his assistant divide up the remaining jobs in the following manner. The Chef takes the unfinished job with least index, the assistant takes the unfinished job with the second least index, the Chef takes the unfinished job with the third least index, etc. That is, if the unfinished jobs were listed in increasing order of their index then the Chef would take every other one starting with the first job in the list and the assistant would take every other one starting with the second job on in the list. The cooks logged which jobs they finished before they left. Unfortunately, these jobs were not recorded in any particular order. Given an unsorted list of finished jobs, you are to determine which jobs the Chef must complete and which jobs his assitant must complete before closing the kitchen for the evening. -----Input----- The first line contains a single integer T ≤ 50 indicating the number of test cases to follow. Each test case consists of two lines. The first line contains two numbers n,m satisfying 0 ≤ m ≤ n ≤ 1000. Here, n is the total number of jobs that must be completed before closing and m is the number of jobs that have already been completed. The second line contains a list of m distinct integers between 1 and n. These are the indices of the jobs that have already been completed. Consecutive integers are separated by a single space. -----Output----- The output for each test case consists of two lines. The first line is a list of the indices of the jobs assigned to the Chef. The second line is a list of the indices of the jobs assigned to his assistant. Both lists must appear in increasing order of indices and consecutive integers should be separated by a single space. If either the Chef or the assistant is not assigned any jobs, then their corresponding line should be blank. -----Example----- Input: 3 6 3 2 4 1 3 2 3 2 8 2 3 8 Output: 3 6 5 1 1 4 6 2 5 7 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python T = int(input()) for _ in range(T): n,m = map(int,input().split()) completed = list(map(int,input().split())) jobs = [] for i in range(1,n+1): if i not in completed: jobs.append(i) jobs.sort() chef = [] ass = [] for i in range(len(jobs)): if i%2==0: chef.append(str(jobs[i])) else: ass.append(str(jobs[i])) print(' '.join(chef)) print(' '.join(ass)) ```
{ "language": "python", "test_cases": [ { "input": "3\n6 3\n2 4 1\n3 2\n3 2\n8 2\n3 8\n", "output": "3 6\n5\n1\n1 4 6\n2 5 7\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CLEANUP" }
vfc_2958
false
false
false
false
false
apps
verifiable_code
1030
Solve the following coding problem using the programming language python: Let's consider a rooted binary tree with the following properties: - The number of nodes and edges in the tree is infinite - The tree root is labeled by $1$ - A node labeled by $v$ has two children: $2 \cdot v$ (the left child of $v$), and $2 \cdot v + 1$ (the right child of $v$). Here is an image of the first several layers of such a tree: Let's consider four operations that you are allowed to apply during the tree traversal: - move to the left child - move from $v$ to $2 \cdot v$ - move to the right child - move from $v$ to $2 \cdot v + 1$ - move to the parent as a left child - move from $v$ to $\frac{v}{2}$ if $v$ is an even integer - move to the parent as a right child - move from $v$ to $\frac{v - 1}{2}$ if $v$ is an odd integer It can be proven, that for any pair of nodes $u$ and $v$, there is only one sequence of commands that moves from $u$ to $v$ and visits each node of the tree at most once. Let's call such a sequence of commands a path configuration for a pair of nodes $(u, v)$. You are asked to process a series of the following queries: You are given three integers $n$, $u$ and $v$ ($1 \leq u, v \leq n$). Count the pairs of nodes $(w, t)$ ($1 \leq w, t \leq n$) such that the path configuration for $(w, t)$ is the same with the path configuration for $(u, v)$. -----Input----- - The first line of input contains a single integer $Q$, denoting the number of queries to process. - Each of the next $Q$ lines contains three space-separated integers $n$, $u$ and $v$ denoting a query. -----Output----- For each query, print the answer on a separate line. -----Constraints----- - $1 \leq Q \leq 2 \cdot 10^4$ - $1 \leq u, v \leq n \leq 10^{9}$ -----Example Input----- 3 11 9 11 10 2 2 8 1 8 -----Example Output----- 2 10 1 -----Explanation----- In the first query from the example test case, you should count pairs $(5, 7)$ and $(9, 11)$. In the second query from the example test case, you should count the following pairs: $(1, 1)$, $(2, 2)$, $(3, 3)$, $(4, 4)$, $(5, 5)$, $(6, 6)$, $(7, 7)$, $(8, 8)$, $(9, 9)$ and $(10, 10)$. In the third query from the example test case, you should only count a pair $(1, 8)$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) while(t>0): t-=1; n,l,r = list(map(int,input().split())); a = bin(l)[2:]; b = bin(r)[2:]; # find matching z = 0; l = min(len(a),len(b)); for i in range(l): if a[i]==b[i]: z+=1; else: break; #find base string a = a[z:] b = b[z:] if(len(a)==0 and len(b)==0): print(n); else : m = max(len(a),len(b)) #print m; zz = bin(n)[2:] x= len(zz) y = zz[:x-m] f1 = y+a; f2 = y+b; ans = int(y,2) if(int(f1,2)>n or int(f2,2)>n): ans-=1; print(ans) ```
{ "language": "python", "test_cases": [ { "input": "3\n11 9 11\n10 2 2\n8 1 8\n", "output": "2\n10\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COOK69/problems/BINTREEQ" }
vfc_2962
false
false
false
false
false
apps
verifiable_code
1031
Solve the following coding problem using the programming language python: Chef taught his brother Chefu about right angled triangle and its properties. Chefu says that he has understood everything about right angled triangles. Chef wants to check learning of his brother by asking the following question "Can you find a right angled triangle whose length of hypotenuse is H and its area is S?" Chefu is confused how to solve it. I hope you are not. Please solve this by finding a right angled triangle with hypotenuse H and area S. If it not possible to do so, then output -1. -----Input----- The first line of the input contains a single integer T denoting the number of test-cases. T test cases follow. For each test case, there will be a single line containing two space separated integers H and S. -----Output----- Output the answer for each test-case in a single line. If it is not possible to find such a triangle, output -1. Otherwise print 3 real numbers corresponding to the lengths of the sides of the triangle sorted in non-decreasing order. Please note that the length of the triangle sides should not differ by more than 0.01 in absolute value from the correct lengths. -----Constraints----- - 1 ≤ T ≤ 105 - 1 ≤ H ≤ 106 - 1 ≤ S ≤ 1012 -----Example----- Input:4 5 6 6 10 258303 89837245228 616153 77878145466 Output:3.00000 4.00000 5.00000 -1 -1 285168.817674 546189.769984 616153.000000 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math t = eval(input()) while(t > 0): h,s = input().split() h = int(h) s = int(s) if(((h*h*h*h) - (16*s*s)) < 0): print("-1") else: B = (math.sqrt((h*h) + math.sqrt((h*h*h*h) - (16*s*s))))/math.sqrt(2) P = (2*s)/B if(B > P): print('{0:.6f}'.format(P),'{0:.6f}'.format(B),'{0:.6f}'.format(h)) else: print('{0:.6f}'.format(B),'{0:.6f}'.format(P),'{0:.6f}'.format(h)) t = t-1 ```
{ "language": "python", "test_cases": [ { "input": "4\n5 6\n6 10\n258303 89837245228\n616153 77878145466\n", "output": "3.00000 4.00000 5.00000\n-1\n-1\n285168.817674 546189.769984 616153.000000\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COOK71/problems/RIGHTTRI" }
vfc_2966
false
false
false
false
true
apps
verifiable_code
1032
Solve the following coding problem using the programming language python: Help Saurabh with his Chemistry Assignment. Saurabh has been given a chemistry assignment by Ruby Mam. Though the assignment is simple but Saurabh has to watch India vs Pakistan Match and he has no time to do the assignment by himself. So Saurabh wants you to do his assignment so that he doesn’t get scolded by Ruby Mam . The assignment is as follows , Suppose there are X particles initially at time t=0 in a box. At a time t the number of particles in box becomes t times the number of particles at time t-1 . You will be given N and X where N is time at which the number of particles in box is to be calculated and X is the number of particles at time t=0. -----Input----- The first line will contain the integer T, the number of test cases. Each test case consists of two space separated integers N and X . -----Output----- For each test case, output the answer to the query. Since the output can be very large, output the answer modulo 10^6+3 -----Constraints----- - 1 ≤ T ≤ 100000 - 1 ≤ N,X ≤ 10^18 -----Example----- Input: 2 1 2 2 1 Output: 2 2 -----Explanation----- Example case 2.At t=0 particles are 1 ,so at t=1 ,particles are 1*1 = 1 particles. At t=2, particles are 2*1 = 2 particles. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a = [1] M = 10**6 + 3 for ii in range(1, 1000005): a.append((a[-1]*ii)%M) for __ in range(eval(input())): n, x = list(map(int, input().split())) if n>=M: print(0) else: print((a[n]*x)%M) ```
{ "language": "python", "test_cases": [ { "input": "2\n1 2\n2 1\n", "output": "2\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/REC2016/problems/RECIICHA" }
vfc_2970
false
false
false
false
false
apps
verifiable_code
1033
Solve the following coding problem using the programming language python: Given the values at the leaf nodes of a complete binary tree. The total number of nodes in the binary tree, is also given. Sum of the values at both the children of a node is equal to the value of the node itself. You can add any value or subtract any value from a node. Print the minimum change(difference made) in the sum of values of all the nodes in the tree, such that all the leaf nodes have the same value. Note: If a value transfers from one node to another, then that is not a change, but if an extra is needed to be added or subtracted to the entire total value of the nodes, then that is a change. Input Description: Input will contain an integer N, the number of nodes in the tree on a newline, followed by N space separated integers representing the values at the leaf nodes of the tree. Output Description: Print the required value on a newline. Constraints: 1<=N<=20000 1<=Value at each node in the leaves<=1000 Example 1: Input: 1 50 Output: 0 Explanation: Since there is only one node, it is a leaf node itself and no change needs to be made. Example 2: Input: 3 200 800 Output: 0 Explanation: There are two leaf nodes, and they can be made to 500 500, since no change in the total was made so difference made is 0. Example 3: Input: 30 29 33 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Output: 6 Output: A total change of 6 needs to be changed to the entire value of the nodes, to get the leaf nodes equal. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python print(0) ```
{ "language": "python", "test_cases": [ { "input": "1:\nInput:\n1\n50\n", "output": "0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/KM252020/problems/KM25P5C" }
vfc_2974
false
false
false
false
true
apps
verifiable_code
1034
Solve the following coding problem using the programming language python: A manufacturing project consists of exactly $K$ tasks. The board overviewing the project wants to hire $K$ teams of workers — one for each task. All teams begin working simultaneously. Obviously, there must be at least one person in each team. For a team of $A$ workers, it takes exactly $A$ days to complete the task they are hired for. Each team acts independently, unaware of the status of other teams (whether they have completed their tasks or not), and submits their result for approval on the $A$-th day. However, the board approves the project only if all $K$ teams complete their tasks on the same day — it rejects everything submitted on any other day. The day after a team finds out that its result was rejected, it resumes work on the same task afresh. Therefore, as long as a team of $A$ workers keeps getting rejected, it submits a new result of their task for approval on the $A$-th, $2A$-th, $3A$-th day etc. The board wants to hire workers in such a way that it takes exactly $X$ days to complete the project. Find the smallest number of workers it needs to hire. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $K$ and $X$. -----Output----- For each test case, print a single line containing one integer — the smallest required number of workers. -----Constraints----- - $1 \le T \le 40$ - $2 \le K, X \le 10^6$ -----Example Input----- 2 2 3 2 6 -----Example Output----- 4 5 -----Explanation----- Example case 1: We can hire a team of $3$ workers for task $1$ and $1$ worker for task $2$. The one-man team working on task $2$ completes it and submits the result for approval on each day, but it is rejected on the first and second day. On the third day, the team working on task $1$ also completes their task, so the project gets approved after exactly $3$ days. Example case 2: We can hire a team of $3$ workers for task $1$ and a team of $2$ workers for task $2$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import log2; import bisect; from bisect import bisect_left,bisect_right import sys; from math import gcd,sqrt sys.setrecursionlimit(10**7) from collections import defaultdict inf=float("inf") # n=int(input()) # n,m=map(int,input().split()) # l=list(map(int,input().split())) def get_factors(x): if x==1: return []; sqrta=int(sqrt(x))+1 for i in range(2,sqrta): if x%i==0: return [i]+get_factors(x//i) return [x] def min_generator(fac,k,index,new_list): if index==len(fac): return sum(new_list) mina=inf; for i in range(0,min(index+1,len(new_list))): new_list[i]*=fac[index] theta=min_generator(fac,k,index+1,new_list) if theta<mina: mina=theta; new_list[i]//=fac[index] return mina; def fun(k,x): dict=defaultdict(lambda :1) factors=get_factors(x) for i in factors: dict[i]*=i; if len(dict)==k: print(sum(dict.values())) return; if len(dict)<k: suma=sum(dict.values()) left=k-len(dict) suma+=left; print(suma) return; if k==1: print(x) return; fac=list(dict.values()) new_list=[1]*k theta=min_generator(fac,k,0,new_list) print(theta) for i in range(int(input())): k,x=map(int,input().split()) fun(k,x) ```
{ "language": "python", "test_cases": [ { "input": "2\n2 3\n2 6\n", "output": "4\n5\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/HIRINGWO" }
vfc_2978
false
false
false
false
false
apps
verifiable_code
1035
Solve the following coding problem using the programming language python: Mathison and Chef are playing a new teleportation game. This game is played on a $R \times C$ board where each cell $(i, j)$ contains some value $V_{i, j}$. The purpose of this game is to collect a number of values by teleporting from one cell to another. A teleportation can be performed using a tel-pair. A player is given $N$ tel-pairs. Each tel-pair can be used at most once and a player can use them in any order they like. Suppose a player is at cell $(a, b)$ and the tel-pair is $(dx, dy)$. Then, the player can reach in one teleportation any cell $(c, d)$ from $(a, b)$ such that $|a − c| = dx$ and $|b − d| = dy$. It is Mathison’s turn next in the game to make a sequence of moves. He would like to know what is the highest value of a path of length at most $N+1$ that starts in $(Sx, Sy)$ and uses some (possibly none) of the tel-pairs given. The length of a path is equal to the number of cells in the path. The value of a path is equal to the sum of $V_{i, j}$ over all cells in the path. -----Input----- - The first line contains a single integer, $T$, the number of tests. - Each test starts with three integers, $R$, $C$, and $N$, representing the number of rows, columns, and tel-pairs. - The next line contains two integers, $Sx$, and $Sy$, representing the coordinates of the starting cell. - The next two lines will contain the description of the tel-pairs, each containing $N$ space separated integers. The first will contain the $x$-component of each tel-pair, the second one will contain the y-component of each tel-pair. - Finally, there will be $R$ lines, each containing $C$ space-separated integers, the description of the board. -----Output----- The output file will contain $T$ lines. Each line will contain the answer (i.e. the highest value of a path) to the corresponding test. -----Constraints and notes----- - $1 \leq T \leq 100$ - $1 \leq R, C \leq 1000$ - $1 \leq N \leq 9$ - $0 \leq Sx < R$ - $0 \leq Sy < C$ - $0 \leq dx \leq R$ - $0 \leq dy \leq C$ - $1 \leq V_{i, j} \leq 10^6$ - You are allowed to visit a cell multiple times in a path, and the value for the cell must be added each time you visit it. -----Subtaks----- Subtask #1 (15 points): - $1 \leq T \leq 100$ - $1 \leq R, C \leq 10$ - $1 \leq N \leq 4$ Subtask #2 (25 points): - $1 \leq T \leq 25$ - $1 \leq R, C \leq 100$ - $1 \leq N \leq 8$ Subtask #3 (30 points): - $1 \leq T \leq 5$ - $1 \leq R, C \leq 1000$ - $1 \leq N \leq 8$ Subtask #4 (30 points): - $1 \leq T \leq 5$ - $1 \leq R, C \leq 1000$ - $1 \leq N \leq 9$ -----Example Input----- 3 5 5 2 2 2 1 2 2 1 10 11 62 14 15 57 23 34 75 21 17 12 14 11 53 84 61 24 85 22 43 89 14 15 43 3 3 2 0 0 1 1 1 1 9 8 7 5 6 4 1 3 2 2 2 1 1 1 2 2 5 6 8 3 -----Example Output----- 188 24 3 -----Explanation----- Test Case 1: Mathison starts at $(2, 2)$. Mathison has two tel-pairs $(2, 1)$ and $(1, 2)$. The following path (i.e. bolded numbers) generates the maximum value: $(2, 2)$ → $(4, 1)$ → $(3, 3)$ Test Case 2: Mathison starts at $(0, 0)$. Mathison has two tel-pairs $(1, 1)$ and $(1, 1)$. The following path (i.e. bolded numbers) generates the maximum value: $(0, 0)$ → $(1, 1)$ → $(0, 0)$ Test Case 3: Mathison starts at $(1, 1)$. Mathison has one tel-pair, $(2, 2)$. He can't use the tel-pair so the answer is $3$ (the value of the starting cell). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here from collections import namedtuple CurrentPosition = namedtuple('current_position', 'points, cell, pairs') T = int(input()) for _ in range(T): R, C, N = map(int, input().split()) Sx, Sy = map(int, input().split()) tx = map(int, input().split()) ty = map(int, input().split()) tel_pairs = list(zip(tx, ty)) board = [] for _ in range(R): board += [[int(c) for c in input().split()]] def explore(p): next_pos = [] for i, (dx, dy) in enumerate(p.pairs): sx, sy = p.cell new_pairs = p.pairs[:i]+p.pairs[i+1:] # case (+, +) px, py = sx + dx, sy + dy if px < R and py < C: next_pos += [CurrentPosition(p.points+board[px][py], (px, py), new_pairs)] # case (+, -) px, py = sx + dx, sy - dy if px < R and 0 <= py: next_pos += [CurrentPosition(p.points+board[px][py], (px, py), new_pairs)] # case (-, +) px, py = sx - dx, sy + dy if 0 <= px and py < C: next_pos += [CurrentPosition(p.points+board[px][py], (px, py), new_pairs)] # case (-, -) px, py = sx - dx, sy - dy if 0 <= px and 0 <= py: next_pos += [CurrentPosition(p.points+board[px][py], (px, py), new_pairs)] return next_pos pos = [CurrentPosition(board[Sx][Sy], (Sx, Sy), tel_pairs)] result = board[Sx][Sy] while pos: p = pos.pop(0) if p.pairs: pos += explore(p) else: result = max(result, p.points) print(result) ```
{ "language": "python", "test_cases": [ { "input": "3\n5 5 2\n2 2\n1 2\n2 1\n10 11 62 14 15\n57 23 34 75 21\n17 12 14 11 53\n84 61 24 85 22\n43 89 14 15 43\n3 3 2\n0 0\n1 1\n1 1\n9 8 7\n5 6 4\n1 3 2\n2 2 1\n1 1\n2\n2\n5 6\n8 3\n", "output": "188\n24\n3\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/MATTEG" }
vfc_2982
false
false
false
false
false
apps
verifiable_code
1036
Solve the following coding problem using the programming language python: Consider a 2d-grid. That is, each cell is identified by (i,j). You have received reports of two snake-sightings on this grid. You want to check whether they could be partial sightings of the same snake or not. Each of the snake sightings correspond to a straight, axis-parallel line segment in the grid, and the starting and ending cells for each are given to you. Now consider a graph, where each cell in the 2d-grid is a vertex. And there is an edge between 2 vertices if and only if the cells corresponding to these two vertices are consecutive cells in at least one of the two snakes. That is, at least in one of the snakes, when you go from one end point to the other end point, these two cells should occur consecutively. The two sightings/snakes are said to be same, if both these conditions are satisfied: - The union of the set of cells in the first snake and the set of cells in the second snake, should form a connected component in this graph. - No vertex should have degree more than 2 in the graph. In other words, the induced subgraph on the union set must be a path graph. -----Input----- - The first line contains a single integer, T, which is the number of testcases. The description of each testcase follows. - The first line of each testcase contains four integers: X11, Y11, X12, Y12. This represents the fact that the first snake's end points are (X11, Y11) and (X12, Y12). - The second line of each testcase contains four integers: X21, Y21, X22, Y22. This represents the fact that the second snake's end points are (X21, Y21) and (X22, Y22). -----Output----- - For each testcase, output "yes" if the snakes are the same, as per the definition given above. Output "no" otherwise. -----Constraints----- - 1 ≤ T ≤ 105 - -109 ≤ Xij,Yij ≤ 109 - The two end points of every snake is guaranteed to be either on the same row or on the same column. Thus, the snake occupies all the cells between these cells, including the end points. -----Example----- Input: 4 2 1 8 1 11 1 7 1 2 1 8 1 11 1 9 1 2 1 8 1 3 1 3 -2 2 1 8 1 2 1 2 -2 Output: yes no no yes -----Explanation----- In the images, the first snake is red, the second snake is yellow, and the intersections, if any, are in orange. The first test case corresponds to: Both the conditions on the graph are satisfied, and hence this is a "yes". The second test case corresponds to: There is no edge between the vertex corresponding to the (8,1) cell and the vertex corresponding to (9,1), Hence, the union set is disconnected, and thus the answer is "no". The third test case corresponds to: The vertex corresponding to the cell (3,1) has degree 3, which is more than 2, and thus the answer is "no". The fourth test case corresponds to: Both the conditions on the graph are satisfied, and hence this is a "yes". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) for _ in range(t): x1,y1,x2,y2=map(int,input().split()) x3,y3,x4,y4=map(int,input().split()) if (x1==x3 and y1==y3)or(x2==x4 and y2==y4): print("yes") elif (x1==x4 and y1==y4)or(x2==x3 and y2==y3): print("yes") else: if(y1==y2)and(y1==y3)and(y1==y4): a1=max(x1,x2);a2=min(x1,x2) b1=max(x3,x4);b2=min(x3,x4) if a1>=b2 and a2<=b1: print("yes") else: print("no") elif (x1==x2)and(x1==x3)and(x1==x4): a1=max(y1,y2);a2=min(y1,y2) b1=max(y3,y4);b2=min(y3,y4) if a1>=b2 and a2<=b1: print("yes") else: print("no") else: print("no") ```
{ "language": "python", "test_cases": [ { "input": "4\n2 1 8 1\n11 1 7 1\n2 1 8 1\n11 1 9 1\n2 1 8 1\n3 1 3 -2\n2 1 8 1\n2 1 2 -2\n", "output": "yes\nno\nno\nyes\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/SAMESNAK" }
vfc_2986
false
false
false
false
false
apps
verifiable_code
1037
Solve the following coding problem using the programming language python: Ada is playing pawn chess with Suzumo. Pawn chess is played on a long board with N$N$ squares in one row. Initially, some of the squares contain pawns. Note that the colours of the squares and pawns do not matter in this game, but otherwise, the standard chess rules apply: - no two pawns can occupy the same square at the same time - a pawn cannot jump over another pawn (they are no knights!), i.e. if there is a pawn at square i$i$, then it can only be moved to square i−2$i-2$ if squares i−1$i-1$ and i−2$i-2$ are empty - pawns cannot move outside of the board (outs are forbidden) The players alternate turns; as usual, Ada plays first. In each turn, the current player must choose a pawn and move it either one or two squares to the left of its current position. The player that cannot make a move loses. Can Ada always beat Suzumo? Remember that Ada is a chess grandmaster, so she always plays optimally. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first and only line of each test case contains a single string S$S$ with length N$N$ describing the initial board from left to right. An empty square and a square containing a pawn are denoted by the characters '.' and 'P' respectively. -----Output----- For each test case, print a single line containing the string "Yes" if Ada wins the game or "No" otherwise (without quotes). -----Constraints----- - 1≤T≤500$1 \le T \le 500$ - 2≤N≤128$2 \le N \le 128$ - S$S$ contains only characters '.' and 'P' -----Example Input----- 1 ..P.P -----Example Output----- Yes -----Explanation----- Example case 1: Ada can move the first pawn two squares to the left; the board after this move looks like P...P and now, Suzumo can only move the second pawn. If he moves it one square to the left, Ada will move it two squares to the left on her next move, and if he moves it two squares to the left, Ada will move it one square to the left, so the board after Ada's next move will look like PP... and Suzumo cannot make any move here. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for _ in range(int(input())): s = input().strip() a = [] last = 0 for i in range(len(s)): if s[i] == 'P': a.append(i - last) last = i + 1 x = 0 a = a[::-1] for v in a[::2]: x ^= v % 3 print('Yes' if x else 'No') ```
{ "language": "python", "test_cases": [ { "input": "1\n..P.P\n", "output": "Yes\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/ADAPWNS" }
vfc_2990
false
false
false
false
false
apps
verifiable_code
1038
Solve the following coding problem using the programming language python: Two's company, three's a crowd! It's been one year since Chef met his brother. Last year, his younger brother came to visit him during this time of the year. This year, the Chef is planning to go visit his brother. Chef's brother has planned to throw a "Welcome Party" for him. He wants to invite people from his neighbourhood (i.e. from the street where he lives). There are N houses on the street in a single line (not considering the brother's house). He wants the party to be fun and he will not like to invite people who might spoil the mood of the party. If people are invited from three consecutive houses on the street, they might create trouble. As they say, three's a crowd! He doesn't want to ruin the Chef's Welcome Party and so he will not want to send invites to any three consecutive houses. He wants you to tell him how many ways are there for him to go wrong. Note that he can play safe by not inviting anyone to avoid a crowd. -----Input:----- First line of the input contains a single integer T, the number of test cases. Each test case contains a line containing a single integer N described above. -----Output:----- For each test case output a single integer denoting the number of ways the brother can go wrong with planning the party. The answer can get quite large. So output the total number of ways modulo 109+7. -----Constraints:----- 1<=T<=10000 1<=N<=1015 -----Example:-----Input: 2 3 4 Output: 1 3 Explanation: Case 1: The only way he can go wrong is by inviting all the houses. Case 2: First way of getting wrong is by inviting houses (1,2,3). Second way to get wrong is by inviting houses (2,3,4). Third way of going wrong is by inviting all 4 houses i.e. (1,2,3,4). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python MOD = int(1e9+7) def mult(a, b): rsp = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] for i in range(3): for j in range(3): for k in range(3): rsp[i][j] += a[i][k] * b[k][j] rsp[i][j] %= MOD return rsp ident = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] m = [[1, 1, 0], [1, 0, 1], [1, 0, 0]] powers = [m] for _ in range(53): p = powers[-1] powers.append(mult(p ,p)) def pow2(e): y = ident i = 0 for p in powers: if e & (1 << i): y = mult(p, y) i += 1 return y t = eval(input()) for _ in range(t): n = eval(input()) if n < 3: print(0) continue r = pow(2, n, MOD) b = pow2(n - 2) # print(b) r -= (4 * b[0][0]) % MOD r -= (2 * b[1][0]) % MOD r -= b[2][0] r = (MOD + r) % MOD print(r) ```
{ "language": "python", "test_cases": [ { "input": "2\n3\n4\n", "output": "1\n3\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CROWD" }
vfc_2994
false
false
false
false
false
apps
verifiable_code
1039
Solve the following coding problem using the programming language python: “I am not in danger, Skyler. I am the danger. A guy opens his door and gets shot, and you think that of me? No! I am the one who knocks!” Skyler fears Walter and ponders escaping to Colorado. Walter wants to clean his lab as soon as possible and then go back home to his wife. In order clean his lab, he has to achieve cleaning level of lab as $Y$. The current cleaning level of the lab is $X$. He must choose one positive odd integer $a$ and one positive even integer $b$. Note that, he cannot change $a$ or $b$ once he starts cleaning. He can perform any one of the following operations for one round of cleaning: - Replace $X$ with $X+a$. - Replace $X$ with $X-b$. Find minimum number of rounds (possibly zero) to make lab clean. -----Input:----- - First line will contain $T$, number of test cases. $T$ testcases follow : - Each test case contains two space separated integers $X, Y$. -----Output:----- For each test case, output an integer denoting minimum number of rounds to clean the lab. -----Constraints----- - $1 \leq T \leq 10^5$ - $ |X|,|Y| \leq 10^9$ -----Sample Input:----- 3 0 5 4 -5 0 10000001 -----Sample Output:----- 1 2 1 -----EXPLANATION:----- - For the first testcase, you can convert $X$ to $Y$ by choosing $a=5$ and $b=2$. It will cost minimum of $1$ cleaning round. You can select any other combination of $a, b$ satisfying above condition but will take minimum of $1$ cleaning round in any case. - For the second testcase, you can convert $X$ to $Y$ by choosing $a=1$ and $b=10$. In first round they will replace $X$ to $X+a$ and then in second round replace to $X-b$. You can perform only one operation in one round. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=int(input()) for i in range(t): ans=0 x,y=list(map(int,input().split())) if y>x: if (y-x)%4==0:ans=3 elif (y-x)%2==0: ans=2 else: ans=1 if y<x: if (y-x)%2==0:ans=1 else: ans=2 print(ans) ```
{ "language": "python", "test_cases": [ { "input": "3\n0 5\n4 -5\n0 10000001\n", "output": "1\n2\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/EOOPR" }
vfc_2998
false
false
false
false
false
apps
verifiable_code
1040
Solve the following coding problem using the programming language python: A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times. You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $Q$. - The second line contains a single string $S$ with length $N$. - Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query. -----Output----- For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring. -----Constraints----- - $1 \le T \le 10$ - $1 \le N, Q \le 10^5$ - $1 \le L \le R \le N$ - $S$ contains only lowercase English letters -----Example Input----- 1 10 2 helloworld 1 3 1 10 -----Example Output----- NO YES The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=int(input()) for _ in range(t): n,q=map(int,input().split()) s=input() l=[0]*(n-1) for i in range(n-2): a,b,c=s[i],s[i+1],s[i+2] if len(set([a,b,c]))<3: l[i]=l[i-1]+1 else: l[i]=l[i-1] for i in range(q): left,right=map(int,input().split()) left-=1 right-=1 if right-left+1 <3: print('NO') continue if (l[right-2]-l[left-1])>0: print('YES') else: print('NO') ```
{ "language": "python", "test_cases": [ { "input": "1\n10 2\nhelloworld\n1 3\n1 10\n", "output": "NO\nYES\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/RICHSTR" }
vfc_3002
false
false
false
false
false
apps
verifiable_code
1041
Solve the following coding problem using the programming language python: Ripul was skilled in the art of lapidary. He used to collect stones and convert it into decorative items for sale. There were n stone shops. Each shop was having one exclusive stone of value s[i] , where 1<=i<=n. If number of stones collected are more than 1, then total value will be product of values of all the stones he collected. Ripul wants to have maximum value of stones he collected. Help Ripul in picking up the subarray which leads to maximum value of stones he collected. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - The first line of each testcase contains an integer $N$, denoting number of elements in the given array. - The second line contains $N$ space-separated integers $S1$, $S2$, …, $SN$ denoting the value of stone in each shop. -----Output:----- For each testcase, output the maximum value of stones possible, the starting index and ending index of the chosen subarray (0-based indexing). If there are multiple subarrays with same value, print the one with greater starting index. If there are multiple answer subarrays with same starting index, print the one with greater ending index. (The answer will fit in 64 bit binary number). -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $-100 \leq S[i] \leq 100$ -----Subtasks----- - 30 points : $1 \leq N \leq 10^3$ - 70 points : $1 \leq N \leq 10^5$ -----Sample Input:----- 1 3 1 2 3 -----Sample Output:----- 6 1 2 -----EXPLANATION:----- If Ripul collects all the all the three gems, total value will be 6 (1 * 2 * 3). If Ripul collects last two gems, total value will be 6 (1 * 2 * 3). So, he picks the subarray with greater starting index. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for u in range(int(input())): n=int(input()) l=list(map(int,input().split())) d=[] dd=[] s=1 for i in range(n-1): s=l[i] d.append(s) dd.append([i,i]) for j in range(i+1,n): s=s*l[j] d.append(s) dd.append([i,j]) d.append(l[n-1]) dd.append([n-1,n-1]) k=len(d) m=max(d) x,y=0,0 for i in range(k): if(d[i]==m): x=dd[i] print(m,*x) ```
{ "language": "python", "test_cases": [ { "input": "1\n3\n1 2 3\n", "output": "6 1 2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COH12020/problems/RIPUGEMS" }
vfc_3006
false
false
false
false
false
apps
verifiable_code
1042
Solve the following coding problem using the programming language python: Chef wants to gift pairs to his friends this new year. But his friends like good pairs only. A pair (a , b) is called a good pair if 1 <= a < b <= N such that GCD(a*b , P) = 1. Since Chef is busy in preparation for the party, he wants your help to find all the good pairs. ————————————————————————————————————— INPUT • The first line of the input contains a single integer T. • The first and only line of each test case contain two integer N,P. ———————————————————————————————————————— OUTPUT For each test case, print a single line containing one integer — the total number of good pairs ———————————————————————————————————————— CONSTRAINTS • 1 ≤ T≤ 50 • 2 ≤ N,P ≤10^5 ————————————————————————————————————— Example Input 2 2 3 3 3 ———————————————————————————————————————— Example Output 1 1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here def G(x, y): while(y): x, y = y, x % y return x # t=int(input()) # l=list(map(int,input().split())) for _ in range(int(input())): n,p=map(int,input().split()) c=0 for i in range(1,n+1): if G(i,p)==1: c+=1 ans=c*(c-1)//2 print(ans) ```
{ "language": "python", "test_cases": [ { "input": "2\n2 3\n3 3\n————————————————————————————————————————\n", "output": "1\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/NITJ2021/problems/CIN004" }
vfc_3010
false
false
false
false
false
apps
verifiable_code
1043
Solve the following coding problem using the programming language python: Forgotten languages (also known as extinct languages) are languages that are no longer in use. Such languages were, probably, widely used before and no one could have ever imagined that they will become extinct at some point. Unfortunately, that is what happened to them. On the happy side of things, a language may be dead, but some of its words may continue to be used in other languages. Using something called as the Internet, you have acquired a dictionary of N words of a forgotten language. Meanwhile, you also know K phrases used in modern languages. For each of the words of the forgotten language, your task is to determine whether the word is still in use in any of these K modern phrases or not. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of a test case description contains two space separated positive integers N and K. The second line of the description contains N strings denoting a dictionary of the forgotten language. Each of the next K lines of the description starts with one positive integer L denoting the number of words in the corresponding phrase in modern languages. The integer is followed by L strings (not necessarily distinct) denoting the phrase. -----Output----- For each test case, output a single line containing N tokens (space-separated): if the ith word of the dictionary exists in at least one phrase in modern languages, then you should output YES as the ith token, otherwise NO. -----Constraints----- - 1 ≤ T ≤ 20 - 1 ≤ N ≤ 100 - 1 ≤ K, L ≤ 50 - 1 ≤ length of any string in the input ≤ 5 -----Example----- Input: 2 3 2 piygu ezyfo rzotm 1 piygu 6 tefwz tefwz piygu ezyfo tefwz piygu 4 1 kssdy tjzhy ljzym kegqz 4 kegqz kegqz kegqz vxvyj Output: YES YES NO NO NO NO YES The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python test_case = int(input()) for w in range(test_case): n, k = map(int,input().split()) l = list(map(str,input().split())) ans = [] for q in range(k): l2 = list(map(str,input().split())) ans.extend(l2[1:]) for i in l: if i in ans: print('YES',end=' ') else: print('NO',end=' ') print()# cook your dish here ```
{ "language": "python", "test_cases": [ { "input": "2\n3 2\npiygu ezyfo rzotm\n1 piygu\n6 tefwz tefwz piygu ezyfo tefwz piygu\n4 1\nkssdy tjzhy ljzym kegqz\n4 kegqz kegqz kegqz vxvyj\n", "output": "YES YES NO\nNO NO NO YES\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/FRGTNLNG" }
vfc_3014
false
false
false
false
false
apps
verifiable_code
1044
Solve the following coding problem using the programming language python: You're given an integer N. Write a program to calculate the sum of all the digits of N. -----Input----- The first line contains an integer T, the total number of testcases. Then follow T lines, each line contains an integer N. -----Output----- For each test case, calculate the sum of digits of N, and display it in a new line. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 1000000 -----Example----- Input 3 12345 31203 2123 Output 15 9 8 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here number = int(input()) for i in range(number): a = list(input()) for k in range(len(a)): a[k] = eval(a[k]) print(sum(a)) ```
{ "language": "python", "test_cases": [ { "input": "3\n12345\n31203\n2123\n", "output": "15\n9\n8\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/FLOW006" }
vfc_3018
false
false
false
false
false
apps
verifiable_code
1045
Solve the following coding problem using the programming language python: You are given Name of chef's friend and using chef's new method of calculating value of string , chef have to find the value of all the names. Since chef is busy , he asked you to do the work from him . The method is a function $f(x)$ as follows - - $f(x)$ = $1$ , if $x$ is a consonent - $f(x)$ = $0$ , if $x$ is a vowel Your task is to apply the above function on all the characters in the string $S$ and convert the obtained binary string in decimal number N. Since the number N can be very large, compute it modulo $10^9+7$ . Input: - First line will contain $T$, number of testcases. Then the testcases follow. - Each test line contains one String $S$ composed of lowercase English alphabet letters. -----Output:----- For each case, print a single line containing one integer $N$ modulo $10^9+7$ . -----Constraints----- - $1 \leq T \leq 50$ - $1 \leq |S| \leq 10^5$ -----Sample Input:----- 1 codechef -----Sample Output:----- 173 -----EXPLANATION:----- The string "codechef" will be converted to 10101101 ,using the chef's method function . Which is equal to 173. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python vow = ['a', 'e', 'i','o', 'u'] for _ in range(int(input())): name = str(input()) tmp = '' for i in range(len(name)): if name[i] not in vow and name[i].isalpha(): tmp+='1' elif name[i] in vow and name[i].isalpha(): tmp+='0' print( int(tmp, 2)% (10**9 + 7)) ```
{ "language": "python", "test_cases": [ { "input": "1\ncodechef\n", "output": "173\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/LDT42020/problems/NAMEVAL" }
vfc_3022
false
false
false
false
false
apps
verifiable_code
1046
Solve the following coding problem using the programming language python: Bears love candies and games involving eating them. Limak and Bob play the following game. Limak eats 1 candy, then Bob eats 2 candies, then Limak eats 3 candies, then Bob eats 4 candies, and so on. Once someone can't eat what he is supposed to eat, he loses. Limak can eat at most A candies in total (otherwise he would become sick), while Bob can eat at most B candies in total. Who will win the game? Print "Limak" or "Bob" accordingly. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains two integers A and B denoting the maximum possible number of candies Limak can eat and the maximum possible number of candies Bob can eat respectively. -----Output----- For each test case, output a single line containing one string — the name of the winner ("Limak" or "Bob" without the quotes). -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ A, B ≤ 1000 -----Example----- Input: 10 3 2 4 2 1 1 1 2 1 3 9 3 9 11 9 12 9 1000 8 11 Output: Bob Limak Limak Bob Bob Limak Limak Bob Bob Bob -----Explanation----- Test case 1. We have A = 3 and B = 2. Limak eats 1 candy first, and then Bob eats 2 candies. Then Limak is supposed to eat 3 candies but that would mean 1 + 3 = 4 candies in total. It's impossible because he can eat at most A candies, so he loses. Bob wins, and so we print "Bob". Test case 2. Now we have A = 4 and B = 2. Limak eats 1 candy first, and then Bob eats 2 candies, then Limak eats 3 candies (he has 1 + 3 = 4 candies in total, which is allowed because it doesn't exceed A). Now Bob should eat 4 candies but he can't eat even a single one (he already ate 2 candies). Bob loses and Limak is the winner. Test case 8. We have A = 9 and B = 12. The game looks as follows: - Limak eats 1 candy. - Bob eats 2 candies. - Limak eats 3 candies (4 in total). - Bob eats 4 candies (6 in total). - Limak eats 5 candies (9 in total). - Bob eats 6 candies (12 in total). - Limak is supposed to eat 7 candies but he can't — that would exceed A. Bob wins. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for t in range(int(input())): limakMax, bobMax = list(map(int, input().split())) limakEat = 0; bobEat = 0 eating = 1 while limakEat <= limakMax or bobEat <= bobMax: if eating % 2 != 0 and limakEat <= limakMax: limakEat += eating eating += 1 if limakEat > limakMax: print("Bob") break elif eating % 2 == 0 and bobEat <= bobMax: bobEat += eating eating += 1 if bobEat > bobMax: print("Limak") break ```
{ "language": "python", "test_cases": [ { "input": "10\n3 2\n4 2\n1 1\n1 2\n1 3\n9 3\n9 11\n9 12\n9 1000\n8 11\n", "output": "Bob\nLimak\nLimak\nBob\nBob\nLimak\nLimak\nBob\nBob\nBob\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CANDY123" }
vfc_3026
false
false
false
false
false
apps
verifiable_code
1047
Solve the following coding problem using the programming language python: Chef bought a huge (effectively infinite) planar island and built $N$ restaurants (numbered $1$ through $N$) on it. For each valid $i$, the Cartesian coordinates of restaurant $i$ are $(X_i, Y_i)$. Now, Chef wants to build $N-1$ straight narrow roads (line segments) on the island. The roads may have arbitrary lengths; restaurants do not have to lie on the roads. The slope of each road must be $1$ or $-1$, i.e. for any two points $(x_1, y_1)$ and $(x_2, y_2)$ on the same road, $|x_1-x_2| = |y_1-y_2|$ must hold. Let's denote the minimum distance Chef has to walk from restaurant $i$ to reach a road by $D_i$. Then, let's denote $a = \mathrm{max}\,(D_1, D_2, \ldots, D_N)$; Chef wants this distance to be minimum possible. Chef is a busy person, so he decided to give you the job of building the roads. You should find a way to build them that minimises $a$ and compute $a \cdot \sqrt{2}$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - $N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $X_i$ and $Y_i$. -----Output----- For each test case, print a single line containing one real number — the minimum distance $a$ multiplied by $\sqrt{2}$. Your answer will be considered correct if its absolute or relative error does not exceed $10^{-6}$. -----Constraints----- - $1 \le T \le 100$ - $2 \le N \le 10^4$ - $|X_i|, |Y_i| \le 10^9$ for each valid $i$ -----Subtasks----- Subtask #1 (10 points): - $1 \le T \le 10$ - $2 \le N \le 5$ - $|X_i|, |Y_i| \le 10$ for each valid $i$ - $a \cdot \sqrt{2}$ is an integer Subtask #2 (90 points): original constraints -----Example Input----- 2 3 0 0 0 1 0 -1 3 0 1 1 0 -1 0 -----Example Output----- 0.5 0 -----Explanation----- Example case 1: We should build roads described by equations $y-x+0.5 = 0$ and $y-x-0.5 = 0$. Example case 2: We should build roads described by equations $y-x-1 = 0$ and $y+x-1 = 0$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def rinput(): return list(map(int, sys.stdin.readline().strip().split())) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) t=iinput() for _ in range(t): n=iinput() p=[] mi=[] for i in range(n): x,y=rinput() p.append(x+y) mi.append(x-y) p.sort() mi.sort() m=float('inf') for i in range(1,n): if(p[i]-p[i-1]<m): m=p[i]-p[i-1] if(mi[i]-mi[i-1]<m): m=mi[i]-mi[i-1] if m%2==0: print(m//2) else: print(m/2) ```
{ "language": "python", "test_cases": [ { "input": "2\n3\n0 0\n0 1\n0 -1\n3\n0 1\n1 0\n-1 0\n", "output": "0.5\n0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/WTBTR" }
vfc_3030
false
false
false
false
true
apps
verifiable_code
1048
Solve the following coding problem using the programming language python: There are three squares, each with side length a placed on the x-axis. The coordinates of centers of these squares are (x1, a/2), (x2, a/2) and (x3, a/2) respectively. All of them are placed with one of their sides resting on the x-axis. You are allowed to move the centers of each of these squares along the x-axis (either to the left or to the right) by a distance of at most K. Find the maximum possible area of intersections of all these three squares that you can achieve. That is, the maximum area of the region which is part of all the three squares in the final configuration. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers a, K denoting side length of the squares, and the maximum distance that you can move the center of any square. - The second line contains three space separated integers x1, x2, x3 -----Output----- For each test case, output a real number corresponding to the maximum area of the intersection of the three squares that you can obtain. Your answer will be considered correct if it has an absolute error of less than or equal to 10-2. -----Constraints----- - 1 ≤ T ≤ 105 - 1 ≤ a ≤ 105 - 0 ≤ K ≤ 106 - -106 ≤ x1, x2, x3 ≤ 106 -----Example----- Input 3 1 0 1 2 3 1 1 1 2 3 1 1 1 4 6 Output 0.000000 1.0000 0.0 -----Explanation----- Testcase 1: The figure below shows the three squares: Since K = 0, they cannot be moved, and since there is no region which belongs to all three squares, the answer is 0. Testcase 2: The starting configuration is the same as above, but now each of the squares can move 1 unit. So we can move the first square 1 unit to the right and the third square one unit to the left, and have all the three squares at x-coordinate = 2. Thus the entire square is part of all three squares, and the answer is 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=int(input()) for i in range(t): a,k=list(map(int,input().split())) x1,x2,x3=list(map(int,input().split())) big=max(x1,x2,x3) small=min(x1,x2,x3) q=big-small-2*k if q>=a: print(0) elif -1*q>=0: print(a*a) else: print(a*(a-q)) ```
{ "language": "python", "test_cases": [ { "input": "3\n1 0\n1 2 3\n1 1\n1 2 3\n1 1\n1 4 6\n", "output": "0.000000\n1.0000\n0.0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/TALESQUA" }
vfc_3034
false
false
false
false
false
apps
verifiable_code
1049
Solve the following coding problem using the programming language python: ----- ARRAY AND DISTINCT ELEMENTS ----- Chef is multitalented but he mistakenly took part in 2 contest which will take place at the same time. So while chef is busy at one cooking contest, he wants you to take part in coding contest. Chef wants u to solve this program for him. You have been given an array of size n. You have to calculate a subarray of size k with maximum sum having distinct elements same as original array. -----Input Format----- First line contains no. of test cases. Second line contains n and k. Third line contains array of n integers. -----Output----- Print maximum possible sum as stated in question -----Example Text Case----- Input: 1 10 6 8 8 3 5 3 8 5 7 7 7 Output: 37 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # for _ in range(int(input())): n,k = list(map(int,input().split())) arr = list(map(int,input().split())) s=set(arr) t1=len(s) max=-1 for i in range(n-k+1): temp=set(arr[i:i+k]) #print(temp,i,k+i+1) t=len(temp) if t1 == t: if max<sum(arr[i:k+i]): max=sum(arr[i:k+i]) print(max) ```
{ "language": "python", "test_cases": [ { "input": "1\n10 6\n8 8 3 5 3 8 5 7 7 7\n", "output": "37\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/APO12020/problems/APOC2_02" }
vfc_3038
false
false
false
false
false
apps
verifiable_code
1050
Solve the following coding problem using the programming language python: Lira is now very keen on compiler development. :) She knows that one of the most important components of a compiler, is its parser. A parser is, in simple terms, a software component that processes text, and checks it's semantic correctness, or, if you prefer, if the text is properly built. As an example, in declaring and initializing an integer, in C/C++, you can't do something like: int = x ;4 as the semantics of such statement is incorrect, as we all know that the datatype must precede an identifier and only afterwards should come the equal sign and the initialization value, so, the corrected statement should be: int x = 4; Today, Lira is concerned with an abstract instruction which is composed of the characters "<" and ">" , which she will use on the design of her language, L++ :D. She is using it as an abstraction for generating XML code Tags in an easier fashion and she understood that, for an expression to be valid, a "<" symbol must always have a corresponding ">" character somewhere (not necessary immediately) after it. Moreover, each ">" symbol should correspond to exactly one "<" symbol. So, for instance, the instructions: <<>> <> <><> are all valid. While: >> ><>< are not. Given some expressions which represent some instructions to be analyzed by Lira's compiler, you should tell the length of the longest prefix of each of these expressions that is valid, or 0 if there's no such a prefix. -----Input----- Input will consist of an integer T denoting the number of test cases to follow. Then, T strings follow, each on a single line, representing a possible expression in L++. -----Output----- For each expression you should output the length of the longest prefix that is valid or 0 if there's no such a prefix. -----Constraints----- - 1 ≤ T ≤ 500 - 1 ≤ The length of a single expression ≤ 106 - The total size all the input expressions is no more than 5*106 -----Example----- Input: 3 <<>> >< <>>> Output: 4 0 2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) for j in range(t): s=input() st=[] ans=0 for i in range(len(s)): if(s[i]=='>'): if(len(st)!=0 and st[-1]=='<'): st.pop() if(len(st)==0): ans=i+1 else: break else: st.append('<') print(ans) ```
{ "language": "python", "test_cases": [ { "input": "3\n<<>>\n><\n<>>>\n\n", "output": "4\n0\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/COMPILER" }
vfc_3042
false
false
false
false
false
apps
verifiable_code
1051
Solve the following coding problem using the programming language python: The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 3 2 3 4 -----Sample Output:----- 0 *1 **2 0 *1 **2 ***3 0 *1 **2 ***3 ****4 -----EXPLANATION:----- No need, else pattern can be decode easily. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) for i in range(0,t): my_ip = int(input().strip()) for xyz in range(my_ip+1): for abc in range(0,xyz+1): if abc == xyz: print(xyz,end="") else: print('*',end="") print() ```
{ "language": "python", "test_cases": [ { "input": "3\n2\n3\n4\n\n", "output": "0\n*1\n**2\n0\n*1\n**2\n***3\n0\n*1\n**2\n***3\n****4\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PTRN2020/problems/ITGUY38" }
vfc_3046
false
false
false
false
false
apps
verifiable_code
1052
Solve the following coding problem using the programming language python: You are given positive integers $N$ and $D$. You may perform operations of the following two types: - add $D$ to $N$, i.e. change $N$ to $N+D$ - change $N$ to $\mathop{\mathrm{digitsum}}(N)$ Here, $\mathop{\mathrm{digitsum}}(x)$ is the sum of decimal digits of $x$. For example, $\mathop{\mathrm{digitsum}}(123)=1+2+3=6$, $\mathop{\mathrm{digitsum}}(100)=1+0+0=1$, $\mathop{\mathrm{digitsum}}(365)=3+6+5=14$. You may perform any number of operations (including zero) in any order. Please find the minimum obtainable value of $N$ and the minimum number of operations required to obtain this value. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $N$ and $D$. -----Output----- For each test case, print a single line containing two space-separated integers — the minimum value of $N$ and the minimum required number of operations. -----Constraints----- - $1 \le T \le 10$ - $1 \le N, D \le 10^{10}$ -----Subtasks----- Subtask #1 (30 points): $1 \le N, D \le 100$ Subtask #2 (70 points): original constraints -----Example Input----- 3 2 1 9 3 11 13 -----Example Output----- 1 9 3 2 1 4 -----Explanation----- Example case 1: The value $N=1$ can be achieved by 8 successive "add" operations (changing $N$ to $10$) and one "digit-sum" operation. Example case 2: You can prove that you cannot obtain $N=1$ and $N=2$, and you can obtain $N=3$. The value $N=3$ can be achieved by one "add" and one "digitsum" operation, changing $9$ to $12$ and $12$ to $3$. Example case 3: $N=1$ can be achieved by operations "add", "add", "digitsum", "digitsum": $11 \rightarrow 24 \rightarrow 37 \rightarrow 10 \rightarrow 1$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from collections import deque T=int(input()) def break_down(num): count=0 while(len(num)!=1): temp=0 for i in range(0,len(num)): temp=temp+int(num[i]) num=str(temp) count=count+1 return (int(num),count) def digit_sum(num): temp=0 for i in range(0,len(num)): temp=temp+int(num[i]) num=temp return (num) while(T): queue=deque() count_n=0 count_d=0 T=T-1 N,d=[i for i in input().split()] n,count_n=break_down(N) D,count_D=break_down(d) dic={} if(D==1 or D==2 or D==4 or D==5 or D==7 or D==8): mini=1 elif(D==3 or D==6): mini=min(digit_sum(str(n+3)),digit_sum(str(n+6)),digit_sum(str(n+9))) else: mini=n queue.append((int(N),0)) ele=int(N) count=0 while(len(queue)!=0): ele,count=queue.popleft() if(ele==mini): break else: if(len(str(ele))==1): temp1=ele+int(d) queue.append((temp1,count+1)) else: temp2=digit_sum(str(ele)) temp1=ele+int(d) queue.append((temp2,count+1)) queue.append((temp1,count+1)) print(ele,count) ```
{ "language": "python", "test_cases": [ { "input": "3\n2 1\n9 3\n11 13\n\n", "output": "1 9\n3 2\n1 4\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/MINDSUM" }
vfc_3050
false
false
false
false
false
apps
verifiable_code
1053
Solve the following coding problem using the programming language python: Chef has a binary array in an unsorted manner. Cheffina challenges chef to find the transition point in the sorted (ascending) binary array. Here indexing is starting from 0. Note: Transition point always exists. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains two lines of input, $N$. - N space-separated binary numbers. -----Output:----- For each test case, output in a single line answer. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ -----Sample Input:----- 1 5 0 1 0 0 1 -----Sample Output:----- 3 -----EXPLANATION:----- binary array in sorted form will look like = [0, 0, 0, 1, 1] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for _ in range(int(input())): n=int(input()) A=list(map(int,input().split())) A.sort() for i in range(len(A)): if A[i]==1: print(i) break ```
{ "language": "python", "test_cases": [ { "input": "1\n5\n0 1 0 0 1\n\n", "output": "3\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PSTR2020/problems/ANITGUY5" }
vfc_3054
false
false
false
false
false
apps
verifiable_code
1054
Solve the following coding problem using the programming language python: Chef likes strings a lot but he likes palindromic strings even more. Today he found an old string s in his garage. The string is so old that some of its characters have faded and are unidentifiable now. Faded characters in the string are represented by '.' whereas other characters are lower case Latin alphabets i.e ['a'-'z']. Chef being the palindrome lover decided to construct the lexicographically smallest palindrome by filling each of the faded character ('.') with a lower case Latin alphabet. Can you please help him completing the task? -----Input----- First line of input contains a single integer T denoting the number of test cases. T test cases follow. First and the only line of each case contains string s denoting the old string that chef has found in his garage. -----Output----- For each test case, print lexicographically smallest palindrome after filling each faded character - if it possible to construct one. Print -1 otherwise. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ |s| ≤ 12345 - String s consists of ['a'-'z'] and '.' only. -----Subtasks-----Subtask #1 (47 points) - 1 ≤ T ≤ 50, 1 ≤ |S| ≤ 123 Subtask #2 (53 points) - 1 ≤ T ≤ 50, 1 ≤ |S| ≤ 12345 -----Example-----Input 3 a.ba cb.bc a.b Output abba cbabc -1 -----Explanation----- In example 1, you can create a palindrome by filling the faded character by 'b'. In example 2, you can replace the faded character by any character from 'a' to 'z'. We fill it by 'a', as it will generate the lexicographically smallest palindrome. In example 3, it is not possible to make the string s a palindrome. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python test=int(input()) for i in range(test): s=input() b=len(s) list1=[] for j in range(len(s)): if s[j]=='.': list1.append(j) for i in list1: if b-i-1 in list1 : if i!=b-i-1 and ((s[i] and s[b-i-1]) != 'a' ): s=s[:i]+'a'+s[i+1:b-i-1]+'a'+s[b-i:] else: s=s[:i]+'a'+s[i+1:] else: s=s[:i]+s[b-i-1]+s[i+1:] if s==s[::-1]: print(s) else: print(-1) ```
{ "language": "python", "test_cases": [ { "input": "3\na.ba\ncb.bc\na.b\n", "output": "abba\ncbabc\n-1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/LEXOPAL" }
vfc_3058
false
false
false
false
false
apps
verifiable_code
1055
Solve the following coding problem using the programming language python: Chef is planning a huge party for all of you and has ordered M pizzas. He wants to invite as many people to the party. However, he knows that everyone will have exactly one slice of a pizza (regardless of the size) and he wants to make sure that he has enough pizza slices. Chef is very lazy and will only make a total of N straight cuts among all the pizzas. Each pizza is also of different size and to avoid the slices getting too small the chef can only make a max of Ai cuts to the ith pizza. He wants to maximize the number of slices of pizza. Since chef is busy with preparing other aspects of the party he wants you to find out the maximum number of slices he can get following the constraints. If a pizza is not cut at all then it is considered as 1 slice. -----Input----- First line contains two integers M and N. The second line of input contains the array A. -----Output----- Output a single integer - the maximum number of slices chef can get. -----Constraints----- - 1 ≤ M ≤ 2*105 - 1 ≤ N,Ai ≤ 2*105 -----Subtasks----- - Subtask 1: 1 ≤ M,N ≤ 100 - 10 points - Subtask 2: 1 ≤ N ≤ 100, 1 ≤ M ≤ 105 - 20 points - Subtask 3: Original Constraints - 70 points -----Example----- Input: 5 10 1 2 3 4 5 Output: 31 -----Explanation----- Example case 1. One of the optimal way to cut would be to do {0, 1, 0, 4, 5} cuts. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here m,n=[int(i) for i in input().split()] arr=list(map(int,input().split())) arr=sorted(arr,reverse=True) ans=0 w=0 q=m for m in range(q): if(arr[m]>n): w=1 break ans+=1+(arr[m]*(arr[m]+1))//2 n-=arr[m] if(n==0): print(ans) else: if(w==1): print(ans+q-m+(n*(n+1))//2) else: print(ans) ```
{ "language": "python", "test_cases": [ { "input": "5 10\n1 2 3 4 5\n\n", "output": "31\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/PIZPAR" }
vfc_3062
false
false
false
false
false
apps
verifiable_code
1056
Solve the following coding problem using the programming language python: Write a program to check whether a triangle is valid or not, when the three angles of the triangle are the inputs. A triangle is valid if the sum of all the three angles is equal to 180 degrees. -----Input----- The first line contains an integer T, the total number of testcases. Then T lines follow, each line contains three angles A, B and C, of the triangle separated by space. -----Output----- For each test case, display 'YES' if the triangle is valid, and 'NO', if it is not, in a new line. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ A,B,C ≤ 180 -----Example----- Input 3 40 40 100 45 45 90 180 1 1 Output YES YES NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) for i in range(n): a,b,c=map(int,input().split()) if a>0 and b>0 and c>0 and a+b+c==180: print("YES") else: print("NO") ```
{ "language": "python", "test_cases": [ { "input": "3 \n40 40 100\n45 45 90\n180 1 1\n\n", "output": "YES\nYES\nNO\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/FLOW013" }
vfc_3066
false
false
false
false
false
apps
verifiable_code
1057
Solve the following coding problem using the programming language python: Alice is a very brilliant student. He considers '4' and '7' as Magic numbers. The numbers containing only magic numbers are also magical. Given a magic number N ,he wants to know what could be the next magical number greater than the given number. -----Input----- First line of input contains number of test cases T. For each test case, there is exits only one line containing a magic number N. -----Output----- For each test case, output a single line containing the next greater magical number. -----Constraints----- 1<=T<=1000 4<= N<=10^100 -----Example----- Input: 2 4 47 Output: 7 74 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math def magic(a,digits): m=a%10 if(m==4): return a+3 elif(m==7): p=list(str(a)) #print p for i in range(digits-1,-1,-1): #print p[i] if (p[i]=='4'): #print 'four' p[i]='7' p = ''.join(str(n) for n in p) return int(p) if ((p[i]=='7')&(i==0)): #print 'seven' p[i]='4' p.insert(0,4) p = ''.join(str(n) for n in p) return int(p) if(p[i]=='7'): #print 'seven only' p[i]='4' #print p[i] t=eval(input()) n=[] op=[] for i in range(0,t): n.append(eval(input())) for i in range(0,t): digits = int(math.log10(n[i]))+1 #print digits op.append(magic(n[i],digits)) #for i in range(0,t): #print n[i] for i in range(0,t): print(op[i]) ```
{ "language": "python", "test_cases": [ { "input": "2\n4\n47\n\n\n", "output": "7\n74\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ALKH2012/problems/NMAGIC" }
vfc_3070
false
false
false
false
false
apps
verifiable_code
1058
Solve the following coding problem using the programming language python: There is a popular apps named “Exbook” like “Facebook”. To sign up in this app , You have to make a strong password with more than 3 digits and less than 10 digits . But I am a pro hacker and so I make a Exbook hacking site . You need to login in this site to hack exbook account and then you will get a portal. You can give any user exbook login link using this site and when anyone login into exbook using your link ,you can see his/her password . But I made a mistake and so you cannot find original password in your portal . The portal showing you by adding two in every digit . So , now you have to find out the original password of an user if I give you the password which is showing in your portal . -----Input:----- The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains a single integer n which is showing in your portal . Mind it , every digit of n is greater than one . -----Output:----- Print , original password of user . -----Sample Input:----- 2 3527 47269 -----Sample Output:----- 1305 25047 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) for i in range(0,t): p=input() l=list(p) for j in range(0,len(l)): l[j]=int(l[j]) l[j]=l[j]-2 for j in range(0,len(l)): l[j]=str(l[j]) q=''.join(l) print(q) ```
{ "language": "python", "test_cases": [ { "input": "2\n3527\n47269\n", "output": "1305\n25047\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ABCC2020/problems/PASSHACK" }
vfc_3074
false
false
false
false
false
apps
verifiable_code
1059
Solve the following coding problem using the programming language python: You have an array A of size N containing only positive numbers. You have to output the maximum possible value of A[i]%A[j] where 1<=i,j<=N. -----Input----- The first line of each test case contains a single integer N denoting the size of the array. The next N lines contains integers A1, A2, ..., AN denoting the numbers -----Output----- Output a single integer answering what is asked in the problem. -----Subtask 1 (20 points)----- - 1 ≤ N ≤ 5000 - 1 ≤ A[i] ≤ 2*(10^9) -----Subtask 2 (80 points)----- - 1 ≤ N ≤ 1000000 - 1 ≤ A[i] ≤ 2*(10^9) -----Example----- Input: 2 1 2 Output: 1 -----Explanation----- There will be four values, A[0]%A[0] = 0, A[0]%A[1]=1, A[1]%A[0]=0, A[1]%A[1]=0, and hence the output will be the maximum among them all, that is 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) a = [] for i in range(n): a.append(int(input())) m1 = 0 m2 = 0 for e in a: if (e > m1): m2 = m1 m1 = e elif (e > m2 and e != m1): m2 = e ans = 0 for e in a: temp = m1%e if (temp>ans): ans = temp print(max(m2%m1,ans)) ```
{ "language": "python", "test_cases": [ { "input": "2\n1\n2\n", "output": "1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/LOCMAR16/problems/MOD" }
vfc_3078
false
false
false
false
false
apps
verifiable_code
1060
Solve the following coding problem using the programming language python: Chef and Abhishek both are fighting for the post of Chairperson to be part of ACE committee and are trying their best. To select only one student their teacher gave them a binary string (string consisting of only 0's and 1's) and asked them to find number of sub-strings present in the given string that satisfy the following condition: The substring should start with 0 and end with 1 or the substring should start with 1 and end with 0 but not start with 0 and end with 0 and start with 1 and end with 1. More formally, strings such as 100,0101 are allowed since they start and end with different characters. But strings such as 0110,1101 are not allowed because they start and end with same characters. Both Chef and Abhishek try their best to solve it but couldn't do it. You being a very good friend of Chef, he asks for your help so that he can solve it and become the Chairperson. -----Input:----- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the length of the string. The second line of each test case contains a binary string of length N. -----Output:----- For each test case, print a single line containing one integer ― the number of sub strings satisfying above conditions. -----Constraints----- - $1 \leq T \leq 1000$ - $2 \leq N \leq 10^9$ Binary string consist's only 0 and 1. -----Sample Input:----- 1 4 1010 -----Sample Output:----- 4 -----EXPLANATION:----- All possible substring are : { (1),(0),(1),(0),(10),(01),(10),(101),(010),(1010) }. Out of these only 4 substrings {(10),(01),(10),(1010)} start and end with different characters. Hence the answer 4. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def countSubstr(str, n, x, y): tot_count = 0 count_x = 0 for i in range(n): if str[i] == x: count_x += 1 if str[i] == y: tot_count += count_x return tot_count t=int(input()) for _ in range(t): n=int(input()) str=input() x='0' y='1' x1='1' y1='0' c1=countSubstr(str,n,x,y) c2=countSubstr(str,n,x1,y1) print(c1+c2) ```
{ "language": "python", "test_cases": [ { "input": "1\n4\n1010\n", "output": "4\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/CEFT2020/problems/QFUN" }
vfc_3082
false
false
false
false
false
apps
verifiable_code
1061
Solve the following coding problem using the programming language python: Chef is playing with an expression which consists of integer operands and the following binary Bitwise operators - AND, OR and XOR. He is trying to figure out that what could be the Maximum possible answer of the expression, given that he can perform the operation in any order i.e not necessarily follow the rule of Precedence of operators while evaluating the expression. After some time of consistent work Chef starts feeling exhausted and wants you to automate this process for him. Can you help him out? The expression has Bitwise operators in symbol format: - & stands for AND - | stands for OR - ^ stands for XOR NOTE : It is guaranteed that the expression will always be valid, also each OPERATOR will always be preceded and succeeded by an OPERAND. -----Input:----- - The first line of input contains a single integer $T$ denoting the number of test cases. - The only line of input for each test case is a $string$ which is the Chef's expression to evaluate. -----Output:----- For each test case print a single integer i.e the maximum possible value of Chef's expression. -----Constraints----- - $1 \leq T \leq 100$. - The number of OPERATORS in the expression will be atleast 1 and atmost 10. - Each OPERAND may range from 0 to $10^9$. -----Subtasks----- - 10 points : The number of OPERATORS in the expression will be atmost 5. - 20 points : The number of OPERATORS in the expression will be atmost 8. - 70 points : Original constraints. -----Sample Input:----- 2 3^40|10^2 92^95|56&2&3 -----Sample Output:----- 43 95 -----EXPLANATION:-----CASE 2 : - If we first compute (56 & 2), the expression becomes 92^95|0&3, since (56 & 2) yields $0$. - Now on computing (95 | 0), the expression becomes 92^95&3. - Further on computing (95 & 3), the expression becomes 92^3. - Finally (92 ^ 3) yields 95, which is the maximum value of the expression. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here def value(a, b, c): if(c == '&'): return a&b elif(c == '^'): return a^b elif(c == '|'): return a|b def break_rules(n, operator): if(len(n) == 1): return n elif(len(n) == 2): return [value(n[0], n[1], operator[0])] else: cont_ans = [] for i in range(1,len(n)): l1 = n[:i] l2 = n[i:] o1 = operator[:i] o2 = operator[i:] l1_ans = break_rules(l1, o1) l2_ans = break_rules(l2, o2) for k in l1_ans: for j in l2_ans: cont_ans.append(value(k, j, operator[i - 1])) return cont_ans t = int(input()) while t > 0 : operator = [] num = [] exp = input() temp = '' for i in range(len(exp)): if(ord(exp[i]) > 47 and ord(exp[i]) < 58): temp = temp + exp[i] else: num.append(int(temp)) temp = '' operator.append(exp[i]) if(i == len(exp) - 1): num.append(int(temp)) t -= 1 # print(num,operator) print(max(break_rules(num, operator))) ```
{ "language": "python", "test_cases": [ { "input": "2\n3^40|10^2\n92^95|56&2&3\n", "output": "43\n95\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/BIT2C" }
vfc_3086
false
false
false
false
false
apps
verifiable_code
1062
Solve the following coding problem using the programming language python: Ho, Ho, Ho! It's Christmas time and our friendly grandpa Santa Claus is busy distributing gifts to all the nice children. With the rising population, Santa's workload every year gets increased and he seeks your help to wrap the gifts with fancy wrapping papers while he gets them distributed. Everything was going great until you realised that you'll fall short of wrapping paper. But luckily, you get a very innovative idea, that will allow you to pack all the remaining gifts without worrying about lack of wrapping paper. Any guesses what the idea is? Using ice for wrapping, obviously! That's the only thing available at the North Pole. Now, in order to reduce your trips to the ice factory, you decide to write a program that helps you visualize how much ice is needed for a particular gift. -----Input:----- Input will consist of a single line with size $n$. -----Output:----- Print the ice wrapped gift box for the given size. -----Constraints----- - $0 \leq n \leq 1000$ -----Sample Input:----- 4 -----Sample Output:----- 4 4 4 4 4 4 4 4 3 3 3 3 3 4 4 3 2 2 2 3 4 4 3 2 1 2 3 4 4 3 2 2 2 3 4 4 3 3 3 3 3 4 4 4 4 4 4 4 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here def read_i_l(l=False): m = list(map(int, input().strip().split(" "))) if l: return list(m) else: return m def i(): return int(input().strip()) T = i() L = [] """for current in range(T): line = "" for i in range(current): line+=str((T-i)%10) for i in range(2*(T-current)-1): line+=str((T-current)%10) for i in range(current-1,-1,-1): line+=str((T-i)%10) L.append(line) L += L[-2::-1]""" if T >= 1: L = ["1"] for i in range(2,T+1): nL = [str(i)+(2*i-2)*(" "+str(i))] for l in L: nL.append(str(i)+" "+l+" "+str(i)) nL.append(str(i)+(2*i-2)*(" "+str(i))) L = nL for l in L: print(l) ```
{ "language": "python", "test_cases": [ { "input": "4\n", "output": "4 4 4 4 4 4 4\n4 3 3 3 3 3 4\n4 3 2 2 2 3 4\n4 3 2 1 2 3 4\n4 3 2 2 2 3 4\n4 3 3 3 3 3 4\n4 4 4 4 4 4 4\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/NQST2020/problems/ICEWRAP" }
vfc_3090
false
false
false
false
true
apps
verifiable_code
1063
Solve the following coding problem using the programming language python: Write a program to find the remainder when an integer A is divided by an integer B. -----Input----- The first line contains an integer T, the total number of test cases. Then T lines follow, each line contains two Integers A and B. -----Output----- For each test case, find the remainder when A is divided by B, and display it in a new line. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ A,B ≤ 10000 -----Example----- Input 3 1 2 100 200 40 15 Output 1 100 10 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python number = int(input()) for i in range(number): x = list(map(int, input().split(' '))) print(x[0]%x[1]) ```
{ "language": "python", "test_cases": [ { "input": "3\n1 2\n100 200\n40 15\n", "output": "1\n100\n10\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/FLOW002" }
vfc_3094
false
false
false
false
false
apps
verifiable_code
1064
Solve the following coding problem using the programming language python: Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import gcd import sys input=lambda : sys.stdin.readline().strip() c=lambda x: 10**9 if(x=="?") else int(x) def main(): for _ in range(int(input())): s=list(input())[::-1] l=['F','E','H','C'] i=0 while(i<len(s)): if(i+3<len(s)): f=True for j in range(i,i+4): if(l[j-i]==s[j] or s[j]=='?'): pass else: f=False break if(f): for j in range(i,i+4): s[j]=l[j-i] if(s[i]=="?"): s[i]='A' else: if(s[i]=="?"): s[i]="A" i+=1 print(*s[::-1],sep='') main() ```
{ "language": "python", "test_cases": [ { "input": "5\n????CIELIS???E?\n????CIELISOUR???F\nT?KEITE?SY\n????????\n???C???\n", "output": "CHEFCIELISACHEF\nCHEFCIELISOURCHEF\nTAKEITEASY\nCHEFCHEF\nAAACHEF\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/MANYCHEF" }
vfc_3098
false
false
false
false
false
apps
verifiable_code
1065
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−2$N+M-2$ inclusive, Chef wants to calculate the number of unordered pairs of distinct houses with distance equal to d$d$. Please help him! -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each i$i$ (1≤i≤N$1 \le i \le N$), the i$i$-th of these lines contains a binary string with length M$M$; for each j$j$ (1≤j≤M$1 \le j \le M$), the j$j$-th character of this string is '1' if the cell in the i$i$-th row and j$j$-th column contains a house or '0' if it is empty. -----Output----- For each test case, print a single line containing N+M−2$N+M-2$ space-separated integers. For each valid i$i$, the i$i$-th integer should denote the number of pairs with distance i$i$. -----Constraints----- - 1≤T≤3$1 \le T \le 3$ - 2≤N,M≤300$2 \le N, M \le 300$ -----Subtasks----- Subtask #1 (50 points): N,M≤50$N, M \le 50$ Subtask #2 (50 points): original constraints -----Example Input----- 1 3 4 0011 0000 0100 -----Example Output----- 1 0 1 1 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```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 range(len(c[g])): for j in range(len(c)): for k in range(len(c[j])): if (j>g) or(j==g and k>h): if c[g][h]-c[j][k]>=0: i.append(c[g][h]-c[j][k]+j-g) else: i.append(-1*(c[g][h]-c[j][k])+j-g) l=[m for m in range(1,N+M-1)] for n in l: print(i.count(n),end=' ') ```
{ "language": "python", "test_cases": [ { "input": "1\n3 4\n0011\n0000\n0100\n", "output": "1 0 1 1 0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/AVGMAT" }
vfc_3102
false
false
false
false
false
apps
verifiable_code
1066
Solve the following coding problem using the programming language python: ----- CHEF N TIMINGS ----- One day chef was working with some random numbers. Then he found something interesting. He observed that no 240, 567, 9999 and 122 and called these numbers nice as the digits in numbers are in increasing order. Also he called 434, 452, 900 are not nice as digits are in decreasing order Now you are given a no and chef wants you to find out largest "nice" integer which is smaller than or equal to the given integer. -----Constraints----- 1< t < 1000 1< N < 10^18 -----Input Format----- First line contains no. of test cases t. Then t test cases follow. Each test case contain a integer n. -----Output----- Output a integer for each test case in a new line which is largest nice integer smaller or equal to the given integer. -----Example Text Case----- Input: 1 132 Output: 129 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for _ in range(int(input())): n=input().rstrip() n=[ele for ele in n] l=len(n) m=10**18+8 ini=1 for i in range(l-1,-1,-1): if int(n[i])<=m: if ini==1: m=int(n[i]) else: m=max(m,n[i]) else: m=int(n[i])-1 n[i]=str(m) for j in range(l-1,i,-1): n[j]='9' i=0 while n[i]=='0': i+=1 print("".join(n[i:])) ```
{ "language": "python", "test_cases": [ { "input": "1\n132\n", "output": "129\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/APO12020/problems/APOC2_04" }
vfc_3106
false
false
false
false
false
apps
verifiable_code
1067
Solve the following coding problem using the programming language python: Chef has a strip of length $N$ units and he wants to tile it using $4$ kind of tiles -A Red tile of $2$ unit length -A Red tile of $1$ unit length -A Blue tile of $2$ unit length -A Blue tile of $1$ unit length Chef is having an infinite supply of each of these tiles. He wants to find out the number of ways in which he can tile the strip. Help him find this number. Since this number can be large, output your answer modulo 1000000007 ($10^9 + 7$). -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, an integer $N$. -----Output:----- For each testcase, output in a single line your answer modulo 1000000007. -----Constraints----- - $1 \leq T \leq 1000$ - $2 \leq N \leq 10^{18}$ -----Sample Input:----- 1 2 -----Sample Output:----- 6 -----EXPLANATION:----- It can be seen that for a strip of length $2$, there are $6$ possible configurations. $NOTE : $ 2 tiles of 1 unit length are different from 1 tile of 2 unit length. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # Fibonacci Series using # Optimized Method # function that returns nth # Fibonacci number MOD = 1000000007 def fib(n): F = [[2, 2], [1, 0]] power(F, n - 1) ans = [6, 2] return (F[0][0] * 6 + F[0][1] * 2) % MOD # return F[0][0] def multiply(F, M): x = (F[0][0] * M[0][0] + F[0][1] * M[1][0]) % MOD y = (F[0][0] * M[0][1] + F[0][1] * M[1][1]) % MOD z = (F[1][0] * M[0][0] + F[1][1] * M[1][0]) % MOD w = (F[1][0] * M[0][1] + F[1][1] * M[1][1]) % MOD F[0][0] = x F[0][1] = y F[1][0] = z F[1][1] = w def power(F, n): if n == 0 or n == 1: return M = [[2, 2], [1, 0]] power(F, n // 2) multiply(F, F) if n % 2 != 0: multiply(F, M) for _ in range(int(input())): n = int(input()) ans = 1 if n == 0: ans = 1 elif n == 1: ans = 2 elif n == 2: ans = 6 else: ans = fib(n-1) print(ans) ```
{ "language": "python", "test_cases": [ { "input": "1\n2\n", "output": "6\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COG2020/problems/COG2004" }
vfc_3110
false
false
false
false
false
apps
verifiable_code
1068
Solve the following coding problem using the programming language python: Ada's classroom contains $N \cdot M$ tables distributed in a grid with $N$ rows and $M$ columns. Each table is occupied by exactly one student. Before starting the class, the teacher decided to shuffle the students a bit. After the shuffling, each table should be occupied by exactly one student again. In addition, each student should occupy a table that is adjacent to that student's original table, i.e. immediately to the left, right, top or bottom of that table. Is it possible for the students to shuffle while satisfying all conditions of the teacher? -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $N$ and $M$. -----Output----- For each test case, print a single line containing the string "YES" if it is possible to satisfy the conditions of the teacher or "NO" otherwise (without quotes). -----Constraints----- - $1 \le T \le 5,000$ - $2 \le N, M \le 50$ -----Example Input----- 2 3 3 4 4 -----Example Output----- NO YES -----Explanation----- Example case 2: The arrows in the following image depict how the students moved. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) for _ in range(t): N, M=map(int,input().split()) if(N%2==0 or M%2==0): print("YES") else: print("NO") ```
{ "language": "python", "test_cases": [ { "input": "2\n3 3\n4 4\n", "output": "NO\nYES\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/ADASCOOL" }
vfc_3114
false
false
false
false
false
apps
verifiable_code
1069
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. -----Input----- The first line contains an integer T, the total number of test cases. Then follow T lines, each line contains two Integers A and B. -----Output----- For each test case, add A and B and display it in a new line. -----Constraints----- - 1 ≤ T ≤ 1000 - 0 ≤ A,B ≤ 10000 -----Example----- Input 3 1 2 100 200 10 40 Output 3 300 50 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```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, input().split(' '))) ans = a + b print(ans) ```
{ "language": "python", "test_cases": [ { "input": "3\n1 2\n100 200\n10 40\n", "output": "3\n300\n50\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/FLOW001" }
vfc_3118
false
false
false
false
false
apps
verifiable_code
1070
Solve the following coding problem using the programming language python: and Bengali as well. There are N$N$ cats (numbered 1$1$ through N$N$) and M$M$ rats (numbered 1$1$ through M$M$) on a line. Each cat and each rat wants to move from some point to some (possibly the same) point on this line. Naturally, the cats also want to eat the rats when they get a chance. Both the cats and the rats can only move with constant speed 1$1$. For each valid i$i$, the i$i$-th cat is initially sleeping at a point a_i$a_i$. At a time s_i$s_i$, this cat wakes up and starts moving to a final point b_i$b_i$ with constant velocity and without any detours (so it arrives at this point at the time e_i = s_i + |a_i-b_i|$e_i = s_i + |a_i-b_i|$). After it arrives at the point b_i$b_i$, it falls asleep again. For each valid i$i$, the i$i$-th rat is initially hiding at a point c_i$c_i$. At a time r_i$r_i$, this rat stops hiding and starts moving to a final point d_i$d_i$ in the same way as the cats ― with constant velocity and without any detours, arriving at the time q_i = r_i + |c_i-d_i|$q_i = r_i + |c_i-d_i|$ (if it does not get eaten). After it arrives at the point d_i$d_i$, it hides again. If a cat and a rat meet each other (they are located at the same point at the same time), the cat eats the rat, the rat disappears and cannot be eaten by any other cat. A sleeping cat cannot eat a rat and a hidden rat cannot be eaten ― formally, cat i$i$ can eat rat j$j$ only if they meet at a time t$t$ satisfying s_i \le t \le e_i$s_i \le t \le e_i$ and r_j \le t \le q_j$r_j \le t \le q_j$. Your task is to find out which rats get eaten by which cats. It is guaranteed that no two cats will meet a rat at the same time. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each i$i$ (1 \le i \le N$1 \le i \le N$), the i$i$-th of these lines contains three space-separated integers a_i$a_i$, b_i$b_i$ and s_i$s_i$. - M$M$ more lines follow. For each i$i$ (1 \le i \le M$1 \le i \le M$), the i$i$-th of these lines contains three space-separated integers c_i$c_i$, d_i$d_i$ and r_i$r_i$. -----Output----- For each test case, print M$M$ lines. For each valid i$i$, the i$i$-th of these lines should contain a single integer ― the number of the cat that will eat the i$i$-th rat, or -1$-1$ if no cat will eat this rat. -----Constraints----- - 1 \le T \le 10$1 \le T \le 10$ - 0 \le N \le 1,000$0 \le N \le 1,000$ - 1 \le M \le 1,000$1 \le M \le 1,000$ - 1 \le a_i, b_i, s_i \le 10^9$1 \le a_i, b_i, s_i \le 10^9$ for each valid i$i$ - 1 \le c_i, d_i, r_i \le 10^9$1 \le c_i, d_i, r_i \le 10^9$ for each valid i$i$ - all initial and final positions of all cats and rats are pairwise distinct -----Example Input----- 2 8 7 2 5 1 1 4 1 9 14 10 20 7 9 102 99 1 199 202 1 302 299 3 399 402 3 6 3 1 10 15 10 100 101 1 201 200 1 300 301 5 401 400 5 1000 1010 1020 8 8 2 8 2 12 18 2 22 28 4 32 38 4 48 42 2 58 52 3 68 62 1 78 72 3 3 6 3 13 19 3 21 25 3 31 39 3 46 43 4 59 53 2 65 61 4 79 71 2 -----Example Output----- 1 4 5 6 7 8 -1 1 2 3 4 5 6 7 8 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here # cook your dish here class Animal: def __init__(self): start, end, starting_time = map(int, input().split()) self.ending_time = starting_time + abs(start - end) self.velocity = 1 if end >= start else -1 self.eaten_by = -1, 10 ** 10 self.start = start self.end = end self.starting_time = starting_time def will_collide(self, z): if self.starting_time > z.ending_time or self.ending_time < z.starting_time: return False if self.velocity == z.velocity: if self.starting_time > z.starting_time: self, z = z, self if z.start == self.start + self.velocity * (z.starting_time - self.starting_time): return z.starting_time else: return False if self.velocity == -1: self, z = z, self t = ( z.start - self.start + z.starting_time + self.starting_time ) / 2 return t if self.starting_time <= t <= self.ending_time and z.starting_time <= t <= z.ending_time else False def main(): for _ in range(int(input())): no_cats, no_rats = map(int, input().split()) Cats = [Animal() for i in range(no_cats)] for i in range(no_rats): rat = Animal() for j in range(no_cats): time = rat.will_collide(Cats[j]) if time: # print(time) if time < rat.eaten_by[1]: rat.eaten_by = j + 1, time print(rat.eaten_by[0]) main() ```
{ "language": "python", "test_cases": [ { "input": "2\n8 7\n2 5 1\n1 4 1\n9 14 10\n20 7 9\n102 99 1\n199 202 1\n302 299 3\n399 402 3\n6 3 1\n10 15 10\n100 101 1\n201 200 1\n300 301 5\n401 400 5\n1000 1010 1020\n8 8\n2 8 2\n12 18 2\n22 28 4\n32 38 4\n48 42 2\n58 52 3\n68 62 1\n78 72 3\n3 6 3\n13 19 3\n21 25 3\n31 39 3\n46 43 4\n59 53 2\n65 61 4\n79 71 2\n", "output": "1\n4\n5\n6\n7\n8\n-1\n1\n2\n3\n4\n5\n6\n7\n8\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CATSRATS" }
vfc_3122
false
false
false
false
true
apps
verifiable_code
1071
Solve the following coding problem using the programming language python: Bob just learned about bitwise operators. Since Alice is an expert, she decided to play a game, she will give a number $x$ to Bob and will ask some questions: There will be 4 different kinds of queries:- - Alice gives an integer $i$ and Bob has to report the status of the $i^{th}$ bit in $x$, the answer is $"ON"$ if it is on else $"OFF"$. - Alice gives an integer $i$ and Bob has to turn on the $i^{th}$ bit in $x$. - Alice gives an integer $i$ and Bob has to turn off the $i^{th}$ bit in $x$. - Alice gives two integers $p$ and $q$ and in the binary representation of $x$ Bob has to swap the $p^{th}$ and the $q^{th}$ bits. The value of $x$ changes after any update operation. positions $i$, $p$, and $q$ are always counted from the right or from the least significant bit. If anyone of $i$, $p$, or $q$ is greater than the number of bits in the binary representation of $x$, consider $0$ at that position. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - the first line of each test case contains two space-separated integers $x, Q$. - $2Q$ lines follow. - first line is an integer, the query type. - for each query of type 1 to 3, there will be the integer $i$ - for the query of type 4, there will be two space-separated integers, the integers $p, q$ -----Output:----- For the queries of the first kind, print $"ON"$ or $"OFF"$. -----Constraints----- - $1 \leq T \leq 10^3$ - $1 \leq x \leq 10^9$ - $1 \leq Q \leq 10^3$ - $1 \leq i,p,q \leq 30$ -----Sample Input----- 1 2 2 2 1 1 1 -----Sample Output:----- ON -----EXPLANATION:----- the binary representation of 2 is 10 for query 1, we just have to update x to 11 (or 3 in decimal). for the next query, x is now 3 or 11 in binary so the answer is ON. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) while t>0: n,q=list(map(int,input().split())) blst=[0] for i in range(1,65): blst.append(0) i=1 while n>0: if n%2: blst[i]=1 n//=2 i+=1 while q>0: n=int(input()) if n==1: p=int(input()) if blst[p]: print('ON') else: print('OFF') elif n==2: p=int(input()) if blst[p]==0: blst[p]=1 elif n==3: p=int(input()) if blst[p]==1: blst[p]=0 else: p,r=list(map(int,input().split())) if blst[p]!=blst[r]: blst[p]+=1 blst[p]%=2 blst[r]+=1 blst[r]%=2 q-=1 t-=1 ```
{ "language": "python", "test_cases": [ { "input": "1\n2 2\n2\n1\n1\n1\n", "output": "ON\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ENDE2020/problems/ENCDEC03" }
vfc_3126
false
false
false
false
false
apps
verifiable_code
1072
Solve the following coding problem using the programming language python: Problem description. Winston and Royce love sharing memes with each other. They express the amount of seconds they laughed ar a meme as the number of ‘XD’ subsequences in their messages. Being optimization freaks, they wanted to find the string with minimum possible length and having exactly the given number of ‘XD’ subsequences. -----Input----- - The first line of the input contains an integer T denoting the number of test cases. - Next T lines contains a single integer N, the no of seconds laughed. -----Output----- - For each input, print the corresponding string having minimum length. If there are multiple possible answers, print any. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 - 1 ≤ Sum of length of output over all testcases ≤ 5*105 -----Example----- Input: 1 9 Output: XXXDDD -----Explanation----- Some of the possible strings are - XXDDDXD,XXXDDD,XDXXXDD,XDXDXDD etc. Of these, XXXDDD is the smallest. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=int(input()) for i in range(t): n=int(input()) r=int(n**(.5)) d=n-r*r m=d%r print('X'*m+'D'*(m>0)+'X'*(r-m)+'D'*(r+d//r)) ```
{ "language": "python", "test_cases": [ { "input": "1\n9\n", "output": "XXXDDD\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/XDS" }
vfc_3130
false
false
false
false
false
apps
verifiable_code
1073
Solve the following coding problem using the programming language python: You are given two integers $N$ and $M$. Find the number of sequences $A_1, A_2, \ldots, A_N$, where each element is an integer between $1$ and $M$ (inclusive) and no three consecutive elements are equal. Since this number could be very large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $N$ and $M$. -----Output----- For each test case, print a single line containing one integer ― the number of valid sequences modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le N, M \le 10^{18}$ -----Subtasks----- Subtask #1 (50 points): - $T \le 20$ - $N \le 10^5$ Subtask #2 (50 points): original constraints -----Example Input----- 2 2 2 3 4 -----Example Output----- 4 60 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys def fin(): return sys.stdin.readline().strip() def fout(s, end="\n"): sys.stdout.write(str(s)+end) MOD = pow(10, 9)+7 t = int(input()) while t>0: t -= 1 n, m = list(map(int, fin().split())) if n == 1: print(m%MOD) continue dp1 = m*(m-1) dp2 = m for i in range(3, n+1): temp = dp2 dp2 = dp1 dp1 = (temp*(m-1))%MOD+(dp1*(m-1))%MOD print((dp1+dp2)%MOD) ```
{ "language": "python", "test_cases": [ { "input": "2\n2 2\n3 4\n", "output": "4\n60\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CARR" }
vfc_3134
false
false
false
false
false
apps
verifiable_code
1074
Solve the following coding problem using the programming language python: Chef is making Window frames for his new office, for this he has n wooden Logs whose lengths are l1, l2, … ln respectively. Chef Doesn’t want to break any logs or Stick 2 or more logs together. To make a h × w Window Frame, he needs two Logs with lengths equal h and two with length . The Chef wants as much sunlight in as possible and for it he has decided to make from the available logs as many frames as possible. Help him in finding the number of window Frames that he can make. Note : Chef do not need to use all the logs Input: The first line of the input contains a single integer T denoting the number of test cases. The description of each test case follows :. The first line of each test case contains a single integer n the number of wooden logs. The second line contains n space-separated integers l1,l2,l3….ln The length of each wooden log Output: The only line in Output Contains single Integer denoting the maximum possible number of Wooden Frames. Constraints: 1 ≤ T ≤ 10 1 ≤ n ≤ 100 1 ≤ li ≤ 10000 Example Input: 2 4 1 2 1 2 8 1 2 1 3 4 1 5 6 Example Output: 1 0 Explanation : First Case : We can build a frame of dimension 1x2 as two logs of each dimension are available. Second Case : We can’t build any Frame as no logs of length except 1 have more than one piece. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) j=0 while j<t: n=int(input()) lst=list(map(int,input().split())) s=set() d=list() for i in lst: if i in s: s.remove(i) d.append(i) else: s.add(i) x=len(d) if x%2==0: print(x//2) else: print((x-1)//2) j+=1 ```
{ "language": "python", "test_cases": [ { "input": "2\n4\n1 2 1 2\n8\n1 2 1 3 4 1 5 6\n", "output": "1\n0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/NITJ2021/problems/CIN005" }
vfc_3138
false
false
false
false
false
apps
verifiable_code
1075
Solve the following coding problem using the programming language python: Henry and Derek are waiting on a room, eager to join the Snackdown 2016 Qualifier Round. They decide to pass the time by playing a game. In this game's setup, they write N positive integers on a blackboard. Then the players take turns, starting with Henry. In a turn, a player selects one of the integers, divides it by 2, 3, 4, 5 or 6, and then takes the floor to make it an integer again. If the integer becomes 0, it is erased from the board. The player who makes the last move wins. Henry and Derek are very competitive, so aside from wanting to win Snackdown, they also want to win this game. Assuming they play with the optimal strategy, your task is to predict who wins the game. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the number of integers they wrote on the board. The second line contains N space-separated integers A1, A2, ..., AN denoting the integers themselves. -----Output----- For each test case, output a single line containing either “Henry” or “Derek” (without quotes), denoting the winner of the game. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 100 - 1 ≤ Ai ≤ 1018 -----Example----- Input:2 2 3 4 3 1 3 5 Output:Henry Derek -----Explanation----- Example case 1. In this test case, the numbers on the board are [3,4]. Henry can win by selecting 4 and then dividing it by 2. The integers on the board are now [3,2]. Derek now has a couple of choices: - Derek can divide 2 by 3, 4, 5 or 6, making it 0 and removing it. Now only one integer remains on the board, 3, and Henry can just divide it by 6 to finish, and win, the game. - Derek can divide 3 by 4, 5 or 6, making it 0 and removing it. Now only one integer remains on the board, 2, and Henry can just divide it by 6 to finish, and win, the game. - Derek can divide 2 by 2. Now the integers are [1,3]. Henry can respond by dividing 3 by 3. The integers are now [1,1]. Now Derek has no choice but to divide 1 by 2, 3, 4, 5 or 6 and remove it (because it becomes 0). Henry can respond by dividing the remaining 1 by 2 to finish, and win, the game. - Derek can divide 3 by 2 or 3. Now the integers are [1,2]. Henry can respond by dividing 2 by 2. The integers are now [1,1]. This leads to a situation as in the previous case and Henry wins. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python gb = [0, 1, 2, 2, 3, 3] ga = [0 for x in range(70)] gag = [0 for x in range(70)] ga[0] = 1 gag[0] = 0 for i in range(1, 70): if i % 4 == 0: ga[i] = 1.5 * ga[i-1] gag[i] = 0 else: ga[i] = 2 * ga[i-1] gag[i] = gag[i-1] + 1 def g(n): if n < 6: return gb[n] else: x = n / 6 a = 0 for i, k in enumerate(ga): if k <= x: a = i else: break return gag[a] t = int(input()) for q in range(t): n = int(input()) a = list(map(int, input().split())) res = g(a[0]) for i in range(1, n): res ^= g(a[i]) if res == 0: print("Derek") else: print("Henry") ```
{ "language": "python", "test_cases": [ { "input": "2\n2\n3 4\n3\n1 3 5\n", "output": "Henry\nDerek\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/SNCKQL16/problems/FDIVGAME" }
vfc_3142
false
false
false
false
false
apps
verifiable_code
1076
Solve the following coding problem using the programming language python: You are given an unweighted tree with N$N$ nodes (numbered 1$1$ through N$N$). Let's denote the distance between any two nodes p$p$ and q$q$ by d(p,q)$d(p, q)$. You should answer Q$Q$ queries. In each query, you are given parameters a$a$, da$d_a$, b$b$, db$d_b$, and you should find a node x$x$ such that d(x,a)=da$d(x, a) = d_a$ and d(x,b)=db$d(x, b) = d_b$, or determine that there is no such node. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains two space-separated integers N$N$ and Q$Q$. - Each of the next N−1$N-1$ lines contains two space-separated integers u$u$ and v$v$ denoting that nodes u$u$ and v$v$ are connected by an edge. - Each of the next Q$Q$ lines contains four space-separated integers a$a$, da$d_a$, b$b$ and db$d_b$ describing a query. -----Output----- For each query, print a single line containing one integer ― the number of a node satisfying the given requirements, or −1$-1$ if no such node exists. If there are multiple solutions, you may output any one. -----Constraints----- - 1≤T≤1,000$1 \le T \le 1,000$ - 1≤N,Q≤106$1 \le N, Q \le 10^6$ - 1≤u,v≤N$1 \le u, v \le N$ - the graph on the input is a tree - 1≤a,b≤N$1 \le a, b \le N$ - 1≤da,db<N$1 \le d_a, d_b < N$ - the sum of N$N$ over all test cases does not exceed 106$10^6$ - the sum of Q$Q$ over all test cases does not exceed 106$10^6$ -----Subtasks----- Subtask #1 (50 points): - 1≤N,Q≤1,000$1 \le N, Q \le 1,000$ - the sum of N$N$ over all test cases does not exceed 1,000$1,000$ - the sum of Q$Q$ over all test cases does not exceed 1,000$1,000$ Subtask #2 (50 points): original constraints -----Example Input----- 1 5 3 1 2 2 3 3 4 3 5 2 1 4 1 2 2 4 2 1 1 2 1 -----Example Output----- 3 5 -1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here class TestCase: def __init__(self): [self.node_count, self.query_count] = read_line() def fill_nodes(self): self.nodes = {n+1: [] for n in range(self.node_count)} for i in range(self.node_count -1): new_node_1, new_node_2 = read_line() self.nodes[new_node_1].append(new_node_2) self.nodes[new_node_2].append(new_node_1) def resolve_query(self, query): a, d_a, b, d_b = query suiting_a = self.find_nodes_with_distance(a, d_a) suiting_b = self.find_nodes_with_distance(b, d_b) fitting = [node for node in suiting_a if node in suiting_b] if len(fitting) == 0: return -1 else: return fitting[0] def find_nodes_with_distance(self, start_node, distance): from_nodes = {start_node} passed_nodes = from_nodes for i in range(distance): to_nodes = set() # add all adjacent nodes for node in from_nodes: to_nodes.update(self.nodes[node]) # no backtracking for node in passed_nodes: if node in to_nodes: to_nodes.remove(node) # update which nodes are passed passed_nodes.update(to_nodes) # go another round with the new nodes found from_nodes = to_nodes return list(from_nodes) def read_line(): line = input() return [int(s) for s in line.split(' ')] num_testcases = int(input()) for i in range(num_testcases): testcase = TestCase() testcase.fill_nodes() for q in range(testcase.query_count): query = read_line() print(testcase.resolve_query(query)) ```
{ "language": "python", "test_cases": [ { "input": "1\n5 3\n1 2\n2 3\n3 4\n3 5\n2 1 4 1\n2 2 4 2\n1 1 2 1\n", "output": "3\n5\n-1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/DDQUERY" }
vfc_3146
false
false
false
false
true
apps
verifiable_code
1077
Solve the following coding problem using the programming language python: Chef recently printed directions from his home to a hot new restaurant across the town, but forgot to print the directions to get back home. Help Chef to transform the directions to get home from the restaurant. A set of directions consists of several instructions. The first instruction is of the form "Begin on XXX", indicating the street that the route begins on. Each subsequent instruction is of the form "Left on XXX" or "Right on XXX", indicating a turn onto the specified road. When reversing directions, all left turns become right turns and vice versa, and the order of roads and turns is reversed. See the sample input for examples. -----Input----- Input will begin with an integer T, the number of test cases that follow. Each test case begins with an integer N, the number of instructions in the route. N lines follow, each with exactly one instruction in the format described above. -----Output----- For each test case, print the directions of the reversed route, one instruction per line. Print a blank line after each test case. -----Constraints----- - 1 ≤ T ≤ 15 - 2 ≤ N ≤ 40 - Each line in the input will contain at most 50 characters, will contain only alphanumeric characters and spaces and will not contain consecutive spaces nor trailing spaces. By alphanumeric characters we mean digits and letters of the English alphabet (lowercase and uppercase). -----Sample Input----- 2 4 Begin on Road A Right on Road B Right on Road C Left on Road D 6 Begin on Old Madras Road Left on Domlur Flyover Left on 100 Feet Road Right on Sarjapur Road Right on Hosur Road Right on Ganapathi Temple Road -----Sample Output----- Begin on Road D Right on Road C Left on Road B Left on Road A Begin on Ganapathi Temple Road Left on Hosur Road Left on Sarjapur Road Left on 100 Feet Road Right on Domlur Flyover Right on Old Madras Road -----Explanation----- In the first test case, the destination lies on Road D, hence the reversed route begins on Road D. The final turn in the original route is turning left from Road C onto Road D. The reverse of this, turning right from Road D onto Road C, is the first turn in the reversed route. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) for i in range(t): n = int(input()) dir = [] for j in range(n): dir.append(input().strip().split()) for j in range(n-1): if dir[j+1][0] == 'Right': dir[j][0] = 'Left' else: dir[j][0] = 'Right' dir[n-1][0] = 'Begin' for j in reversed(dir): print(' '.join(j)) ```
{ "language": "python", "test_cases": [ { "input": "2\n4\nBegin on Road A\nRight on Road B\nRight on Road C\nLeft on Road D\n6\nBegin on Old Madras Road\nLeft on Domlur Flyover\nLeft on 100 Feet Road\nRight on Sarjapur Road\nRight on Hosur Road\nRight on Ganapathi Temple Road\n\n", "output": "Begin on Road D\nRight on Road C\nLeft on Road B\nLeft on Road A\n\nBegin on Ganapathi Temple Road\nLeft on Hosur Road\nLeft on Sarjapur Road\nLeft on 100 Feet Road\nRight on Domlur Flyover\nRight on Old Madras Road\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COOK29/problems/DIRECTI" }
vfc_3150
false
false
false
false
false
apps
verifiable_code
1078
Solve the following coding problem using the programming language python: For her next karate demonstration, Ada will break some bricks. Ada stacked three bricks on top of each other. Initially, their widths (from top to bottom) are $W_1, W_2, W_3$. Ada's strength is $S$. Whenever she hits a stack of bricks, consider the largest $k \ge 0$ such that the sum of widths of the topmost $k$ bricks does not exceed $S$; the topmost $k$ bricks break and are removed from the stack. Before each hit, Ada may also decide to reverse the current stack of bricks, with no cost. Find the minimum number of hits Ada needs in order to break all bricks if she performs the reversals optimally. You are not required to minimise the number of reversals. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains four space-separated integers $S$, $W_1$, $W_2$ and $W_3$. -----Output----- For each test case, print a single line containing one integer ― the minimum required number of hits. -----Constraints----- - $1 \le T \le 64$ - $1 \le S \le 8$ - $1 \le W_i \le 2$ for each valid $i$ - it is guaranteed that Ada can break all bricks -----Subtasks----- Subtask #1 (50 points): $W_1 = W_2 = W_3$ Subtask #2 (50 points): original constraints -----Example Input----- 3 3 1 2 2 2 1 1 1 3 2 2 1 -----Example Output----- 2 2 2 -----Explanation----- Example case 1: Ada can reverse the stack and then hit it two times. Before the first hit, the widths of bricks in the stack (from top to bottom) are $(2,2,1)$. After the first hit, the topmost brick breaks and the stack becomes $(2,1)$. The second hit breaks both remaining bricks. In this particular case, it is also possible to hit the stack two times without reversing. Before the first hit, it is $(1, 2, 2)$. The first hit breaks the two bricks at the top (so the stack becomes $(2)$) and the second hit breaks the last brick. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=int(input()) for i in range(t): n,w1,w2,w3=map(int,input().split()) if n>=w1+w2+w3: print(1) elif n>=w1+w2 or n>=w2+w3: print(2) else: print(3) ```
{ "language": "python", "test_cases": [ { "input": "3\n3 1 2 2\n2 1 1 1\n3 2 2 1\n", "output": "2\n2\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/BRKBKS" }
vfc_3154
false
false
false
false
false
apps
verifiable_code
1079
Solve the following coding problem using the programming language python: Kostya likes the number 4 much. Of course! This number has such a lot of properties, like: - Four is the smallest composite number; - It is also the smallest Smith number; - The smallest non-cyclic group has four elements; - Four is the maximal degree of the equation that can be solved in radicals; - There is four-color theorem that states that any map can be colored in no more than four colors in such a way that no two adjacent regions are colored in the same color; - Lagrange's four-square theorem states that every positive integer can be written as the sum of at most four square numbers; - Four is the maximum number of dimensions of a real division algebra; - In bases 6 and 12, 4 is a 1-automorphic number; - And there are a lot more cool stuff about this number! Impressed by the power of this number, Kostya has begun to look for occurrences of four anywhere. He has a list of T integers, for each of them he wants to calculate the number of occurrences of the digit 4 in the decimal representation. He is too busy now, so please help him. -----Input----- The first line of input consists of a single integer T, denoting the number of integers in Kostya's list. Then, there are T lines, each of them contain a single integer from the list. -----Output----- Output T lines. Each of these lines should contain the number of occurences of the digit 4 in the respective integer from Kostya's list. -----Constraints----- - 1 ≤ T ≤ 105 - (Subtask 1): 0 ≤ Numbers from the list ≤ 9 - 33 points. - (Subtask 2): 0 ≤ Numbers from the list ≤ 109 - 67 points. -----Example----- Input: 5 447474 228 6664 40 81 Output: 4 0 1 1 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here x=int(input()) for i in range(x): h=input() print(h.count('4')) ```
{ "language": "python", "test_cases": [ { "input": "5\n447474\n228\n6664\n40\n81\n", "output": "4\n0\n1\n1\n0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/LUCKFOUR" }
vfc_3158
false
false
false
false
false
apps
verifiable_code
1080
Solve the following coding problem using the programming language python: Every day, Mike goes to his job by a bus, where he buys a ticket. On the ticket, there is a letter-code that can be represented as a string of upper-case Latin letters. Mike believes that the day will be successful in case exactly two different letters in the code alternate. Otherwise, he believes that the day will be unlucky. Please see note section for formal definition of alternating code. You are given a ticket code. Please determine, whether the day will be successful for Mike or not. Print "YES" or "NO" (without quotes) corresponding to the situation. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first and only line of each test case contains a single string S denoting the letter code on the ticket. -----Output----- For each test case, output a single line containing "YES" (without quotes) in case the day will be successful and "NO" otherwise. -----Note----- Two letters x, y where x != y are said to be alternating in a code, if code is of form "xyxyxy...". -----Constraints----- - 1 ≤ T ≤ 100 - S consists only of upper-case Latin letters Subtask 1 (50 points): - |S| = 2 Subtask 2 (50 points): - 2 ≤ |S| ≤ 100 -----Example----- Input: 2 ABABAB ABC Output: YES NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def res(s): if len(s) == 2: if s[0] == s[1]: print("NO") else: print("YES") elif s[0] != s[1]: counte = 0 for i in range(2, len(s)): if i % 2 == 0: if s[i] != s[0]: counte = 1 break else: if s[i] != s[1]: counte = 1 break if counte == 0: print("YES") else: print("NO") else: print("NO") def __starting_point(): t = int(input()) for _ in range(t): stri = str(input()) res(stri) __starting_point() ```
{ "language": "python", "test_cases": [ { "input": "2\nABABAB\nABC\n", "output": "YES\nNO\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/TICKETS5" }
vfc_3162
false
false
false
false
false
apps
verifiable_code
1081
Solve the following coding problem using the programming language python: A spy needs your help to encrypt a 5 letter message. To encrypt the message the following steps are need to be done: 1)Each letter of the message will be converted to it's equivalent number (i.e A=0, B=1,..Z=25) 2)98, 57, 31, 45, 46 are the 5 numbers in the key. 3)Add the equivalent number of the first letter of the message to the first number of the key, then the equivalent number of the second letter of the message to the second number of the key and so on. 4)Take modulo 26 of the sum of each expression. 5)Convert the newly obtained numbers into it's equivalent letters (i.e 0=A, 1=B,…25=Z) Print the final encrypted string. Note: It is guaranteed that the given string only contains Only Upper-case Latin Letters. -----Input:----- - The first line of the input contains a single Integer $T$. $T$ denoting the number of testcases, description of each testcases contains. - The first line of the each testcase contains a single string $S$. $S$ denoting the string which you need to encrypt -----Output:----- - For each test case, print encypted string i.e result of above operation. Note: Each testcase output must be printed on new line -----Constraints:----- - $1 \leq T \leq 100$ - $1 \leq S \leq 5$ -----Sample Input:----- 2 HELLO HELL -----Sample Output:----- BJQEI BJQE -----Explanation:----- - For 1st Testcase : The conversion of "HELLO" to its equivalent number will be 7, 4, 11, 11, 14 respectively. Then we add each equivalent number of the message with corresponding number in the key: 7 + 98 = 105 4 + 57 = 61 11 + 31 = 42 11 + 45 = 56 14 + 46 = 60 Further we take the sum: 105 modulo 26 = 1 61 modulo 26 = 9 42 modulo 26 = 16 56 modulo 26 = 4 60 modulo 26 = 8 Finally, convert the newly obtained numbers into it's equivalent letters: B J Q E I Print the final string: BJQEI The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here import string from collections import OrderedDict from itertools import zip_longest dic = OrderedDict(zip(string.ascii_uppercase, range(0, 26))) keys = [98, 57, 31, 45, 46] t = int(input()) # number of test cases s1 = [] for i in range(t): s = input() for i in s: if i in dic.keys(): s1.append(int(i.replace(i, str(dic[i])))) s2 = [sum(t) for t in zip_longest(s1, keys, fillvalue=0)] inv_dic = {v:k for k,v in dic.items()} s_1 = list() for i in range(len(s1)): s_1.append(s2[i]%26) res= [inv_dic[i] for i in s_1] print(''.join(res)) inv_dic.clear() res.clear() s1.clear() s2.clear() s_1.clear() ```
{ "language": "python", "test_cases": [ { "input": "2\nHELLO\nHELL\n", "output": "BJQEI\nBJQE\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/STRG2020/problems/ENCYP" }
vfc_3166
false
false
false
false
false
apps
verifiable_code
1082
Solve the following coding problem using the programming language python: $Jaggu$ monkey a friend of $Choota$ $Bheem$ a great warrior of $Dholakpur$. He gets everything he wants. Being a friend of $Choota$ $Bheem$ he never has to struggle for anything, because of this he is in a great debt of $Choota$ $Bheem$, he really wants to pay his debt off. Finally the time has come to pay his debt off, $Jaggu$ is on a magical tree. He wants to collect apples from different branches but he is in a hurry. $Botakpur$ has attacked on $Dholakpur$ and $Bheem$ is severely injured, as been instructed by the village witch, $Bheem$ can only be saved by the apples of the magical tree. Each apple is placed in Tree Node structure and each apple has some sweetness. Now there's a problem as $Jaggu$ is also injured so he can only slide downwards and alse is collecting apples in his hand so he can't climb. You would be given $Q$ queries. Queries are of 2 type :- - Ending Node Node of $Jaggu$ is given. $format$ - type of query node -(1 2) - Sweetness of Apple on a given node is changed. $format$ - type of query node new sweetness(2 3 10) $Note$: $Jaggu$ is always on the top of tree initially in each query.The sweetness is always positive. Help $Jaggu$ in saving $Bheem$ -----Input:----- - First line contains $N$ - (number of nodes). - Next line contains $N$ integers with space giving sweetness of apple on Nodes $(1 to N)$ - Next $N-1$ lines contain $N1$ $N2$ connected nodes. - Next line contains single integer $Q$ Number of queries -----Output:----- - For each query of type 1, print total sweetness of apples. -----Constraints----- - $1 \leq N \leq 10^4$ - $2 \leq Q \leq 10^4$ -----Sample Input:----- 10 10 12 6 8 1 19 0 5 13 17 1 2 1 3 1 4 3 10 4 8 8 9 4 5 5 7 5 6 3 1 1 2 3 20 1 8 -----Sample Output:----- 10 23 -----EXPLANATION:----- This sweetness array is : $[10,2,6,8,1,19,0,5,13,17]$ The tree is: 1 / | \ 2 3 4 / / \ 10 8 5 / / \ 9 7 6 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python counter = -1 def flattree(node): nonlocal counter if visited[node]==1: return else: visited[node]=1 counter += 1 i_c[node] = counter flat_tree[counter] = swt[node] for i in graph[node]: if visited[i]==0: flattree(i) counter += 1 o_c[node] = counter flat_tree[counter] = -swt[node] return def getsum(BITTree, i): s = 0 # initialize result i = i + 1 while i > 0: s += BITTree[i] i -= i & (-i) return s def upd(BITTree, n, i, v): i += 1 while i <= n: BITTree[i] += v i += i & (-i) def construct(arr, n): BITTree = [0] * (n + 1) for i in range(n): upd(BITTree, n, i, arr[i]) return BITTree from collections import defaultdict n = int(input()) swt = list(map(int, input().split())) graph = defaultdict(list) for i in range(n-1): n1, n2 = list(map(int, input().split())) graph[n1-1].append(n2-1) graph[n2-1].append(n1-1) flat_tree = [0]*(2*n+1) i_c = [0]*n o_c = [0]*n visited = [0]*n flattree(0) tre = construct(flat_tree, 2*n) q = int(input()) for i in range(q): query = list(map(int, input().split())) if query[0] == 1: node = query[1] - 1 answer = getsum(tre, i_c[node]) print(answer) else: node = query[1]-1 upd(flat_tree, (2*n), i_c[node], query[2]) upd(flat_tree, (2*n), o_c[node], -query[2]) ```
{ "language": "python", "test_cases": [ { "input": "10\n10 12 6 8 1 19 0 5 13 17\n1 2\n1 3\n1 4\n3 10\n4 8\n8 9\n4 5\n5 7\n5 6\n3\n1 1\n2 3 20\n1 8\n", "output": "10\n23\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COFY2020/problems/GKSMNHM" }
vfc_3170
false
false
false
false
false
apps
verifiable_code
1083
Solve the following coding problem using the programming language python: Probably everyone has experienced an awkward situation due to shared armrests between seats in cinemas. A highly accomplished cinema manager named "Chef" decided to solve this problem. When a customer wants to buy a ticket, the clerk at the ticket window asks the visitor if they need the armrests, and if so, which of them: left, right, or both. We know that out of the audience expected to show up, L of them only need the left armrest, R of them need just the right one, Z need none and B need both. Your task is to calculate the maximum number of people that can attend the show. In the cinema hall there are N rows with M seats each. There is only one armrest between two adjacent seats. Seats at the beginning and at the end of the row have two armrests -----Input----- Input begins with an integer T: the number of test cases. Each test case consists of a single line with 6 space-separated integers: N, M, Z, L, R, B. -----Output----- For each test case, output a line containing the answer for the task. -----Constraints and Subtasks----- - 1 ≤ T ≤ 105 Subtask 1 : 10 points - 1 ≤ N, M ≤ 3 - 0 ≤ Z, L, R, B ≤ 3 Subtask 2 : 20 points - 1 ≤ N, M ≤ 30 - 0 ≤ Z, L, R ≤ 30 - 0 ≤ B ≤ 109 Subtask 3 : 30 points - 1 ≤ N, M ≤ 106 - 0 ≤ Z, L, R ≤ 106 - 0 ≤ B ≤ 1016 Subtask 4 : 40 points - 1 ≤ N, M ≤ 108 - 0 ≤ Z, L, R, B ≤ 1016 -----Example----- Input:2 2 2 3 2 1 1 3 3 1 2 0 9 Output:4 8 -----Explanation----- 'L' - needs left 'R - needs right 'Z' - doesn't need any 'B' - needs both '-' - empty place Example case 1. ZZ ZB Example case 2. LLB BZB B-B The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for i in range(eval(input())): n,m,z,l,r,b = list(map(int, input().split())) rows=n columns=m hand_rest=n*(m+1) if(m%2==0): hand_rest -=max(0,n-l-r) if(l+r+(2*b)<=hand_rest): # print "kanu" print(min(n*m,l+r+z+b)) else: temp=l+r+(hand_rest-l-r)/2 # print "parth" print(min(n*m,temp+z)) ```
{ "language": "python", "test_cases": [ { "input": "2\n2 2 3 2 1 1\n3 3 1 2 0 9\n", "output": "4\n8\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/DEC15/problems/CHCINEMA" }
vfc_3174
false
false
false
false
false
apps
verifiable_code
1084
Solve the following coding problem using the programming language python: You are given a binary string S. You need to transform this string into another string of equal length consisting only of zeros, with the minimum number of operations. A single operation consists of taking some prefix of the string S and flipping all its values. That is, change all the 0s in this prefix to 1s, and all the 1s in the prefix to 0s. You can use this operation as many number of times as you want over any prefix of the string. -----Input----- The only line of the input contains the binary string, S . -----Output----- Output a single line containing one integer, the minimum number of operations that are needed to transform the given string S into the string of equal length consisting only of zeros. -----Constraints----- - 1 ≤ |S| ≤ 100,000 -----Subtasks----- - Subtask #1 (30 points): 1 ≤ |S| ≤ 2000 - Subtask #2 (70 points): Original constraints. -----Example----- Input: 01001001 Output: 6 -----Explanation----- For the given sample case, let us look at the way where we achieved minimum number of operations. Operation 1: You flip values in the prefix of length 8 and transform the string into 10110110 Operation 2: You flip values in the prefix of length 7 and transform the string into 01001000 Operation 3: You flip values in the prefix of length 5 and transform the string into 10110000 Operation 4: You flip values in the prefix of length 4 and transform the string into 01000000 Operation 5: You flip values in the prefix of length 2 and transform the string into 10000000 Operation 6: You flip values in the prefix of length 1 and finally, transform the string into 00000000 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here s=input() s1=s[::-1] arr=[] cnt=0 for i in range(len(s1)): arr.append(s1[i]) for i in range(len(arr)): if(arr[i]=="1"): for j in range(i,len(arr)): if(arr[j]=="1"): arr[j]="0" else: arr[j]="1" cnt+=1 print(cnt) ```
{ "language": "python", "test_cases": [ { "input": "01001001\n", "output": "6\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/PREFINVS" }
vfc_3178
false
false
false
false
false
apps
verifiable_code
1085
Solve the following coding problem using the programming language python: -----Problem----- Once THANMAY met PK who is from a different planet visiting earth. THANMAY was very fascinate to learn PK's language. The language contains only lowercase English letters and is based on a simple logic that only certain characters can follow a particular character. Now he is interested in calculating the number of possible words of length L and ending at a particular character C. Help THANMAY to calculate this value. -----Input----- The input begins with 26 lines, each containing 26 space-separated integers. The integers can be either 0 or 1. The jth integer at ith line depicts whether jth English alphabet can follow ith English alphabet or not. Next line contains an integer T. T is the number of queries. Next T lines contains a character C and an integer L. -----Output----- For each query output the count of words of length L ending with the character C. Answer to each query must be followed by newline character. The answer may be very large so print it modulo 1000000007. -----Constraints----- - 1 ≤ T ≤ 100 - C is lowercase English alphabet. - 2 ≤ L ≤ 10000000 -----Sample Input----- 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 c 3 b 2 -----Sample Output----- 1 2 -----Explanation----- For query 1, Words of length 3 are: aba, acb, bab, bac, cba. The only word ending with 'c' is bac. p { text-align:justify } The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python d = {} for i in range(26): char = chr(i+ord('a')) d[char] = [] for i in range(26): char = chr(i+ord('a')) temp = list(map(int,input().strip().split())) for j in range(26): if (temp[j] == 1): follow= chr(j+ord('a')) d[follow].append(char) def f(char,i,n,count): if (i==n): return count+1 else: ans = 0 for c in d[char]: ans+=f(c,i+1,n,0) ans%=(10**9+7) return ans for q in range(int(input().strip())): c, n = input().strip().split() n = int(n) print(f(c,1,n,0)) ```
{ "language": "python", "test_cases": [ { "input": "0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n2\nc 3\nb 2\n", "output": "1\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COMN2016/problems/THANPK" }
vfc_3182
false
false
false
false
true