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 | 1186 | Solve the following coding problem using the programming language python:
Bhallaladeva was an evil king who ruled the kingdom of Maahishmati. He wanted to erect a 100ft golden statue of himself and he looted gold from several places for this. He even looted his own people, by using the following unfair strategy:
There are N houses in Maahishmati, and the ith house has Ai gold plates. Each gold plate costs exactly 1 Nimbda, which is the unit of currency in the kingdom of Maahishmati. Bhallaladeva would choose an integer K, and loots all the houses in several steps. In each step:
- He would choose a house i which hasn't been looted yet, pay the owner exactly Ai Nimbdas, and take away all the gold plates in that house (Hence, he also ends up looting this house).
- He would now choose atmost K houses which haven't been looted yet and take away all the gold plates from these houses without paying a single Nimbda (Yes, he takes all of them for free).
He repeats the above steps until all the N houses have been looted. Your task is to devise a strategy for Bhallaladeva to loot the houses in some order, so that the number of nimbdas he has to pay is minimium. You'll also be given multiple values of K (Q of them to be precise), and you need to find the minimum number of nimbdas for each of these values.
-----Input-----
The first line of input consists of a single integer N denoting the number of houses in Maahishmati. The second line of input consists of N space separated integers denoting A1, A2, ..., AN, where Ai denotes the number of gold plates in the ith house. The third line of input consists of a single integer Q denoting the number of values of K to follow. Each of the following Q lines consist of a single integer, where the value on the ith line denotes the value of K for the ith query.
-----Output-----
Output exactly Q integers on separate lines, where the output on the ith line denotes the answer for the ith value of K.
-----Constraints-----
- 1 ≤ N ≤ 105
- 1 ≤ Q ≤ 105
- 0 ≤ K ≤ N-1
- 1 ≤ Ai ≤ 104
-----Example-----
Input:
4
3 2 1 4
2
0
2
Output:
10
3
-----Explanation-----
For the first query, K = 0. Hence, Bhallaladeva cannot take gold plates from any of the houses for free. It will cost him 3 + 2 + 1 + 4 = 10 nimbdas.
For the second query, K = 2. In the first step Bhallaladeva can pay 2 nimbdas for gold plates in house number 2, and take the gold in houses 1 and 4 for free (Note that house 1 has 3 gold plates and house 4 has 4 gold plates). Now, he has looted houses 1, 2 & 4. Now in the second step, he loots house 3, by paying 1 nimbda. Hence, the total cost = 1 + 2 = 3. Note that there might be multiple ways to achieve the minimum cost, and we have explained only one of them.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import math
n = int(input())
a = sorted(map(int,input().split()))
l = [0]*n
for i in range(n):
l[i] = a[i] + l[i-1]
for q in range(int(input())):
print(l[int(math.ceil(float(n)/(int(input())+1)))-1])
``` | {
"language": "python",
"test_cases": [
{
"input": "4\n3 2 1 4\n2\n0\n2\n",
"output": "10\n3\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/AMR15ROL/problems/AMR15D"
} | vfc_3586 | false | false | false | false | false |
apps | verifiable_code | 1187 | Solve the following coding problem using the programming language python:
Chef wants to select a subset $S$ of the set $\{1, 2, \ldots, N\}$ such that there are no two integers $x, y \in S$ which satisfy $\frac{x}{y} = M$.
Help Chef find the maximum size of a subset $S$ he can choose and the number of ways in which he can choose a subset $S$ with this maximum size. Since the number of ways to choose $S$ can be very large, calculate it modulo $998,244,353$.
-----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 two space-separated integers ― the maximum size of a subset Chef can choose and the number of ways to choose a subset with this maximum size modulo $998,244,353$. Note that only the number of ways to choose a subset should be printed modulo $998,244,353$.
-----Constraints-----
- $1 \le T \le 10,000$
- $2 \le M \le N \le 10^{18}$
-----Example Input-----
3
5 2
10 2
100 3
-----Example Output-----
4 1
6 12
76 49152
-----Explanation-----
Example case 1: The only subset $S$ with the maximum size which Chef can choose is $\{1, 3, 4, 5\}$.
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,M = list(map(int,input().split()))
count,e,perm = 0,0,1
while(True):
lim,start = N//(M**e),N//(M**(e + 1)) + 1
num = lim - start + 1
divs = num//M
if((start + divs*M) <= lim):
r = (start+divs*M)%M
if(r == 0 or (r + (lim - (start + divs*M)) >= M)):
divs += 1
cmon = num - divs
if(e % 2 == 0):
count += cmon*((e+2)//2)
else:
count += cmon*(e//2 + 1)
perm = (perm * pow((e + 3)//2,cmon ,998244353))%998244353
e += 1
if(start == 1):
break
print(count,perm)
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n5 2\n10 2\n100 3\n",
"output": "4 1\n6 12\n76 49152\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/BANQUNT"
} | vfc_3590 | false | false | false | false | false |
apps | verifiable_code | 1188 | Solve the following coding problem using the programming language python:
Chef is a private detective. He was asked to investigate a case of murder in the city of Frangton.
Chef arrived in Frangton to find out that the mafia was involved in the case. Chef spent some time watching for people that belong to the clan and was able to build a map of relationships between them. He knows that a mafia's organizational structure consists of a single Don, heading a hierarchical criminal organization. Each member reports exactly to one other member of the clan. It's obvious that there are no cycles in the reporting system of the mafia.
There are N people in the clan, for simplicity indexed from 1 to N, and Chef knows who each of them report to. Member i reports to member Ri.
Now, Chef needs to identfy all potential killers to continue his investigation. Having considerable knowledge about the mafia's activities, Chef knows that the killer must be a minor criminal, that is, one of the members who nobody reports to. Please find the list of potential killers for Chef. Since Don reports to nobody, his Ri will be equal to 0.
-----Input-----
The first line of input contains one integer N.
Next line has N space-separated integers, the ith integer denotes Ri — the person whom the ith member reports to.
-----Output-----
Output a list of space-separated integers in ascending order — the indices of potential killers.
-----Constraints-----
- 1 ≤ N ≤ 105
- 1 ≤ Ri ≤ N except for Don, whose Ri equals to 0.
- It is guaranteed that there are no cycles in the reporting structure.
-----Subtasks-----
- Subtask #1 [50 points]: N ≤ 10000
- Subtask #2 [50 points]: No additional constraints
-----Example-----
Input:
6
0 1 1 2 2 3
Output:
4 5 6
-----Explanation-----
The reporting structure:
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n = eval(input())
r = list(map(int, input().split()))
tree = dict()
i = 1
for j in r:
c = tree.get(j)
if c:
tree[j].append(i)
else:
tree[j] = [i]
if not tree.get(i):
tree[i] = []
i += 1
s = []
for elem in tree:
if not tree[elem]:
s.append(str(elem))
print(' '.join(s))
``` | {
"language": "python",
"test_cases": [
{
"input": "6\n0 1 1 2 2 3\n",
"output": "4 5 6\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/FEB16/problems/CHEFDETE"
} | vfc_3594 | false | false | false | false | false |
apps | verifiable_code | 1189 | Solve the following coding problem using the programming language python:
You are given a sequence $A_1, A_2, \ldots, A_N$. For each $k$ ($1 \le k \le N$), let's define a function $f(k)$ in the following way:
- Consider a sequence $B_1, B_2, \ldots, B_N$, which is created by setting $A_k = 0$. Formally, $B_k = 0$ and $B_i = A_i$ for each valid $i \neq k$.
- $f(k)$ is the number of ways to split the sequence $B$ into two non-empty contiguous subsequences with equal sums.
Find the sum $S = f(1) + f(2) + \ldots + f(N)$.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.
-----Output-----
For each test case, print a single line containing one integer — the sum $S$.
-----Constraints-----
- $1 \le T \le 10$
- $2 \le N \le 2 \cdot 10^5$
- $1 \le |A_i| \le 10^9$ for each valid $i$
-----Example Input-----
2
6
1 2 1 1 3 1
3
4 1 4
-----Example Output-----
6
2
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
from sys import stdin
def gt(num):
if num:
return num
return 0
for __ in range(int(stdin.readline().split()[0])):
n = int(stdin.readline().split()[0])
a = list(map(int, stdin.readline().split()))
cnta = dict()
cnta.setdefault(0)
cntb = dict()
cntb.setdefault(0)
for i in a:
cnta[i] = gt(cnta.get(i)) + 1
asum = 0
bsum = sum(a)
ans = 0
for i in range(n-1):
asum += a[i]
bsum -= a[i]
cnta[a[i]] = gt(cnta.get(a[i])) - 1
cntb[a[i]] = gt(cntb.get(a[i])) + 1
ans += gt(cnta.get(bsum-asum))
ans += gt(cntb.get(asum-bsum))
print(ans)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n6\n1 2 1 1 3 1\n3\n4 1 4\n",
"output": "6\n2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CFINASUM"
} | vfc_3598 | false | false | false | false | false |
apps | verifiable_code | 1190 | Solve the following coding problem using the programming language python:
Tomya is a girl. She loves Chef Ciel very much.
Tomya like a positive integer p, and now she wants to get a receipt of Ciel's restaurant whose total price is exactly p.
The current menus of Ciel's restaurant are shown the following table.
Name of Menupriceeel flavored water1deep-fried eel bones2clear soup made with eel livers4grilled eel livers served with grated radish8savory egg custard with eel16eel fried rice (S)32eel fried rice (L)64grilled eel wrapped in cooked egg128eel curry rice256grilled eel over rice512deluxe grilled eel over rice1024eel full-course2048
Note that the i-th menu has the price 2i-1 (1 ≤ i ≤ 12).
Since Tomya is a pretty girl, she cannot eat a lot.
So please find the minimum number of menus whose total price is exactly p.
Note that if she orders the same menu twice, then it is considered as two menus are ordered. (See Explanations for details)
-----Input-----
The first line contains an integer T, the number of test cases.
Then T test cases follow.
Each test case contains an integer p.
-----Output-----
For each test case, print the minimum number of menus whose total price is exactly p.
-----Constraints-----
1 ≤ T ≤ 5
1 ≤ p ≤ 100000 (105)
There exists combinations of menus whose total price is exactly p.
-----Sample Input-----
4
10
256
255
4096
-----Sample Output-----
2
1
8
2
-----Explanations-----
In the first sample, examples of the menus whose total price is 10 are the following:
1+1+1+1+1+1+1+1+1+1 = 10 (10 menus)
1+1+1+1+1+1+1+1+2 = 10 (9 menus)
2+2+2+2+2 = 10 (5 menus)
2+4+4 = 10 (3 menus)
2+8 = 10 (2 menus)
Here the minimum number of menus is 2.
In the last sample, the optimal way is 2048+2048=4096 (2 menus).
Note that there is no menu whose price is 4096.
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):
n = int(input())
m=0
m=n//(2**11)
n%=(2**11)
while(n>0):
num=n%2
m+=num
n//=2
print(m)
t-=1
``` | {
"language": "python",
"test_cases": [
{
"input": "4\n10\n256\n255\n4096\n",
"output": "2\n1\n8\n2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CIELRCPT"
} | vfc_3602 | false | false | false | false | false |
apps | verifiable_code | 1191 | Solve the following coding problem using the programming language python:
Sandy is a professor at a very reputed institute. The institute mandates that all the lectures be communicated in English. As Sandy is not very good at English(or anything actually) the presentations he displays in class have a lot of spelling mistakes in them.
As you have a dictionary on you containing $N$ words in it, the responsibility of correctly deducing the misspelt word falls upon your shoulders. Sandy's presentation contains in them, $Q$ words which you $think$ are wrongly spelt. A word is misspelt if a $single$ letter in it is missing or is different from the corresponding correctly spelled word in your dictionary.
For each of the misspelt word in the presentation find out the corresponding correct word from your dictionary.
Note :
- For each misspelt word in Sandy's presentation, there exists one and only one correctly spelt word in your dictionary that corresponds to it.
- Out of the $Q$ misspelt words given to you, there might be some which are correctly spelt i.e., that word completely matches a word in your dictionary. (Give Sandy some credit, he's a teacher after all). For such words print the word corresponding to it in your dictionary.
- The maximum length of each string can be $L$.
-----Input:-----
- First line contains a single integer $T$ denoting the number of testcases. Then the testcases follow.
- The first line of each test case contains two space-separated integers $N, Q$ corresponding to the number of words in your dictionary and the number of misspelt word in Sandy's dictionary respectively.
- $N$ lines follow each containing a single string $S$ denoting a word in your dictionary.
- $Q$ lines follow each containing a single string $M$ denoting a misspelt word in Sandy's presentation.
-----Output:-----
In each testcase, for each of the $Q$ misspelt words, print a single word $S$ corresponding to the correctly spelt word in your dictionary.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq N \leq 200$
- $1 \leq Q \leq 200$
- $1 \leq L \leq 100$
- None of the words contain any spaces in them.
- Each letter in every word is in lower case.
-----Subtasks-----
-
Subtask 1 : 10 points
- $1 \leq N \leq 50$
- $1 \leq Q \leq 10$
-
Subtask 2 : 90 points
- Original Constraints
-----Sample Input:-----
1
5 2
szhbdvrngk
qzhxibnuec
jfsalpwfkospl
levjehdkjy
wdfhzgatuh
szhbdvcngk
qzhxbnuec
-----Sample Output:-----
szhbdvrngk
qzhxibnuec
-----EXPLANATION:-----
- In the first misspelt word $szhbdvcngk$, a single letter $c$ is different from the original word $szhbdvrngk$.
- The second misspelt word $qzhxbnuec$ is missing the letter $i$ that was in the original word $qzhxibnuec$.
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 difflib import get_close_matches
import sys, os
def closeMatches(patterns, word):
return get_close_matches(word, patterns, 1, 0.9)[0]
def get_string():
return sys.stdin.readline().strip()
def get_ints():
return map(int, sys.stdin.readline().strip().split())
ans = []
test = int(input())
for i in range(test):
n,q = get_ints()
#ans = []
n = int(n)
q = int(q)
patterns=[]
for j in range(n):
s = get_string()
patterns.append(s)
for j in range(q):
word = get_string()
ans.append(closeMatches(patterns, word))
for j in ans:
sys.stdout.write(j+"\n")
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n5 2\nszhbdvrngk\nqzhxibnuec\njfsalpwfkospl\nlevjehdkjy\nwdfhzgatuh\nszhbdvcngk\nqzhxbnuec\n",
"output": "szhbdvrngk\nqzhxibnuec\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/PROC2020/problems/ENGRIS"
} | vfc_3606 | false | false | false | false | false |
apps | verifiable_code | 1192 | Solve the following coding problem using the programming language python:
You are given a sequence of integers $A_1, A_2, \ldots, A_N$. This sequence is circular ― for each valid $i$, the element $A_{i+1}$ follows after $A_i$, and the element $A_1$ follows after $A_N$.
You may insert any positive integers at any positions you choose in this sequence; let's denote the resulting sequence by $B$. This sequence is also circular. For each pair of its elements $B_s$ and $B_f$, let's denote the (non-circular) sequence created by starting at $B_s$ and moving from each element to the one that follows after it, until we reach $B_f$, by $B(s, f)$. This sequence includes the elements $B_s$ and $B_f$.
For each $K$ from $2$ to $N$ inclusive, find the smallest possible number of elements that need to be inserted into $A$ to form a sequence $B$ for which there is no subsequence $B(p, q)$ such that:
- The size of $B(p, q)$ is at least $K$.
- There is no pair of consecutive elements in $B(p, q)$ such that their GCD is equal to $1$.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.
-----Output-----
For each test case, print a single line containing $N-1$ space-separated integers. For each $i$ ($1 \le i \le N-1$), the $i$-th of these integers should be the smallest number of inserted elements in a valid sequence $B$ for $K = i+1$.
-----Constraints-----
- $1 \le T \le 2,000$
- $2 \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 $2 \cdot 10^6$
-----Example Input-----
1
5
3 6 4 5 9
-----Example Output-----
3 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
from math import gcd
from itertools import groupby
def gcd_split(seq):
gcds= [int(gcd(a,b)==1) for a,b in zip(seq[1:],seq[:-1])]
gcds.append(int(gcd(seq[0],seq[-1])==1))
# print(gcds)
if max(gcds)==0:
return -1
else:
splits=[len(list(x))+1 for num,x in groupby(gcds) if num==0]
# print(splits)
if gcds[0]==gcds[-1]==0:
splits[0] += splits[-1]-1
splits = splits[:-1]
return splits
for _ in range(int(input())):
N=int(input())
A=[int(x) for x in input().split()]
split = gcd_split(A)
# print(split)
res=[]
if split!=-1:
for K in range(2,N+1):
temp=(x for x in split if x>=K)
ins = sum([(x//(K-1)-1 if x%(K-1)==0 else x//(K-1)) for x in temp])
if ins==0:
break
else:
res.append(ins)
else:
for K in range(2,N+1):
ins = N//(K-1)+(N%(K-1)>0)
if ins==0:
break
else:
res.append(ins)
res = res + [0]*(N-1 -len(res))
print(*res)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n5\n3 6 4 5 9\n",
"output": "3 1 1 0\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/MININS"
} | vfc_3610 | false | false | false | false | false |
apps | verifiable_code | 1193 | Solve the following coding problem using the programming language python:
There are $N$ robots who work for $Y$ days and on each day they
produce some toys .on some days a few robots are given rest.
So depending on the availability of robots owner has
made a time table which decides which robots will work on
the particular day.
Only contiguous robots must be selected as they can form
a link of communication among themselves.
Initially, all robots have the capacity of one toy.
On each day capacity for the chosen robot is updated i.e
capacity = capacity $+$$ ($minimum capacity of given range % $1000000007)$ .
After calculating the minimum capacity of a given range, compute
it as modulo 1000000007 ($10^9 + 7$).
After $Y$ days find the minimum capacity of the $N$ robots and compute it as modulo 1000000007 ($10^9 + 7$).
-----Input:-----
- First-line will contain $T$, the number of test cases. Then the test cases follow.
- Next Line contains a single integer N.
- Next Line contains a single integer Y.
- Next $Y$ lines contains l and r range of chosen robots .
-----Output:-----
For each testcase, output in a single line answer , the minimum capacity of the $N$ robots after $Y$ days and compute it as modulo 1000000007 ($10^9 + 7$) .
-----Constraints-----
- $1 \leq T \leq 100$
- $100 \leq N \leq 10^4$
- $200 \leq Y \leq 1000$
- $0<=l , r<=N-1$ , $l<=r$
-----Sample Input:-----
1
5
4
0 3
1 2
4 4
0 4
-----Sample Output:-----
4
-----EXPLANATION:-----
Initial capacity of the $5$ robots
1 1 1 1 1
Minimum in range [0,3] = 1
Update the capacity in the range [0,3]
Now capacity becomes,
Day 1 - 2 2 2 2 1
Similarly capacities changes for each day
Day 2 - 2 4 4 2 1
Day 3 - 2 4 4 2 2
Day 4 - 4 6 6 4 4
so after 4 days minimum capacity is $4$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
MAX = 100005
tree = [0] * MAX;
lazy = [0] * MAX;
def updateRangeUtil(si, ss, se, us, ue, diff) :
if (lazy[si] != 0) :
tree[si] += lazy[si];
if (ss != se) :
lazy[si * 2 + 1] += lazy[si];
lazy[si * 2 + 2] += lazy[si];
lazy[si] = 0;
if (ss > se or ss > ue or se < us) :
return;
if (ss >= us and se <= ue) :
tree[si] += diff;
if (ss != se) :
lazy[si * 2 + 1] += diff;
lazy[si * 2 + 2] += diff;
return;
mid = (ss + se) // 2;
updateRangeUtil(si * 2 + 1, ss,mid, us, ue, diff);
updateRangeUtil(si * 2 + 2, mid + 1,se, us, ue, diff);
tree[si] = min(tree[si * 2 + 1],tree[si * 2 + 2]);
def updateRange(n, us, ue, diff) :
updateRangeUtil(0, 0, n - 1, us, ue, diff);
def getSumUtil(ss, se, qs, qe, si) :
if (lazy[si] != 0) :
tree[si] += lazy[si];
if (ss != se) :
lazy[si * 2 + 1] += lazy[si];
lazy[si * 2 + 2] += lazy[si];
lazy[si] = 0;
if (ss > se or ss > qe or se < qs) :
return 10e9;
if (ss >= qs and se <= qe) :
return tree[si];
mid = (ss + se) // 2;
return min(getSumUtil(ss, mid, qs, qe, 2 * si + 1),getSumUtil(mid + 1, se, qs, qe, 2 * si + 2));
def getSum(n, qs, qe) :
if (qs < 0 or qe > n - 1 or qs > qe) :
#print("Invalid Input", end = "");
return -1;
return getSumUtil(0, n - 1, qs, qe, 0);
def constructSTUtil(arr, ss, se, si) :
if (ss > se) :
return;
if (ss == se) :
tree[si] = arr[ss];
return;
mid = (ss + se) // 2;
constructSTUtil(arr, ss, mid, si * 2 + 1);
constructSTUtil(arr, mid + 1, se, si * 2 + 2);
tree[si] = min(tree[si * 2 + 1], tree[si * 2 + 2]);
def constructST(arr, n) :
constructSTUtil(arr, 0, n - 1, 0);
# Driver code
for _ in range(int(input())):
tree = [0] * MAX;
lazy = [0] * MAX;
n=int(input());
y=int(input());
arr=[1]*n;
constructST(arr, n);
for xyz in range(y):
l,r=list(map(int,input().split()));
updateRange(n, l, r, getSum(n, l, r)%1000000007);
print((getSum(n, 0, n-1)%1000000007));
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n5\n4\n0 3\n1 2\n4 4\n0 4\n",
"output": "4\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/ENDE2020/problems/ENCDEC6"
} | vfc_3614 | false | false | false | false | false |
apps | verifiable_code | 1194 | Solve the following coding problem using the programming language python:
Toby has found a game to entertain himself.The game is like this:
You are in a coordinate system initially at (0,0) and you are given a sequence of steps which lead to your destination.The steps are given in the form of directions: ’U’ ,’D’ , ’L’ and ‘R’ for up, down, left and right respectively.If you are at position (x,y) then:
- U:move to (x,y+1)
- D:move to (x,y-1)
- L:move to (x-1,y)
- R:move to (x+1,y)
The sequence is provided as a string ‘s’ of characters where $s_i$ $(1 \leq i \leq N)$ is one of the direction character as mentioned above.An example of a sequence of steps is: UULRUDR
The destination according to this string is (1,2).
You want to remove maximum number of characters from the string such that the resulting string leads to the same destination as before.
For example in the example above we can remove characters at positions 1,3,4,6 and the resulting path will be UUR which will lead to the same destination i.e (1,2).so we reduced the number of steps by 4,and this is our score.
You need to get maximum score.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- First line of each testcase contains a single integer $N$,size of string.
- Second line of testcase contains a string $s$ of size $N$.
-----Output:-----
For each testcase, output a single line containing the maximum score possible.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 10^5$
-----Sample Input:-----
3
7
ULUDLLU
4
RUUR
4
LRLR
-----Sample Output:-----
2
0
4
-----EXPLANATION:-----
-
test case 1:
The final destination after moving according to the sequence is (-3,2).
One way is to remove characters at positions 3,4 and the resulting string will be ULLLU and destination still remains (-3,2).
-
test case 2: No character can be removed in this case.
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 Counter
try:
for _ in range(int(input())):
n=int(input())
s=input()
d1=dict(Counter(s))
u,d,r,l=0,0,0,0
if 'U' in d1:
u=d1['U']
else:
u=0
if 'D' in d1:
d=d1['D']
else:
d=0
if 'R' in d1:
r=d1['R']
else:
r=0
if 'L' in d1:
l=d1['L']
else:
l=0
x=0
y=0
if l==r:
x=0
elif l>r:
x=-(l-r)
elif r>l:
x=r-l
if u==d:
y=0
elif d>u:
y=-(d-u)
elif u>d:
y=u-d
# print(x,y)
if x==0 and y==0:
print(n)
continue
print(n-(abs(x)+abs(y)))
except:
pass
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n7\nULUDLLU\n4\nRUUR\n4\nLRLR\n",
"output": "2\n0\n4\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/CSEP2020/problems/TOBY"
} | vfc_3618 | false | false | false | false | false |
apps | verifiable_code | 1195 | Solve the following coding problem using the programming language python:
Chefland has all the cities on a straight line. There are $N$ cities in Chefland numbered $1$ to $N$. City $i$ is located at coordinate $x_i$ on the x-axis. Guru wants to travel from city $A$ to city $B$. He starts at time t=0. He has following choices to travel.
- He can walk $1$ metre in $P$ secs.
- There is a train that travels from city $C$ to city $D$ which travels $1$ metre in $Q$ secs which starts at time t=$Y$ secs. Guru can take the train only at city $C$ and leave the train only at city $D$.
Can you help Guru find the minimum time he will need to travel from city $A$ to $B$. Note that you cannot board the train after time t =$Y$.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- First line of each testcase contains eight space separated integers $N, A, B, C, D, P, Q, Y $.
- Second line of each testcase contains $N$ space-separated integers with the $i$-th integer representing $x_i$.
-----Output:-----
For each testcase, output in a single line containing the minimum travel time.
-----Constraints-----
- $1 \leq T \leq 300$
- $2 \leq N \leq 300$
- $-1000 \leq x_i \leq 1000$
- $0 \leq Y \leq 100000$
- $1 \leq A,B,C,D \leq n $
- $A \neq B$
- $C \neq D$
- $1 \leq P, Q \leq 100$
- $x_i < x_j$ if $i < j$
-----Sample Input:-----
1
4 1 3 2 4 3 2 4
1 2 3 4
-----Sample Output:-----
6
-----EXPLANATION:-----
Guru can walk directly in 6 secs.
If Guru takes train, then he will need atleast 11 secs.
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,a,b,c,d,p,q,y=list(map(int,input().split()))
l=list(map(int,input().split()))
ans = abs((l[b-1]-l[a-1]))*p
x=abs(l[c-1]-l[a-1])*p
if x<=y:
x=y+abs(l[d-1]-l[c-1])*q+abs(l[b-1]-l[d-1])*p
ans=min(ans,x)
print(ans)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n4 1 3 2 4 3 2 4\n1 2 3 4\n",
"output": "6\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/WALKFAST"
} | vfc_3622 | false | false | false | false | false |
apps | verifiable_code | 1196 | Solve the following coding problem using the programming language python:
Every college has a stud−max$stud-max$ buoy. JGEC$JGEC$ has its own Atul$Atul$ who loves to impress everyone with his smile. A presentation is going on at the auditorium where there are N$N$ rows of M$M$ chairs with people sitting on it. Everyone votes for Atul's presentation skills, but Atul is interested in knowing the maximum$maximum$ amount of vote that he receives either taking K$K$ people vertically$vertically$ or horizontally$horizontally$. Atul$Atul$, however, wants to finish up the presentation party soon, hence asks for your help so that he can wrap up things faster.
-----Input:-----
- First line will contain T$T$, number of test cases. Then the test cases follow.
- Each testcase contains of a single line of input, three integers N$N$, M$M$ and K$K$.
- N lines follow, where every line contains M numbers$numbers$ denoting the size of the sugar cube
-----Output:-----
For each test case, output in a single line the maximum votes Atul can get.
-----Constraints-----
- 1≤T≤$1 \leq T \leq $5
- 1≤N,M≤500$1 \leq N,M \leq 500$
- K≤min(N,M)$K \leq min(N,M)$
- 1≤numbers≤109$1 \leq numbers \leq 10^9$
-----Sample Input:-----
1
4 4 3
1 4 5 7
2 3 8 6
1 4 8 9
5 1 5 6
-----Sample Output:-----
22
-----EXPLANATION:-----
If Atul starts counting votes from (1,4), then he can take the 3 consecutive elements vertically downwards and those are 7,6 and 9 which is the maximum sum possible.
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):
q = input().split()
n = int(q[0])
m = int(q[1])
k = int(q[2])
sumax = 0
b = []
for j in range(n):
a = [int(k) for k in input().split()]
b = b + [a]
for j in range(n):
su = 0
for x in range(k):
su = su +b[j][x]
if su > sumax:
sumax = su
for a in range(1, m-k+1):
su = su - b[j][a-1] +b[j][k+a-1]
if su > sumax:
sumax = su
for j in range(m):
su = 0
for x in range(k):
su = su + b[x][j]
if su > sumax:
sumax = su
for a in range(1, n-k+1):
su = su - b[a-1][j] + b[a+k-1][j]
if su > sumax:
sumax = su
print(sumax)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n4 4 3\n1 4 5 7\n2 3 8 6\n1 4 8 9\n5 1 5 6\n",
"output": "22\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/AARA2018/problems/ARMBH2"
} | vfc_3626 | false | false | false | false | false |
apps | verifiable_code | 1197 | Solve the following coding problem using the programming language python:
Chef loves saving money and he trusts none other bank than State Bank of Chefland. Unsurprisingly, the employees like giving a hard time to their customers. But instead of asking them to stand them in long queues, they have weird way of accepting money.
Chef did his homework and found that the bank only accepts the money in coins such that the sum of the denomination with any previously deposited coin OR itself can't be obtained by summing any two coins OR double of any coin deposited before. Considering it all, he decided to start with $1$ Chefland rupee and he would keep choosing smallest possible denominations upto $N$ coins. Since chef is busy with his cooking, can you find out the $N$ denomination of coins chef would have to take to the bank? Also find the total sum of money of those $N$ coins.
-----Input:-----
- First line has a single integer $T$ i.e. number of testcases.
- $T$ lines followed, would have a single integer $N$ i.e. the number of coins the chef is taking.
-----Output:-----
- Output for $i$-th testcase ($1 ≤ i ≤ T$) would have 2 lines.
- First line would contain $N$ integers i.e. the denomination of coins chef would deposit to the bank.
- Second line would contain a single integer i.e. the sum of all the coins chef would deposit.
-----Constraints:-----
- $1 ≤ T ≤ 700$
- $1 ≤ N ≤ 700$
-----Subtasks:-----
- $20$ points: $1 ≤ T, N ≤ 80$
- $70$ points: $1 ≤ T, N ≤ 500$
- $10$ points: $1 ≤ T, N ≤ 700$
-----Sample Input:-----
4
1
2
3
4
-----Sample Output:-----
1
1
1 2
3
1 2 4
7
1 2 4 8
15
-----Explanation:-----
For testcase 1: First coin is stated to be 1, hence for $N$ = 1, 1 is the answer.
For testcase 2: Since chef chooses the lowest possible denomination for each $i$-th coin upto $N$ coins, second coin would be 2. Only sum possible with N = 1 would be 1+1 = 2. For N = 2, $\{1+2, 2+2\}$ $\neq$ $2$.
For testcase 3: With first two coins being 1 and 2, next coin couldn't be 3 because 3+1 = 2+2, but $\{4+1, 4+2, 4+4\}$ $\neq$ $\{1+1, 1+2, 2+2\}$
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
lst=[1, 2, 4, 8, 13, 21, 31, 45, 66, 81, 97, 123, 148, 182, 204, 252, 290, 361, 401, 475, 565, 593, 662, 775, 822, 916, 970, 1016, 1159, 1312, 1395, 1523, 1572, 1821, 1896, 2029, 2254, 2379, 2510, 2780, 2925, 3155, 3354, 3591, 3797, 3998, 4297, 4433, 4779, 4851, 5123, 5243, 5298, 5751, 5998, 6374, 6801, 6925, 7460, 7547, 7789, 8220, 8503, 8730, 8942, 9882, 10200, 10587, 10898, 11289, 11614, 11876, 12034, 12931, 13394, 14047, 14534, 14901, 15166, 15688, 15972, 16619, 17355, 17932, 18845, 19071, 19631, 19670, 20722, 21948, 22526, 23291, 23564, 23881, 24596, 24768, 25631, 26037, 26255, 27219, 28566, 29775, 30094, 31311, 32217, 32620, 32912, 34277, 35330, 35469, 36204, 38647, 39160, 39223, 39943, 40800, 41882, 42549, 43394, 44879, 45907, 47421, 47512, 48297, 50064, 50902, 52703, 52764, 54674, 55307, 56663, 58425, 59028, 60576, 60995, 62205, 63129, 64488, 66999, 67189, 68512, 68984, 70170, 71365, 75618, 76793, 77571, 79047, 80309, 83179, 84345, 87016, 87874, 88566, 89607, 91718, 92887, 93839, 95103, 97974, 99583, 101337, 102040, 103626, 104554, 106947, 107205, 108622, 111837, 112800, 113949, 114642, 116291, 117177, 121238, 125492, 126637, 129170, 130986, 131697, 134414, 134699, 136635, 139964, 143294, 144874, 146605, 147499, 148593, 150146, 152318, 152834, 156836, 157150, 160782, 163010, 163502, 164868, 170984, 172922, 174171, 177853, 180249, 182071, 185403, 188314, 190726, 190894, 193477, 196832, 199646, 201472, 202699, 205325, 206811, 208748, 214435, 217182, 218011, 225350, 226682, 229163, 231694, 233570, 234619, 235152, 238727, 240814, 247822, 253857, 254305, 260433, 261620, 262317, 266550, 269195, 271511, 274250, 274753, 280180, 284289, 290005, 293034, 295037, 296506, 298414, 302663, 305782, 308841, 317739, 321173, 323672, 324806, 329181, 331018, 336642, 340901, 343359, 347001, 348110, 348899, 362520, 366119, 368235, 370696, 371542, 377450, 380366, 382012, 382245, 384957, 387479, 390518, 391462, 399174, 403920, 411847, 412671, 416880, 417991, 422453, 433973, 434773, 440619, 441148, 443779, 446065, 456289, 458426, 462402, 470670, 474668, 475800, 481476, 482868, 498435, 501084, 508193, 511258, 514644, 524307, 527197, 535369, 536903, 538331, 542020, 555275, 564016, 566106, 567408, 572027, 582478, 583407, 585871, 593257, 596837, 598426, 599784, 607794, 610404, 621790, 624574, 627703, 633442, 640047, 648858, 659179, 663558, 667337, 672815, 673522, 686013, 691686, 693169, 694279, 696931, 703162, 711364, 723249, 729860, 731008, 739958, 740124, 744403, 753293, 768134, 770113, 773912, 779917, 787407, 794900, 797567, 800658, 813959, 814414, 827123, 829129, 839728, 847430, 850695, 851627, 862856, 880796, 884725, 889285, 896691, 897160, 904970, 909586, 915254, 922852, 935695, 937825, 938876, 959937, 961353, 964857, 970227, 976356, 980581, 986799, 1008106, 1009835, 1016906, 1020306, 1028612, 1033242, 1036012, 1042818, 1050881, 1051783, 1060844, 1086402, 1092043, 1096162, 1103456, 1123464, 1134057, 1136410, 1144080, 1145152, 1147774, 1156687, 1164278, 1166255, 1174751, 1187057, 1195316, 1201262, 1207345, 1212654, 1218610, 1225019, 1227887, 1240777, 1247071, 1258235, 1265462, 1274089, 1279515, 1288613, 1298980, 1306248, 1326918, 1333809, 1341190, 1343482, 1367480, 1372734, 1374779, 1384952, 1388147, 1394240, 1395346, 1409612, 1417336, 1418943, 1423296, 1446209, 1448494, 1462599, 1468933, 1474698, 1496110, 1502217, 1508335, 1513944, 1549693, 1552361, 1558304, 1567726, 1578307, 1593543, 1594370, 1596552, 1604567, 1611655, 1638201, 1657904, 1661549, 1668344, 1684653, 1700848, 1704061, 1712218, 1733148, 1744400, 1756959, 1766186, 1770297, 1774640, 1783782, 1790804, 1797186, 1819167, 1822095, 1835790, 1838687, 1840248, 1843265, 1858487, 1871701, 1874449, 1907155, 1933219, 1941873, 1953108, 1960964, 1970086, 1995385, 2005526, 2006388, 2012407, 2022419, 2027444, 2032071, 2046348, 2049691, 2081218, 2085045, 2107005, 2111011, 2117147, 2128804, 2130734, 2133565, 2163069, 2165643, 2183398, 2186582, 2200866, 2228833, 2238757, 2260397, 2287997, 2303690, 2306210, 2311079, 2319657, 2347177, 2348345, 2364629, 2380657, 2386691, 2392303, 2413369, 2429645, 2435861, 2445907, 2454603, 2461156, 2481207, 2493269, 2496558, 2526270, 2549274, 2559084, 2565601, 2571993, 2574622, 2589585, 2602736, 2606052, 2635578, 2636056, 2649712, 2667175, 2697913, 2705598, 2716472, 2726625, 2740640, 2748032, 2769317, 2773637, 2777175, 2796454, 2808141, 2818050, 2822209, 2828335, 2853048, 2858954, 2879003, 2898699, 2906226, 2928135, 2935468, 2950167, 2955230, 2959204, 2981209, 2999992, 3013106, 3016185, 3016728, 3033485, 3041287, 3046405, 3085842, 3097363, 3129048, 3137101, 3148974, 3153026, 3165425, 3172200, 3187649, 3208795, 3228028, 3239797, 3265353, 3281537, 3310390, 3330139, 3349916, 3351744, 3360950, 3366598, 3375910, 3382995, 3411775, 3438201, 3447140, 3453811, 3471520, 3485127, 3522748, 3569412, 3575690, 3578298, 3585562, 3593337, 3624737, 3626198, 3651501, 3667524, 3674434, 3675907, 3738616, 3754186, 3765841, 3786330, 3807381, 3818043, 3829535, 3831874, 3838373, 3862508, 3910613, 3942689, 3950184, 3954465, 3978469, 3992767, 4014701, 4032219, 4033924, 4065368, 4078004, 4089606, 4101646, 4119004, 4155098, 4166329, 4176904, 4182945, 4197748, 4211593, 4218728, 4253237, 4275441, 4288635, 4298689, 4301972, 4329866, 4357640, 4392330, 4403327, 4415543, 4434657, 4454780, 4460817, 4467239, 4489541, 4518764, 4526891, 4541320, 4560957, 4568090, 4582032, 4609341, 4631837, 4683082, 4688874, 4714962, 4728230, 4733954, 4744119, 4797763, 4819301, 4823437, 4850997, 4865482, 4886981, 4907820, 4931122, 4957782, 5005971, 5014678, 5031077, 5054902, 5059300, 5088659, 5119815, 5135680, 5153376, 5210102, 5213548, 5253584]
for _ in range(int(input())):
n=int(input())
sum=0
for i in range(n):
print(lst[i],end=" ")
sum+=lst[i]
print()
print(sum)
``` | {
"language": "python",
"test_cases": [
{
"input": "4\n1\n2\n3\n4\n",
"output": "1\n1\n1 2\n3\n1 2 4\n7\n1 2 4 8\n15\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/PROC2020/problems/CHFBANK"
} | vfc_3630 | false | false | false | false | false |
apps | verifiable_code | 1198 | Solve the following coding problem using the programming language python:
Chef recently took a course in linear algebra and learned about linear combinations of vectors. Therefore, in order to test his intelligence, Raj gave him a "fuzzy" problem to solve.
A sequence of integers $B_1, B_2, \ldots, B_M$ generates an integer $K$ if it is possible to find a sequence of integers $C_1, C_2, \ldots, C_M$ such that $C_1 \cdot B_1 + C_2 \cdot B_2 + \ldots + C_M \cdot B_M = K$.
In this problem, Chef has a sequence $A_1, A_2, \ldots, A_N$ and he should answer $Q$ queries. In each query, he is given an integer $K$; the answer to this query is the number of pairs $(l, r)$ such that $1 \le l \le r \le N$ and the subsequence $(A_l, A_{l+1}, \ldots, A_r)$ generates $K$.
Chef has no idea how to solve this problem ― can you help him find the answers to all queries?
-----Input-----
- The first line of the input contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.
- The third line contains a single integer $Q$.
- The following $Q$ lines describe queries. Each of these lines contains a single integer $K$.
-----Output-----
For each query, print a single line containing one integer ― the number of contiguous subsequences that generate $K$.
-----Constraints-----
- $1 \le N, Q \le 10^5$
- $1 \le A_i \le 10^9$ for each valid $i$
- $1 \le K \le 10^6$
-----Subtasks-----
Subtask #1 (10 points): $1 \le N \le 1,000$
Subtask #2 (90 points): original constraints
-----Example Input-----
2
2 4
3
1
2
8
-----Example Output-----
0
2
3
-----Explanation-----
The sequence $(2, 4)$ has three contiguous subsequences: $(2)$, $(4)$ and $(2, 4)$.
- In the first query, $1$ cannot be generated by any subsequence.
- In the second query, $2$ is generated by subsequences $(2)$ and $(2, 4)$. For example, for the subsequence $(2, 4)$, we can choose $C_1 = 1$ and $C_2 = 0$.
- In the third query, $8$ is generated by all three subsequences.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import math
import bisect
from functools import reduce
from collections import defaultdict
# import sys
# input = sys.stdin.readline
def inn():
return int(input())
def inl():
return list(map(int, input().split()))
MOD = 10**9+7
INF = inf = 10**18+5
n = inn()
a = inl()
k = []
for q in range(inn()):
k.append(inn())
gcdn = reduce(math.gcd, a)
lim = max(k)+1
ans = defaultdict(int)
ans[1] = 0
for i in range(n):
cur_gcd = a[i]
for j in range(i, n):
cur_gcd = math.gcd(cur_gcd, a[j])
if cur_gcd==1 or cur_gcd//gcdn==1:
ans[cur_gcd] += (n-j)
break
ans[cur_gcd] += 1
# print(ans)
keys = list(ans.keys())
ans1 = [0]*lim
for i in keys:
for j in range(i, lim, i):
ans1[j] += ans[i]
# print(ans1[:10])
for i in k:
print(ans1[i])
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n2 4\n3\n1\n2\n8\n",
"output": "0\n2\n3\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/FUZZYLIN"
} | vfc_3634 | false | false | false | false | false |
apps | verifiable_code | 1199 | Solve the following coding problem using the programming language python:
In a country called Chef Land, there was a lot of monetary fraud, so Chefu, the head of the country, decided to choose new denominations of the local currency ― all even-valued coins up to an integer $N$ should exist. After a few days, a citizen complained that there was no way to create an odd value, so Chefu decided that he should also introduce coins with value $1$. Formally, you are given an integer $N$; for $v = 1$ and each even positive integer $v \le N$, coins with value $v$ exist.
You are also given an integer $S$. To handle transactions quickly, find the minimum number of coins needed to pay a price $S$.
-----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 $S$ and $N$.
-----Output-----
For each test case, print a single line containing one integer ― the minimum number of coins.
-----Constraints-----
- $1 \le T \le 10,000$
- $1 \le S \le 10^9$
- $2 \le N \le 10^9$
- $N$ is even
-----Subtasks-----
Subtask #1 (100 points): original constraints
-----Example Input-----
4
2 2
1 14
30 10
31 4
-----Example Output-----
1
1
3
9
-----Explanation-----
Example case 1: One coin with value $2$ is sufficient.
Example case 2: We need to use one coin with value $1$.
Example case 3: We need $3$ coins, each with value $10$.
Example case 4: We can use seven coins with value $4$, one coin with value $2$ and one coin with value $1$.
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())):
n,k=list(map(int,input().split()))
t=0
if n%2!=0:
n-=1
t+=1
t+=(n//k)
if n%k!=0:
t+=1
print(t)
``` | {
"language": "python",
"test_cases": [
{
"input": "4\n2 2\n1 14\n30 10\n31 4\n",
"output": "1\n1\n3\n9\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CHFMOT18"
} | vfc_3638 | false | false | false | false | false |
apps | verifiable_code | 1200 | Solve the following coding problem using the programming language python:
Two sisters, A and B, play the piano every day. During the day, they can play in any order. That is, A might play first and then B, or it could be B first and then A. But each one of them plays the piano exactly once per day. They maintain a common log, in which they write their name whenever they play.
You are given the entries of the log, but you're not sure if it has been tampered with or not. Your task is to figure out whether these entries could be valid or not.
-----Input-----
- The first line of the input contains an integer $T$ denoting the number of test cases. The description of the test cases follows.
- The first line of each test case contains a string $s$ denoting the entries of the log.
-----Output-----
- For each test case, output yes or no according to the answer to the problem.
-----Constraints-----
- $1 \le T \le 500$
- $2 \le |s| \le 100$
- $|s|$ is even
- Each character of $s$ is either 'A' or 'B'
-----Example Input-----
4
AB
ABBA
ABAABB
AA
-----Example Output-----
yes
yes
no
no
-----Explanation-----
Testcase 1: There is only one day, and both A and B have played exactly once. So this is a valid log. Hence 'yes'.
Testcase 2: On the first day, A has played before B, and on the second day, B has played first. Hence, this is also a valid log.
Testcase 3: On the first day, A played before B, but on the second day, A seems to have played twice. This cannot happen, and hence this is 'no'.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def checkValidity(s):
count = 0
previous = ""
for x in s:
if count == 0:
previous = x
count += 1
elif count == 1:
count = 0
if previous == x:
return "no"
return "yes"
t = int(input())
for _ in range(t):
s = input()
print(checkValidity(s))
``` | {
"language": "python",
"test_cases": [
{
"input": "4\nAB\nABBA\nABAABB\nAA\n",
"output": "yes\nyes\nno\nno\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/PLAYPIAN"
} | vfc_3642 | false | false | false | false | false |
apps | verifiable_code | 1201 | Solve the following coding problem using the programming language python:
Kim has broken in to the base, but after walking in circles, perplexed by the unintelligible base design of the JSA, he has found himself in a large, empty, and pure white, room.
The room is a grid with H∗W$H*W$ cells, divided into H$H$ rows and W$W$ columns. The cell (i,j)$(i,j)$ is at height A[i][j]$A[i][j]$. Unfortunately, his advanced sense of smell has allowed him to sense a mercury leak, probably brought in by Jishnu to end his meddling. The mercury leak has a power (which determines what height the mercury can reach before dissipating into harmless quantities) and a source cell. It spreads from cells it has already reached to other cells in the four cardinal directions: north, south, east, and west. (That is, the mercury can spread up, down, right, or left in the grid, but not diagonally.) Mercury can only spread to a cell if the cell's height is strictly less than the power value.
Unfortunately, Kim does not exactly know the starting cell or the power value of the mercury leak. However, his impressive brain has determined that it must be one of Q$Q$ (power, starting cell) combinations. For each combination, he wants to find out how many cells are dangerous for him to go to: that is, how many cells will eventually be reached by the mercury. This will help him determine a suitable cell to stay in and slowly fix the leak from above.
Can you help Kim achieve this objective?
Note: If the starting cell's height is not less than the power level, the mercury immediately dissipates. So, in this case, output 0.
-----Input:-----
- First line will contain T$T$, number of testcases. Then the testcases follow.
- The first line in each testcase contains three integers, H$H$, W$W$, and Q$Q$.
- On the 2$2$nd to (H+1)$(H+1)$th lines of each testcase: The (i+1)$(i+1)$th line contains W$W$ space-separated integers, representing the heights of the cells on the i$i$th row of the grid.
- On the (H+2)$(H+2)$th to (H+Q+1)$(H+Q+1)$th lines of each testcase: The (i+H+1)$(i+H+1)$th line contains 3 space-separated integers, r[i]$r[i]$, c[i]$c[i]$, and p[i]$p[i]$, which represents a (power, starting cell) combination. For this specific combination, the mercury leak originates on the cell (r[i],c[i])$(r[i],c[i])$ and has power p[i]$p[i]$.
-----Output:-----
For each testcase, output Q$Q$ lines, with each line containing one integer. The i$i$th line should output the number of dangerous cells, given that the leak originated on cell (r[i],c[i])$(r[i],c[i])$ with power p[i]$p[i]$, as defined in the input. Read the sample and sample explanation for more details.
-----Constraints-----
- 1≤T≤2$1 \leq T \leq 2$
- 1≤H≤1000$1 \leq H \leq 1000$
- 1≤W≤1000$1 \leq W \leq 1000$
- 1≤Q≤2∗105$1 \leq Q \leq 2*10^5$
- 1≤r[i]≤H$1 \leq r[i] \leq H$ for all i$i$
- 1≤c[i]≤W$1 \leq c[i] \leq W$ for all i$i$
- 0≤A[i][j]≤109$0 \leq A[i][j] \leq 10^9$ for all (i,j)$(i,j)$
- 0≤p[i]≤109$0 \leq p[i] \leq 10^9$ for all i$i$.
-----Subtasks-----
- 10 points : A[i][j]=$A[i][j] =$ constant k$k$ for all (i,j)$(i,j)$ (the heights of all cells are equal).
- 20 points : H=1$H=1$, Q≤1000$Q \leq 1000$.
- 30 points: r[i]=$r[i] =$ constant x$x$, c[i]=$c[i] =$ constant y$y$ for all i$i$ (the starting cell is fixed).
- 40 points: No additional constraints.
-----Sample Input:-----
1
5 5 3
4 3 9 7 2
8 6 5 2 8
1 7 3 4 3
2 2 4 5 6
9 9 9 9 9
3 4 6
3 2 5
1 4 9
-----Sample Output:-----
10
0
19
-----EXPLANATION:-----
For the first query, the cell (3,4)$(3,4)$ has height 4. The mercury can reach the following cells: (2,3)$(2,3)$, (2,4)$(2,4)$, (3,1)$(3,1)$, (3,3)$(3,3)$, (3,4)$(3,4)$, (3,5)$(3,5)$, (4,1)$(4,1)$, (4,2)$(4,2)$, (4,3)$(4,3)$, (4,4)$(4,4)$, for a total of 10. Note that it cannot reach cell (4,5)$(4,5)$ because the height (6) is equal to the power value (6).
For the second query, the cell (3,2)$(3,2)$ has height 7. Since the power value of 5 is less than or equal to the height of 7, the mercury immediately dissipates and so it reaches 0 cells.
For the third query, the mercury can reach all cells except the bottom row and the cell (1,3)$(1,3)$.
Note that (x,y)$(x,y)$ means the cell on the x$x$-th row and y$y$-th column.
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 solve(l,r,c,row,col,po):
count=0
visited=set()
stack=set()
stack.add((l[row][col],row,col))
while stack:
ele=stack.pop()
visited.add((ele[1],ele[2]))
if ele[0]<po:
count+=1
if ele[1]-1>=0 and (ele[1]-1,ele[2]) not in visited:
if l[ele[1]-1][ele[2]]<po:
stack.add((l[ele[1]-1][ele[2]],ele[1]-1,ele[2]))
if ele[1]+1<r and (ele[1]+1,ele[2]) not in visited:
if l[ele[1]+1][ele[2]]<po:
stack.add((l[ele[1]+1][ele[2]],ele[1]+1,ele[2]))
if ele[2]-1>=0 and (ele[1],ele[2]-1) not in visited:
if l[ele[1]][ele[2]-1]<po:
stack.add((l[ele[1]][ele[2]-1],ele[1],ele[2]-1))
if ele[2]+1<c and (ele[1],ele[2]+1) not in visited:
if l[ele[1]][ele[2]+1]<po:
stack.add((l[ele[1]][ele[2]+1],ele[1],ele[2]+1))
return count
for _ in range(int(input())):
r,c,q=map(int,input().split())
l=[]
for i in range(r):
a=list(map(int,input().split()))
l.append(a)
for i in range(q):
row,col,po=map(int,input().split())
print(solve(l,r,c,row-1,col-1,po))
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n5 5 3\n4 3 9 7 2\n8 6 5 2 8\n1 7 3 4 3\n2 2 4 5 6\n9 9 9 9 9\n3 4 6\n3 2 5\n1 4 9\n",
"output": "10\n0\n19\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/UWCOI20C"
} | vfc_3646 | false | false | false | false | false |
apps | verifiable_code | 1202 | Solve the following coding problem using the programming language python:
Mark loves eating chocolates and also likes to be fit. Given the calorie count for every chocolate he eats, find what he has to do to burn the calories.
The name of the chocolates along with its calorie count are given as follows:
Calories per one whole bar:
Dairy milk (D)
238
Twix (T)
244
Milky Bar (M)
138
Bounty (B)
279
Crunchie (C)
186
The exercises preferred by him and the calories burnt are as follows:
Calories burnt per km:
Running
50
Cycling
5
Walking
0.5
Find the number of kilometers he has to run, cycle or walk to burn all of the calories.
Priority given to the exercises is as follows:
Running > Cycling > Walking
-----Input:-----
- It is a one line string consisting of the names(initial letter) of all the chocolates he has eaten.
-----Output:-----
Print three lines. First line representing how much he ran(in km), second line representing how much he cycled(in km), third line representing how much he walked(in km).
-----Constraints-----
- 1 <= length of input string <= 10.
-----Sample Input:-----
DDTM
-----Sample Output:-----
17
1
6
-----EXPLANATION:-----
Calorie intake = 238 + 238 + 244 + 138 = 858
( 17km x 50 ) + ( 1km x 5 ) + ( 6km x 0.5 ) = 858.
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
d = { 'D': 238, 'T': 244, 'M': 138, 'B': 279, 'C': 186 }
s = list(input())
totalCal = 0
for i in range(len(s)):
if s[i] == 'D':
totalCal += d['D']
if s[i] == 'T':
totalCal += d['T']
if s[i] == 'M':
totalCal += d['M']
if s[i] == 'B':
totalCal += d['B']
if s[i] == 'C':
totalCal += d['C']
R = totalCal // 50
Rm = totalCal % 50
C = Rm // 5
Cm = Rm % 5
x = totalCal - (R * 50 + C * 5)
# print(totalCal - R * 50 + C * 5)
W = int(x * 4 * 0.5)
# print(R * 50 + C * 5 + W * 0.5)
print(R)
print(C)
print(W)
``` | {
"language": "python",
"test_cases": [
{
"input": "DDTM\n",
"output": "17\n1\n6\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/RTCG2020/problems/RTCG002"
} | vfc_3650 | false | false | false | false | false |
apps | verifiable_code | 1203 | Solve the following coding problem using the programming language python:
Given a set of N natural numbers 1,2,3........N and Q query.For each query you have to calculate the total number of subset in which Ith.
number of set come at Kth postion.Elements of every subset should be in sorted order.
The answer could be very large so you have to print answer modulo 1e9+7.
-----Input:-----
- The first line of input cotains a single integer T denoting the number of test cases.
- For every test case it contains two number N and Q.
- Next Q line contains two number I and K.
-----Output:-----
For each test case print required answer.
-----Constraints and Subtasks:-----
- 1<=T<=5
- 1<=N, K<=4000
- 1<=Q<=1000000
Subtask 3: 5 points
- 1<=T<=5
- 1<=N, K<=16
- 1<=Q<=1000
Subtask 1: 25 points
- T=1
- 1<=N, K<=4000
- 1<=Q<=100000
Subtask 2: 70 points
- Original Constraints.
-----Example:-----
Input:
1
3 3
1 2
2 1
3 2
Output:
0
2
2
-----Explanation:-----
For N=3
total subsets are:
{1}
{2}
{3}
{1,2}
{1,3}
{2,3}
{1,2,3}
Now we can see that for I=1 and K=2 there is no subset in which 1 come at 2nd position so the answer is Zero for that query.
For 2nd query I=2 and K=1 there are two subset i.e {2,3} and {2} in which 2 come at 1st position.
Same for 3rd querry there is two subset i.e{1,3} and {2,3}.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import math
f = math.factorial
for u in range(eval(input())):
n, q = list(map(int, input().split()))
for j in range(q):
i,k = list(map(int, input().split()))
if k>i:
c=0
print(c)
else:
a=2**(n-i)
b=1
d=int(i-1)
e=1
h=1
g=1
#b=f(i-1)/f(k-1)/f(i-k)
if(k-1>i-k):
for z in range(i-k):
b=b*d
d=d-1
e=e*h
h=h+1
b=b/e
else:
for z in range(k-1):
b=b*d
d=d-1
e=e*g
g=g+1
b=b/e
c=a*b
c=c%1000000007
print(c)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n3 3\n1 2\n2 1\n3 2\n",
"output": "0\n2\n2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/CDVA16/problems/CDVA1607"
} | vfc_3654 | false | false | false | false | false |
apps | verifiable_code | 1204 | Solve the following coding problem using the programming language python:
You are given two strings $S$ and $R$. Each of these strings has length $N$. We want to make $S$ equal to $R$ by performing the following operation some number of times (possibly zero):
- Choose two integers $a$ and $b$ such that $1 \le a \le b \le N$.
- For each $i$ such that $a \le i \le b$, replace the $i$-th character of $S$ by the $i$-th character of $R$.
Suppose that we make $S$ equal to $R$ by performing this operation $k$ times, in such a way that the total number of replaced characters (i.e. the sum of all $k$ values of $b-a+1$) is $l$. Then, the cost of this process is defined as $k \cdot l$.
Find the minimum cost with which we can make $S$ equal to $R$.
-----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 string $S$.
- The second line contains a single string $R$.
-----Output-----
For each test case, print a single line containing one integer ― the minimum cost.
-----Constraints-----
- $1 \le T \le 4,000$
- $1 \le N \le 10^6$
- $|S| = |R| = N$
- $S$ and $R$ contain only lowercase English letters
- the sum of $N$ over all test cases does not exceed $2 \cdot 10^6$
-----Example Input-----
1
adefb
bdefa
-----Example Output-----
4
-----Explanation-----
Example case 1: $S$ can be made equal to $R$ in two moves. First, we replace $S_1$ by $R_1$ and then replace $S_5$ by $R_5$. We have $k = l = 2$, so the cost is $2 \cdot 2 = 4$. If we wanted to perform only one operation, the cost would be $5$.
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())):
s = input()
r = input()
diff = list()
index = list()
cnt = 0
for i in range(len(s)):
if s[i] != r[i]:
cnt += 1
index.append(i)
for i in range(1, len(index)):
diff.append(index[i] - index[i - 1] - 1)
diff.sort()
fmin = cnt ** 2
oper = cnt ; moves = cnt
for i in diff:
moves += i
oper -= 1
fmin = min(fmin, moves * oper)
print(fmin)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\nadefb\nbdefa\n",
"output": "4\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/MINOPS"
} | vfc_3658 | false | false | false | false | false |
apps | verifiable_code | 1205 | Solve the following coding problem using the programming language python:
Do you know Professor Saeed? He is the algorithms professor at Damascus University. Yesterday, he gave his students hard homework (he is known for being so evil) - for a given binary string $S$, they should compute the sum of $F(S, L, R)$ over all pairs of integers $(L, R)$ ($1 \le L \le R \le |S|$), where the function $F(S, L, R)$ is defined as follows:
- Create a string $U$: first, set $U = S$, and for each $i$ ($L \le i \le R$), flip the $i$-th character of $U$ (change '1' to '0' or '0' to '1').
- Then, $F(S, L, R)$ is the number of valid pairs $(i, i + 1)$ such that $U_i = U_{i+1}$.
As usual, Professor Saeed will give more points to efficient solutions. Please help the students solve this homework.
-----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 a single string $S$.
-----Output-----
For each test case, print a single line containing one integer $\sum_{1 \le L \le R \le |S|} F(S, L, R)$.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le |S| \le 3 \cdot 10^6$
- the sum of $|S|$ over all test cases does not exceed $6 \cdot 10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $1 \le |S| \le 300$
- the sum of $|S|$ over all test cases does not exceed $600$
Subtask #2 (50 points): original constraints
-----Example Input-----
1
001
-----Example Output-----
6
-----Explanation-----
Example case 1:
- $L = 1, R = 1$: $U$ is "101", $F = 0$
- $L = 2, R = 2$: $U$ is "011", $F = 1$
- $L = 3, R = 3$: $U$ is "000", $F = 2$
- $L = 1, R = 2$: $U$ is "111", $F = 2$
- $L = 2, R = 3$: $U$ is "010", $F = 0$
- $L = 1, R = 3$: $U$ is "110", $F = 1$
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()
n=len(s)
t=0
ans=0
for i in range(n-1):
if(s[i]==s[i+1]):
t=t+1
x=t
for i in range(n):
t=x
if(i!=0):
if(s[i]==s[i-1]):
t=t-1
else:
t=t+1
y=t
for j in range(i,n):
t=y
try:
if(s[j]==s[j+1]):
t=t-1
else:
t=t+1
except:
pass
ans=ans+t
print(ans)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n001\n",
"output": "6\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/EVIPRO"
} | vfc_3662 | false | false | false | false | false |
apps | verifiable_code | 1206 | Solve the following coding problem using the programming language python:
As you might remember, the collector of Siruseri had ordered
a complete revision of the Voters List. He knew that constructing
the list of voters is a difficult task, prone to errors. Some
voters may have been away on vacation, others may have moved
during the enrollment and so on.
To be as accurate as possible, he entrusted the task to three different
officials. Each of them was to independently record the list of voters and
send it to the collector. In Siruseri, every one has a ID number and
the list would only list the ID numbers of the voters and not their names.
The officials were expected to arrange the ID numbers in ascending order
in their lists.
On receiving the lists, the Collector realised that there were
discrepancies - the three lists were not identical. He decided
to go with the majority. That is, he decided to construct the
final list including only those ID numbers that appeared in at
least 2 out of the 3 lists. For example if the three lists
were
23 30 42 57 90
21 23 35 57 90 92
21 23 30 57 90
then the final list compiled by the collector would be:
21 23 30 57 90
The ID numbers 35, 42 and 92 which appeared in only one list
each do not figure in the final list.
Your task is to help the collector by writing a program that
produces the final list from the three given lists.
Input format
The first line of the input contains 3 integers
N1, N2 and
N3. N1 is the number of
voters in the first list, N2 is the number of
voters in the second list and N3 is the number of
voters in the third list. The next N1 lines
(lines 2,...,N1+1) contain one positive integer
each and describe the first list in ascending order. The following
N2 lines (lines
N1+2,...,N1+N2+1)
describe the second list in ascending order and the final
N3 lines (lines
N1+N2+2,...,N1+N2+N3+1)
describe the third list in ascending order.
Output format
The first line of the output should contain a single integer
M indicating the number voters in the final list. The next
M lines (lines 2,...,M+1) should contain one
positive integer each, describing the list of voters in the final
list, in ascending order.
Test data
You may assume that 1 ≤
N1,N2,N3
≤ 50000. You may also assume that in 50% of the inputs 1 ≤ N1,N2,N3 ≤ 2000.
Example
Sample input:
5 6 5
23
30
42
57
90
21
23
35
57
90
92
21
23
30
57
90
Sample output:
5
21
23
30
57
90
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
from sys import stdout, stdin
n,m,o = list(map(int, stdin.readline().split()))
n= n+m+o
l=[]
a=[]
for i in range(n):
b= int(stdin.readline())
if(b in l and b not in a):
l.append(b)
a.append(b)
elif(b not in l):
l.append(b)
a.sort()
stdout.write(str(len(a)) + '\n')
stdout.write(''.join([str(id) + '\n' for id in a]))
``` | {
"language": "python",
"test_cases": [
{
"input": "5 6 5\n23\n30\n42\n57\n90\n21\n23\n35\n57\n90\n92\n21\n23\n30\n57\n90\nSample output:\n5\n21\n23\n30\n57\n90\n",
"output": "",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/IARCSJUD/problems/VOTERSDI"
} | vfc_3666 | false | false | false | false | false |
apps | verifiable_code | 1207 | Solve the following coding problem using the programming language python:
Chef is the new king of the country Chefland. As first and most important responsibility he wants to reconstruct the road system of Chefland. There are N (1 to N) cities in the country and each city i has a population Pi. Chef wants to build some bi-directional roads connecting different cities such that each city is connected to every other city (by a direct road or through some other intermediate city) and starting from any city one can visit every other city in the country through these roads. Cost of building a road between two cities u and v is Pu x Pv. Cost to build the road system is the sum of cost of every individual road that would be built.
Help king Chef to find the minimum cost to build the new road system in Chefland such that every city is connected to each other.
-----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 contains an integer N denoting the number of cities in the country. Second line contains N space separated integers Pi, the population of i-th city.
-----Output-----
For each test case, print a single integer, the minimum cost to build the new road system on separate line.
-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 105
- 1 ≤ Pi ≤ 106
-----Example-----
Input:
2
2
5 10
4
15 10 7 13
Output:
50
266
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=int(input())
a=list(map(int,input().split()))
a.sort()
s=sum(a)
if a[0]*(s-a[0])<=a[n-1]*(s-a[n-1]):
print(a[0]*(s-a[0]))
else:
print(a[n-1]*(s-a[n-1]))
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n2\n5 10\n4\n15 10 7 13\n",
"output": "50\n266\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/KINGSHIP"
} | vfc_3670 | false | false | false | false | false |
apps | verifiable_code | 1208 | Solve the following coding problem using the programming language python:
Chef is solving mathematics problems. He is preparing for Engineering Entrance exam. He's stuck in a problem.
$f(n)=1^n*2^{n-1}*3^{n-2} * \ldots * n^{1} $
Help Chef to find the value of $f(n)$.Since this number could be very large, compute it modulo $1000000007$.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, $N$.
-----Output:-----
For each testcase, output in a single line the value of $f(n)$ mod $1000000007$.
-----Constraints-----
- $1 \leq T \leq 10^6$
- $1 \leq N \leq 10^6$
-----Subtasks-----
Subtask 1(24 points) :
- $1 \leq T \leq 5000$
- $1 \leq N \leq 5000$
Subtask 2(51 points) : original constraints
-----Sample Input:-----
1
3
-----Sample Output:-----
12
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())
t=[]
for _ in range(T):
N=int(input())
t.append(N)
N=max(t)+1
l=[0 for i in range(N)]
p=1
a=1
for i in range(1,N):
a=(a*i)%1000000007
p=p*a%1000000007
l[i]=p
for i in t:
print(l[i])
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n3\n",
"output": "12\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/MATHL"
} | vfc_3674 | false | false | false | false | false |
apps | verifiable_code | 1209 | Solve the following coding problem using the programming language python:
It's winter and taking a bath is a delicate matter. Chef has two buckets of water. The first bucket has $v_1$ volume of cold water at temperature $t_1$. The second has $v_2$ volume of hot water at temperature $t_2$. Chef wants to take a bath with at least $v_3$ volume of water which is at exactly $t_3$ temperature. To get the required amount of water, Chef can mix some water obtained from the first and second buckets.
Mixing $v_x$ volume of water at temperature $t_x$ with $v_y$ volume of water at temperature $t_y$ yields $v_x + v_y$ volume of water at temperature calculated as
vxtx+vytyvx+vyvxtx+vytyvx+vy\frac{v_x t_x + v_y t_y}{v_x + v_y}
Help Chef figure out if it is possible for him to take a bath with at least $v_3$ volume of water at temperature $t_3$.
Assume that Chef has no other source of water and that no heat is lost by the water in the buckets with time, so Chef cannot simply wait for the water to cool.
-----Input-----
- The first line contains $n$, the number of test cases. $n$ cases follow.
- Each testcase contains of a single line containing 6 integers $v_1, t_1, v_2, t_2, v_3, t_3$.
-----Output-----
- For each test case, print a single line containing "YES" if Chef can take a bath the way he wants and "NO" otherwise.
-----Constraints-----
- $1 \leq n \leq 10^5$
- $1 \leq v_1, v_2, v_3 \leq 10^6$
- $1 \leq t_1 < t_2 \leq 10^6$
- $1 \leq t_3 \leq 10^6$
-----Sample Input-----
3
5 10 5 20 8 15
5 10 5 20 1 30
5 10 5 20 5 20
-----Sample Output-----
YES
NO
YES
-----Explanation-----
- Case 1: Mixing all the water in both buckets yields 10 volume of water at temperature 15, which is more than the required 8.
- Case 2: It is not possible to get water at 30 temperature.
- Case 3: Chef can take a bath using only the water in the second bucket.
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 i in range(int(input())):
v1,t1,v2,t2,v3,t3=map(int,input().split())
ok = 0
if t1 <= t3 <= t2:
x, y = t2 - t3, t3 - t1
ok = x * v3 <= (x + y) * v1 and y * v3 <= (x + y) * v2
print('YES' if ok else 'NO')
except:
pass
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n5 10 5 20 8 15\n5 10 5 20 1 30\n5 10 5 20 5 20\n",
"output": "YES\nNO\nYES\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CLBATH"
} | vfc_3678 | false | false | false | false | false |
apps | verifiable_code | 1210 | Solve the following coding problem using the programming language python:
-----Problem Statement-----
One of the things JEC is known for is its GR (Group Recreation) where juniors and seniors do friendly interaction ;P
As for the new session of 2020 seniors decided to have their first GR and give them some treat. Juniors were excited about it they came to college canteen aligned in a line and counted themselves one by one from left to right so that every junior gets his/her treat. But seniors played a game and they will treat only the ones who passes in this game. Game is simple all they need to do is to alternate their language (between Hindi and English) while telling their positions that is if the junior just before you told 2 in English you need to say 3 in Hindi . You do not want to be the one left without a treat.
You are the junior standing at position $X$ from left and the counting could start from left or right you have to predict which number you have to speak and in which language when your turn comes.
-----Input:-----
-
First line will contain $T$, number of testcases. Then the testcases follow.
-
Each testcase contains 2 lines first consist 2 space separated integers, $N$ (total count) , $X$ (your position from left), next line consist of 2 space separated characters L or R (Direction from which counting starts L-left, R-Right) and H or E (the language to start counting).
-----Output:-----
For each testcase, output a single line consisting space seperated Integer P and Character L
where P is the number you will speak and L is the language (H or E).
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 120$
- $1 \leq X \leq N$
-----Sample Input:-----
2
15 5
L H
20 14
R E
*try to trim extra white spaces like new line during input in case of wrong answer
-----Sample Output:-----
5 H
7 E
-----EXPLANATION:-----
- When counting starts from left with H it alternates like H E H E H….. on the fifth position H comes
- When Count starts from right with E it alternates like E H E H E H E….. with E on the position of 14th student from right.
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:
t=t-1
n,x=input().split()
n=int(n)
x=int(x)
d,l=input().split()
if d=='L':
p=x
elif d=='R':
p=(n-x)+1
if p%2==1:
if l=='H':
lang='H'
else:
lang='E'
elif p%2==0:
if l=='H':
lang='E'
else:
lang='H'
print(p,lang)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n15 5\nL H\n20 14\nR E\n*try to trim extra white spaces like new line during input in case of wrong answer\n",
"output": "5 H\n7 E\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/COPT2020/problems/GRDAY1"
} | vfc_3682 | false | false | false | false | false |
apps | verifiable_code | 1211 | Solve the following coding problem using the programming language python:
The chef is having one string of English lower case alphabets only. The chef wants to remove all "abc" special pairs where a,b,c are occurring consecutively. After removing the pair, create a new string and again remove "abc" special pair from a newly formed string. Repeate the process until no such pair remains in a string.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, $String$.
-----Output:-----
For each testcase, output in a single line answer, new String with no "abc" special pair.
-----Constraints:-----
$T \leq 2 $
$1 \leq String length \leq 1000 $
-----Sample Input:-----
2
aabcc
bababccc
-----Sample Output:-----
ac
bc
-----EXPLANATION:-----
For 1) after removing "abc" at middle we get a new string as ac.
For 2) string = bababccc
newString1 = babcc // After removing middle "abc"
newString2 = bc //After removing "abc"
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())):
s=input()
while(s.count("abc")!=0):
s=s.replace("abc","")
print(s)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\naabcc\nbababccc\n",
"output": "ac\nbc\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/PBK12020/problems/ITGUY15"
} | vfc_3686 | false | false | false | false | false |
apps | verifiable_code | 1212 | Solve the following coding problem using the programming language python:
"Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up."
Let's call a string balanced if all characters that occur in this string occur in it the same number of times.
You are given a string $S$; this string may only contain uppercase English letters. You may perform the following operation any number of times (including zero): choose one letter in $S$ and replace it by another uppercase English letter. Note that even if the replaced letter occurs in $S$ multiple times, only the chosen occurrence of this letter is replaced.
Find the minimum number of operations required to convert the given string to a balanced string.
-----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 a single string $S$.
-----Output-----
For each test case, print a single line containing one integer ― the minimum number of operations.
-----Constraints-----
- $1 \le T \le 10,000$
- $1 \le |S| \le 1,000,000$
- the sum of $|S|$ over all test cases does not exceed $5,000,000$
- $S$ contains only uppercase English letters
-----Subtasks-----
Subtask #1 (20 points):
- $T \le 10$
- $|S| \le 18$
Subtask #2 (80 points): original constraints
-----Example Input-----
2
ABCB
BBC
-----Example Output-----
1
1
-----Explanation-----
Example case 1: We can change 'C' to 'A'. The resulting string is "ABAB", which is a balanced string, since the number of occurrences of 'A' is equal to the number of occurrences of 'B'.
Example case 2: We can change 'C' to 'B' to make the string "BBB", which is a balanced string.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
from sys import stdin
from collections import Counter
def func(arr,n,l):
count=0
k=l//n
if n<len(arr):
for ele in arr[0:n]:
count+=max(0,k-ele)
else:
for ele in arr:
count+=max(0,ele-k)
return count
for _ in range(int(stdin.readline())):
s=stdin.readline().strip()
d=Counter(s)
arr=sorted(list(d.values()),reverse=True)
l=len(s)
val=[1]
for i in range(2,27):
if l%i==0:
val.append(i)
ans = float('inf')
for ele in val:
x = func(arr,ele,l)
if x < ans:
ans = x
print(ans)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\nABCB\nBBC\n",
"output": "1\n1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/ARTBALAN"
} | vfc_3690 | false | false | false | false | true |
apps | verifiable_code | 1213 | Solve the following coding problem using the programming language python:
Chef and his competitor Kefa own two restaurants located at a straight road. The position of Chef's restaurant is $X_1$, the position of Kefa's restaurant is $X_2$.
Chef and Kefa found out at the same time that a bottle with a secret recipe is located on the road between their restaurants. The position of the bottle is $X_3$.
The cooks immediately started to run to the bottle. Chef runs with speed $V_1$, Kefa with speed $V_2$.
Your task is to figure out who reaches the bottle first and gets the secret recipe (of course, it is possible that both cooks reach the bottle at the same time).
-----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 five space-separated integers $X_1$, $X_2$, $X_3$, $V_1$ and $V_2$.
-----Output-----
For each test case, print a single line containing the string "Chef" if Chef reaches the bottle first, "Kefa" if Kefa reaches the bottle first or "Draw" if Chef and Kefa reach the bottle at the same time (without quotes).
-----Constraints-----
- $1 \le T \le 10^5$
- $|X_1|, |X_2|, |X_3| \le 10^5$
- $X_1 < X_3 < X_2$
- $1 \le V_1 \le 10^5$
- $1 \le V_2 \le 10^5$
-----Example Input-----
3
1 3 2 1 2
1 5 2 1 2
1 5 3 2 2
-----Example Output-----
Kefa
Chef
Draw
-----Explanation-----
Example case 1. Chef and Kefa are on the same distance from the bottle, but Kefa has speed $2$, while Chef has speed $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 t in range(int(input())):
x1,x2,x3,v1,v2=[int(x)for x in input().rstrip().split()]
t1=abs(x3-x1)/v1
t2=abs(x3-x2)/v2
if t1<t2:
print("Chef")
elif t1>t2:
print("Kefa")
elif t1==t2:
print("Draw")
else:
pass
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n1 3 2 1 2\n1 5 2 1 2\n1 5 3 2 2\n",
"output": "Kefa\nChef\nDraw\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CHEFRUN"
} | vfc_3694 | false | false | false | false | false |
apps | verifiable_code | 1214 | Solve the following coding problem using the programming language python:
Everybody is worried about Rakesh as the boy does not have much knowledge about the real world. He can not go from one place to another on his own. It's high time he learned to explore the city. He is going to a relative's house situated on the other side of the city on his own. As this is his first time, he is carrying a GPS tracker of a special kind. The tracker continuously sends information to the family of Rakesh about his movement. The information is sent using the following four letters: U, D, R, and L. Those letters indicate the moves taken by Rakesh.
The city can be considered as a grid. Rakesh starts his journey from (0, 0) position of the grid. His relative's house is situated at (Rx, Ry). Rakesh can move in four directions: up, down, right, or left indicated by U, D, R, and L respectively.
Any position of the city with x ordinate negative or greater than M is considered as dangerous. Also, any position of the city with y ordinate negative or greater than N is considered as dangerous. You will be given the total sequence of Rakesh's movement. You need to determine if Rakesh ended up being at his relative's house, at a dangerous place, or at a random place in the city.
To make things clear,
- U indicates a move that increases position along y-axis by 1
- D indicates a move that decreases position along y-axis by 1
- R indicates a move that increases position along x-axis by 1
- L indicates a move that decreases position along x-axis by 1
Note that we are interested in the position of Rakesh at the end of his journey only. He may visit some dangerous place or his relative's house at some intermediate point but that won't affect the answer.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases. The description of each test case follows.
- The first line of each test case contains two integers M and N.
- The second line contains two integers Rx and Ry.
- The third line contains the length of Rakesh's move sequence.
- The next line contains the move sequence containing letters U, D, R, and L only with no space.
-----Output-----
For each test case, print "Case i: ", and then the answer, where i is the testcase number, 1-indexed. The answer should be any of the following three strings:
- "REACHED" if Rakesh could reach his relative's house
- "DANGER" if Rakesh ended up being in a dangerous place
- "SOMEWHERE" if Rakesh ended up being in somewhere safe place in the city other than his relative's place
Don't print any quotation mark. Check the sample output.
-----Constraints-----
- 1 ≤ T ≤ 10
- 0 ≤ M, N ≤ 10000
- 0 ≤ Rx ≤ M
- 0 ≤ Ry ≤ N
- 0 ≤ Sum of the lengths of all sequences ≤ 10000
-----Example-----
Input:
2
20 20
4 5
13
LLUUUUURRRRRR
10 10
3 4
7
UDUDDRR
Output:
Case 1: REACHED
Case 2: DANGER
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 i in range(t):
print("Case {}:".format(i+1), end=" ")
m, n = map(int,input().split())
x, y = map(int,input().split())
l = int(input())
a=input()
destx = a.count("R")-a.count("L")
desty = a.count("U")-a.count("D")
#print(destx, desty)
if (destx<0 or destx>m) or (desty<0 or desty>n):
result = "DANGER"
elif destx == x and desty == y:
result = "REACHED"
else:
result = "SOMEWHERE"
print(result)
except:
pass
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n20 20\n4 5\n13\nLLUUUUURRRRRR\n10 10\n3 4\n7\nUDUDDRR\n",
"output": "Case 1: REACHED\nCase 2: DANGER\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/ZUBREACH"
} | vfc_3698 | false | false | false | false | false |
apps | verifiable_code | 1215 | Solve the following coding problem using the programming language python:
Two friends David and Rojer were preparing for their weekly class-test.
The are preparing for the math test, but because of continuously adding big integers and solving equations they got exhausted. They decided to take break and play a game. They play a game which will help them in both(for having fun and will also help to prepare for math test).
There are N words and they have to find RESULT with the help of these words (just like they have N integers and they have to find integer RESULT=sum of N integers) . Since they are playing this game for the first time! They are not too good in this. Help them in finding weather their RESULT is correct or not.
NOTE:- Total number of unique characters in N words are not greater than 10. All input words and RESULT are in UPPER-CASE only!
Refer Here for how to add numbers : https://en.wikipedia.org/wiki/Verbal_arithmetic
-----Input:-----
- First line consist of an integer N (total number of words).
- Next N line contains words with which they have to find RESULT.
- Last line consist of the RESULT they found.
-----Output:-----
If RESULT is correct print true else print false.
-----Sample Input:-----
3
THIS
IS
TOO
FUNNY
-----Sample Output:-----
true
-----Constraints-----
- $2 \leq N \leq 10$
- $1 \leq Length of The word \leq 10$
- $1 \leq Length of The Result \leq 11$
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def isSolvable( W, R):
LW, LR, F, ML, AW, V, LMap = len(W), len(R), set([w[0] for w in W+[R]]), max(map(len, W+[R])), W+[R], set(), {}
if LR < ML: return False
def dfs(d,i,c):
if d == ML: return c == 0
if i == len(W) + 1:
s = sum(LMap[w[-d-1]] if d < len(w) else 0 for w in W) + c
return dfs(d+1,0,s//10) if s % 10 == LMap[R[-d-1]] else False
if i < LW and d >= len(W[i]): return dfs(d,i+1,c)
ch = AW[i][-d-1]
if ch in LMap: return dfs(d,i+1,c)
for x in range((ch in F), 10):
if x not in V:
LMap[ch], _ = x, V.add(x)
if dfs(d,i+1,c): return True
V.remove(LMap.pop(ch))
return dfs(0,0,0)
n=int(input())
W=[]
for i in range(n):
W.append(str(input()))
R=input()
a=(isSolvable(W,R))
if a==True:
print("true")
else:
print("false")
``` | {
"language": "python",
"test_cases": [
{
"input": "3\nTHIS\nIS\nTOO\nFUNNY\n",
"output": "true\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/CHLG2020/problems/MATH88"
} | vfc_3702 | false | false | false | false | false |
apps | verifiable_code | 1216 | Solve the following coding problem using the programming language python:
Today Chef wants to evaluate the dishes of his $N$ students. He asks each one to cook a dish and present it to him.
Chef loves his secret ingredient, and only likes dishes with at least $X$ grams of it.
Given $N$, $X$ and the amount of secret ingredient used by each student $A_i$, find out whether Chef will like at least one dish.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- The first line of each testcase contains two integers $N$
(number of students) and $X$ (minimum amount of secret ingredient that a dish must contain for Chef to like it).
- The next line contains $N$ space separated integers, $A_i$ denoting the amount of secret ingredient used by the students in their dishes.
-----Output:-----
For each testcase, print a single string "YES" if Chef likes at least one dish. Otherwise, print "NO". (Without quotes).
-----Constraints:-----
- $1 \leq T \leq 100$
- $1 \leq N \leq 1000$
- $1 \leq X \leq 1000000$
- $1 \leq A_i \leq 1000000$
-----Sample Input:-----
3
5 100
11 22 33 44 55
5 50
10 20 30 40 50
5 45
12 24 36 48 60
-----Sample Output:-----
NO
YES
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 i in range(t):
n,k=map(int,input().split())
m=list(map(int,input().split()))
a=0
for i in m:
if i>=k:
a=1
break
if a==1:
print('YES')
else:
print('NO')
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n5 100\n11 22 33 44 55\n5 50\n10 20 30 40 50\n5 45\n12 24 36 48 60\n",
"output": "NO\nYES\nYES\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/PCJ18A"
} | vfc_3706 | false | false | false | false | false |
apps | verifiable_code | 1217 | Solve the following coding problem using the programming language python:
Chef has an array A = (A1, A2, ..., AN), which has N integers in it initially. Chef found that for i ≥ 1, if Ai > 0, Ai+1 > 0, and Ai+2 exists, then he can decrease both Ai, and Ai+1 by one and increase Ai+2 by one. If Ai+2 doesn't exist, but Ai > 0, and Ai+1 > 0, then he can decrease both Ai, and Ai+1 (which will be the currently last two elements of the array) by one and add a new element at the end, whose value is 1.
Now Chef wants to know the number of different arrays that he can make from A using this operation as many times as he wishes. Help him find this, and because the answer could be very large, he is fine with you reporting the answer modulo 109+7.
Two arrays are same if they have the same number of elements and if each corresponding element is the same. For example arrays (2,1,1) and (1,1,2) are different.
-----Input-----
- The first line of the input contains a single integer T denoting the number of test cases.
- The first line contains a single integer N denoting the initial number of elements in A.
- The second line contains N space-separated integers: A1, A2, ... , AN.
-----Output-----
For each test case, output answer modulo 109+7 in a single line.
-----Constraints-----
- 1 ≤ T ≤ 5
- 1 ≤ N ≤ 50
- 0 ≤ Ai ≤ 50
-----Subtasks-----
- Subtask 1 (20 points) : 1 ≤ N ≤ 8, 0 ≤ Ai ≤ 4
- Subtask 2 (80 points) : Original constraints
-----Example-----
Input:
3
3
2 3 1
2
2 2
3
1 2 3
Output:
9
4
9
-----Explanation-----
Example case 1.
We'll list the various single steps that you can take (ie. in one single usage of the operation):
- (2, 3, 1) → (2, 2, 0, 1)
- (2, 2, 0, 1) → (1, 1, 1, 1)
- (1, 1, 1, 1) → (1, 1, 0, 0, 1)
- (1, 1, 0, 0, 1) → (0, 0, 1, 0, 1)
- (1, 1, 1, 1) → (1, 0, 0, 2)
- (1, 1, 1, 1) → (0, 0, 2, 1)
- (2, 3, 1) → (1, 2, 2)
- (1, 2, 2) → (0, 1, 3)
So all the arrays you can possibly get are:
(2, 3, 1), (2, 2, 0, 1), (1, 1, 1, 1), (1, 1, 0, 0, 1), (0, 0, 1, 0, 1), (1, 0, 0, 2), (0, 0, 2, 1), (1, 2, 2), and (0, 1, 3)
Since there are 9 different arrays that you can reach, the answer is 9.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def fun(a,cur,n,cnt):
if cur>=n-1:
return
for i in range(cur,n-1):
if i<n-2:
if a[i]>0 and a[i+1]>0:
a[i]-=1
a[i+1]-=1
a[i+2]+=1
cnt[0]=(cnt[0]+1)%1000000007
fun(a,i,n,cnt)
a[i]+=1
a[i+1]+=1
a[i+2]-=1
else:
if a[i]>0 and a[i+1]>0:
a[i]-=1
a[i+1]-=1
a.append(1)
cnt[0]=(cnt[0]+1)%1000000007
fun(a,i,n+1,cnt)
a[i]+=1
a[i+1]+=1
a.pop()
tc=int(input())
for case in range(tc):
n=int(input())
a=list(map(int,input().split()))
cnt=[1]
fun(a,0,n,cnt)
print(cnt[0]%1000000007)
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n3\n2 3 1\n2\n2 2\n3\n1 2 3\n\n\n",
"output": "9\n4\n9\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/AUG17/problems/CHEFFA"
} | vfc_3710 | false | false | false | false | true |
apps | verifiable_code | 1218 | Solve the following coding problem using the programming language python:
Richik$Richik$ has just completed his engineering and has got a job in one of the firms at Sabrina$Sabrina$ which is ranked among the top seven islands in the world in terms of the pay scale.
Since Richik$Richik$ has to travel a lot to reach the firm, the owner assigns him a number X$X$, and asks him to come to work only on the day which is a multiple of X$X$. Richik joins the firm on 1-st day but starts working from X-th day. Richik$Richik$ is paid exactly the same amount in Dollars as the day number. For example, if Richik$Richik$ has been assigned X=3$X = 3$, then he will be paid 3$3$ dollars and 6$6$ dollars on the 3rd$3rd$ and 6th$6th$ day on which he comes for work.
On N−th$N-th$ day, the owner calls up Richik$Richik$ and asks him not to come to his firm anymore. Hence Richik$Richik$ demands his salary of all his working days together. Since it will take a lot of time to add, Richik$Richik$ asks help from people around him, let's see if you can help him out.
-----Input:-----
- First line will contain T$T$, number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, two integers X,N$X, N$.
-----Output:-----
For each testcase, output in a single line which is the salary which Richik$Richik$ demands.
-----Constraints-----
- 1≤T≤1000$1 \leq T \leq 1000$
- 1≤X<=N≤107$1 \leq X<=N \leq 10^7$
-----Sample Input:-----
1
3 10
-----Sample Output:-----
18
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):
x,n=[int(g) for g in input().split()]
sal=0
day=x
while day<n:
sal=sal+day
day+=x
print(sal)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n3 10\n",
"output": "18\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/AARA2018/problems/ARMBH1"
} | vfc_3714 | false | false | false | false | false |
apps | verifiable_code | 1219 | Solve the following coding problem using the programming language python:
You have found $M$ different types of jewels in a mine and each type of jewel is present in an infinite number.
There are $N$ different boxes located at position $(1 ,2 ,3 ,...N)$.
Each box can collect jewels up to a certain number ( box at position $i$ have $i$ different partitions and each partition can collect at most one jewel of any type).
Boxes at odd positions are already fully filled with jewels while boxes at even positions are completely empty.
Print the total number of different arrangements possible so that all boxes can be fully filled.
As the answer can be very large you can print it by doing modulo with 1000000007(10^9+7).
-----Input:-----
- First line will contain $T$, number of testcases.
- Each testcase contains of a single line of input, two integers $N , M$.
-----Output:-----
For each testcase, Print the total number of different arrangement.
-----Constraints-----
- $1 \leq T \leq 20000$
- $1 \leq N \leq 1e9$
- $1 \leq M \leq 1e14$
-----Sample Input:-----
2
1 10
5 2
-----Sample Output:-----
1
64
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:
M = 1000000007
n, m = list(map(int, input().split()))
ans = 1
tt = n//2
tt = tt * (tt + 1)
ans = pow(m, tt, M)
print(ans)
t -= 1
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n1 10\n5 2\n",
"output": "1\n64\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/INRO2021/problems/JEWELMIN"
} | vfc_3718 | false | false | false | false | false |
apps | verifiable_code | 1220 | Solve the following coding problem using the programming language python:
Lets wish Horsbug98 on his birthday and jump right into the question.
In Chefland, $6$ new mobile brands have appeared each providing a range of smartphones. For simplicity let the brands be represented by numbers $1$ to $6$. All phones are sold at the superstore.
There are total $N$ smartphones. Let $P_i$ & $B_i$ be the price and the brand of the $i^{th}$ smartphone. The superstore knows all the price and brand details beforehand.
Each customer has a preference for brands. The preference is a subset of the brands available (i.e $1$ to $6$). Also, the customer will buy the $K^{th}$ costliest phone among all the phones of his preference.
You will be asked $Q$ queries. Each query consists of the preference of the customer and $K$.
Find the price the customer has to pay for his preference. If no such phone is available, print $-1$
Note that for each query the total number of smartphones is always $N$ since, after each purchase, the phones are replaced instantly.
-----Input:-----
- First Line contains $N$ and $Q$
- Second-line contains $N$ integers $P_1,P_2,...,P_N$ (Price)
- Third line contains $N$ integers $B_1,B_2,...,B_N$ (Brand)
- Each of the next Q lines contains a query, the query is describes below
- First line of each quey contains $b$ and $K$ where $b$ is the size of the preference subset.
- Second line of each query contains $b$ integers, describing the preference subset.
-----Output:-----
For each query, print the price to be paid.
-----Constraints-----
- $1 \leq N, Q, P_i \leq 10^5$
- $1 \leq B_i, b \leq 6$
- $1 \leq K \leq N$
-----Sample Input:-----
4 2
4 5 6 7
1 2 3 4
3 3
1 2 3
3 4
4 5 6
-----Sample Output:-----
4
-1
-----Explaination-----
Query 1: The preference subset is {1, 2, 3}, The prices of phones available of these brands are {4, 5, 6}. The third costliest phone is 4.
Query 2: The preference subset is {4, 5, 6}, The prices of phones available of these brands are {7}.
Fouth costliest phone is required, which is not available. Hence, 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
import sys
from collections import defaultdict
from copy import copy
R = lambda t = int: t(eval(input()))
RL = lambda t = int: [t(x) for x in input().split()]
RLL = lambda n, t = int: [RL(t) for _ in range(n)]
def solve():
N, Q = RL()
P = RL()
B = RL()
phones = sorted(zip(P, B))
S = defaultdict(lambda : [])
for p, b in phones:
for i in range(2**7):
if (i>>b) & 1:
S[i] += [p]
B = set(B)
I = [0] * len(B)
for _ in range(Q):
b, K = RL()
s = RL()
x = 0
for b in s:
x += 1<<b
if len(S[x]) < K:
print(-1)
else:
print(S[x][-K])
T = 1#R()
for t in range(1, T + 1):
solve()
``` | {
"language": "python",
"test_cases": [
{
"input": "4 2\n4 5 6 7\n1 2 3 4\n3 3\n1 2 3\n3 4\n4 5 6\n",
"output": "4\n-1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/ENJU2020/problems/ECJN209"
} | vfc_3722 | false | false | false | false | false |
apps | verifiable_code | 1221 | Solve the following coding problem using the programming language python:
Chef played an interesting game yesterday. This game is played with two variables $X$ and $Y$; initially, $X = Y = 0$. Chef may make an arbitrary number of moves (including zero). In each move, he must perform the following process:
- Choose any positive integer $P$ such that $P \cdot P > Y$.
- Change $X$ to $P$.
- Add $P \cdot P$ to $Y$.
Unfortunately, Chef has a bad memory and he has forgotten the moves he made. He only remembers the value of $X$ after the game finished; let's denote it by $X_f$. Can you tell him the maximum possible number of moves he could have made in the game?
-----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 a single integer $X_f$.
-----Output-----
For each test case, print a single line containing one integer — the maximum number of moves Chef could have made.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le X_f \le 10^9$
-----Example Input-----
3
3
8
9
-----Example Output-----
3
5
6
-----Explanation-----
Example case 2: One possible sequence of values of $X$ is $0 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 5 \rightarrow 8$.
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
T = int(input())
ans = []
for _ in range(T):
X = int(input())
count = 0
x = 0
y = 0
while(x<=X):
p = int(sqrt(y))
count += 1
if(p*p>y):
x = p
y += p**2
else:
x = p+1
y += (p+1)**2
if(x<=X):
ans.append(count)
else:
ans.append(count-1)
for i in ans:
print(i)
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n3\n8\n9\n",
"output": "3\n5\n6\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/TWOVRIBL"
} | vfc_3726 | false | false | false | false | false |
apps | verifiable_code | 1222 | Solve the following coding problem using the programming language python:
The EEE classes are so boring that the students play games rather than paying attention during the lectures. Harsha and Dubey are playing one such game.
The game involves counting the number of anagramic pairs of a given string (you can read about anagrams from here). Right now Harsha is winning. Write a program to help Dubey count this number quickly and win the game!
-----Input-----
The first line has an integer T which is the number of strings. Next T lines each contain a strings. Each string consists of lowercase english alphabets only.
-----Output-----
For each string, print the answer in a newline.
-----Constraints-----
- 1 ≤ T ≤ 1000
- 1 ≤ length of each string ≤ 100
-----Example-----
Input:
3
rama
abba
abcd
Output:
2
4
0
-----Explanation-----
rama has the following substrings:
- r
- ra
- ram
- rama
- a
- am
- ama
- m
- ma
- a
Out of these, {5,10} and {6,9} are anagramic pairs.
Hence the answer is 2.
Similarly for other strings as well.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def sort_str(s):
o = []
for c in s:
o.append(c)
o.sort()
return "".join(o)
def find_ana(s):
if len(s) <= 1:
return 0
h = {}
c = 0
for i in range(len(s)):
for j in range(i+1, len(s)+1):
t = sort_str(s[i:j])
if t in h:
c += h[t]
h[t] += 1
else:
h[t] = 1
return c
t = int(input())
for _ in range(t):
print(find_ana(input()))
``` | {
"language": "python",
"test_cases": [
{
"input": "3\nrama\nabba\nabcd\n",
"output": "2\n4\n0\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/COCU2016/problems/CURR2"
} | vfc_3730 | false | false | false | false | false |
apps | verifiable_code | 1223 | Solve the following coding problem using the programming language python:
It's the annual military parade, and all the soldier snakes have arrived at the parade arena, But they aren't standing properly. The entire parade must be visible from the main podium, and all the snakes must be in a line. But the soldiers are lazy, and hence you must tell the soldiers to move to their new positions in such a manner that the total movement is minimized.
Formally, the entire parade strip can be thought of as the integer line. There are N snakes, where each snake is a line segment of length L. The i-th snake is initially at the segment [Si, Si + L]. The initial positions of the snakes can overlap. The only segment of the strip visible from the podium is [A, B], and hence all the snakes should be moved so that all of them are visible from the podium. They should also all be in a line without gaps and every consecutive pair touching each other. In other words, they should occupy the segments [X, X + L], [X + L, X + 2*L], ... , [X + (N-1)*L, X + N*L], for some X, such that A ≤ X ≤ X + N*L ≤ B. You are guaranteed that the visible strip is long enough to fit all the snakes.
If a snake was initially at the position [X1, X1 + L] and finally is at the position [X2, X2 + L], then the snake is said to have moved a distance of |X2 - X1|. The total distance moved by the snakes is just the summation of this value over all the snakes. You need to move the snakes in such a manner that it satisfies all the conditions mentioned above, as well as minimize the total distance. You should output the minimum total distance achievable.
-----Input-----
- The first line contains a single integer, T, the number of testcases. The description of each testcase follows.
- The first line of each testcase contains four integers, N, L, A and B, where N denotes the number of snakes, L denotes the length of each snake, and [A, B] is the segment visible from the podium.
- The next line contains N integers, the i-th of which is Si. This denotes that the i-th snake is initially in the segment [Si, Si + L].
-----Output-----
- For each testcase, output a single integer in a new line: the minimum total distance achievable.
-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 105
- 1 ≤ Si ≤ 109
- 1 ≤ L ≤ 109
- 1 ≤ A ≤ B ≤ 109
- N * L ≤ B - A
-----Example-----
Input:
2
3 4 11 23
10 11 30
3 4 11 40
10 11 30
Output:
16
16
-----Explanation-----
In the first testcase, the three snakes are initially at segments [10, 14], [11, 15], and [30, 34]. One optimal solution is to move the first snake which was at [10, 14] to [15, 19] and the third snake which was at [30, 34] to [19, 23]. After this, the snakes would form a valid parade because they will be from [11, 15], [15, 19] and [19, 23]. Hence they are all in a line without any gaps in between them, and they are all visible, because they all lie in the visible segment, which is [11, 23].
The distance traveled by the first snake is |15 - 10| = 5, by the second snake is |11 - 11| = 0 and by the third snake is |19 - 30| = 11. Hence the total distance traveled is 5 + 0 + 11 = 16. This is the best that you can do, and hence the answer is 16.
In the second testcase, only the visible segment has increased. But you can check that the same final configuration as in the first subtask is still optimal here. Hence the answer is 16.
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 vsense(val,a,l):
sense=0
ctr=a
for c in range(n):
if val[c]<=ctr:
sense+=-1
else:
sense+=1
ctr+=l
return sense
while t:
n,l,a,b=list(map(int,input().split()))
val=list(map(int,input().split()))
val.sort()
sense=0
if b==a+n*l or vsense(val,a,l)<=0:
loc=a
else:
st=a
end=b-n*l
while st<=end:
m=(st+end)/2
chk=vsense(val,m,l)
if chk==0:
loc=m
break
elif chk<0:
end=m-1
else:
loc=m
st=m+1
ans=0
st=loc
for c in range(n):
ans+=abs(st-val[c])
st+=l
print(ans)
t-=1
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n3 4 11 23\n10 11 30\n3 4 11 40\n10 11 30\n",
"output": "16\n16\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CONSESNK"
} | vfc_3734 | false | false | false | false | false |
apps | verifiable_code | 1224 | Solve the following coding problem using the programming language python:
Eugene loves sequences, especially arithmetic progressions. One day he was asked to solve a difficult problem.
If a sequence of numbers A1, A2, ... , AN form an arithmetic progression A, he was asked to calculate sum of F(Ai), for L ≤ i ≤ R.
F(X) is defined as:
If X < 10 then F(X) = X.
Else F(X) = F(sum_of_digits(X)).
Example:
F(1378) =
F(1+3+7+8) =
F(19) =
F(1 + 9) =
F(10) =
F(1+0) =
F(1) = 1
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- Each test case is described in one line containing four integers: A1 denoting the first element of the arithmetic progression A, D denoting the common difference between successive members of A, and L and R as described in the problem statement.
-----Output-----
- For each test case, output a single line containing one integer denoting sum of F(Ai).
-----Constraints-----
- 1 ≤ T ≤ 105
- 1 ≤ A1 ≤ 109
- 0 ≤ D ≤ 109
- 1 ≤ R ≤ 1018
- 1 ≤ L ≤ R
-----Subtasks-----
- Subtask 1: 0 ≤ D ≤ 100, 1 ≤ A1 ≤ 109, 1 ≤ R ≤ 100 - 15 points
- Subtask 2: 0 ≤ D ≤ 109, 1 ≤ A1 ≤ 109, 1 ≤ R ≤ 106 - 25 points
- Subtask 3: Original constraints - 60 points
-----Example-----
Input:
2
1 1 1 3
14 7 2 4
Output:
6
12
-----Explanation-----
Example case 1.
A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...}
A1 = 1
A2 = 2
A3 = 3
F(A1) = 1
F(A2) = 2
F(A3) = 3
1+2+3=6
Example case 2.
A = {14, 21, 28, 35, 42, 49, 56, 63, 70, 77, ...}
A2 = 21
A3 = 28
A4 = 35
F(A2) = 3
F(A3) = 1
F(A4) = 8
3+1+8=12
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import fractions
import sys
f = sys.stdin
if len(sys.argv) > 1:
f = open(sys.argv[1], "rt")
sum_cache = {}
def sum_func(x):
if x < 10:
return x
r = sum_cache.get(x)
if r is not None:
return r
xx = 0
while x > 0:
xx += x % 10
x /= 10
r = sum_func(xx)
sum_cache[x] = r
return r
def test():
for n in range(1):
print(n, sum_func(n))
print(sum_func(int(10**18 - 1)))
#~ test()
#~ sys.exit(1)
cycle_table = [
# Cycle len, markers # D_kfunc
[9, [1, 1, 1, 1, 1, 1, 1, 1, 1]], # 1
[9, [1, 1, 1, 1, 1, 1, 1, 1, 1]], # 2
[3, [1, 0, 0, 1, 0, 0, 1, 0, 0]], # 3
[9, [1, 1, 1, 1, 1, 1, 1, 1, 1]], # 4
[9, [1, 1, 1, 1, 1, 1, 1, 1, 1]], # 5
[3, [1, 0, 0, 1, 0, 0, 1, 0, 0]], # 6
[9, [1, 1, 1, 1, 1, 1, 1, 1, 1]], # 7
[9, [1, 1, 1, 1, 1, 1, 1, 1, 1]], # 8
[1, [1, 0, 0, 0, 0, 0, 0, 0, 0]], # 9
]
NUMBER = 9
def calc(A_1, D, L, R):
#~ print('calc ===', A_1, D, L, R)
A_L = A_1 + D * (L - 1)
A_L_kfunc = sum_func(A_L)
D_kfunc = sum_func(D)
#~ print(A_L, A_L_kfunc, D_kfunc)
n = R - L + 1
if D == 0:
return n * A_L_kfunc
cycle_len = cycle_table[D_kfunc - 1][0]
cycle_markers = list(cycle_table[D_kfunc - 1][1]) # copy
#~ print('cycle_len', cycle_len)
whole_part = n // cycle_len
remainder = n % cycle_len
#~ print('whole_part, remainder = ', whole_part, remainder)
counts = [whole_part * x for x in cycle_markers]
#~ print(counts)
pos = 0
for i in range(remainder):
counts[pos] += 1
pos = (pos + D_kfunc) % NUMBER
#~ print(counts)
r = 0
for i, x in enumerate(counts):
value = (A_L_kfunc - 1 + i) % NUMBER + 1
r += value * x
return r
def calc_dumb(A_1, D, L, R):
#~ print('dumb ===', A_1, D, L, R)
a = A_1 + D * (L - 1)
n = R - L + 1
r = 0
for i in range(n):
value = sum_func(a)
#~ print(a, value)
r += value
a += D
return r
def test1():
a1 = 1
L = 1
R = 1000
for d in range(100):
r1 = calc_dumb(a1, d, L, R)
r2 = calc(a1, d, L, R)
if r1 != r2:
print(a1, d, L, R, ":", r1, r2)
def test2():
a1 = 1
d = 9
L = 1
R = 9
r1 = calc_dumb(a1, d, L, R)
r2 = calc(a1, d, L, R)
print(r1, r2)
#~ test1()
#~ sys.exit(1)
T = int(f.readline().strip())
for case_id in range(1, T+1):
A_1, D, L, R = list(map(int, f.readline().strip().split()))
r = calc(A_1, D, L, R)
print(r)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n1 1 1 3\n14 7 2 4\n\n\n",
"output": "6\n12\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/NOV15/problems/KFUNC"
} | vfc_3738 | false | false | false | false | false |
apps | verifiable_code | 1225 | Solve the following coding problem using the programming language python:
Tomya is a girl. She loves Chef Ciel very much.
Today, too, Tomya is going to Ciel's restaurant.
Of course, Tomya would like to go to Ciel's restaurant as soon as possible.
Therefore Tomya uses one of the shortest paths from Tomya's house to Ciel's restaurant.
On the other hand, Tomya is boring now to use the same path many times.
So Tomya wants to know the number of shortest paths from Tomya's house to Ciel's restaurant.
Your task is to calculate the number under the following assumptions.
This town has N intersections and M two way roads.
The i-th road connects from the Ai-th intersection to the Bi-th intersection, and its length is
Ci.
Tomya's house is in the 1st intersection, and Ciel's restaurant is in the N-th intersection.
-----Input-----
The first line contains an integer T, the number of test cases.
Then T test cases follow.
The first line of each test case contains 2 integers N, M.
Then next M lines contains 3 integers denoting Ai, Bi and Ci.
-----Output-----
For each test case, print the number of shortest paths from Tomya's house to Ciel's restaurant.
-----Constraints-----
1 ≤ T ≤ 10
2 ≤ N ≤ 10
1 ≤ M ≤ N ∙ (N – 1) / 2
1 ≤ Ai, Bi ≤ N
1 ≤ Ci ≤ 10
Ai ≠ Bi
If i ≠ j and Ai = Aj, then Bi ≠ Bj
There is at least one path from Tomya's house to Ciel's restaurant.
-----Sample Input-----
2
3 3
1 2 3
2 3 6
1 3 7
3 3
1 2 3
2 3 6
1 3 9
-----Sample Output-----
1
2
-----Explanations-----
In the first sample, only one shortest path exists, which is 1-3.
In the second sample, both paths 1-2-3 and 1-3 are the shortest paths.
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())
def func(k,n,x,dist,graph):
if k==n:
x+=[dist[n]]
return
for i in range(1,n+1):
if graph[k][i]!=0 and dist[i]==-1:
dist[i]=dist[k]+graph[k][i]
func(i,n,x,dist,graph)
dist[i]=-1
while t:
graph=[[0 for i in range(11)]for j in range(11)]
v,e=list(map(int,input().split()))
for i in range(e):
x,y,w=list(map(int,input().split()))
graph[x][y]=w
graph[y][x]=w
x=[]
dist=[-1]*(v+1)
dist[1]=0
func(1,v,x,dist,graph)
x.sort()
val=x[0]
ans=0
for i in range(len(x)):
if val==x[i]:
ans+=1
print(ans)
t-=1
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n3 3\n1 2 3\n2 3 6\n1 3 7\n3 3\n1 2 3\n2 3 6\n1 3 9\n",
"output": "1\n2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CIELTOMY"
} | vfc_3742 | false | false | false | false | false |
apps | verifiable_code | 1226 | Solve the following coding problem using the programming language python:
It is well-known that the elephants are afraid of mouses. The Little Elephant from the Zoo of Lviv is not an exception.
The Little Elephant is on a board A of n rows and m columns (0-based numeration). At the beginning he is in cell with coordinates (0; 0) and he wants to go to cell with coordinates (n-1; m-1). From cell (x; y) Little Elephant can go either to (x+1; y) or (x; y+1).
Each cell of the board contains either 1 or 0. If A[i][j] = 1, then there is a single mouse in cell (i; j). Mouse at cell (i; j) scared Little Elephants if and only if during the path there was at least one such cell (x; y) (which belongs to that path) and |i-x| + |j-y| <= 1.
Little Elephant wants to find some correct path from (0; 0) to (n-1; m-1) such that the number of mouses that have scared the Little Elephant is minimal possible. Print that number.
-----Input-----
First line contains single integer T - the number of test cases. Then T test cases follow. First line of each test case contain pair of integers n and m - the size of the board. Next n lines contain n strings, each of size m and consisted of digits 0 and 1.
-----Output-----
In T lines print T integer - the answers for the corresponding test.
-----Constraints-----
1 <= T <= 50
2 <= n, m <= 100
-----Example-----
Input:
2
3 9
001000001
111111010
100100100
7 9
010101110
110110111
010011111
100100000
000010100
011011000
000100101
Output:
9
10
-----Explanation-----
Example case 1:
The optimized path is: (0, 0) -> (0, 1) -> (0, 2) -> (0, 3) -> (0, 4) -> (0, 5) -> (0, 6) -> (0, 7) -> (0, 8) -> (1, 8) -> (2, 8). The mouses that scared the Little Elephant are at the following cells: (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 7), (0, 2), (0, 8).
Example case 2:
The optimized path is: (0, 0) -> (1, 0) -> (1, 1) -> (2, 1) -> (2, 2) -> (3, 2) -> (3, 3) -> (4, 3) -> (4, 4) -> (5, 4) -> (5, 5) -> (6, 5) -> (6, 6) -> (6, 7) -> (6, 8). The 10 mouses that scared the Little Elephant are at the following cells: (0, 1), (1, 0), (1, 1), (2, 1), (3, 3), (4, 4), (5, 4), (5, 5), (6, 6), (6, 8).
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
from itertools import product
def solve(mouse,n,m):
# shadow matrix will contains the count of mice which affect (i,j) position
# if there is a mice at position (i,j) then in shadow matrix it will affect all four adjacent blocks
shadow=[[0 for i in range(m)]for j in range(n)]
for i,j in product(list(range(n)),list(range(m))):
if mouse[i][j]==1:
if i>0:
shadow[i-1][j]+=1
if j>0:
shadow[i][j-1]+=1
if i<n-1:
shadow[i+1][j]+=1
if j<m-1:
shadow[i][j+1]+=1
# dp is a dictionary which contains a tuple of 3 values (i,j,0)=>we are coming at destination (i,j) from left side
# (i,j,1)=> we are coming at destination (i,j) from top
dp=defaultdict(int)
#
dp[(0,0,0)]=dp[(0,0,1)]=shadow[0][0]-mouse[0][0]
# fill only first row
# in first row we can only reach at (0,j) from (0,j-1,0) as we can't come from top.
# so here we will assign count of mices which will affect current cell (shadow[0][i]) + previous result i.e,(0,j-1,0) and
# if mouse is in the current cell than we have to subtract it bcoz we have add it twice i.e, when we enter at this block
# and when we leave this block
for i in range(1,m):
dp[(0,i,0)]=dp[(0,i,1)]=shadow[0][i]-mouse[0][i]+dp[(0,i-1,0)]
# same goes for first column
# we can only come at (i,0) from (i-1,0) i.e top
for i in range(1,n):
dp[(i,0,0)]=dp[(i,0,1)]=shadow[i][0]-mouse[i][0]+dp[(i-1,0,1)]
# for rest of the blocks
# for a block (i,j) we have to add shadow[i][j] and subtract mouse[i][j] from it for double counting
# now for each block we have two choices, either take its previous block with same direction or take previous block with different
# direction and subtract corner double counted mouse. We have to take min of both to find optimal answer
for i,j in product(list(range(1,n)),list(range(1,m))):
a=shadow[i][j]-mouse[i][j]
b=a
a+=min(dp[(i,j-1,0)],dp[(i,j-1,1)]-mouse[i-1][j])
b+=min(dp[(i-1,j,1)],dp[(i-1,j,0)]-mouse[i][j-1])
dp[(i,j,0)]=a
dp[(i,j,1)]=b
# what if [0][0] and [n-1][m-1] have mice, so we have to add them as we haven't counted them yet.
return min(dp[(n-1,m-1,0)],dp[(n-1,m-1,1)])+mouse[0][0]+mouse[n-1][m-1]
for _ in range(int(input())):
n,m=list(map(int,input().split( )))
mouse=[]
for i in range(n):
x=input()
mouse.append(list(map(int,x)))
print(solve(mouse,n,m))
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n3 9\n001000001\n111111010\n100100100\n7 9\n010101110\n110110111\n010011111\n100100000\n000010100\n011011000\n000100101\n",
"output": "9\n10\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/LEMOUSE"
} | vfc_3746 | false | false | false | false | false |
apps | verifiable_code | 1227 | Solve the following coding problem using the programming language python:
One day, Chef found a cube which has each of its sides painted in some color out of black, blue, red, green, yellow and orange.
Now he asks you to check if he can choose three sides such that they are pairwise adjacent and painted in the same color.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- A single line of each test case contains six words denoting the colors of painted sides in the order: front, back, left, right, top and bottom, respectively.
-----Output-----
For each test case, output a single line containing the word "YES" or "NO" (without quotes) corresponding to the answer of the problem.
-----Constraints-----
- 1 ≤ T ≤ 50000
- Each color will be from the list {"black", "blue", "red", "green", "yellow", "orange"}
-----Subtasks-----
Subtask 1: (25 points)
- 1 ≤ T ≤ 12000
- For each test case there will be at most three different colors
Subtask 2: (75 points)
- Original constraints
-----Example-----
Input:
2
blue yellow green orange black green
green yellow green orange black green
Output:
NO
YES
-----Explanation-----
Example case 1.
There are no three sides with the same color.
Example case 2.
In this test case, the front, bottom and left sides are green (see picture).
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())):
l=list(map(str,input().split()))
a=[(1,3,5),(1,3,6),(1,4,5),(1,4,6),(2,3,5),(2,3,6),(2,4,5),(2,4,6)]
c=0
for i in a:
if len(set([l[i[0]-1],l[i[1]-1],l[i[2]-1]]))==1:
c=1
break
if c==1:
print("YES")
else:
print("NO")
``` | {
"language": "python",
"test_cases": [
{
"input": "2\nblue yellow green orange black green\ngreen yellow green orange black green\n",
"output": "NO\nYES\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CHCUBE"
} | vfc_3750 | false | false | false | false | false |
apps | verifiable_code | 1228 | Solve the following coding problem using the programming language python:
Chef has $N$ axis-parallel rectangles in a 2D Cartesian coordinate system. These rectangles may intersect, but it is guaranteed that all their $4N$ vertices are pairwise distinct.
Unfortunately, Chef lost one vertex, and up until now, none of his fixes have worked (although putting an image of a point on a milk carton might not have been the greatest idea after all…). Therefore, he gave you the task of finding it! You are given the remaining $4N-1$ points and you should find the missing one.
-----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$.
- Then, $4N-1$ lines follow. Each of these lines contains two space-separated integers $x$ and $y$ denoting a vertex $(x, y)$ of some rectangle.
-----Output-----
For each test case, print a single line containing two space-separated integers $X$ and $Y$ ― the coordinates of the missing point. It can be proved that the missing point can be determined uniquely.
-----Constraints-----
- $T \le 100$
- $1 \le N \le 2 \cdot 10^5$
- $|x|, |y| \le 10^9$
- the sum of $N$ over all test cases does not exceed $2 \cdot 10^5$
-----Subtasks-----
Subtask #1 (20 points):
- $T = 5$
- $N \le 20$
Subtask #2 (30 points): $|x|, |y| \le 10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
1
2
1 1
1 2
4 6
2 1
9 6
9 3
4 3
-----Example Output-----
2 2
-----Explanation-----
The original set of points are:
Upon adding the missing point $(2, 2)$, $N = 2$ rectangles can be formed:
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=[]
b=[]
for i in range(4*n-1):
c,d=list(map(int,input().split()))
a.append(c)
b.append(d)
c1=0
c2=0
for i in a:
c1^=i
for i in b:
c2^=i
print(c1,c2)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n2\n1 1\n1 2\n4 6\n2 1\n9 6\n9 3\n4 3\n",
"output": "2 2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/PTMSSNG"
} | vfc_3754 | false | false | false | false | false |
apps | verifiable_code | 1229 | Solve the following coding problem using the programming language python:
Motu and Tomu are very good friends who are always looking for new games to play against each other and ways to win these games. One day, they decided to play a new type of game with the following rules:
- The game is played on a sequence $A_0, A_1, \dots, A_{N-1}$.
- The players alternate turns; Motu plays first, since he's earlier in lexicographical order.
- Each player has a score. The initial scores of both players are $0$.
- On his turn, the current player has to pick the element of $A$ with the lowest index, add its value to his score and delete that element from the sequence $A$.
- At the end of the game (when $A$ is empty), Tomu wins if he has strictly greater score than Motu. Otherwise, Motu wins the game.
In other words, Motu starts by selecting $A_0$, adding it to his score and then deleting it; then, Tomu selects $A_1$, adds its value to his score and deletes it, and so on.
Motu and Tomu already chose a sequence $A$ for this game. However, since Tomu plays second, he is given a different advantage: before the game, he is allowed to perform at most $K$ swaps in $A$; afterwards, the two friends are going to play the game on this modified sequence.
Now, Tomu wants you to determine if it is possible to perform up to $K$ swaps in such a way that he can win this game.
-----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 number of elements in the sequence and the maximum number of swaps Tomu can perform.
- The second line contains $N$ space-separated integers $A_0, A_1, \dots, A_{N-1}$.
-----Output-----
For each test case, print a single line containing the string "YES" if Tomu can win the game or "NO" otherwise (without quotes).
-----Constraints-----
- $1 \le T \le 100$
- $1 \le N \le 10,000$
- $0 \le K \le 10,000$
- $1 \le A_i \le 10,000$ for each valid $i$
-----Subtasks-----
Subtask #1 (20 points): $1 \le N \le 100$
Subtask #2 (80 points): original constraints
-----Example Input-----
2
6 0
1 1 1 1 1 1
5 1
2 4 6 3 4
-----Example Output-----
NO
YES
-----Explanation-----
Example case 1: At the end of the game, both Motu and Tomu will have scores $1+1+1 = 3$. Tomu is unable to win that game, so the output is "NO".
Example case 2: If no swaps were performed, Motu's score would be $2+6+4 = 12$ and Tomu's score would be $4+3 = 7$. However, Tomu can swap the elements $A_2 = 6$ and $A_3 = 3$, which makes Motu's score at the end of the game equal to $2+3+4 = 9$ and Tomu's score equal to $4+6 = 10$. Tomu managed to score higher than Motu, so the output is "YES".
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 = map(int, input().split())
arr= list(map(int, input().split()))
motu, tomu = [], []
for i in range(n):
if i%2 == 0:
motu.append(arr[i])
else:
tomu.append((arr[i]))
motu.sort(reverse=True)
tomu.sort()
for i in range(len(motu)):
if len(tomu)-1<i:
break
if k==0:
break
if tomu[i]<motu[i]:
tomu[i], motu[i] = motu[i], tomu[i]
k-=1
if sum(tomu) > sum(motu):
print("YES")
else:
print("NO")
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n6 0\n1 1 1 1 1 1\n5 1\n2 4 6 3 4\n",
"output": "NO\nYES\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/MTYFRI"
} | vfc_3758 | false | false | false | false | false |
apps | verifiable_code | 1230 | Solve the following coding problem using the programming language python:
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0<=A[i]<2^64), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 <= i1 < i2 < i3 < i4 <= n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
-----Input-----
First line contains one integer n (4<=n<=100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
-----Output-----
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
-----Example-----
Input:
5
1 0 2 3 7
Output:
Yes
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
dic = {}
#num = "1"
#def tonum(num):
# res=0
# for i in range(len(num)):
# res = 2*res + int(num[i])
# return res
#for i in range(64):
# number = tonum(num)
# dic[num] = []
# num = num+"0"
n = int(input())
flag=0
if n >= 68:
inp = input()
print("Yes")
else:
inp = [int(x) for x in input().split()]
for i in range(len(inp)-1):
for j in range(i+1,len(inp)):
xor = inp[i]^inp[j]
if xor in list(dic.keys()):
for pair in dic[xor]:
(x,y) = pair
if x != i and y!=j and x!=j and y!=i:
flag = 1
break
dic[xor].append((i,j))
else:
dic[xor] = []
dic[xor].append((i,j))
if flag is 1:
break
if flag is 1:
break
if flag is 1:
print("Yes")
else:
print("No")
``` | {
"language": "python",
"test_cases": [
{
"input": "5\n1 0 2 3 7\n",
"output": "Yes\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/JULY12/problems/GRAYSC"
} | vfc_3762 | false | false | false | false | false |
apps | verifiable_code | 1231 | Solve the following coding problem using the programming language python:
The chef won a duet singing award at Techsurge & Mridang 2012. From that time he is obsessed with the number 2.
He just started calculating the powers of two. And adding the digits of the results.
But he got puzzled after a few calculations. So gave you the job to generate the solutions to 2^n and find their sum of digits.
-----Input-----
N : number of inputs N<=100
then N lines with input T<=2000
-----Output-----
The output for the corresponding input T
-----Example-----
Input:
3
5
10
4
Output:
5
7
7
Explanation:
2^5=32
3+2=5
2^10=1024
1+0+2+4=7
2^4=16
1+6=7
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
from operator import add
from functools import reduce
choices=[]
for x in range(1800):
num_str = list(map (int, str (2**x)))
suma = reduce (add, num_str)
choices.append(suma)
N=int(input())
for x in range(N):
t=int(input())
print(choices[t])
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n5\n10\n4\n",
"output": "5\n7\n7\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/AGTK2012/problems/ALG2N"
} | vfc_3766 | false | false | false | false | false |
apps | verifiable_code | 1232 | Solve the following coding problem using the programming language python:
AND gates and OR gates are basic components used in building digital circuits. Both gates have two input lines and one output line. The output of an AND gate is 1 if both inputs are 1, otherwise the output is 0. The output of an OR gate is 1 if at least one input is 1, otherwise the output is 0.
You are given a digital circuit composed of only AND and OR gates where one node (gate or input) is specially designated as the output. Furthermore, for any gate G and any input node I, at most one of the inputs to G depends on the value of node I.
Now consider the following random experiment. Fix some probability p in [0,1] and set each input bit to 1 independently at random with probability p (and to 0 with probability 1-p). The output is then 1 with some probability that depends on p. You wonder what value of p causes the circuit to output a 1 with probability 1/2.
-----Input-----
The first line indicates the number of test cases to follow (about 100).
Each test case begins with a single line containing a single integer n with 1 ≤ n ≤ 100 indicating the number of nodes (inputs and gates) in the circuit. Following this, n lines follow where the i'th line describes the i'th node. If the node is an input, the line simply consists of the integer 0. Otherwise, if the node is an OR gate then the line begins with a 1 and if the node is an AND gate then the line begins with a 2. In either case, two more integers a,b follow, both less than i, which indicate that the outputs from both a and b are used as the two input to gate i.
As stated before, the circuit will be such that no gate has both of its inputs depending on the value of a common input node.
Test cases are separated by a blank line including a blank line preceding the first test case.
-----Output-----
For each test case you are to output a single line containing the value p for which the output of node n is 1 with probability exactly 1/2 if the inputs are independently and randomly set to value 1 with probability p. The value p should be printed with exactly 5 digits after the decimal.
-----Example-----
Input:
4
1
0
3
0
0
1 1 2
3
0
0
2 1 2
5
0
0
0
2 1 2
1 3 4
Output:
0.50000
0.29289
0.70711
0.40303
-----Temporary Stuff-----
A horizontal rule follows.
***
Here's a definition list (with `definitionLists` option):
apples
: Good for making applesauce.
oranges
: Citrus!
tomatoes
: There's no "e" in tomatoe.
#PRACTICE
- This must be done
[http:codechef.com/users/dpraveen](http:codechef.com/users/dpraveen)
(0.8944272−0.44721360.4472136−0.8944272)(10005)(0.89442720.4472136−0.4472136−0.8944272)(10005)
\left(\begin{array}{cc}
0.8944272 & 0.4472136\\
-0.4472136 & -0.8944272
\end{array}\right)
\left(\begin{array}{cc}
10 & 0\\
0 & 5
\end{array}\right)
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 node:
def __init__(self,a,b=0,c=0):
self.val=a
self.a=b
self.b=c
arr=[]
def finder(node,val):
if(arr[node].val==0):
return val
else:
a=finder(arr[node].a,val)
b=finder(arr[node].b,val)
if(arr[node].val==1):
return a+b-a*b
else:
return a*b
t=int(input())
while(t>0):
x=input()
n=int(input())
arr.append(node(0))
for i in range(0,n):
vals=input().split()
sz=len(vals)
for i in range(0,sz):
vals[i]=int(vals[i])
if(vals[0]==0):
next=node(0)
arr.append(next)
else:
next=node(vals[0],vals[1],vals[2])
arr.append(next)
lower=0.0
higher=1.0
eps=1e-9
while((higher-lower)>eps):
mid=(higher+lower)/2.0
if(finder(n,mid)>0.5):
higher=mid
else:
lower=mid
print("%.5f" %(higher))
arr=[]
# print(higher)
t-=1
``` | {
"language": "python",
"test_cases": [
{
"input": "4\n\n1\n0\n\n3\n0\n0\n1 1 2\n\n3\n0\n0\n2 1 2\n\n5\n0\n0\n0\n2 1 2\n1 3 4\n\n\n",
"output": "0.50000\n0.29289\n0.70711\n0.40303\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CIRCUITS"
} | vfc_3770 | false | false | false | false | false |
apps | verifiable_code | 1233 | Solve the following coding problem using the programming language python:
There are players standing in a row each player has a digit written on their T-Shirt (multiple players can have the same number written on their T-Shirt).
You have to select a group of players, note that players in this group should be standing in $\textbf{consecutive fashion}$. For example second player of chosen group next to first player of chosen group, third player next to second and similarly last player next to second last player of chosen group. Basically You've to choose a contiguous group of players.
After choosing a group, players can be paired if they have the same T-Shirt number (one player can be present in at most one pair), finally the chosen group is called “good” if at most one player is left unmatched. Your task is to find the size of the maximum “good” group.
Formally, you are given a string $S=s_{1}s_{2}s_{3}...s_{i}...s_{n}$ where $s_{i}$ can be any digit character between $'0'$ and $'9'$ and $s_{i}$ denotes the number written on the T-Shirt of $i^{th}$ player. Find a value $length$ such that there exist pair of indices $(i,j)$ which denotes $S[i...j]$ is a “good” group where $i\geq1$ and $j\leq S.length$ and $i\leq j$ and $(j-i+1)=length$ and there exist no other pair $(i’,j’)$ such that $(j’-i’+1)>length$ and $S[i'...j']$ is a "good" group.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- $i^{th}$ testcase consist of a single line of input, a string $S$.
-----Output:-----
For each testcase, output in a single line maximum possible size of a "good" group.
-----Constraints-----
$\textbf{Subtask 1} (20 points)$
- $1 \leq T \leq 10$
- $S.length \leq 10^{3}$
$\textbf{Subtask 2} (80 points)$
- $1 \leq T \leq 10$
- $S.length \leq 10^{5}$
-----Sample Input:-----
1
123343
-----Sample Output:-----
3
-----EXPLANATION:-----
1$\textbf{$\underline{2 3 3}$}$43
Underlined group is a “good” group because the second player(number 2 on T-Shirt) is the only player who is left unmatched and third and fourth player can form a pair, no other group has length greater than 3 that are “good”. However note that we have other “good” group also 12$\textbf{$\underline{334}$}$3 but length is 3 which is same as our answer.
-----Sample Input:-----
1
95665
-----Sample Output:-----
5
-----EXPLANATION:-----
$\textbf{$\underline{95665}$}$ is “good” group because first player is the only player who is left unmatched second and fifth player can form pair and third and fourth player also form pair.
-----Sample Input:-----
2
2323
1234567
-----Sample Output:-----
4
1
-----EXPLANATION:-----
For first test case
$\textbf{$\underline{2323}$}$ is a “good” group because there are no players who are left unmatched first and third player form pair and second and fourth player form pair.
For second test
Only length one "good" group is possible.
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 GRIG(L):
LENT = len(L)
MINT = 1
GOT = 0
DY = [ [{x: 0 for x in range(0, 10)}, 0, 0] ]
for i in L:
DY.append([{x: 0 for x in range(0, 10)}, 0, 0])
GOT += 1
for j in range(0, GOT):
if DY[j][0][i] == 1:
DY[j][0][i] = 0
DY[j][1] -= 1
else:
DY[j][0][i] = 1
DY[j][1] += 1
DY[j][2] += 1
if DY[j][1] <= 1 and DY[j][2] > MINT:
MINT = DY[j][2]
return MINT
TESTCASES = int(input().strip())
for i in range(0, TESTCASES):
L = [int(x) for x in list(input().strip())]
print(GRIG(L))
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n123343\n",
"output": "3\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/COVO2020/problems/GRIG"
} | vfc_3774 | false | false | false | false | false |
apps | verifiable_code | 1234 | Solve the following coding problem using the programming language python:
There is a new prodigy in town and he has challenged people to a game. They have to give him an integer N and he will immediately respond with a number which has more than N factors.
What the prodigy really does is to do some complex calculations in his head to find out the smallest integer with more than N factors. However, he has a weakness - he knows to do those calculation only on numbers up to 1019, so if the answer to be reported is more than 1019, he will not be able to come up with his solution and will lose the game.
Given the integer the people give to the prodigy, you need to report whether he wins the game or not. If he wins, also output his answer.
-----Input-----
The first line of input contains T (T ≤ 25), the number of test cases. Following this are T lines, each containing an integer N (1 ≤ N ≤ 109) - the number which is given to the prodigy.
-----Output-----
Output one line per test case. If the smallest integer X with more than N factors is bigger than 1019 so that the prodigy does not win, print "lose". Otherwise print "win X" (quotes for clarity).
-----Example-----
Input:
3
3
5
12345678
Output:
win 6
win 12
lose
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
divisors = [1 , 2 , 3 , 4 , 6 , 8 , 9 , 10 , 12 , 16 , 18 , 20 , 24 , 30 , 32 , 36 , 40 , 48 , 60 , 64 , 72 , 80 , 84 , 90 , 96 , 100 , 108 , 120 , 128 , 144 , 160 , 168 , 180 , 192 , 200 , 216 , 224 , 240 , 256 , 288 , 320 , 336 , 360 , 384 , 400 , 432 , 448 , 480 , 504 , 512 , 576 , 600 , 640 , 672 , 720 , 768 , 800 , 864 , 896 , 960 , 1008 , 1024 , 1152 , 1200 , 1280 , 1344 , 1440 , 1536 , 1600 , 1680 , 1728 , 1792 , 1920 , 2016 , 2048 , 2304 , 2400 , 2688 , 2880 , 3072 , 3360 , 3456 , 3584 , 3600 , 3840 , 4032 , 4096 , 4320 , 4608 , 4800 , 5040 , 5376 , 5760 , 6144 , 6720 , 6912 , 7168 , 7200 , 7680 , 8064 , 8192 , 8640 , 9216 , 10080 , 10368 , 10752 , 11520 , 12288 , 12960 , 13440 , 13824 , 14336 , 14400 , 15360 , 16128 , 16384 , 17280 , 18432 , 20160 , 20736 , 21504 , 23040 , 24576 , 25920 , 26880 , 27648 , 28672 , 28800 , 30720 , 32256 , 32768 , 34560 , 36864 , 40320 , 41472 , 43008 , 46080 , 48384 , 49152 , 51840 , 53760 , 55296 , 57600 , 61440 , 62208 , 64512 , 65536 , 69120 , 73728 , 80640 , 82944 , 86016 , 92160 , 96768 , 98304 , 103680 , 107520 , 110592 , 115200 , 122880 , 124416 , 129024 , 131072 , 138240 , 147456 , 153600 , 161280 , 165888 , 172032 , 184320 , 193536 , 196608 , 207360 , 215040 , 221184 , 230400 , 245760]
numbers = [1 , 2 , 4 , 6 , 12 , 24 , 36 , 48 , 60 , 120 , 180 , 240 , 360 , 720 , 840 , 1260 , 1680 , 2520 , 5040 , 7560 , 10080 , 15120 , 20160 , 25200 , 27720 , 45360 , 50400 , 55440 , 83160 , 110880 , 166320 , 221760 , 277200 , 332640 , 498960 , 554400 , 665280 , 720720 , 1081080 , 1441440 , 2162160 , 2882880 , 3603600 , 4324320 , 6486480 , 7207200 , 8648640 , 10810800 , 14414400 , 17297280 , 21621600 , 32432400 , 36756720 , 43243200 , 61261200 , 73513440 , 110270160 , 122522400 , 147026880 , 183783600 , 245044800 , 294053760 , 367567200 , 551350800 , 698377680 , 735134400 , 1102701600 , 1396755360 , 2095133040 , 2205403200 , 2327925600 , 2793510720 , 3491888400 , 4655851200 , 5587021440 , 6983776800 , 10475665200 , 13967553600 , 20951330400 , 27935107200 , 41902660800 , 48886437600 , 64250746560 , 73329656400 , 80313433200 , 97772875200 , 128501493120 , 146659312800 , 160626866400 , 240940299600 , 293318625600 , 321253732800 , 481880599200 , 642507465600 , 963761198400 , 1124388064800 , 1606268664000 , 1686582097200 , 1927522396800 , 2248776129600 , 3212537328000 , 3373164194400 , 4497552259200 , 6746328388800 , 8995104518400 , 9316358251200 , 13492656777600 , 18632716502400 , 26985313555200 , 27949074753600 , 32607253879200 , 46581791256000 , 48910880818800 , 55898149507200 , 65214507758400 , 93163582512000 , 97821761637600 , 130429015516800 , 195643523275200 , 260858031033600 , 288807105787200 , 391287046550400 , 577614211574400 , 782574093100800 , 866421317361600 , 1010824870255200 , 1444035528936000 , 1516237305382800 , 1732842634723200 , 2021649740510400 , 2888071057872000 , 3032474610765600 , 4043299481020800 , 6064949221531200 , 8086598962041600 , 10108248702552000 , 12129898443062400 , 18194847664593600 , 20216497405104000 , 24259796886124800 , 30324746107656000 , 36389695329187200 , 48519593772249600 , 60649492215312000 , 72779390658374400 , 74801040398884800 , 106858629141264000 , 112201560598327200 , 149602080797769600 , 224403121196654400 , 299204161595539200 , 374005201994424000 , 448806242393308800 , 673209363589963200 , 748010403988848000 , 897612484786617600 , 1122015605983272000 , 1346418727179926400 , 1795224969573235200 , 2244031211966544000 , 2692837454359852800 , 3066842656354276800 , 4381203794791824000 , 4488062423933088000 , 6133685312708553600 , 8976124847866176000 , 9200527969062830400]
t = int(input())
for x in range(0, t):
a = int(input())
i = 0
for y in divisors:
if y > a:
print("win", numbers[i])
break
i = i+1
else:
print("lose")
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n3\n5\n12345678\n",
"output": "win 6\nwin 12\nlose\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/IOPC2012/problems/IOPC1211"
} | vfc_3778 | false | false | false | false | false |
apps | verifiable_code | 1235 | Solve the following coding problem using the programming language python:
Given a number n. Find the last two digits of 5 ^ n ( 5 to the power of n ).
Remember that overflow can occur.
-----Input:-----
- N — the power in which you need to raise number 5.
-----Output:-----
Last two digits of 5^n.
-----Constraints-----
- $2 \leq N \leq 2.1018$
-----Sample Input:-----
2
-----Sample Output:-----
25
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
print(25)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n",
"output": "25\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/CMR12121/problems/DGLST"
} | vfc_3782 | false | false | false | false | false |
apps | verifiable_code | 1236 | Solve the following coding problem using the programming language python:
You are given a set of n pens, each of them can be red, blue, green, orange, and violet in color. Count a minimum number of pens that should be taken out from the set so that any two neighboring pens have different colors. Pens are considered to be neighboring if there are no other pens between them.
-----Input:-----
-
The first line contains t denoting the number of test cases.
-
The first line of each test case will contain a single integer n.
-
The second line of each test case will contain a string s.
(s contains only 'R', 'B', 'G', 'O', and 'V' characters denoting red, blue, green, orange, and violet respectively)
-----Output:-----
For each test case, print single line containing one integer - The minimum number of pens that need to be taken out.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq n \leq 1000$
-----Sample Input:-----
2
5
RBBRG
5
RBGOV
-----Sample Output:-----
1
0
-----EXPLANATION:-----
In first test case, two blue pens are neighboring each other, if we take out any one of them then the string will be RBRG in which each pen has different neighbors.
In second test case, no pen needs to be taken out as each pen has different neighbors.
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 = int(input())
a = input().strip()
prev = a[0]
ans = -1
for i in a:
if prev == i:
ans += 1
prev = i
print(ans)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n5\nRBBRG\n5\nRBGOV\n",
"output": "1\n0\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/CDGO2021/problems/DIFFPENS"
} | vfc_3786 | false | false | false | false | false |
apps | verifiable_code | 1237 | Solve the following coding problem using the programming language python:
You are standing near a very strange machine. If you put C cents in the machine, the remaining money in your purse will transform in an unusual way. If you have A dollars and B cents remaining in your purse after depositing the C cents, then after the transformation you will have B dollars and A cents. You can repeat this procedure as many times as you want unless you don't have enough money for the machine. If at any point C > B and A > 0, then the machine will allow you to break one of the A dollars into 100 cents so you can place C cents in the machine. The machine will not allow you to exchange a dollar for 100 cents if B >= C.
Of course, you want to do this to maximize your profit. For example if C=69 and you have 9 dollars and 77 cents then after you put 69 cents in the machine you will have 8 dollars and 9 cents (9.77 --> 9.08 --> 8.09). But I should warn you that you can't cheat. If you try to throw away 9 cents before the transformation (in order to obtain 99 dollars and 8 cents after), the machine will sense you are cheating and take away all of your money. You need to know how many times you should do this transformation in order to make a maximum profit. Since you are very busy man, you want to obtain the maximum possible profit in the minimum amount of time.
-----Input-----
The first line contains a single integer T <= 40, the number of test cases. T test cases follow. The only line of each test case contains three nonnegative integers A, B and C where A, B, C < 100. It means that you have A dollars and B cents in your purse and you need to put C cents in the machine to make the transformation.
-----Output-----
For each test case, output a single line containing the minimal number of times you should do this transformation in order to make a maximal profit. It is guaranteed that the answer is less than 10000.
-----Example-----
Input:
2
9 77 69
98 99 69
Output:
4
0
-----Explanation-----
In the first test we have the following sequence: 9.77, 8.09, 40.07, 38.39, 70.37, 68.69, 0.68. After last step we have not enough money for further transformations. The maximal profit will be after 4 transformations.
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())):
a,b,c=list(map(int, input().split()))
p=a*100+b
mx=p
ans, cnt = 0, 0
while True:
cnt+=1
if p<c or cnt==10000:
break
else:
p-=c
a=p//100
b=p%100
p=b*100+a
if p>mx:
mx=p
ans=cnt
print(ans)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n9 77 69\n98 99 69\n\n\n",
"output": "4\n0\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/MONTRANS"
} | vfc_3790 | false | false | false | false | false |
apps | verifiable_code | 1238 | Solve the following coding problem using the programming language python:
Chef likes to play with big numbers. Today, he has a big positive integer N. He can select any two digits from this number (the digits can be same but their positions should be different) and orders them in any one of the two possible ways. For each of these ways, he creates a two digit number from it (might contain leading zeros). Then, he will pick a character corresponding to the ASCII value equal to this number, i.e. the number 65 corresponds to 'A', 66 to 'B' and so on till 90 for 'Z'. Chef is only interested in finding which of the characters in the range 'A' to 'Z' can possibly be picked this way.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases.
The first line of the input contains an integer N.
-----Output-----
For each test case, output a string containing characters Chef can pick in sorted order If the resulting size of string is zero, you should output a new line.
-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 10100000
-----Subtasks-----
- Subtask #1 (40 points) N ≤ 1010
- Subtask #2 (60 points) Original Constraints
-----Example-----
Input:
4
65
566
11
1623455078
Output:
A
AB
ACDFGHIJKLNPQRSTUVW
-----Explanation-----
Example case 1. Chef can pick digits 6 and 5 and create integers 56 and 65. The integer 65 corresponds to 'A'.
Example case 2. Chef can pick digits 6 and 5 and create 'A' as it equals 65. He can pick 6 and 6 (they are picked from position 2 and position 3, respectively) to create 'B' too. Hence answer is "AB".
Example case 3. It's not possible to create any character from 'A' to 'Z'. Hence, we just print a new line.
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):
N=input()
X=[]
list2=[]
for x in N:
X.append(x)
list1=[]
list1=list(set(X))
output=''
for x in list1:
for y in X:
if int(x)>=6:
n=int(x)*10+int(y)
list2.append(n)
for j in list1:
if int(j)>=6:
m=int(j)*10+int(j)
list2.remove(m)
list2.sort()
if len(list2)==0:
print(" ")
else:
list2.sort()
for k in list2:
if chr(k) not in output and 64<k<91:
output+=chr(k)
print(output)
``` | {
"language": "python",
"test_cases": [
{
"input": "4\n65\n566\n11\n1623455078\n",
"output": "A\nAB\nACDFGHIJKLNPQRSTUVW\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CHEFPDIG"
} | vfc_3794 | false | false | false | false | false |
apps | verifiable_code | 1239 | 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:-----
2
2
4
-----Sample Output:-----
2
21
210
21
2
4
43
432
4321
43210
4321
432
43
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 _ in range(t):
n = int(input())
for i in range(n+1):
b = n
for space in range(n-i):
print(" ",end="")
for j in range(i+1):
print(b,end="")
b-=1
print()
for l in range(n):
a = n
for j1 in range(0,l+1):
print(" ",end="")
for k in range(n-l):
print(a,end="")
a-=1
print()
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n2\n4\n",
"output": "2\n21\n210\n21\n2\n4\n43\n432\n4321\n43210\n4321\n432\n43\n4\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/PBK32020/problems/ITGUY28"
} | vfc_3798 | false | false | false | false | false |
apps | verifiable_code | 1240 | Solve the following coding problem using the programming language python:
The chef is having one array of natural numbers. Cheffina challenges chef that find the sum of weights all the natural numbers present in the array, but the main problem is that all numbers have not original weights. After every 6 natural numbers weight is set to 1 as weight increases by 1 after that. (i.e. weight of 1 is 1, weight of 2 is 2 but the weight of 7 is 1 and weight of 8 is 2 and so on…). Help the chef to find the sum.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains two lines of input, one integer $N$.
- Next line has N space separate natural numbers.
-----Output:-----
For each testcase, output in a single line answer.
-----Constraints-----
- $1 \leq T \leq 10$
- $1 \leq N \leq 10^5$
- $1 \leq arr[i] \leq 10^6$
-----Sample Input:-----
1
6
6 7 9 11 4 16
-----Sample Output:-----
23
-----EXPLANATION:-----
Array after conversion = [6, 1, 3, 5, 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
t = int(input())
while t:
x = int(input())
arr = [int(i) for i in input().split()]
total = 0
for i in arr:
if i % 6 == 0:
total += 6
else:
total += (i % 6)
print(total)
t -= 1
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n6\n6 7 9 11 4 16\n",
"output": "23\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/PEND2020/problems/ITGUY00"
} | vfc_3802 | false | false | false | false | false |
apps | verifiable_code | 1241 | Solve the following coding problem using the programming language python:
"Don't Drink and Drive, but when you do, Better Call Saul."
Once Jesse and Walter were fighting over extra cash, and Saul decided to settle it with a game of stone piles whose winner gets the extra money. The game is described as follows :
There are N$N$ piles of stones with A$A$1$1$,$,$ A$A$2$2$,$,$ ...$...$ A$A$N$N$ stones in each pile.
Jesse and Walter move alternately, and in one move, they remove one pile entirely.
After a total of X$X$ moves, if the Sum of all the remaining piles is odd, Walter wins the game and gets the extra cash, else Jesse is the winner.
Walter moves first.
Determine the winner of the game if both of them play optimally.
-----Input:-----
- The first line will contain T$T$, number of testcases. T$T$ testcases follow :
- The first line of each testcase contains two space-separated integers N,X$N, X$.
- The second line of each testcase contains N$N$ space-separated integers A$A$1$1$,$,$ A$A$2$2$,$,$ ...,$...,$A$A$N$N$.
-----Output:-----
For each test case, print a single line containing the string "Jesse" (without quotes), if Jesse wins the game or "Walter" (without quotes) if Walter wins.
-----Constraints-----
- 1≤T≤104$1 \leq T \leq 10^4$
- 2≤N≤105$2 \leq N \leq 10^5$
- 1≤X≤N−1$1 \leq X \leq N-1$
- 1≤A$1 \leq A$i$i$ ≤100$ \leq 100$
- The sum of N$N$ over all test cases does not exceed 106$10^6$
-----Sample Input:-----
2
5 3
4 4 4 3 4
7 4
3 3 1 1 1 2 4
-----Sample Output:-----
Jesse
Walter
-----EXPLANATION:-----
-
For Test Case 1 : Playing optimally, Walter removes 4. Jesse removes 3 and then Walter removes 4. Jesse wins as 4+4=8$4 + 4 = 8$ is even.
-
For Test Case 2 : Playing optimally, Walter removes 4, Jesse removes 3, Walter removes 2 and Jesse removes 1. Walter wins as 3+3+1=7$3 + 3 + 1 = 7$ is odd.
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):
k,x=map(int,input().split())
l=list(map(int,input().split()))
f,e,o=0,0,0
for i in l:
if(i%2==0):
e+=1
else:
o+=1
if(o<=x//2):
f=1
elif(e<=x//2):
if((k-x)%2!=0):
f=0
else:
f=1
else:
if(x%2==0):
f=1
else:
f=0
if(f==1):
print('Jesse')
else:
print('Walter')
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n5 3\n4 4 4 3 4\n7 4\n3 3 1 1 1 2 4\n",
"output": "Jesse\nWalter\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/GMSTN"
} | vfc_3806 | false | false | false | false | false |
apps | verifiable_code | 1242 | Solve the following coding problem using the programming language python:
Chef loves to play with arrays by himself. Today, he has an array A consisting of N distinct integers. He wants to perform the following operation on his array A.
- Select a pair of adjacent integers and remove the larger one of these two. This decreases the array size by 1. Cost of this operation will be equal to the smaller of them.
Find out minimum sum of costs of operations needed to convert the array into a single element.
-----Input-----
First line of input contains a single integer T denoting the number of test cases. First line of each test case starts with an integer N denoting the size of the array A. Next line of input contains N space separated integers, where the ith integer denotes the value Ai.
-----Output-----
For each test case, print the minimum cost required for the transformation.
-----Constraints-----
- 1 ≤ T ≤ 10
- 2 ≤ N ≤ 50000
- 1 ≤ Ai ≤ 105
-----Subtasks-----
- Subtask 1 : 2 ≤ N ≤ 15 : 35 pts
- Subtask 2 : 2 ≤ N ≤ 100 : 25 pts
- Subtask 3 : 2 ≤ N ≤ 50000 : 40 pts
-----Example-----
Input
2
2
3 4
3
4 2 5
Output
3
4
-----Explanation-----Test 1 : Chef will make only 1 move: pick up both the elements (that is, 3 and 4), remove the larger one (4), incurring a cost equal to the smaller one (3).
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 *
for t in range(int(input())):
n = int(input())
numberlist = list(map(int,input().split()))
numberlist.sort()
print(numberlist[0]* ( len(numberlist) -1))
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n2\n3 4\n3\n4 2 5\n",
"output": "3\n4\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/MNMX"
} | vfc_3810 | false | false | false | false | false |
apps | verifiable_code | 1243 | Solve the following coding problem using the programming language python:
Chef has a number N, Cheffina challenges the chef to check the divisibility of all the permutation of N by 5. If any of the permutations is divisible by 5 then print 1 else print 0.
-----Input:-----
- First-line will contain $T$, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input,$N$.
-----Output:-----
For each test case, output in a single line answer 1 or 0.
-----Constraints-----
- $1 \leq T \leq 10^6$
- $1 \leq N \leq 10^6$
-----Sample Input:-----
2
19
385
-----Sample Output:-----
0
1
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())
for i in range(a):
b = input()
if '5' in b or '0' in b:
print(1)
continue
print(0)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n19\n385\n",
"output": "0\n1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/PSTR2020/problems/ITGUY06"
} | vfc_3814 | false | false | false | false | false |
apps | verifiable_code | 1244 | Solve the following coding problem using the programming language python:
A Train started its journey at x=-infinity is travelling on the x-Coordinate Axis. Given n passengers and the coordinates $b_i$ and $d_i$ for each of the $ith$ passenger at which they board and leave from the train respectively. Due to current COVID-19 crisis, the train is monitored at each mile/coordinate. The infection degree of the train at each mile is equal to the total passengers present in the train at that mile/coordinate. The train stops its journey if no more passengers are there to board the train. The term infection severity of the journey is defined as the sum of infection degrees noted at each mile. Find the Infection severity of the journey. Note: A passenger is considered for calculation of infection degree at both boarding and leaving stations.
Since the answer can be very large, print it modulo $(10^9)+7$.
-----Input:-----
- First line will contain $N$, number of passengers. Then the $N$ lines follow.
- $i$th line contains two integers $b_i, d_i$ , the boarding and departure mile of $i$th passenger.
-----Output:-----
Print a single value, the Infection Severity of the journey modulo $(10^9)+7$.
-----Constraints-----
- $0 \leq N \leq 100000$
- $-500000 \leq b_i \leq 500000$
- $-500000 \leq d_i \leq 500000$
-----Sample Input:-----
3
0 2
1 3
-1 4
-----Sample Output:-----
12
-----EXPLANATION:-----
Infection degree at :
-1 is 1
0 is 2
1 is 3
2 is 3
3 is 2
4 is 1
Calculation started at mile -1 and ended at mile 4. Infection Severity = 1+2+3+3+2+1 =12
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
c=0
for i in range (int(input ())):
a, b=map(int, input().split())
c+=abs(a-b)+1
print(c%((10**9) +7))
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n0 2\n1 3\n-1 4\n",
"output": "12\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/COW42020/problems/COW3C"
} | vfc_3818 | false | false | false | false | false |
apps | verifiable_code | 1245 | 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:-----
1
13
57
135
7911
131517
1357
9111315
17192123
25272931
-----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
n = int(input())
l = [0] * n
for x in range(n):
l[x] = int(input())
for i in range(n):
z = 1
for j in range(1,l[i]+1):
for k in range(1,l[i]+1):
print(z,end='')
z += 2
print()
``` | {
"language": "python",
"test_cases": [
{
"input": "4\n1\n2\n3\n4\n",
"output": "1\n13\n57\n135\n7911\n131517\n1357\n9111315\n17192123\n25272931\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/PTRN2021/problems/ITGUY51"
} | vfc_3822 | false | false | false | false | false |
apps | verifiable_code | 1246 | Solve the following coding problem using the programming language python:
Walter White and Jesse Pinkman (a drug addict) both love to play with chemicals. One day they were playing with some chemicals to make an energy drink. Unknowingly they made a highly powerful drink. To test the drink on others also they called some of their friends and gave a drop of it to everyone. Now they all were feeling highly energetic and thought of an unique game to play with each other.
After pondering for a while, Jesse came up with an extraordinary idea of competing in a race around a circular globe with N checkpoints each of one unit. Walter and all their other friends agreed with it.They divided themselves in $2$ teams with $N$ teammates in each team.This race has two commencing points $A$ and $B$ strictly facing each other. Walter and his team commences from $A$ point and other team starts from $B$. Both the teams start running at the same time clockwise around the globe. Speed of every player is constant throughout the race. If a player has a speed $X$ then it means that he covers a distance of $X$ units in one second.The race ends when some member of one team overtakes all members of opposite team at any point of time. Now you have to tell if any team will win the race or not.They all are stubborn and can run forever just to win the race. Help them to know if it is possible in anyway that the race will come to an end.
For Clarity, you can visualize the path as a circular paths where $A$ and $B$ are opposite ends of diameter. It can be proven that the actual circumference of circle do not affect the answer.
It is also possible that someone don't run at all.Keep in mind that the fastest one wins the race so does the code.
-----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$ number of teammates in both team.
- The second line contains $N$ space-separated integers $A_1, A_2 \ldots A_N$ denoting speed of A's Team
- The third line contains $N$ space-separated integers $B_1, B_2 \ldots B_N$ denoting speed of B's Team
-----Output:------
For each test case, print a single line denoting YES if the race ends at any point of time else NO
-----Constraints-----
- $1 \leq T \leq 10$
- $1 \leq N \leq 10^5$
- $0 \leq A_i \leq 2^{15}$
- $0 \leq B_i \leq 2^{15}$
-----Subtasks-----
Subtask #1 (30 points):
- $1 \le N \le 20$
- $0 \le A_i \le 11$
- $0 \le B_i \le 11$
Subtask #2 (70 points):
- Original constraints
-----Sample input:-----
1
5
1 2 3 4 5
2 7 8 9 9
-----Sample output-----
YES
-----Sample Explanation:------
Team B can overtake all members of Team A.
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
l1=int(input())
for i in range(l1):
x=int(input())
y=list(map(int,input().split()))
z=list(map(int,input().split()))
if max(z)!=max(y):
print('YES')
else:
print('NO')
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n5\n1 2 3 4 5\n2 7 8 9 9\n\n",
"output": "YES\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/CHPTRS01/problems/FUNRUN"
} | vfc_3826 | false | false | false | false | false |
apps | verifiable_code | 1247 | Solve the following coding problem using the programming language python:
Chef received a permutation $P_1, P_2, \ldots, P_N$ and also an integer $D$ from his good friend Grux, because Grux was afraid he would forget them somewhere. However, since Grux was just playing with the permutation, it was all shuffled, and Chef only likes sorted permutations, so he decided to sort it by performing some swaps.
Chef wants to use the integer $D$ he just received, so he is only willing to swap two elements of the permutation whenever their absolute difference is exactly $D$. He has limited time, so you should determine the minimum number of swaps he needs to perform to sort the permutation, or tell him that it is impossible to sort it his way.
-----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 $P_1, P_2, \ldots, P_N$.
-----Output-----
For each test case, print a single line containing one integer ― the minimum number of swaps, or $-1$ if it is impossible to sort the permutation.
-----Constraints-----
- $1 \le T \le 20$
- $1 \le N \le 200,000$
- $1 \le D \le N$
- $1 \le P_i \le N$ for each valid $i$
- $P_1, P_2, \ldots, P_N$ are pairwise distinct
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $D = 1$
Subtask #2 (30 points):
- $N \le 1,000$
- the sum of $N$ over all test cases does not exceed $10,000$
Subtask #3 (50 points): original constraints
-----Example Input-----
2
5 2
3 4 5 2 1
5 2
4 3 2 1 5
-----Example Output-----
3
-1
-----Explanation-----
Example case 1: Chef can perform the following swaps in this order:
- swap the first and fifth element
- swap the third and fifth element
- swap the second and fourth element
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
sys.setrecursionlimit(10000000)
def mergeSortInversions(arr):
if len(arr) == 1:
return arr, 0
larr=len(arr)
a = arr[:larr//2]
b = arr[larr//2:]
a, ai = mergeSortInversions(a)
b, bi = mergeSortInversions(b)
c = []
i = 0
j = 0
inversions = 0 + ai + bi
la=len(a)
while i < la and j < len(b):
if a[i] <= b[j]:
c.append(a[i])
i += 1
else:
c.append(b[j])
j += 1
inversions += (la-i)
c += a[i:]
c += b[j:]
return c, inversions
for _ in range(int(input())):
n,d=list(map(int,input().split()))
p=[int(o) for o in input().split()]
array=[[] for i in range(d)]
flag=0
for i in range(n):
array[i%d].append(p[i])
if p[i]%((i%d)+1)!=0:
flag=1
ans=0
dumarr=[0]*n
for i in range(d):
array[i],v=mergeSortInversions(array[i])
for j in range(len(array[i])):
dumarr[i+j*d]=array[i][j]
ans+=v
p=sorted(p)
# print(dumarr)
if dumarr==p:
print(ans)
else:
print(-1)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n5 2\n3 4 5 2 1\n5 2\n4 3 2 1 5\n",
"output": "3\n-1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/DPERM"
} | vfc_3830 | false | false | false | false | false |
apps | verifiable_code | 1248 | Solve the following coding problem using the programming language python:
Chef has recently learned about number bases and is becoming fascinated.
Chef learned that for bases greater than ten, new digit symbols need to be introduced, and that the convention is to use the first few letters of the English alphabet. For example, in base 16, the digits are 0123456789ABCDEF. Chef thought that this is unsustainable; the English alphabet only has 26 letters, so this scheme can only work up to base 36. But this is no problem for Chef, because Chef is very creative and can just invent new digit symbols when she needs them. (Chef is very creative.)
Chef also noticed that in base two, all positive integers start with the digit 1! However, this is the only base where this is true. So naturally, Chef wonders: Given some integer N, how many bases b are there such that the base-b representation of N starts with a 1?
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
Each test case consists of one line containing a single integer N (in base ten).
-----Output-----
For each test case, output a single line containing the number of bases b, or INFINITY if there are an infinite number of them.
-----Constraints-----
-----Subtasks-----Subtask #1 (16 points):
- 1 ≤ T ≤ 103
- 0 ≤ N < 103
Subtask #2 (24 points):
- 1 ≤ T ≤ 103
- 0 ≤ N < 106
Subtask #3 (28 points):
- 1 ≤ T ≤ 103
- 0 ≤ N < 1012
Subtask #4 (32 points):
- 1 ≤ T ≤ 105
- 0 ≤ N < 1012
-----Example-----
Input:4
6
9
11
24
Output:4
7
8
14
-----Explanation-----
In the first test case, 6 has a leading digit 1 in bases 2, 4, 5 and 6: 610 = 1102 = 124 = 115 = 106.
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 finder(n):
cnt=0
for i in range(2,n+1):
a=n
while a!=0:
r=a%i
a=a//i
if r==1:
cnt+=1
return cnt
t=int(input())
for _ in range(t):
n=int(input())
if n==0:
print(0)
elif n==1:
print('INFINITY')
else:
print(finder(n))
``` | {
"language": "python",
"test_cases": [
{
"input": "4\n6\n9\n11\n24\n",
"output": "4\n7\n8\n14\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/BASE"
} | vfc_3834 | false | false | false | false | false |
apps | verifiable_code | 1249 | Solve the following coding problem using the programming language python:
For a permutation P = (p1, p2, ..., pN) of numbers [1, 2, ..., N], we define the function f(P) = max(p1, p2) + max(p2, p3) + ... + max(pN-1, pN).
You are given N and an integer K. Find and report a permutation P of [1, 2, ..., N] such that f(P) = K, if such a permutation exists.
Note f([1]) = 0.
-----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 consists of two space-separated integers N, K respectively.
-----Output-----
For each test case, if a permutation satisfying the condition exists, output a single line containing N space-separated integers which denotes any such permutation. If no such permutation exists, output a single integer -1 instead.
Use fast I/O methods since the size of the output is large.
-----Constraints-----
- 1 ≤ T ≤ 40
- 1 ≤ N ≤ 105
- Sum of N over all test cases in each file ≤ 106
- 0 ≤ K ≤ 2 * 1010
-----Example-----
Input:
3
4 12
2 2
5 14
Output:
-1
1 2
5 4 3 2 1
-----Explanation-----
Example 1. There doesn't exist any permutation of numbers [1, 2, 3, 4] that can have its f value equal to 4. Hence answer is -1.
Example 2. The permutations [1, 2] and [2, 1] both have their f values equal to 2. You can print any of these two permutations.
Example 3. The permutation [5, 4, 3, 2, 1]
has f value = max(5, 4) + max(4, 3) + max(3, 2) + max(2, 1) = 5 + 4 + 3 + 2 = 14.
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,k=[int(i) for i in input().split()]
if(n%2==0):
if(k<(n*(n+1))//2 - 1 or k>3*((n//2)**2) - 1):print(-1)
elif(k==(n*(n+1))//2 - 1):
for i in range(1,n+1):print(i,'',end='')
print()
else:
k,count,p,l,x = k-(n*(n+1))//2 + 1,0,0,[0 for i in range(n)],1
while(k>0):p+=2 ;k,count = k-n+p ,count+1
for i in range(n,n-count+1,-1):l[x]=i ;x+=2
k=-k ;l[2*count - 1 +k],p = n-count+1 ,1
for i in range(n):
if(l[i]==0):l[i]=p ; p+=1
for i in l:print(i,'',end='')
print()
else:
if(n==1):print(1) if(k==0) else print(-1)
elif(k<(n*(n+1))//2 - 1 or k>3*(n//2)*(n//2 + 1)):print(-1)
elif(k==(n*(n+1))//2 - 1):
for i in range(1,n+1):print(i,'',end='')
print()
else:
k,count,p,l,x = k-(n*(n+1))//2 + 1,0,0,[0 for i in range(n)],1
while(k>0): p+=2 ; k,count = k-n+p ,count+1
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n4 12\n2 2\n5 14\n",
"output": "-1\n1 2\n5 4 3 2 1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/GENPERM"
} | vfc_3838 | false | false | false | false | false |
apps | verifiable_code | 1250 | Solve the following coding problem using the programming language python:
Our chef has been assigned a task to make a necklace of length N, from gold, diamond and platinum.There is single kind of gold, two types of diamond and three types of platinum available.
A necklace can be represented as strings of the form (G)∗(D1|D2)∗(P1|P2|P3)∗$ (G)*(D1|D2)*(P1|P2|P3)* $ where (x|y) means we can use x or y and ∗$ * $ means we can repeat the previous parenthesized expression 0 or more times.
Instead of making necklace he choose to count all such distinct necklaces possible for a given length N.
-----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.
-Only line of every test case contains an integer N$N$ denoting the length of the necklace required.
-----Output:-----
For each test case, print the number of all such distinct necklaces, with given length. As the number can be really large, print it modulo 109+7$10^{9}+7$.
-----Constraints-----
- 1≤T≤10000$1 \leq T \leq 10000$
- 2≤N≤1000000000$2 \leq N \leq 1000000000$
- String S$S$ consists of only upper case english alphabets.
-----Subtasks-----
- 20 points : 1≤N≤5$1 \leq N \leq 5$
- 70 points : Original$Original$ Constraints$Constraints$
-----Sample Input:-----
2
1
2
-----Sample Output:-----
6
25
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=int(input())
p=10**9+7
a=(pow(3,n+1,p)-1)
b=(pow(2,n+1,p)-1)
print((((3*a)//2)%p-(2*(b))%p+p)%p)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n1\n2\n",
"output": "6\n25\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/ALQU2018/problems/GDP"
} | vfc_3842 | false | false | false | false | false |
apps | verifiable_code | 1251 | Solve the following coding problem using the programming language python:
Chef likes to travel very much. He plans some travel routes and wants to know their lengths. He hired you to make these calculations. But be careful, some of the routes are incorrect. There may be some misspelling in city names or there will be no road between some two consecutive cities in the route. Also note that Chef hates to visit the same city twice during his travel. Even the last city should differ from the first. Two consecutive cities in the route should also be different. So you need to check these conditions for the given routes too.
You will be given the list of all cities and all roads between them with their lengths. All roads are one-way. Also you will be given the list of all travel routes that Chef plans. For each route you should check whether it is correct and find its length in this case.
-----Input-----
The first line contains positive integer N, the number of cities. The second line contains space separated list of N strings, city names. All city names are distinct.
The third line contains non-negative integer M, the number of available roads. Each of the next M lines describes one road and contains names C1 and C2 of two cities followed by the positive integer D, the length of the one-way road that connects C1 with C2. It is guaranteed that C1 and C2 will be correct names of two different cities from the list of N cities given in the second line of the input file. For each pair of different cities there is at most one road in each direction and each road will be described exactly once in the input file.
Next line contains positive integer T, the number of travel routes planned by the Chef. Each of the next T lines contains positive integer K followed by K strings, names of cities of the current route. Cities are given in order in which Chef will visit them during his travel.
All strings in the input file composed only of lowercase, uppercase letters of the English alphabet and hyphens. Each string is non-empty and has length at most 20. If some line of the input file contains more then one element than consecutive elements of this line are separated by exactly one space. Each line of the input file has no leading or trailing spaces.
-----Output-----
For each travel route from the input file output a single line containing word ERROR if the route is incorrect and its length otherwise.
-----Constraints-----
1 <= N <= 50
0 <= M <= N * (N - 1)
1 <= D <= 20000
1 <= T <= 50
1 <= K <= 50
1 <= length of each string <= 20
-----Example-----
Input:
5
Donetsk Kiev New-York Miami Hollywood
9
Donetsk Kiev 560
Kiev New-York 7507
New-York Miami 1764
Miami Hollywood 28
Hollywood Miami 30
Miami New-York 1764
Kiev Donetsk 550
Hollywood New-York 1736
New-York Hollywood 1738
13
5 Donetsk Kiev New-York Miami Hollywood
5 Hollywood Miami New-York Kiev Donetsk
3 Donetsk Kiev Donetsk
2 Kyiv New-York
3 New-York Hollywood Miami
2 New-York Miami
3 Hollywood New-York Miami
4 Donetsk Kiev Miami Hollywood
2 Donetsk Hollywood
1 Donetsk
2 Mumbai Deli
6 Donetsk Kiev New-York Miami Hollywood New-York
2 Miami Miami
Output:
9859
ERROR
ERROR
ERROR
1768
1764
3500
ERROR
ERROR
0
ERROR
ERROR
ERROR
-----Explanation-----
The 2nd route is incorrect since there is no road from New-York to Kiev. Note however that inverse road from Kiev to New-York exists.
The 3rd route is incorrect since the first city coincides with the last one.
The 4th route is incorrect since there is no city with name Kyiv (Probably Chef means Kiev but he misspells this word).
The 8th route is incorrect since there is no road from Miami to Kiev.
The 9th route is incorrect since there is no road from Donetsk to Hollywood.
The 10th route is correct. Note that a route composed of exactly one city is always correct provided that city name is written correctly.
The 11th route is incorrect since there is no cities with names Mumbai and Deli. (Probably Chef is not so good in geography :))
The 12th route is incorrect since city New-York is visited twice.
Finally the 13th route is incorrect since we have equal consecutive cities.
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 _r(*conv) :
r = [conv[i](x) for i, x in enumerate(input().strip().split(' '))]
return r[0] if len(r) == 1 else r
def _ra(conv) :
return list(map(conv, input().strip().split(' ')))
def _rl() :
return list(input().strip())
def _rs() :
return input().strip()
def _a(k, *v) :
return all(x == k for x in v)
def _i(conv) :
for line in sys.stdin :
yield conv(line)
##################################################################
n = _r(int)
lookup = dict([(x, i) for i, x in enumerate(_ra(str))])
g = [(set(), dict()) for _ in range(n)]
m = _r(int)
for _ in range(m) :
c1, c2, d = _r(str, str, int)
i1 = lookup[c1]
g[i1][0].add(c2)
g[i1][1][c2] = d
t = _r(int)
for _ in range(t) :
k = _ra(str)[1:]
failed = False
if len(set(k)) != len(k) :
failed = True
if not failed :
if k[0] not in lookup :
failed = True
else :
r = 0
v = g[lookup[k[0]]]
for i in range(1, len(k)) :
if k[i] not in v[0] :
failed = True
break
r += v[1][k[i]]
v = g[lookup[k[i]]]
if not failed :
print(r)
if failed :
print('ERROR')
``` | {
"language": "python",
"test_cases": [
{
"input": "5\nDonetsk Kiev New-York Miami Hollywood\n9\nDonetsk Kiev 560\nKiev New-York 7507\nNew-York Miami 1764\nMiami Hollywood 28\nHollywood Miami 30\nMiami New-York 1764\nKiev Donetsk 550\nHollywood New-York 1736\nNew-York Hollywood 1738\n13\n5 Donetsk Kiev New-York Miami Hollywood\n5 Hollywood Miami New-York Kiev Donetsk\n3 Donetsk Kiev Donetsk\n2 Kyiv New-York\n3 New-York Hollywood Miami\n2 New-York Miami\n3 Hollywood New-York Miami\n4 Donetsk Kiev Miami Hollywood\n2 Donetsk Hollywood\n1 Donetsk\n2 Mumbai Deli\n6 Donetsk Kiev New-York Miami Hollywood New-York\n2 Miami Miami\n",
"output": "9859\nERROR\nERROR\nERROR\n1768\n1764\n3500\nERROR\nERROR\n0\nERROR\nERROR\nERROR\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/TRAVELER"
} | vfc_3846 | false | false | false | false | false |
apps | verifiable_code | 1252 | Solve the following coding problem using the programming language python:
Given a number $n$, give the last digit of sum of all the prime numbers from 1 to $n$ inclusive.
-----Input:-----
- First line contains number of testcase $t$.
- Each testcase contains of a single line of input, number $n$.
-----Output:-----
Last digit of sum of every prime number till n.
-----Constraints-----
- $1 \leq T \leq 10$
- $2 \leq N \leq 10^6$
-----Sample Input:-----
1
10
-----Sample Output:-----
7
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 math
N = 10**6
sum_arr = [0] * (N + 1)
def lprime():
arr = [0] * (N + 1)
arr[0] = 1
arr[1] = 1
for i in range(2, math.ceil(math.sqrt(N) + 1)):
if arr[i] == 0:
for j in range(i * i, N + 1, i):
arr[j] = 1
curr_prime_sum = 0
for i in range(1, N + 1):
if arr[i] == 0:
curr_prime_sum += i
sum_arr[i] = curr_prime_sum
n=int(input())
lprime()
for _ in range(n):
x=int(input())
print(sum_arr[x]%10)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n10\n",
"output": "7\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/CSEP2020/problems/IRVS"
} | vfc_3850 | false | false | false | false | false |
apps | verifiable_code | 1253 | Solve the following coding problem using the programming language python:
People in Karunanagar are infected with Coronavirus. To understand the spread of disease and help contain it as early as possible, Chef wants to analyze the situation in the town. Therefore, he does the following:
- Chef represents the population of Karunanagar as a binary string of length $N$ standing in a line numbered from $1$ to $N$ from left to right, where an infected person is represented as $1$ and an uninfected person as $0$.
- Every day, an infected person in this binary string can infect an adjacent (the immediate left and right) uninfected person.
- Therefore, if before Day 1, the population is $00100$, then at the end of Day 1, it becomes $01110$ and at the end of Day 2, it becomes $11111$.
But people of Karunanagar are smart and they know that if they 'socially isolate' themselves as early as possible, they reduce the chances of the virus spreading. Therefore on $i$-th day, person numbered $P_i$ isolates himself from person numbered $P_i - 1$, thus cannot affect each other. This continues in the town for $D$ days.
Given the population binary string before Day 1, Chef wants to calculate the total number of infected people in Karunanagar at the end of the day $D$. Since Chef has gone to wash his hands now, can you help do the calculation for him?
-----Input:-----
- First line will contain $T$, number of testcases. Then the test cases follow.
- The first line of each test case contains a single integer $N$ denoting the length of the binary string.
- The next line contains a binary string of length $N$ denoting the population before the first day, with $1$ for an infected person and $0$ for uninfected.
- The next line contains a single integer $D$ - the number of days people isolate themselves.
- The next line contains $P$ - a list of $D$ distinct space-separated integers where each $P_{i}$ denotes that at the start of $i^{th}$ day, person $P_{i}$ isolated him/herself from the person numbered $P_i-1$.
-----Output:-----
For each test case, print a single integer denoting the total number of people who are infected after the end of $D^{th}$ day.
-----Constraints-----
- $1 \leq T \leq 200$
- $2 \leq N \leq 10^{4}$
- $1 \leq D < N$
- $2 \leq P_{i} \leq N$ for $1 \le i \le D$
-----Subtasks-----
Subtask #1(30 points): $1 \leq T \leq 100$, $2 \leq N \leq 2000$
Subtask #2(70 points): Original Constraints
-----Sample Input:-----
2
9
000010000
3
2 5 8
5
00001
1
5
-----Sample Output:-----
6
1
-----EXPLANATION:-----
For the purpose of this explanation, a social distance can be denoted with a '$|$'.
For the first testcase:
-
Before Day $1$, the population is: $0|00010000$
-
Before Day $2$, the population is: $0|001|11000$
-
Before Day $3$, the population is: $0|011|111|00$
-
Therefore, after Day $3$, the population will be: $0|111|111|00$
So, there are $6$ infected persons.
For the second testcase:
Before Day $1$, the population is: $0000|1$
Therefore, there is one infected person.
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(T):
N,data,D,People = int(input()),list(map(int,list(input()))),int(input()),list(map(int,input().split()))
data.insert(0,"|"),data.append("|")
infected = []
for i in range(1,N+1):
if(data[i]==1):
infected.append(i)
i = 0
while(i<D):
boundary = People[i] + i
data.insert(boundary,"|")
times = len(infected)
for p in range(times):
index = infected[p]
if(index>=boundary):
index+=1
infected[p]+=1
if(data[index]==1):
if(data[index+1]==0):
data[index+1] = 1
infected.append(index+1)
if(data[index-1]==0):
data[index-1] = 1
infected.append(index-1)
else:
infected.remove(index)
times-=1
i+=1
infected.sort()
print(data.count(1))
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n9\n000010000\n3\n2 5 8\n5\n00001\n1\n5\n",
"output": "6\n1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/CHPTRS01/problems/WASHHAND"
} | vfc_3854 | false | false | false | false | false |
apps | verifiable_code | 1254 | Solve the following coding problem using the programming language python:
Chef wants to organize a contest. Predicting difficulty levels of the problems can be a daunting task. Chef wants his contests to be balanced in terms of difficulty levels of the problems.
Assume a contest had total P participants. A problem that was solved by at least half of the participants (i.e. P / 2 (integer division)) is said to be cakewalk difficulty. A problem solved by at max P / 10 (integer division) participants is categorized to be a hard difficulty.
Chef wants the contest to be balanced. According to him, a balanced contest must have exactly 1 cakewalk and exactly 2 hard problems. You are given the description of N problems and the number of participants solving those problems. Can you tell whether the contest was balanced or not?
-----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, P denoting the number of problems, number of participants respectively.
The second line contains N space separated integers, i-th of which denotes number of participants solving the i-th problem.
-----Output-----
For each test case, output "yes" or "no" (without quotes) denoting whether the contest is balanced or not.
-----Constraints-----
- 1 ≤ T, N ≤ 500
- 1 ≤ P ≤ 108
- 1 ≤ Number of participants solving a problem ≤ P
-----Subtasks-----
- Subtask #1 (40 points): P is a multiple of 10
- Subtask #2 (60 points): Original constraints
-----Example-----
Input
6
3 100
10 1 100
3 100
11 1 100
3 100
10 1 10
3 100
10 1 50
4 100
50 50 50 50
4 100
1 1 1 1
Output
yes
no
no
yes
no
no
-----Explanation-----
Example case 1.: The problems are of hard, hard and cakewalk difficulty. There is 1 cakewalk and 2 hard problems, so the contest is balanced.
Example case 2.: The second problem is hard and the third is cakewalk. There is 1 cakewalk and 1 hard problem, so the contest is not balanced.
Example case 3.: All the three problems are hard. So the contest is not balanced.
Example case 4.: The problems are of hard, hard, cakewalk difficulty. The contest is balanced.
Example case 5.: All the problems are cakewalk. The contest is not balanced.
Example case 6.: All the problems are hard. The contest is not balanced.
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 z in range(t) :
n,p = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
c = [x for x in a if x >= p//2]
h = [x for x in a if x <= p//10]
if len(c)==1 and len(h)==2 :
print("yes")
else:
print("no")
``` | {
"language": "python",
"test_cases": [
{
"input": "6\n3 100\n10 1 100\n3 100\n11 1 100\n3 100\n10 1 10\n3 100\n10 1 50\n4 100\n50 50 50 50\n4 100\n1 1 1 1\n",
"output": "yes\nno\nno\nyes\nno\nno\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/PERFCONT"
} | vfc_3858 | false | false | false | false | false |
apps | verifiable_code | 1255 | Solve the following coding problem using the programming language python:
You are teaching students to generate strings consisting of unique lowercase latin characters (a-z). You give an example reference string $s$ to the students.
You notice that your students just copy paste the reference string instead of creating their own string. So, you tweak the requirements for strings submitted by the students.
Let us define a function F(s, t) where s and t are strings as the number of characters that are same in both the strings. Note that the position doesn't matter. Here are a few examples of F(s, t):
F("abc", "def") = 0
F("abc", "acb") = 3
F("back", "abcd") = 3
Now you ask your students to output a string t with lowercase unique characters of the same length as $s$, such that F(s, t) $\leq k$ where you are also given the value of $k$. If there are multiple such strings, you ask them to output the lexicographically smallest possible string. If no such string is possible, output the string "NOPE" without quotes.
-----Input:-----
- The first line will contain $T$, the number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, which contains a string $s$ and an integer $k$.
-----Output:-----
For each testcase, output in a single line the lexicographically smallest string t such that F(s, t) <= k or "NOPE" without quotes if no such string exists.
-----Constraints-----
- $1 \leq T \leq 10000$
- $1 \leq $length of string s $(|s|) \leq 26$
- $s$ only consists of characters $a$ to $z$
- There are no repeating characters in s
- $0 \leq k \leq |s|$
-----Sample Input:-----
4
helowrd 0
background 0
abcdefghijklmnopqrstuvwxyz 0
b 1
-----Sample Output:-----
abcfgij
efhijlmpqs
NOPE
a
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,k=map(str,input().split())
k=int(k)
n="NOPE"
al=[0]*26
for ele in s:
al[ord(ele)-ord('a')]=1
l=len(s)
ans=[]
# print(al)
for i in range(26):
if len(ans)==l:
break
elif al[i]==1 and k>0:
k-=1
ans.append(chr(i+ord('a')))
elif al[i]==0:
ans.append(chr(i+ord('a')))
if len(ans)!=l:
print(n)
else:
print("".join(ans))
``` | {
"language": "python",
"test_cases": [
{
"input": "4\nhelowrd 0\nbackground 0\nabcdefghijklmnopqrstuvwxyz 0\nb 1\n",
"output": "abcfgij\nefhijlmpqs\nNOPE\na\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/SDIFFSTR"
} | vfc_3862 | false | false | false | false | false |
apps | verifiable_code | 1256 | Solve the following coding problem using the programming language python:
Little chef has just been introduced to the world of numbers! While experimenting with addition and multiplication operations, the little chef came up with the following problem:
Given an array A of non-negative integers, how many pairs of indices i and j exist such that A[i]*A[j] > A[i]+A[j] where i < j .
Now being a learner, little chef isn't able to solve this problem efficiently and hence turns to you for help.
-----Input-----
First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of integers in the array. The next line contains N space separated integers where the ith integer represents A[i].
Note : There may be trailing spaces on each line of input.
-----Output-----
For each test, print the required number of pairs in a single line.
-----Constraints-----
- 1 ≤ T ≤ 10
- 2 ≤ N ≤ 100000 (105)
- 0 ≤ A[i] ≤ 1000000 (106)
-----Example-----
Input:
2
3
3 4 5
4
1 1 1 1
Output:
3
0
-----Explanation-----
Example case 1.
All pairs of numbers satisfy the criteria. Total number of pairs equals 3.
Example case 2.
No pair of numbers satisfy the criteria.
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())
res = []
for i in range(t):
n = int(input())
arr = [int(i) for i in input().split()]
num_2 = 0
num = 0
for j in range(len(arr)):
if arr[j] == 2:
num_2 += 1
if arr[j] > 2:
num += 1
res.append(num_2 * num + (num * (num - 1)) // 2)
for z in res:
print(z)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n3\n3 4 5\n4\n1 1 1 1\n",
"output": "3\n0\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/PROSUM"
} | vfc_3866 | false | false | false | false | false |
apps | verifiable_code | 1257 | Solve the following coding problem using the programming language python:
The chef was chatting with his friend who was a mathematician.
Chef said "Hi !".
His friend replied that '!' is the symbol of factorial.
Chef had never heard about it and he asked more about it. Then his friend taught him how to calculate the factorial of a number.
Chef loved that But as always he got tired after calculating a few values and asked you to do it for him.
-----Input-----
N : Number of inputs
then N lines with input T
N<10
T<=200
-----Output-----
The result for the corresponding value of T
-----Example-----
Input:
3
5
4
6
Output:
120
24
720
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
factorials=[1]
for x in range(1,201):
factorials.append(factorials[x-1]*x)
x=int(input())
for x in range(x):
n=int(input())
print(factorials[n])
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n5\n4\n6\n",
"output": "120\n24\n720\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/AGTK2012/problems/ALGFACT"
} | vfc_3870 | false | false | false | false | false |
apps | verifiable_code | 1258 | Solve the following coding problem using the programming language python:
Chef Zidane likes the number 9. He has a number N, and he wants to turn it into a multiple of 9. He cannot add or remove digits, and he can only change one digit at a time. The only allowed operation is to increment or decrement a digit by one, and doing this takes exactly one second. Note that he cannot increase a digit 9 or decrease a digit 0, and the resulting number must not contain any leading zeroes unless N has a single digit.
Chef Zidane wants to know the minimum amount of time (in seconds) needed to accomplish this. Please help him, before his kitchen gets overwhelmed with mist!
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
Each test case consists of one line containing a single positive integer N.
-----Output-----
For each test case, output a single line containing the answer.
-----Constraints-----
- 1 ≤ T ≤ 105
- 1 ≤ N ≤ 10105
- N will not contain leading zeroes.
- Each test file is at most 3Mb in size.
-----Example-----
Input:4
1989
86236
90210
99999999999999999999999999999999999999988
Output:0
2
3
2
-----Explanation-----
Example case 1. 1989 is already divisible by 9, so no operations are needed to be performed.
Example case 2. 86236 can be turned into a multiple of 9 by incrementing the first and third digit (from the left), to get 96336. This takes 2 seconds.
Example case 3. 90210 can be turned into a multiple of 9 by decrementing the third digit twice and the fourth digit once, to get 90000. This takes 3 seconds.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
s=int(input())
while(s>0):
s-=1
a=input()
c=0
for x in a:
c+=int(x)
if(c<9 and len(a)!=1):
print(9-c%9)
else:
print(min(9-c%9,c%9))
``` | {
"language": "python",
"test_cases": [
{
"input": "4\n1989\n86236\n90210\n99999999999999999999999999999999999999988\n",
"output": "0\n2\n3\n2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/DIVNINE"
} | vfc_3874 | false | false | false | false | false |
apps | verifiable_code | 1259 | Solve the following coding problem using the programming language python:
Vasya likes the number $239$. Therefore, he considers a number pretty if its last digit is $2$, $3$ or $9$.
Vasya wants to watch the numbers between $L$ and $R$ (both inclusive), so he asked you to determine how many pretty numbers are in this range. 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 $L$ and $R$.
-----Output-----
For each test case, print a single line containing one integer — the number of pretty numbers between $L$ and $R$.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le L \le R \le 10^5$
-----Subtasks-----
Subtask #1 (100 points): original constraints
-----Example Input-----
2
1 10
11 33
-----Example Output-----
3
8
-----Explanation-----
Example case 1: The pretty numbers between $1$ and $10$ are $2$, $3$ and $9$.
Example case 2: The pretty numbers between $11$ and $33$ are $12$, $13$, $19$, $22$, $23$, $29$, $32$ and $33$.
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())
count=0
for i in range(n,m+1):
p=str(i)
if p[-1]=='2' or p[-1]=='3' or p[-1]=='9':
count+=1
print(count)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n1 10\n11 33\n",
"output": "3\n8\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/NUM239"
} | vfc_3878 | false | false | false | false | false |
apps | verifiable_code | 1260 | Solve the following coding problem using the programming language python:
Lavanya and Nikhil have K months of holidays ahead of them, and they want to go on exactly K road trips, one a month. They have a map of the various cities in the world with the roads that connect them. There are N cities, numbered from 1 to N. We say that you can reach city B from city A if there is a sequence of roads that starts from city A and ends at city B. Note that the roads are bidirectional. Hence, if you can reach city B from city A, you can also reach city A from city B.
Lavanya first decides which city to start from. In the first month, they will start from that city, and they will visit every city that they can reach by road from that particular city, even if it means that they have to pass through cities that they have already visited previously. Then, at the beginning of the second month, Nikhil picks a city that they haven't visited till then. In the second month, they first fly to that city and visit all the cities that they can reach from that city by road. Then, in the third month, Lavanya identifies a city, and they fly there and visit all cities reachable from there by road. Then in the fourth month it is Nikhil's turn to choose an unvisited city to start a road trip, and they alternate like this. Note that the city that they fly to (that is, the city from where they start each month's road trip) is also considered as being visited.
Each city has some museums, and when they visit a city for the first time, Lavanya makes them visit each of the museums there. Lavanya loves going to museums, but Nikhil hates them. Lavanya always makes her decisions so that they visit the maximum number of museums possible that month, while Nikhil picks cities so that the number of museums visited that month is minimized.
Given a map of the roads, the number of museums in each city, and the number K, find the total number of museums that they will end up visiting at the end of K months. Print -1 if they will have visited all the cities before the beginning of the Kth month, and hence they will be left bored at home for some of the K months.
-----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 three integers: N, M and K, which represents the number of cities, number of roads and the number of months.
- The ith of the next M lines contains two integers, ui and vi. This denotes that there is a direct road between city ui and city vi.
- The next line contains N integers, the ith of which represents the number of museums in city i.
-----Output-----
For each test case, if they can go on K road trips, output a single line containing a single integer which should be the total number of museums they visit in the K months. Output -1 if they can't go on K road trips.
-----Constraints-----
- 1 ≤ T ≤ 3
- 1 ≤ N ≤ 106
- 0 ≤ M ≤ 106
- 1 ≤ K ≤ 106
- 1 ≤ ui, vi ≤ N
- There is no road which goes from one city to itself. ie. ui ≠ vi.
- There is at most one direct road between a pair of cities.
- 0 ≤ Number of museums in each city ≤ 1000
- Sum of N over all testcases in a file will be ≤ 1.5 * 106
-----Subtasks-----
- Subtask 1 (11 points): M = 0
- Subtask 2 (21 points): Each city has at most two roads of which it is an end point. That is, for every i, there are at most two roads (u, v) in the input, such that u = i or v = i.
- Subtask 3 (68 points): Original constraints.
-----Example-----
Input:
3
10 10 3
1 3
3 5
5 1
1 6
6 2
5 6
2 5
7 10
4 7
10 9
20 0 15 20 25 30 30 150 35 20
10 10 2
1 3
3 5
5 1
1 6
6 2
5 6
2 5
7 10
4 7
10 9
20 0 15 20 25 30 30 150 35 20
10 10 5
1 3
3 5
5 1
1 6
6 2
5 6
2 5
7 10
4 7
10 9
20 0 15 20 25 30 30 150 35 20
Output:
345
240
-1
-----Explanation-----
Notice that in all the three testcases, everything is the same, except for the value of K. The following figure represents the road map in these testcases. Each node denotes a city, with a label of the form "n (m)", where n is the city number, between 1 and N, and m is the number of museums in this city. For example, the node with label "5 (25)" represents city 5, which has 25 museums.
Testcase 1: Lavanya will first choose to fly to city 8. In the first month, they visit only that city, but they visit 150 museums.
Then in the second month, Nikhil could choose to fly to city 3, and they visit the cities 1, 2, 3, 5 and 6, and visit 20 + 0 + 15 + 25 + 30 = 90 museums that month. Note that Nikhil could have instead chosen to fly to city 1 or 2 or 5 or 6, and they would all result in the same scenario.
Then, Lavanya could choose city 7, and in the third month they will visit the cities 7, 4, 10 and 9. Note that Lavanya could have chosen to fly to city 4 or 10 or 9, and they would all result in the same scenario.
In total, they have visited 345 museums in the three months (which is in fact all the museums), and that is the answer.
Testcase 2: It is same as the previous testcase, but now they have only 2 months. So they visit only 150 + 90 = 240 museums in total.
Testcase 3: It is same as the previous testcase, but now they have 5 months of holidays. But sadly, they finish visiting all the cities within the first three months itself, and hence 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
def merge(intervals,start,mid,end):
al = mid-start+1
bl = end-mid
A = intervals[start:mid+1]
B = intervals[mid+1:end+1]
p=0;q=0;k=start;
while(p<al and q<bl):
if(A[p]<B[q]):
intervals[k] = A[p]
k+=1;p+=1;
else:
intervals[k] = B[q]
k+=1;q+=1;
while(p<al):
intervals[k] = A[p]
k+=1;p+=1;
while(q<bl):
intervals[k] = B[q]
k+=1;q+=1;
def mergesort(intervals, start, end):
if(start<end):
mid = int((start+end)/2)
mergesort(intervals,start,mid)
mergesort(intervals,mid+1,end)
merge(intervals,start,mid,end)
t = int(input())
for _ in range(t):
n,m,k = map(int, input().split())
cities = [[0,[]] for i in range(n)]
for i in range(m):
a,b = map(int, input().split())
cities[a-1][1].append(b-1)
cities[b-1][1].append(a-1)
li = list(map(int, input().split()))
def specialfunction():
mergesort(li,0,n-1)
if(k>len(li)):
print(-1)
else:
sum = 0
front = 0
rear = len(li)-1
for i in range(k):
if(i%2==0):
sum += li[rear]
rear -= 1
else:
sum += li[front]
front += 1
print(sum)
if(m == 0):
specialfunction()
continue
for i in range(n):
cities[i][0] = li[i]
visited = [-1 for i in range(n)]
count = 0
museummonths = []
def searchUnvisited():
for i in range(n):
if(visited[i] == -1):
return i
return -1
def bfs(ind,count):
museumcount = 0
queue = []
queue.append(ind)
visited[ind] = 1
museumcount += cities[ind][0]
count += 1
front = 0
rear = 0
while(front<=rear):
noe = len(cities[ind][1])
for i in range(noe):
if(visited[cities[ind][1][i]] == -1):
queue.append(cities[ind][1][i])
rear += 1
count += 1
museumcount += cities[cities[ind][1][i]][0]
visited[cities[ind][1][i]] = 1
front += 1
try:
ind = queue[front]
except:
break
museummonths.append(museumcount)
return count
while(count<n):
for i in range(n):
if(visited[i] == -1):
count = bfs(i,count)
mergesort(museummonths,0,len(museummonths)-1)
#print(museummonths)
if(k>len(museummonths)):
print(-1)
else:
sum = 0
front = 0
rear = len(museummonths)-1
for i in range(k):
if(i%2==0):
sum += museummonths[rear]
rear -= 1
else:
sum += museummonths[front]
front += 1
print(sum)
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n10 10 3\n1 3\n3 5\n5 1\n1 6\n6 2\n5 6\n2 5\n7 10\n4 7\n10 9\n20 0 15 20 25 30 30 150 35 20\n10 10 2\n1 3\n3 5\n5 1\n1 6\n6 2\n5 6\n2 5\n7 10\n4 7\n10 9\n20 0 15 20 25 30 30 150 35 20\n10 10 5\n1 3\n3 5\n5 1\n1 6\n6 2\n5 6\n2 5\n7 10\n4 7\n10 9\n20 0 15 20 25 30 30 150 35 20\n",
"output": "345\n240\n-1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/INOIPRAC/problems/ROADTRIP"
} | vfc_3882 | false | false | false | false | false |
apps | verifiable_code | 1261 | Solve the following coding problem using the programming language python:
You have been appointed as the designer for your school's computer network.
In total, there are N computers in the class, and M computer-to-computer connections need to be made. Also, there are three mandatory conditions the design should fulfill.
The first requirement is that any computer in the network should be able to communicate with any other computer through the connections, possibly, through some other computers.
Network attacks are possible, so the second requirement is that even if any one computer from the network gets disabled so that the rest of the computers are unable to communicate with it, the rest of the computers can still communicate with each other. In other words, the first requirement still holds for any subset of (N-1) computers.
The third requirement is that there shouldn't be any irrelevant connections in the network. We will call a connection irrelevant if and only if after its' removal, the above two requirements are still held.
Given N, M, please build a network with N computers and M connections, or state that it is impossible.
-----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 pair of space-separated integers N and M denoting the number of computers and the number of connections.
-----Output-----
Output T blocks.
If it is impossible to construct a network with the given parameters for the corresponding test case, output just -1 -1. Otherwise, output M lines, each of which contains a space-separated pairs of integers denoting the IDs of the computers that should be connected. Note that multiple connections between any pair of computers and connections connecting a computer to itself are implicitly not allowed due to the third requirement.
-----Constraints-----
- 1 ≤ T ≤ 1000
- 1 ≤ M ≤ N * (N - 1) / 2
- 1 ≤ Sum of all N ≤ 1000
- Subtask 1 (21 point): 1 ≤ N ≤ 4
- Subtask 2 (79 points): 1 ≤ N ≤ 100
-----Example-----
Input:2
10 1
5 5
Output:-1 -1
1 2
2 3
3 4
4 5
5 1
-----Explanation-----
Example case 1. There are not enough connections even to satisfy the first requirement.
Example case 2. The obtained network satisfies all the requirements.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import fractions
import sys
f = sys.stdin
if len(sys.argv) > 1:
f = open(sys.argv[1], "rt")
def calc(N, M):
if M != N:
return [(-1, -1)]
r = [(i+1, ((i+1) % N)+1) for i in range(N)]
return r
T = int(f.readline().strip())
for case_id in range(1, T+1):
N, M = list(map(int, f.readline().strip().split()))
rr = calc(N, M)
for a, b in rr:
print(a, b)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n10 1\n5 5\n",
"output": "-1 -1\n1 2\n2 3\n3 4\n4 5\n5 1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/LTIME31/problems/EXNETWRK"
} | vfc_3886 | false | false | false | false | true |
apps | verifiable_code | 1262 | Solve the following coding problem using the programming language python:
An area named Renus, is divided into $(N \times M)$ cells. According to archaeological survey the area contains huge amount of treasure. Some cells out of $(N \times M)$ cells contain treasure. But problem is, you can't go to every cell as some of the cells are blocked.
For every $a_{ij}$ cell($1 \leq i \leq N$,$1 \leq j \leq M$), your task is to find the distance of the nearest cell having treasure.
Note:
- You can only traverse up, down, left and right from a given cell.
- Diagonal movements are not allowed.
- Cells having treasure can't be blocked, only empty cells ( cells without treasure) can be blocked.
-----Input Format:------
- First line contains $T$, the number of test cases.
- Second line contains two space-separated integers $N\ and\ M$.
- Third line contains a single integer $X$ denoting number of cells having treasures, followed by $X$ lines containing two space-separated integers $x_i$ and $y_i$ denoting position of row and column of $i^{th}$ treasure, for every $1\leq i \leq X$
- The next line contains a single integer $Y$ denoting the number of cells that are blocked, and it is followed by subsequent $Y$ lines containing two space-separated integers $u_i$ and $v_i$ denoting position of row and column of blocked cells , for every $1\leq i \leq Y$
-----Constraints:------
- $1\le T \le 100$
- $1 \le N, M \le 200$
- $1 \le X < N*M$
- $0 \le Y <(N*M) - X$
- $1 \le x_i,u_j \le N, for\ every\ 1 \le i \le X\ and\ 1 \le j \le Y$
- $1 \le y_i,v_j \le M, for\ every\ 1 \le i \le X\ and\ 1 \le j \le Y$
-----Output Format:------
For each test case print a $N \times M$ matrix where each cell consists of distance of nearest treasure. Cells that are blocked will show "$X$" (without quotes). Also cells that doesn't have access to any treasure will show "$-1$" (without quotes).
Note: Co-ordinate of top left cell is $(1,1)$.
-----Sample Input-----
1
3 3
2
1 1
1 3
2
2 1
2 2
-----Sample Output-----
0 1 0
X X 1
4 3 2
-----Explanation:-----
- Coordinates (1,1) and (1,3) shows "0" because they contain treasure and nearest distance is 0.
- Coordinates (2,1) and (2,2) shows "X" as they are blocked.
- Rest shows distance of nearest cell having treasure.
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=[int(x) for x in input().split()]
mat=[]
ans=[]
for i in range(n+2):
l=[]
p=[]
for j in range(m+2):
l.append(0)
p.append(1000000000)
mat.append(l)
ans.append(p)
y=int(input())
for i in range(y):
a,b=[int(x) for x in input().split()]
mat[a][b]=1
ans[a][b]=0
y=int(input())
for i in range(y):
a,b=[int(x) for x in input().split()]
mat[a][b]=1000000000
ans[a][b]=1000000000
for i in range(1,n+1):
for j in range(1,m+1):
if mat[i][j]==1 or mat[i][j]==1000000000:
continue
else:
ans[i][j]=min(ans[i][j],ans[i][j-1]+1,ans[i-1][j]+1)
for i in range(n,0,-1):
for j in range(m,0,-1):
if mat[i][j] == 1 or mat[i][j] == 1000000000:
continue
else:
ans[i][j]=min(ans[i][j],ans[i+1][j]+1,ans[i][j+1]+1)
for i in range(1,n+1):
for j in range(m, 0, -1):
if mat[i][j] == 1 or mat[i][j] == 1000000000:
continue
else:
ans[i][j] = min(ans[i][j], ans[i - 1][j] + 1, ans[i][j + 1] + 1)
for i in range(n, 0, -1):
for j in range(1,m+1):
if mat[i][j] == 1 or mat[i][j] == 1000000000:
continue
else:
ans[i][j] = min(ans[i][j], ans[i + 1][j] + 1, ans[i][j - 1] + 1)
for i in range(1,n+1):
for j in range(1,m+1):
if mat[i][j]==1 or mat[i][j]==1000000000:
continue
else:
ans[i][j]=min(ans[i][j],ans[i][j-1]+1,ans[i-1][j]+1)
for i in range(n,0,-1):
for j in range(m,0,-1):
if mat[i][j] == 1 or mat[i][j] == 1000000000:
continue
else:
ans[i][j]=min(ans[i][j],ans[i+1][j]+1,ans[i][j+1]+1)
for i in range(1,n+1):
for j in range(m, 0, -1):
if mat[i][j] == 1 or mat[i][j] == 1000000000:
continue
else:
ans[i][j] = min(ans[i][j], ans[i - 1][j] + 1, ans[i][j + 1] + 1)
for i in range(n, 0, -1):
for j in range(1,m+1):
if mat[i][j] == 1 or mat[i][j] == 1000000000:
continue
else:
ans[i][j] = min(ans[i][j], ans[i + 1][j] + 1, ans[i][j - 1] + 1)
for i in range(1,n+1):
for j in range(1,m+1):
if mat[i][j]==1000000000:
print('X',end=" ")
elif ans[i][j]>=1000000000:
print('-1',end=" ")
else:
print(ans[i][j],end=" ")
print()
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n3 3\n2\n1 1\n1 3\n2\n2 1\n2 2\n",
"output": "0 1 0\nX X 1\n4 3 2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/CACD2020/problems/AJP"
} | vfc_3890 | false | false | false | false | false |
apps | verifiable_code | 1263 | Solve the following coding problem using the programming language python:
The chef was playing with numbers and he found that natural number N can be obtained by sum various unique natural numbers, For challenging himself chef wrote one problem statement, which he decided to solve in future.
Problem statement: N can be obtained as the sum of Kth power of integers in multiple ways, find total number ways?
After that Cheffina came and read what chef wrote in the problem statement, for having some fun Cheffina made some changes in the problem statement as.
New problem statement: N can be obtained as the sum of Kth power of unique +ve integers in multiple ways, find total number ways?
But, the chef is now confused, how to solve a new problem statement, help the chef to solve this new problem statement.
-----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, two integers $N, K$.
-----Output:-----
For each test case, output in a single line answer to the problem statement.
-----Constraints-----
- $1 \leq T \leq 10$
- $1 \leq N \leq 1000$
- $1 \leq K \leq 6$
-----Sample Input:-----
2
4 1
38 2
-----Sample Output:-----
2
1
-----EXPLANATION:-----
For 1) 4 can be obtained by as [ 4^1 ], [1^1, 3^1], [2^1, 2^1]. (here ^ stands for power)
But here [2^1, 2^1] is not the valid way because it is not made up of unique +ve integers.
For 2) 38 can be obtained in the way which is [2^2, 3^2, 5^2] = 4 + 9 + 25
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())):
x,n = map(int,input().split())
reach = [0]*(x+1)
reach[0] = 1
i=1
while i**n<=x:
j = 1
while j+i**n<=x:
j+=1
j-=1
while j>=0:
if reach[j]>0:
reach[j+i**n]+=reach[j]
j-=1
i+=1
#print(reach)
print(reach[-1])
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n4 1\n38 2\n",
"output": "2\n1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/PBK22020/problems/ITGUY24"
} | vfc_3894 | false | false | false | false | false |
apps | verifiable_code | 1264 | Solve the following coding problem using the programming language python:
Mohit's girlfriend is playing a game with Nicky. The description of the game is as follows:
- Initially on a table Player 1 will put N gem-stones.
- Players will play alternatively, turn by turn.
- At each move a player can take at most M gem-stones (at least 1 gem-stone must be taken) from the available gem-stones on the table.(Each gem-stone has same cost.)
- Each players gem-stone are gathered in player's side.
- The player that empties the table purchases food from it (using all his gem-stones; one gem-stone can buy one unit of food), and the other one puts all his gem-stones back on to the table. Again the game continues with the "loser" player starting.
- The game continues until all the gem-stones are used to buy food.
- The main objective of the game is to consume maximum units of food.
Mohit's girlfriend is weak in mathematics and prediction so she asks help from Mohit, in return she shall kiss Mohit. Mohit task is to predict the maximum units of food her girlfriend can eat, if, she starts first. Being the best friend of Mohit, help him in predicting the answer.
-----Input-----
- Single line contains two space separated integers N and M.
-----Output-----
- The maximum units of food Mohit's girlfriend can eat.
-----Constraints and Subtasks-----
- 1 <= M <= N <= 100
Subtask 1: 10 points
- 1 <= M <= N <= 5
Subtask 2: 20 points
- 1 <= M <= N <= 10
Subtask 3: 30 points
- 1 <= M <= N <= 50
Subtask 3: 40 points
- Original Constraints.
-----Example-----
Input:
4 2
Output:
2
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
r=[0,1,1,2,1,4,2,6,1,8,4]
n,m=[int(x) for x in input().split()]
if m==1:
while n%2!=1:
n=n/2
if n==1:
print(1)
else:
print(n-1)
elif (n+1)/2<m:
print(m)
else:
print(n-m)
``` | {
"language": "python",
"test_cases": [
{
"input": "4 2\n",
"output": "2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/CDVA16/problems/CDVA1610"
} | vfc_3898 | false | false | false | false | false |
apps | verifiable_code | 1265 | Solve the following coding problem using the programming language python:
The Head Chef is receiving a lot of orders for cooking the best of the problems lately. For this, he organized an hiring event to hire some talented Chefs. He gave the following problem to test the skills of the participating Chefs. Can you solve this problem and be eligible for getting hired by Head Chef.
A non-negative number n is said to be magical if it satisfies the following property. Let S denote the multi-set of numbers corresponding to the non-empty subsequences of the digits of the number n in decimal representation. Please note that the numbers in the set S can have leading zeros. Let us take an element s of the multi-set S, prod(s) denotes the product of all the digits of number s in decimal representation.
The number n will be called magical if sum of prod(s) for all elements s in S, is even.
For example, consider a number 246, its all possible non-empty subsequence will be S = {2, 4, 6, 24, 46, 26, 246}. Products of digits of these subsequences will be {prod(2) = 2, prod(4) = 4, prod(6) = 6, prod(24) = 8, prod(46) = 24, prod(26) = 12, prod(246) = 48, i.e. {2, 4, 6, 8, 24, 12, 48}. Sum of all of these is 104, which is even. Hence 246 is a magical number.
Please note that multi-set S can contain repeated elements, e.g. if number is 55, then S = {5, 5, 55}. Products of digits of these subsequences will be {prod(5) = 5, prod(5) = 5, prod(55) = 25}, i.e. {5, 5, 25}. Sum of all of these is 35 which is odd. Hence 55 is not a
magical number.
Consider a number 204, then S = {2, 0, 4, 20, 04, 24, 204}. Products of digits of these subsequences will be {2, 0, 4, 0, 0, 8, 0}. Sum of all these elements will be 14 which is even. So 204 is a magical number.
The task was to simply find the Kth magical number.
-----Input-----
- First line of the input contains an integer T denoting the number of test cases.
- Each of the next T lines contains a single integer K.
-----Output-----
For each test case, print a single integer corresponding to the Kth magical number.
-----Constraints-----
- 1 ≤ T ≤ 105
- 1 ≤ K ≤ 1012.
-----Subtasks-----
Subtask #1 : (20 points)
- 1 ≤ T ≤ 100
- 1 ≤ K ≤ 104.
Subtask 2 : (80 points) Original Constraints
-----Example-----
Input:
2
2
5
Output:
2
8
-----Explanation-----
Example case 1.
2 is the 2nd magical number, since it satisfies the property of the magical number. The first magical number will be of course 0.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def base5(n):
if n == 0: return
for x in base5(n // 5): yield x
yield n % 5
def seq(n):
return int(''.join(str(2 * x) for x in base5(n)) or '0')
for i in range(eval(input())):
k=eval(input())
while(i<k):
i=i+1
print(seq(i-1))
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n2\n5\n\n\n",
"output": "2\n8\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/JUNE16/problems/CHEARMY"
} | vfc_3902 | false | false | false | false | false |
apps | verifiable_code | 1266 | Solve the following coding problem using the programming language python:
Chef's younger brother is in town. He's a big football fan and has a very important match to watch tonight. But the Chef wants to watch the season finale of MasterChef which will be aired at the same time. Now they don't want to fight over it like they used to when they were little kids. They want to decide it in a fair way. So they agree to play a game to make a decision. Their favourite childhood game!
The game consists of C boards. Each board i is a grid of dimension ni x mi.
Rules of the game:
- A coin is placed at (1,1) on every board initially.
- Each one takes a turn alternatively.
- In one turn, a player can choose any one board and move a coin from a cell (i,j) to one of the following cells:
(i+1,j) OR (i+2,j) OR (i,j+1) OR (i,j+2) OR (i+1,j+1) OR (i+2,j+2).
- A coin cannot be moved out of the board at any point during the game.
- A coin cannot be moved once it reaches the cell (n,m) where n and m are the dimensions of the board of that coin.
- A player MUST make one valid move.
- The player who makes the last move gets to watch TV.
Both of them are passionate about their interests and want to watch their respective shows. So they will obviously make optimal moves in every turn. The Chef, being the elder brother, takes the first turn.
Your task is to predict which show they will be watching tonight.
-----Input:-----
The first line of input contains a single integer T, the number of test cases. T tests follow.Each test case starts with a single line containing C, the number of boards in the game.
Then follow C lines: each containing 2 integers ni and mi, the dimensions of the ith board.
-----Output:-----
Given the number and dimensions of boards, for each test case, output in a single line: "MasterChef" if the Chef wins or "Football" if his brother wins.
-----Constraints:-----
1<=T<=10000
1<=C<=20
2<=ni,mi<=1000
-----Example:-----Input:
1
1
2 2
Output:
MasterChef
Explanation:
The Chef can move the coin on the board from (1,1)->(2,2). This coin cannot be moved any further. And so, the Chef wins.
Notice that if the Chef moves it to any other valid position, i.e. either to (1,2) or (2,1) he will lose!
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
res=""
for _ in range(int(input())):
ans=0
c=int(input())
for i in range(c):
n,m=list(map(int,input().split( )))
ans^=(n+m-2)%3
if ans:
res+="MasterChef\n"
else:
res+="Football\n"
print(res)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n1\n2 2\n",
"output": "MasterChef\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CHEFBRO"
} | vfc_3906 | false | false | false | false | false |
apps | verifiable_code | 1267 | Solve the following coding problem using the programming language python:
Chef likes problems related to learning new languages. He only knows first N letters of English alphabet. Also he explores all M-letter words formed by the characters he knows. Define cost for a given M-letter word S, cost(S) = P1, S1+P2, S2+...+PM, SM, where Pi, j is i, jth entry of matrix P. Sort all the words by descending cost, if costs are equal, sort them lexicographically. You need to find K-th M-letter word in Chef's order.
-----Input-----
First line contains three positive integer numbers N, M and K.
Next M lines contains N integers per line, denoting the matrix P.
-----Output-----
Output in a single line K-th M-letter in Chef's order.
-----Constraints-----
- 1 ≤ N ≤ 16
- 1 ≤ M ≤ 10
- 1 ≤ K ≤ NM
- 0 ≤ Pi, j ≤ 109
-----Subtasks-----
- Subtask #1: (20 points) 1 ≤ K ≤ 10000
- Subtask #2: (20 points) 1 ≤ M ≤ 5
- Subtask #3: (60 points) Original constraints
-----Example-----
Input:2 5 17
7 9
13 18
10 12
4 18
3 9
Output:aaaba
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def dfs(ind,m,n,k):
if(ind == m):
return [""]
else:
temp = dfs(ind+1,m,n,k)
ans = []
if(len(temp)<k):
for i in temp:
for j in range(97,97+n):
ans += [chr(j)+i]
else:
for i in temp:
ans += ["z"+i]
return ans
n,m,k = list(map(int,input().split()))
p = []
mr= []
for _ in range(m):
inp = [int(x) for x in input().split()]
mc = inp[0]
mi = 0
for j in range(1,n):
if(mc<inp[j]):
mc = inp[j]
mi = j
p += [inp]
mr += [mi]
ans = dfs(0,m,n,k)
w = []
for i in ans:
cst = 0
s = ""
for j in range(m):
if(i[j]!="z"):
s+=i[j]
cst += p[j][ord(i[j])-97]
else:
s += chr(mr[j]+97)
w += [(-cst,s)]
w.sort()
print(w[k-1][1])
``` | {
"language": "python",
"test_cases": [
{
"input": "2 5 17\n7 9\n13 18\n10 12\n4 18\n3 9\n",
"output": "aaaba\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/LTIME40/problems/LTM40EF"
} | vfc_3910 | false | false | false | false | false |
apps | verifiable_code | 1268 | Solve the following coding problem using the programming language python:
There is a universal library, where there is a big waiting room with seating capacity for maximum $m$ people, each of whom completes reading $n$ books sequentially. Reading each book requires one unit of time.
Unfortunately, reading service is provided sequentially. After all of the $m$ people enter the library, the entrance gate is closed. There is only one reading table. So when someone reads, others have to wait in the waiting room.
At first everybody chooses $n$ books they want to read. It takes $x$ amount of time. People can choose books simultaneously. Then they enter the waiting room. After reading $n$ books the person leaves the library immediately.
As nothing is free, the cost of reading is also not free. If a person stays in the library $t$ units of time then the cost of reading is $\left \lfloor \frac{t-n}{m} \right \rfloor$ units of money. So, the $i^{th}$ person pays for time $x$ he needs to choose books and the time $(i-1)*n$ he needs to wait for all the persons before him to complete reading.
Note: $\left \lfloor a \right \rfloor$ denotes the floor($a$).
-----Input-----
- Each case contains three space-separated positive integers $n$, $m$ and $x$ where $n, x \leq 1000$ and $m \leq 10^{15}$.
- End of input is determined by three zeros.
- There are no more than 1000 test cases.
-----Output-----
- For each case, output in a single line the total unit of money the library gets in that day.
-----Sample Input-----
1 100 9
11 2 10
12 2 11
0 0 0
-----Sample Output-----
9
15
16
-----Explanation:-----
Testcase 2: Here, $n=11$, $m=2$, $x=10$.
For 1st person,
$t=21$ and he/she gives $\left \lfloor \frac{21-11}{2} \right \rfloor = 5$ units of money.
For 2nd person,
$t=32$ and he/she gives $\left \lfloor \frac{32-11}{2} \right \rfloor= 10$ units of money.
So, total units of money $= 5+10 = 15$
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
while(True):
n, m, x = map(int, input().split())
if(n==0 and m==0 and x==0):
break
money=0
for i in range(n):
money=money + (x+m*i)//n
print(money)
``` | {
"language": "python",
"test_cases": [
{
"input": "1 100 9\n11 2 10\n12 2 11\n0 0 0\n",
"output": "9\n15\n16\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/READCOST"
} | vfc_3914 | false | false | false | false | false |
apps | verifiable_code | 1269 | Solve the following coding problem using the programming language python:
Once again, we have a lot of requests from coders for a challenging problem on geometry. Geometry expert Nitin is thinking about a problem with parabolas, icosahedrons, crescents and trapezoids, but for now, to encourage beginners, he chooses to work with circles and rectangles.
You are given two sequences $A_1, A_2, \ldots, A_N$ and $B_1, B_2, \ldots, B_N$. You should choose a permutation $P_1, P_2, \ldots, P_N$ of the integers $1$ through $N$ and construct $N$ rectangles with dimensions $A_1 \times B_{P_1}, A_2 \times B_{P_2}, \ldots, A_N \times B_{P_N}$. Then, for each of these rectangles, you should construct an inscribed circle, i.e. a circle with the maximum possible area that is completely contained in that rectangle.
Let $S$ be the sum of diameters of these $N$ circles. Your task is to find the maximum value of $S$.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.
- The third line contains $N$ space-separated integers $B_1, B_2, \ldots, B_N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum value of $S$. It is guaranteed that this value is always an integer.
-----Constraints-----
- $1 \le T \le 50$
- $1 \le N \le 10^4$
- $1 \le A_i, B_i \le 10^9$ for each valid $i$
-----Subtasks-----
Subtask #1 (20 points):
- $A_1 = A_2 = \ldots = A_N$
- $B_1 = B_2 = \ldots = B_N$
Subtask #2 (80 points): original constraints
-----Example Input-----
2
4
8 8 10 12
15 20 3 5
3
20 20 20
10 10 10
-----Example Output-----
30
30
-----Explanation-----
Example case 1: Four rectangles with dimensions $8 \times 3$, $8 \times 5$, $10 \times 20$ and $12 \times 15$ lead to an optimal answer.
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()))
b=list(map(int,input().split()))
a.sort()
b.sort()
s=0
for i in range(n):
s+=min(a[i],b[i])
print(s)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n4\n8 8 10 12\n15 20 3 5\n3\n20 20 20\n10 10 10\n",
"output": "30\n30\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/SNUG_FIT"
} | vfc_3918 | false | false | false | false | false |
apps | verifiable_code | 1270 | Solve the following coding problem using the programming language python:
Get excited, folks, because it is time for the final match of Codechef Premier League (CPL)!
Mike and Tracy also want to watch the grand finale, but unfortunately, they could not get tickets to the match. However, Mike is not someone who gives up so easily — he has a plan to watch the match.
The field where the match is played is surrounded by a wall with height $K$. Outside, there are $N$ boxes (numbered $1$ through $N$). For each valid $i$, the $i$-th box has a height $H_i$.
Mike wants to take some boxes and stack them on top of each other to build two towers. The height of each tower is the sum of heights of all the boxes that form it. Of course, no box may be in both towers. The height of each tower should be at least $K$. Then Mike can climb on top of one tower and Tracy on top of the other, and they can watch the match uninterrupted!
While Mike is busy stacking the boxes, Tracy would like to know the smallest number of boxes required to build two towers such that each of them has height at least $K$, or at least that it is impossible to build such towers. Can you help Tracy?
-----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 $H_1, H_2, \ldots, H_N$.
-----Output-----
For each test case, print a single line containing one integer — the smallest number of boxes required to build two towers, or $-1$ if it is impossible.
-----Constraints-----
- $1 \leq T \leq 5$
- $1 \leq N, K \leq 4,000$
- $1 \leq H_i \leq 10^5$ for each valid $i$
-----Subtasks-----
Subtask #1 (30 points):
- $1 \leq N, K \leq 100$
- $1 \leq H_i \leq 100$ for each valid $i$
Subtask #2 (70 points): original constraints
-----Example Input-----
2
8 38
7 8 19 7 8 7 10 20
4 5
2 10 4 9
-----Example Output-----
7
2
-----Explanation-----
Example case 1: The first tower can be built with boxes $8 + 10 + 20 = 38$ and the second tower with boxes $7 + 7 + 8 + 19 = 41$. In this case, the box with height $7$ is left unused.
Example case 2: We only need the box with height $10$ for one tower and the box with height $9$ for the other tower.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
sys.setrecursionlimit(100000)
memo = {}
def recurse(arr, T1, T2, k, i):
if T1 >= k and T2 >= k:
return i
if i >= len(arr):
return float('inf')
if (T1, T2) in memo:
return memo[(T1, T2)]
t1 = recurse(arr, T1 + arr[i], T2, k, i+1)
t2 = recurse(arr, T1, T2 + arr[i], k, i+1)
memo[(T1, T2)] = min(t1, t2)
return memo[(T1, T2)]
for _ in range(int(input())):
n, k = list(map(int, input().split()))
lst = list(map(int, input().split()))
lst.sort(reverse = True)
memo = {}
res = recurse(lst, 0, 0, k, 0)
if res == float('inf'):
print(-1)
else:
print(res)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n8 38\n7 8 19 7 8 7 10 20\n4 5\n2 10 4 9\n",
"output": "7\n2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/WIPL"
} | vfc_3922 | false | false | false | false | false |
apps | verifiable_code | 1271 | Solve the following coding problem using the programming language python:
You are given a set $S$ and $Q$ queries. Initially, $S$ is empty. In each query:
- You are given a positive integer $X$.
- You should insert $X$ into $S$.
- For each $y \in S$ before this query such that $y \neq X$, you should also insert $y \oplus X$ into $S$ ($\oplus$ denotes the XOR operation).
- Then, you should find two values $E$ and $O$: the number of elements of $S$ with an even number of $1$-s and with an odd number of $1$-s in the binary representation, respectively.
Note that a set cannot have duplicate elements, so if you try to insert into $S$ an element that is already present in $S$, then nothing happens.
-----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 $Q$.
- Each of the next $Q$ lines contains a single integer $X$ describing a query.
-----Output-----
For each query, print a single line containing two space-separated integers $E$ and $O$.
-----Constraints-----
- $1 \le T \le 5$
- $1 \le Q, X \le 10^5$
-----Subtasks-----
Subtask #1 (30 points):
- $1 \le Q \le 1,000$
- $1 \le X \le 128$
Subtask #2 (70 points): original constraints
-----Example Input-----
1
3
4
2
7
-----Example Output-----
0 1
1 2
3 4
-----Explanation-----
Example case 1:
- Initially, the set is empty: $S = \{\}$.
- After the first query, $S = \{4\}$, so there is only one element with an odd number of $1$-s in the binary representation ("100").
- After the second query, $S = \{4,2,6\}$, there is one element with an even number of $1$-s in the binary representation ($6$ is "110") and the other two elements have an odd number of $1$-s.
- After the third query, $S = \{4,2,6,7,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
# fast io
import sys
def fop(s): sys.stdout.write(str(s)+'\n')
def fip(): return sys.stdin.readline()
fintinp = lambda : int(fip())
def flistinp(func= int): return list(map(func,fip().split()))
def fnsepline(n,func=str): return [func(fip()) for _ in range(n)]
#-------------------code------------------------
def even(x):
x = bin(x).count('1')
return x%2==0
for _ in range(fintinp()):
q =fintinp()
o = e =0
nums = set()
for qn in range(q):
qn = fintinp()
if qn not in nums:
if even(qn): e+=1
else: o+=1
for n in set(nums):
x = n^qn
if x not in nums:
if even(x): e+=1
else: o+=1
nums.add(x)
nums.add(qn)
print(e,o)
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n3\n4\n2\n7\n",
"output": "0 1\n1 2\n3 4\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/PRTAGN"
} | vfc_3926 | false | false | false | false | false |
apps | verifiable_code | 1272 | Solve the following coding problem using the programming language python:
Given an integer $x$, find two non-negative integers $a$ and $b$ such that $(a \wedge b) + (a \vee b) = x$, where $\wedge$ is the bitwise AND operation and $\vee$ is the bitwise OR operation.
-----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 a single integer $x$.
-----Output-----
If there is no valid pair $(a, b)$, print a single line containing the integer $-1$. Otherwise, print a single line containing two space-separated integers $a$ and $b$.
If there are multiple solutions, you may print any one of them.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le x \le 10^{18}$
-----Subtasks-----
Subtask #1 (30 points):
- $1 \le T \le 200$
- $1 \le x \le 200$
Subtask #2 (70 points): original constraints
-----Example Input-----
2
1
8
-----Example Output-----
0 1
5 3
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
n=int(input())
for _ in range(n):
a=int(input())
if(a%2==0):
f=(a//2)-1
s=a-f
else:
f=(a//2)
s=a-f
print(f,s)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n1\n8\n",
"output": "0 1\n5 3\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/ANDOR"
} | vfc_3930 | false | false | false | false | true |
apps | verifiable_code | 1273 | Solve the following coding problem using the programming language python:
There is a haunted town called HauntedLand. The structure of HauntedLand can be thought of as a grid of size n * m. There is a house in each cell of the grid. Some people have fled from their houses because they were haunted. '.' represents a haunted house whereas '*' represents a house in which people are living.
One day, Devu, the famous perfumer came to town with a perfume whose smell can hypnotize people. Devu can put the perfume in at most one of the houses. This takes Devu one second. Then, the perfume spreads from one house (need not be inhabited by people) to all its adjacent houses in one second, and the cycle continues. Two houses are said to be a adjacent to each other, if they share a corner or an edge, i.e., each house (except those on the boundaries) will have 8 adjacent houses.
You want to save people from Devu's dark perfumery by sending them a message to flee from the town. So, you need to estimate the minimum amount of time Devu needs to hypnotize all the people? Note that if there are no houses inhabited by people, Devu doesn't need to put perfume in any cell.
-----Input-----
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
First line of each test case contains two space separated integers n, m denoting the dimensions of the town.
For each of next n lines, each line has m characters (without any space) denoting a row of houses of the town.
-----Output-----
For each test case, output a single integer corresponding to the answer of the problem.
-----Constraints-----
- 1 ≤ T ≤ 20
Subtask #1: (40 points)
- 1 ≤ n, m ≤ 100Subtask #2: (60 points)
- 1 ≤ n, m ≤ 1000
-----Example-----
Input:
2
2 2
*...
3 4
.*..***..*..
Output:
1
2
-----Explanation-----
In the first example, it will take Devu one second for putting the perfume at the only house. So, the answer is 1.
In the second example, He will first put the perfume at the * at cell (1, 1) (assuming 0-based indexing).
Now, it will take Devu 1 secs to put perfume. In the next second, the perfume will spread to all of its adjacent cells, thus making each house haunted.
So, 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
import math
t = int(input().strip())
for _ in range(t):
n, m = list(map(int, input().strip().split()))
a = []
v = [-1] * 4
for i in range(n):
a.append(input().strip())
for i, ai in enumerate(a):
if ai.find('*') > -1:
v[2] = i
break
if v[2] == -1:
print(0)
else:
for i, ai in reversed(list(enumerate(a))):
if ai.find('*') > -1:
v[3] = i
break
for i in range(m):
x = [ai[i] for ai in a]
if '*' in x:
v[0] = i
break
for i in reversed(range(m)):
x = [ai[i] for ai in a]
if '*' in x:
v[1] = i
break
if v.count(v[0]) == len(v):
print(1)
else:
print(int(math.ceil(max(v[3] - v[2], v[1] - v[0]) / 2.0)) + 1)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n2 2\n*.\n..\n3 4\n.*..\n***.\n.*..\n",
"output": "1\n2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/JAN16/problems/DEVPERF"
} | vfc_3934 | false | false | false | false | false |
apps | verifiable_code | 1274 | 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:-----
1121
1222
112131
122232
132333
11213141
12223242
13233343
14243444
-----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
for _ in range(int(input().strip())):
n = int(input().strip())
lst = []
for i in range(n):
lst.append(i+1)
lst.append(1)
#print(lst)
for i in range(n):
print(''.join(str(e) for e in lst))
for x in range(n):
lst[x * 2 + 1] += 1
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n2\n3\n4\n",
"output": "1121\n1222\n112131\n122232\n132333\n11213141\n12223242\n13233343\n14243444\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/PTRN2020/problems/ITGUY35"
} | vfc_3938 | false | false | false | false | false |
apps | verifiable_code | 1275 | Solve the following coding problem using the programming language python:
N Soldiers are lined up for a memory test. They are numbered from 0 to N-1 from left to right.
In the test, there are M rounds. In each round, Captain selects one position. Soldier at that position will be numbered 0. All the soldiers to the right of selected position will be numbered one greater than the soldier to his left. All the soldiers to the left of selected position will be numbered one greater than the soldier to his right.
eg. if N = 6 and selected position is 3, then the numbering will be [3, 2, 1, 0, 1, 2].
After M rounds, Captain asked each soldier to shout out the greatest number he was assigned during the M rounds. In order to check the correctness, Captain asked you to produce the correct values for each soldier (That is the correct value each soldier should shout out).
-----Input-----
The first line of the input contains an integer T denoting the number of test cases.
First line of each test case contains two integers, N and M.
Second line of each test case contains M integers, the positions selected by Captain, in that order.
-----Output-----
For each test case, output one line with N space separated integers.
-----Constraints-----
- 1 ≤ T ≤ 10^4
- 1 ≤ N ≤ 10^5
- 1 ≤ M ≤ 10^5
- 1 ≤ Sum of N over all testcases ≤ 10^5
- 1 ≤ Sum of M over all testcases ≤ 10^5
- 0 ≤ Positions selected by captain ≤ N-1
-----Example-----
Input
2
4 1
1
6 2
2 3
Output
1 0 1 2
3 2 1 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
test = int(input())
for _ in range(test):
n, m = map(int, input().split())
indexArray = list(map(int, input().split()))
mini = min(indexArray)
maxi = max(indexArray)
result = n*[0]
for i in range(n):
result[i] = max(maxi - i, i - mini)
print(result[i], end=" ")
print()
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n4 1\n1\n6 2\n2 3\n",
"output": "1 0 1 2\n3 2 1 1 2 3\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/ANUARM"
} | vfc_3942 | false | false | false | false | false |
apps | verifiable_code | 1276 | Solve the following coding problem using the programming language python:
Chef Tobby is trying to run a code given to him by Bhuvan for an experiment they want to include in the manuscript to be submitted to a conference. The deadline to submit the manuscript is within a couple of hours and Chef Tobby needs to finish the experiments before then.
The code given by Bhuvan is the following which runs given an array of N integers and another integer K :
void recurse ( array a, int n )
{
// n = size of array
define array b currently empty
consider all 2^n subsets of a[]
{
x = bitwise OR of elements in the subsets
add x into "b" if it is not present yet
}
if (sizeof( b ) == 1 << k)
{
printf(“Won”);
return;
}
recurse ( b, sizeof( b ) );
}
Chef Tobby tried to run an experiment with only one integer in the array with value 2 and K = 3. To his horror, he found out that the algorithm is resulting in an infinite loop. He is livid with the possibility that the algorithm can lead to infinite loops for certain cases. On closer inspection he determines that it might be possible to insert additional elements in the initial array to subvert the problem. Since time is very less, Chef Tobby would like to insert the minimum number of elements.
Chef Tobby has to finish writing the paper, so he asks his graduate student Leamas to fix it. Leamas has no idea how to fix the problem so he asks you for help.
-----Input section-----
The first line contains T, the number of test cases.
Each test case consists of 2 lines. The first line contains 2 integers N and K, denoting the number of elements in the array and parameter mentioned in problem statement.
Next line contains N space separated integers, denoting the elements of the array.
-----Output section-----
Output the minimum number of elements that need to be inserted so that inifinite loop can be avoided.
-----Input constraints-----
1 ≤ T ≤ 10
1 ≤ Sum of N over all test cases ≤ 105
1 ≤ K ≤ 20
0 ≤ A[i] ≤ 2K-1, where A[i] denotes the ith element of the array.
-----Sample Input - 1-----
1
2 2
3 1
-----Sample Output - 1-----
1
-----Explanation - 1-----
You can win the game by inserting the element 2 into the array.
-----Sample Input - 2-----
1
7 3
3 7 5 4 6 2 1
-----Sample Output - 2-----
0
-----Explanation - 2-----
The initial array will result will terminate in the first step of algorithm only. Thus there is no need to insert any new element.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
from bisect import *
for x in range(eval(input())):
n,k = list(map(int,input().split()))
arr = list(map(int,input().split()))
arr.sort()
t = 1
result = 0
y = 0
while y < n:
if arr[y]<t:
y += 1
elif arr[y]==t:
t = t*2
y += 1
else:
result += 1
t = t*2
while t < 2**(k):
result += 1
t = t*2
print(result)
``` | {
"language": "python",
"test_cases": [
{
"input": "- 1\n1\n2 2\n3 1\n",
"output": "1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/COOK86/problems/LIKECS03"
} | vfc_3946 | false | false | false | false | false |
apps | verifiable_code | 1277 | Solve the following coding problem using the programming language python:
Chef recently opened a big e-commerce website where her recipes can be bought online. It's Chef's birthday month and so she has decided to organize a big sale in which grand discounts will be provided.
In this sale, suppose a recipe should have a discount of x percent and its original price (before the sale) is p. Statistics says that when people see a discount offered over a product, they are more likely to buy it. Therefore, Chef decides to first increase the price of this recipe by x% (from p) and then offer a discount of x% to people.
Chef has a total of N types of recipes. For each i (1 ≤ i ≤ N), the number of recipes of this type available for sale is quantityi, the unit price (of one recipe of this type, before the x% increase) is pricei and the discount offered on each recipe of this type (the value of x) is discounti.
Please help Chef find the total loss incurred due to this sale, if all the recipes are sold out.
-----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 number of recipe types.
- N lines follow. The i-th of these lines contains three space-separated integers pricei, quantityi and discounti describing the i-th recipe type.
-----Output-----
For each test case, print a single line containing one real number — the total amount of money lost. Your answer will be considered correct if it has an absolute error less than 10-2.
-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 105
- 1 ≤ pricei, quantityi ≤ 100 for each valid i
- 0 ≤ discounti ≤ 100 for each valid i
-----Subtasks-----
Subtask #1 (30 points): 1 ≤ N ≤ 100
Subtask #2 (70 points): original constraints
-----Example-----
Input:
2
2
100 5 10
100 1 50
3
10 10 0
79 79 79
100 1 100
Output:
30.000000000
3995.0081000
-----Explanation-----
Example case 1: There are two recipes.
There are 5 recipes of the first type, each of them has a price of 100 and there is a 10% discount provided on it. Therefore, Chef first increases the price of each recipe by 10%, from 100 to 110. After that, she decreases the price by 10%, which makes the final price 99. The amount of money lost for each unit is 1, thus losing 5 for recipes of the first type.
There is only one recipe of the second type, with price 100 and a 50% discount. Therefore, Chef increases the price of the recipe by 50% from 100 to 150 and after that, she decreases its price by 50% to make its final price 75. She loses 25 for this recipe.
Overall, the amount of money Chef loses is 30.
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())):
n=int(input())
s=0
for i in range(n):
a,b,c=map(int,input().split())
d=(c/100)*a
e=a+d
f=e-((c/100)*e)
g=a-f
h=b*g
s=s+h
print(s)
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n2\n100 5 10\n100 1 50\n3\n10 10 0\n79 79 79\n100 1 100\n",
"output": "30.000000000\n3995.0081000\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/BIGSALE"
} | vfc_3950 | false | false | false | false | false |
apps | verifiable_code | 1278 | Solve the following coding problem using the programming language python:
A plot of land can be described by $M x N$ dots such that horizontal and vertical distance between any two dots is 10m.
Mr. Wolf would like to build a house in the land such that all four sides of the house are equal. Help Mr. Wolf to find the total number of unique positions where houses can be built. Two positions are different if and only if their sets of four dots are different.
-----Input:-----
The first line of the input gives the number of test cases, $T$. $T$ lines follow. Each line has two integers $M$ and $N$: the number of dots in each row and column of the plot, respectively.
-----Output:-----
For each test case, output one single integer containing the total number of different positions where the house can be built.
-----Constraints-----
- $1 \leq T \leq 100$
- $2 \leq M \leq 10^9$
- $2 \leq N \leq 10^9$
-----Sample Input:-----
4
2 4
3 4
4 4
1000 500
-----Sample Output:-----
3
10
20
624937395
-----EXPLANATION:-----
Map 1
Map 2
Map 3
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,stdout,setrecursionlimit
from math import ceil
mod = 1000000007
t = int(stdin.readline())
for _ in range(t):
m,n = list(map(int,input().split()))
if m < n:
m,n = n,m
y = n-1
s1 = ((y*(y+1)) //2)%mod
s2 = ((y*(y+1)*(2*y+1)) //6)%mod
s3 = ((y*y*(y+1)*(y+1)) //4)%mod
ans = (m*n*s1 - (m+n)*s2+s3)%mod
# ans = (m*(m+1)*(2*m*n + 4*n + 2 - m*m - m)//12)
print(ans)
``` | {
"language": "python",
"test_cases": [
{
"input": "4\n2 4\n3 4\n4 4\n1000 500\n",
"output": "3\n10\n20\n624937395\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/COX22020/problems/DCODEX2"
} | vfc_3954 | false | false | false | false | false |
apps | verifiable_code | 1279 | Solve the following coding problem using the programming language python:
Chef hates unoptimized codes and people who write such codes. One fine day he decided to look through the kitchen's codebase and found a function whose pseudo-code is given here:
input: integer N, list X[1, 2, ..., N], list Y[1, 2, ..., N]
output: integer res
function:
set res = 0;
for i := 1 to N do
for j := 1 to N do
for k := 1 to N do
if (X[i] = X[j]) OR (X[j] = X[k]) OR (X[k] = X[i])
continue
else
set res = max(res, Y[i] + Y[j] + Y[k])
return res
Luckily enough this code gets triggered only if the Head Chef makes a submission. But still there is a possibility that this can crash the judge. So help Chef by writing a new function which does the same thing but is faster.
-----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 an integer N denoting the number of elements in the two lists.
- The i-th of the next N lines contains a pair of space-separated integers denoting the values of X[i] and Y[i] respectively.
-----Output-----
For each test case, output an integer corresponding to the return value of the function.
-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 105
- 1 ≤ X[i], Y[i] ≤ 108
-----Example-----
Input
2
3
1 3
3 1
1 2
5
1 3
2 4
1 2
3 2
3 4
Output
0
11
-----Explanation-----
Testcase 2: The maximum is attained when i = 1, j = 2 and k = 5. This leads to res being 3 + 4 + 4 = 11. This value is attained in other iterations as well, but it never exceeds this, and hence this is the answer.
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())):
d=dict()
ls=[]
for i in range(int(input())):
ls=list(map(int,input().split()))
if ls[0] in d:
d[ls[0]]=max(ls[1],d[ls[0]])
else:
d[ls[0]]=ls[1]
# print(d)
if len(d)<3:
print(0)
else:
kd=list(d.values())
kd.sort()
# print(kd)
print(kd[-1]+kd[-2]+kd[-3])
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n3\n1 3\n3 1\n1 2\n5\n1 3\n2 4\n1 2\n3 2\n3 4\n",
"output": "0\n11\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/OPTCODE"
} | vfc_3958 | false | false | false | false | false |
apps | verifiable_code | 1280 | Solve the following coding problem using the programming language python:
Emily and Mia are friends. Emily got Mia’s essay paper, but since she is a prankster, she decided to meddle with the words present in the paper. She changes all the words in the paper into palindromes. To do this, she follows two rules:
- In one operation she can only reduce the value of an alphabet by 1, i.e. she can change ‘d’ to ‘c’, but she cannot change ‘c’ to ‘d’ or ‘d’ to ‘b’.
- The alphabet ‘a’ will not be reduced any further.
Each reduction in the value of any alphabet is counted as a single operation. Find the minimum number of operations required to convert a given string into a palindrome.
-----Input:-----
- The first line contains an integer $T$, denoting the number of test cases.
- Each test case consists of a string $S$ containing only lowercase characters with no spaces.
-----Output:-----
For each test case on a new line, print the minimum number of operations for the corresponding test case.
-----Constraints-----
- $1<=T<=10$
- $1<=|S|<=10^7$, where $|S|$ denotes length of string S.
-----Sample Input:-----
4
abc
abcba
abcd
cba
-----Sample Output:-----
2
0
4
2
-----EXPLANATION:-----
For the first test case string = “abc”
c->b->a so the string become “aba” which is a palindrome. For this we perform 2 operations
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):
xyz = input().strip()
string = 0
for i in range(len(xyz)//2):
string = string + (abs(ord(xyz[i])-ord(xyz[len(xyz)-i-1])))
print(string)
``` | {
"language": "python",
"test_cases": [
{
"input": "4\nabc\nabcba\nabcd\ncba\n",
"output": "2\n0\n4\n2\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/RTCG2020/problems/RTCG004"
} | vfc_3962 | false | false | false | false | true |
apps | verifiable_code | 1281 | Solve the following coding problem using the programming language python:
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----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 an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 ≤ T ≤ 100
- 7 ≤ N ≤ 100
- 1 ≤ Ai ≤ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6.
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())
x=[1,2,3,4,5,6,7]
for i in range(t):
N=int(input())
a=list(map(int,input().split()))
rev=a[::-1]
dup=set(a)
if rev== a and list(dup) ==x:
print("yes")
else:
print("no")
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n19\n1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1\n14\n1 2 3 4 5 6 7 6 5 4 3 2 1 1\n13\n1 2 3 4 5 6 8 6 5 4 3 2 1\n",
"output": "yes\nno\nno\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/RAINBOWA"
} | vfc_3966 | false | false | false | false | true |
apps | verifiable_code | 1282 | Solve the following coding problem using the programming language python:
You are given positive integers $L$ and $R$. You have to find the sum
S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,,
where $\wedge$ denotes the bitwise AND operation. Since the sum could be 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 $L$ and $R$.
-----Output-----
For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le L \le R \le 10^{18}$
-----Example Input-----
2
1 4
4 10
-----Example Output-----
1
16
-----Explanation-----
Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1.
Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
l= []
for i in range(62):
l.append(2**i)
T = int(input())
flag = 0
for t in range(T):
L,R = [int(i) for i in input().split()]
bL = bin(L)
lL = len(bL)-2
index = 1
ans = 0
temp = 0
while(index<=lL):
temp = L%l[index]
if temp>=l[index-1]:
if(l[index]-temp<=R-L+1):
ans= (ans +(l[index-1])*(l[index]-temp))%1000000007
else :
ans=(ans+(l[index-1])*(R-L+1))%1000000007
index+=1
print(ans)
# 4378578345 584758454958
# 18091037982636824985 8589934592 4429185025 4294967296
``` | {
"language": "python",
"test_cases": [
{
"input": "2\n1 4\n4 10\n",
"output": "1\n16\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/RGAND"
} | vfc_3970 | false | false | false | false | false |
apps | verifiable_code | 1283 | Solve the following coding problem using the programming language python:
Chef likes prime numbers. However, there is one thing he loves even more. Of course, it's semi-primes! A semi-prime number is an integer which can be expressed as a product of two distinct primes. For example, $15 = 3 \cdot 5$ is a semi-prime number, but $1$, $9 = 3 \cdot 3$ and $5$ are not.
Chef is wondering how to check if an integer can be expressed as a sum of two (not necessarily distinct) semi-primes. Help Chef with this tough task!
-----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 a single integer $N$.
-----Output-----
For each test case, print a single line containing the string "YES" if it is possible to express $N$ as a sum of two semi-primes or "NO" otherwise.
-----Constraints-----
- $1 \le T \le 200$
- $1 \le N \le 200$
-----Example Input-----
3
30
45
62
-----Example Output-----
YES
YES
NO
-----Explanation-----
Example case 1: $N=30$ can be expressed as $15 + 15 = (3 \cdot 5) + (3 \cdot 5)$.
Example case 2: $45$ can be expressed as $35 + 10 = (5 \cdot 7) + (2 \cdot 5)$.
Example case 3: $62$ cannot be expressed as a sum of two semi-primes.
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
n = 201
v = [0 for i in range(n + 1)]
def gen():
for i in range(1, n + 1):
v[i] = i
countDivision = [0 for i in range(n + 1)]
for i in range(n + 1):
countDivision[i] = 2
for i in range(2, n + 1, 1):
if (v[i] == i and countDivision[i] == 2):
for j in range(2 * i, n + 1, i):
if (countDivision[j] > 0):
v[j] = int(v[j] / i)
countDivision[j] -= 1
try:
t=int(sys.stdin.readline())
for _ in range(t):
gen()
x=int(sys.stdin.readline())
flag=0
for i in range(2,x//2+1):
if v[i]==1 and v[x-i]==1:
flag=1
#print(i,x-i)
if flag==1:
print("YES")
else:
print("NO")
except:
pass
``` | {
"language": "python",
"test_cases": [
{
"input": "3\n30\n45\n62\n\n",
"output": "YES\nYES\nNO\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/CHEFPRMS"
} | vfc_3974 | false | false | false | false | false |
apps | verifiable_code | 1284 | Solve the following coding problem using the programming language python:
Coach Khaled is a swag teacher in HIT (Hag Institute of Technology). However, he has some obsession problems.
Recently, coach Khaled was teaching a course in building 8G networks using TV antennas and programming them with assembly. There are $N$ students (numbered $1$ through $N$) in his class; for some reason, this number is always a multiple of $4$. The final exam has finished and Khaled has all the scores of his $N$ students. For each valid $i$, the score of the $i$-th student is $A_i$; each score is an integer between $0$ and $100$. Currently, the score-grade distribution is as follows:
- grade D for score smaller than $60$
- grade C for score greater or equal to $60$, but smaller than $75$
- grade B for score greater or equal to $75$, but smaller than $90$
- grade A for score greater or equal to $90$
However, coach Khaled is not satisfied with this. He wants exactly $N/4$ students to receive each grade (A, B, C and D), so that the grades are perfectly balanced. The scores cannot be changed, but the boundaries between grades can. Therefore, he wants to choose three integers $x$, $y$ and $z$ and change the grade distribution to the following (note that initially, $x = 60$, $y = 75$ and $z = 90$):
- grade D for score smaller than $x$
- grade C for score greater or equal to $x$, but smaller than $y$
- grade B for score greater or equal to $y$, but smaller than $z$
- grade A for score greater or equal to $z$
Your task is to find thresholds $x$, $y$ and $z$ that result in a perfect balance of grades. If there are multiple solutions, choose the one with the maximum value of $x+y+z$ (because coach Khaled wants seem smarter than his students); it can be proved that there is at most one such solution. Sometimes, there is no way to choose the thresholds and coach Khaled would resign because his exam questions were low-quality.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.
-----Output-----
For each test case, if there is no solution, print a single line containing the integer $-1$; otherwise, print a single line containing three space-separated integers $x$, $y$ and $z$.
-----Constraints-----
- $1 \le T \le 1,000$
- $4 \le N \le 100$
- $N$ is divisible by $4$
- $0 \le A_i \le 100$ for each valid $i$
- the sum of $N$ over all test cases does not exceed $5,000$
-----Subtasks-----
Subtask #1 (100 points): original constraints
-----Example Input-----
6
4
90 25 60 75
8
27 29 92 92 67 67 85 92
4
0 1 2 3
4
100 100 100 100
4
30 30 40 50
4
30 40 40 50
-----Example Output-----
60 75 90
-1
1 2 3
-1
-1
-1
-----Explanation-----
Example case 1: The default distribution is the correct one.
Example case 4: All students have the same score and grade, so there is no way to choose the thresholds and coach Khaled must resign.
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())
k = n//4
# a,b,c = map(int,input().split())
a = sorted(map(int,input().split()))
a60 = (a[k-1],a[k])
a75 = (a[2*k-1],a[2*k])
a90 = (a[3*k-1],a[3*k])
if a60[0]==a60[1] or a75[0]==a75[1] or a90[0]==a90[1] :
print(-1)
else :
print(a60[1],a75[1],a90[1])
``` | {
"language": "python",
"test_cases": [
{
"input": "6\n4\n90 25 60 75\n8\n27 29 92 92 67 67 85 92\n4\n0 1 2 3\n4\n100 100 100 100\n4\n30 30 40 50\n4\n30 40 40 50\n",
"output": "60 75 90\n-1\n1 2 3\n-1\n-1\n-1\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/HIT"
} | vfc_3978 | false | false | false | false | false |
apps | verifiable_code | 1285 | Solve the following coding problem using the programming language python:
Chef is learning linear algebra. Recently, he learnt that for a square matrix $M$, $\mathop{\rm trace}(M)$ is defined as the sum of all elements on the main diagonal of $M$ (an element lies on the main diagonal if its row index and column index are equal).
Now, Chef wants to solve some excercises related to this new quantity, so he wrote down a square matrix $A$ with size $N\times N$. A square submatrix of $A$ with size $l\times l$ is a contiguous block of $l\times l$ elements of $A$. Formally, if $B$ is a submatrix of $A$ with size $l\times l$, then there must be integers $r$ and $c$ ($1\le r, c \le N+1-l$) such that $B_{i,j} = A_{r+i-1, c+j-1}$ for each $1 \le i, j \le l$.
Help Chef find the maximum trace of a square submatrix of $A$.
-----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 $i$ ($1 \le i \le N$), the $i$-th of these lines contains $N$ space-separated integers $A_{i,1}, A_{i,2}, \dots, A_{i, N}$ denoting the $i$-th row of the matrix $A$.
-----Output-----
For each test case, print a single line containing one integer — the maximum possible trace.
-----Constraints-----
- $1 \le T \le 100$
- $2 \le N \le 100$
- $1 \le A_{i,j} \le 100$ for each valid $i, j$
-----Subtasks-----
Subtask #1 (100 points): original constraints
-----Example Input-----
1
3
1 2 5
6 3 4
2 7 1
-----Example Output-----
13
-----Explanation-----
Example case 1: The submatrix with the largest trace is
6 3
2 7
which has trace equal to $6 + 7 = 13$. (This submatrix is obtained for $r=2, c=1, l=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 k in range(0,T):
N=int(input())
matrix=[]
for i in range(0,N):
a=list(map(int, input().split()))
matrix.append(a)
max_trace = []
for i in range(0,N):
trace1=0
trace2=0
for j in range(0,i+1):
trace1+=matrix[j][N+j-i-1]
trace2+=matrix[N+j-i-1][j]
max_trace.append(trace1)
max_trace.append(trace2)
print(max(max_trace))
``` | {
"language": "python",
"test_cases": [
{
"input": "1\n3\n1 2 5\n6 3 4\n2 7 1\n",
"output": "13\n",
"type": "stdin_stdout"
}
]
} | {
"difficulty": "interview",
"problem_url": "https://www.codechef.com/problems/TRACE"
} | vfc_3982 | false | false | false | false | false |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.