_id
stringlengths 2
5
| partition
stringclasses 2
values | text
stringlengths 5
289k
| language
stringclasses 1
value | meta_information
dict | title
stringclasses 1
value |
---|---|---|---|---|---|
d601 | train | import math
t = int(input())
a = [-1, 0, 1]
for i in range(58):
temp = a[-1] + a[-2]
temp = temp%10
a.append(temp)
for _ in range(t):
n = int(input())
temp = len(bin(n)) - 3
temp = 2**temp
temp = temp%60
print(a[temp]) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/FIBEASY"
} | |
d602 | train | # cook your dish here
n=(int(input()))
x=[]
for _ in range(n):
a,b=map(int,input().split())
a=[a,a+b]
x.append(a)
x = sorted(x, key= lambda i:i[1])
y=-1
c=0
for i in range(len(x)):
if x[i][0]>y:
c+=1
y=x[i][1]
print(c) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/IARCSJUD/problems/STADIUM"
} | |
d603 | train | m= 9999999
word=''
p= ''
try:
s=input().split()
for i in s:
if(len(i) <= m):
m = len(i)
word = i
p = word
for i in s:
p+= (' '+i+' '+ word)
print(p)
except EOFError:
pass | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/STRG2020/problems/REMIX"
} | |
d604 | train | for i in range(int(input())):
N = int(input())
s = 'zyxwvutsrqponmlkjihgfedcba'
r = ''
while True:
r = s[-N-1:] + r
if N < 26:
break
N -= 25
print(r) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/DECSTR"
} | |
d605 | train | for _ in range(int(input())):
r,c = map(int,input().split())
l = []
for k in range(r):
a = list(map(int,input().split()))
l.append(a)
ans = "Stable"
for i in range(r):
for j in range(c):
p = l[i][j]
count=0
if i-1>=0 and j>=0:
count+=1
if i>=0 and j-1>=0:
count+=1
if i+1<=r-1 and j<=c-1:
count+=1
if i<=r-1 and j+1<=c-1:
count +=1
if count<=p:
ans = "Unstable"
break
print(ans) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/REACTION"
} | |
d606 | train | # cook your dish here
test=int(input())
for _ in range(test):
b=list(map(int,str(input()).split(' ')))
c=str(input())
li1=[0]
li2=[0]
for i1 in range(len(c)):
if c[i1]=='R':
li1.append(li1[len(li1)-1]+1)
elif c[i1]=='L':
li1.append(li1[len(li1)-1]-1)
elif c[i1]=='U':
li2.append(li2[len(li2)-1]+1)
else:
li2.append(li2[len(li2)-1]-1)
if (max(li1)-min(li1)+1)<=b[1] and (max(li2)-min(li2)+1)<=b[0]:
print('safe')
else:
print('unsafe') | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ROBOTG"
} | |
d607 | train | # cook your dish here
T = int(input())
for i in range(T):
l = list(map(int, input().split()))
n, k, m, x = l[0], l[1], l[2], l[3]
if k == 1:
if n == m:
print("yes")
else:
print("no")
elif m % k > 1:
print("no")
elif k == 2:
stack = []
var = 0
while m != 0:
var += m % k
stack.append(m % k)
m //= k
if var > n:
print("no")
elif var == n:
print("yes")
else:
for p in range(100):
for q in range(2, len(stack)):
if stack[q - 1] == 0 and stack[q] >= 1:
stack[q-1] = 2
stack[q] -= 1
var += 1
if var == n:
print("yes")
if var < n:
print("no")
else:
temp = 0
rog = 1
while m != 0:
if m % k > 2:
rog = 0
print("no")
temp += m % k
m //= k
if rog:
if temp == n:
print("yes")
else:
print("no")
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/IMPACT"
} | |
d608 | train | '''input
2
3
2 2 3
4
2 3 3 2
'''
import math
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
count = 0
i = 0
while i < len(a):
if a[i] == 1:
count += 1
i += 1
continue
curr_gcd = a[i]
while i < len(a) and curr_gcd != 1:
curr_gcd = math.gcd(curr_gcd, a[i])
if curr_gcd == 1:
count += 1
i += 1
# print(i)
break
i += 1
print(count)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/DECO2020/problems/DECOGCD"
} | |
d609 | train | for i in range(int(input())):
n = int(input())
P = list(map(float, input().split()))
pr = 1
for p in P:
a = 100+p
pr = (pr*a)/100
pr = (pr-1)*100
x = 6-len(str(int(abs(pr))))
if (x==1):
if (pr==0):
print(0)
elif (pr>0):
print("+"+str("%.1f" % round(pr,x)))
else:
print(str("%.1f" % round(pr,x)))
elif (x==2):
if (pr==0):
print(0)
elif (pr>0):
print("+"+str("%.2f" % round(pr,x)))
else:
print(str("%.2f" % round(pr,x)))
elif (x==3):
if (pr==0):
print(0)
elif (pr>0):
print("+"+str("%.3f" % round(pr,x)))
else:
print(str("%.3f" % round(pr,x)))
elif (x==4):
if (pr==0):
print(0)
elif (pr>0):
print("+"+str("%.4f" % round(pr,x)))
else:
print(str("%.4f" % round(pr,x)))
elif (x==5):
if (pr==0):
print(0)
elif (pr>0):
print("+"+str("%.5f" % round(pr,x)))
else:
print(str("%.5f" % round(pr,x)))
elif (x==6):
if (pr==0):
print(0)
elif (pr>0):
print("+"+str("%.6f" % round(pr,x)))
else:
print(str("%.6f" % round(pr,x))) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ITRA2016/problems/ITRA03"
} | |
d610 | train | # cook your dish here
t=int(input())
while(t):
t=t-1
n,k=list(map(int,input().split()))
q=list(map(int,input().split()))
days,rem=0,0
for i in range(n):
rem+=q[i]
if(rem>=k):
rem-=k
else:
days=i+1
break
days+=1
if(rem>=k):
days+=(rem//k)+1
print(days)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CHEFEZQ"
} | |
d611 | train | # cook your dish here
t=int(input())
while t>0:
n=int(input())
l=list(map(int,input().split()))
l1=[]
c=1
for i in range(len(l)):
if l[i]==1:
l1.append(i)
for j in range(len(l1)-1):
if l1[j+1]-l1[j]<6:
c=0
break
if c:
print("YES")
else:
print("NO")
t-=1 | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/COVIDLQ"
} | |
d612 | train | # cook your dish here
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
d={}
for i in range(n):
if a[i]-1 not in d:
d[a[i]-1]=[i]
else:
d[a[i]-1].append(i)
ans=False
d1={}
for i in d:
if ans==True:
break
for j in d:
if i!=j:
if a[i]==a[j] and i!=j:
ans=True
break
if ans==True:
print('Truly Happy')
else:
print('Poor Chef')
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CHHAPPY"
} | |
d613 | train | # cook your dish here
t=int(input())
for i in range(t):
s=input()
fl=-1
n=len(s)
for i in range(n-2):
if(s[i:i+3]=="010" or s[i:i+3]=="101"):
fl=0
print("Good")
break
if(fl==-1):
print("Bad")
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ERROR"
} | |
d614 | train | def check(s):
arr=[s[0]]
l=len(s)
f1=0
for i in range(1,l):
if arr==[]: arr.append(s[i])
elif arr[-1]!=s[i]:arr.append(s[i])
else: del arr[-1]
if arr==[]: return True
else: return False
count = 0
for t in range(eval(input())):
s=input().strip()
if check(s): count+=1
print(count) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COMN2016/problems/NIKKLIN"
} | |
d615 | train | from itertools import permutations as p
def disc(a,b):
for ai in a:
for bi in b:
if ai==bi:
return False
return True
for i in range(eval(input())):
n = eval(input())
arr = list(map(int,input().split()))
perms = list(p(arr))
m = eval(input())
offer = {}
for i in range(m):
dup = list(map(int,input().split()))
try:
offer[dup[0]].append(dup[1:])
except:
offer[dup[0]] = [dup[1:]]
ans = sum(arr)
if n==1:
print(ans)
elif n==2:
try:
if len(offer[2])>=1:
ans -= min(arr)
except:
pass
print(ans)
elif n==3:
try:
if len(offer[3])>=1:
ans -= min(arr)
except:
pass
try:
if len(offer[2])>=1:
value = 9999999999
for item in perms:
cur = 0
cur += item[0]
cur += max(item[1],item[2])
if cur<value:
value = cur
if value<ans:
ans = value
except:
pass
print(ans)
elif n==4:
try:
if len(offer[4])>=1:
ans -= min(arr)
except:
pass
#print ans
try:
if len(offer[3])>=1:
value = 9999999999
for item in perms:
cur = 0
cur = sum(item)
cur -= min(item[1],item[2],item[3])
if cur<value:
value = cur
if value<ans:
ans = value
except:
pass
#print ans
try:
if len(offer[2])>=1:
value = 9999999999
for item in perms:
cur = 0
cur = sum(item)
cur -= min(item[1],item[2])
if cur<value:
value = cur
if value<ans:
ans = value
#print ans
#print offer[2]
if len(offer[2])>=2:
flg = False
end = len(offer[2])
for i in range(end):
for j in range(i+1,end):
if disc(offer[2][i],offer[2][j]):
flg = True
break
#print flg
if flg:
value = 9999999999
for item in perms:
cur = 0
cur = sum(item)
cur -= min(item[1],item[0])
cur -= min(item[2],item[3])
if cur<value:
value = cur
if value<ans:
ans = value
except:
pass
print(ans)
elif n==5:
try:
if len(offer[5])>=1:
ans -= min(arr)
except:
pass
try:
if len(offer[4])>=1:
value = 9999999999
for item in perms:
cur = 0
cur = sum(item)
cur -= min(item[1],item[2],item[3],item[4])
if cur<value:
value = cur
if value<ans:
ans = value
except:
pass
try:
if len(offer[2])>=1:
value = 9999999999
for item in perms:
cur = 0
cur = sum(item)
cur -= min(item[1],item[2])
if cur<value:
value = cur
if value<ans:
ans = value
if len(offer[2])>=2:
flg = False
end = len(offer[2])
for i in range(end):
for j in range(i+1,end):
if disc(offer[2][i],offer[2][j]):
flg = True
break
if flg:
value = 9999999999
for item in perms:
cur = 0
cur = sum(item)
cur -= min(item[1],item[0])
cur -= min(item[2],item[3])
if cur<value:
value = cur
if value<ans:
ans = value
except:
pass
try:
if len(offer[3])>=1:
value = 9999999999
for item in perms:
cur = 0
cur = sum(item)
cur -= min(item[1],item[2],item[3])
if cur<value:
value = cur
if value<ans:
ans = value
except:
pass
try:
if len(offer[3])>=1 and len(offer[2])>=1:
flg = False
for i in offer[3]:
for j in offer[2]:
if disc(i,j):
flg = True
break
if flg:
value = 9999999999
for item in perms:
cur = 0
cur = sum(item)
cur -= min(item[1],item[0])
cur -= min(item[2],item[3],item[4])
if cur<value:
value = cur
if value<ans:
ans = value
except:
pass
print(ans)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/NOV15/problems/CHEFSHOP"
} | |
d616 | train |
t=int(input())
for i in range(t):
l=list(map(int,input().split(' ')))
a=l[0]
b=l[1]
l1=list(map(int,input().split(' ')))
for i in range(b):
l2=list(map(int,input().split(' ')))
a1=l2[0]
b1=l2[1]
su=0
for j in range(a1-1,b1):
su=(su+l1[j])%1000000000
print(su) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/REER2020/problems/NXS4"
} | |
d617 | train | from sys import stdin
for _ in range(int(stdin.readline())):
m, n = list(map(int, stdin.readline().split()))
final = []
arr = []
val = 0
extra = 0
for j in range(m):
ans = list(map(str, stdin.readline().split()))
if ans.count('N') == n:
val += 1
else:
if val%2 == 0:
arr.append(ans)
extra += val
else:
arr.append(['N']*n)
arr.append(ans)
extra += (val-1)
val = 0
for j in range(len(arr)):
ans = arr[j]
start = -1
for i in range(n):
if ans[i] == 'P':
start = i
break
if start != -1:
for i in range(n-1, -1, -1):
if ans[i] == 'P':
end = i
break
if start != -1:
if len(final) == 0:
final.append([start, end])
else:
if j%2 == 0:
if final[-1][0] > start:
final[-1][0] = start
else:
start = final[-1][0]
else:
if final[-1][1] < end:
final[-1][1] = end
else:
end = final[-1][1]
final.append([start, end])
else:
if len(final) != 0:
start, end = 0, n-1
if j%2 == 0:
if final[-1][0] > start:
final[-1][0] = start
else:
start = final[-1][0]
else:
if final[-1][1] < end:
final[-1][1] = end
else:
end = final[-1][1]
final.append([start, end])
if len(final) == 0:
print(0)
else:
count = 0
for ele in final:
count += (ele[1]-ele[0]+1)
print(count-1+extra)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CHPTRS01/problems/CARLOT"
} | |
d618 | train | def CeilIndex(A, l, r, key):
while (r - l > 1):
m = l + (r - l)//2
if (A[m] >= key):
r = m
else:
l = m
return r
def LongestIncreasingSubsequenceLength(A, size):
# Add boundary case,
# when array size is one
tailTable = [0 for i in range(size + 1)]
len = 0 # always points empty slot
tailTable[0] = A[0]
len = 1
for i in range(1, size):
if (A[i] < tailTable[0]):
# new smallest value
tailTable[0] = A[i]
elif (A[i] > tailTable[len-1]):
# A[i] wants to extend
# largest subsequence
tailTable[len] = A[i]
len+= 1
else:
# A[i] wants to be current
# end candidate of an existing
# subsequence. It will replace
# ceil value in tailTable
tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i]
return len
t=int(input())
for _ in range(t):
n=int(input())
a=[]
for i in range(n):
a.append(list(map(int,input().split())))
a.sort()
b=[0]*n
for i in range(n):
b[i]=a[i][1]
print(LongestIncreasingSubsequenceLength(b, n))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CACD2020/problems/STOCKMAX"
} | |
d619 | train | t=int(input())
for i in range(t):
n,k=list(map(int,input().split(" ")))
arr=list(map(int,input().strip().split(" ")))[:n]
def maxCircularSum(arr, n, k):
if (n < k):
print("Invalid");
return;
sum = 0;
start = 0;
end = k - 1;
for i in range(k):
sum += arr[i];
ans = sum;
for i in range(k, n + k):
sum += arr[i % n] - arr[(i - k) % n];
if (sum > ans):
ans = sum;
start = (i - k + 1) % n;
end = i % n;
print(ans);
def __starting_point():
maxCircularSum(arr, n, k);
__starting_point() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ENCD2020/problems/ECAPR206"
} | |
d620 | train | n=int(input())
for i in range(n):
l=list(map(int,input().split()))
k=l[0]+l[1]
k=k%(2*l[2])
if k>=0 and k<l[2]:
print("CHEF")
else:
print("COOK")
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CHSERVE"
} | |
d621 | train | for _ in range(int(input())):
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
def check(mid):
d,left={},0
for i in range(mid):
if a[i]>k:
if a[i] not in d:
d[a[i]]=1
else:
d[a[i]]+=1
if len(d)==1:
return True
for i in range(mid,n):
if a[left]>k:
d[a[left]]-=1
if d[a[left]]==0:
del d[a[left]]
if a[i]>k:
if a[i] not in d:
d[a[i]]=1
else:
d[a[i]]+=1
if len(d)==1:
return True
left+=1
return False
lo,hi=0,n
while lo<=hi:
mid=(lo+hi)//2
#print(mid,lo,hi)
if check(mid):
res=mid
lo=mid+1
else:
hi=mid-1
print(res)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/SLAEL"
} | |
d622 | train | t = eval(input())
for _ in range(t):
n = eval(input())
a = input().strip().split()
cb, cs = 0, ""
for i in range(len(a[0])):
for j in range(i+1,len(a[0])+1):
al = True
s = a[0][i:j]
for k in a[1:]:
if s not in k:
al = False
break
if al:
if j-i>=cb:
cb = max(cb, j-i)
if len(cs) < cb:
cs = a[0][i:j]
elif len(cs) == cb:
cs = min(cs,a[0][i:j])
print(cs) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COOK63/problems/STEM"
} | |
d623 | train | # cook your dish here folding paper
from collections import Counter
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def dist(a,b):return ((a[0]-b[0])**2+(a[1]-b[1])**2)**0.5
for _ in range(val()):
n,m,w,h=li()
s=Counter(st())
l=[]
for i in range(m):
l.append(li())
ans=float('inf')
l.sort(key=lambda x:x[0])
for j in range(1,50):
for i in range(j,m):
ans=min(ans,dist(l[i-j],l[i]))
for i in l:
if s['D'] or s['U']>1:ans=min(ans,2*i[1])
if s['U'] or s['D']>1:ans=min(ans,2*(h-i[1]))
if s['L'] or s['R']>1:ans=min(ans,2*i[0])
if s['R'] or s['L']>1:ans=min(ans,2*(w-i[0]))
print(ans) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/PAPER"
} | |
d624 | train | t = int(input())
list_to_tri = []
for i in range(t):
list_to_tri.append(int(input()))
list_to_tri.sort()
for i in list_to_tri:
print(i)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/TSORT"
} | |
d625 | train | for _ in range(eval(input())):
n=eval(input())
mod=1000000007
f1,f2=[0]*101000,[0]*101000
f1[1]=0
f1[2]=2
f1[3]=3
f2[1]=1
f2[2]=1
f2[3]=2;
for i in range(4,100001):
f1[i]=f1[i-1]%mod+f1[i-2]%mod+f1[i-3]%mod
f2[i]=f2[i-1]%mod+f2[i-2]%mod+f2[i-3]%mod
print(f1[n]%mod,f2[n]%mod) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ICOD2016/problems/ICODE16C"
} | |
d626 | train | def subCount(arr, n, k):
mod = []
for i in range(k + 1):
mod.append(0)
cumSum = 0
for i in range(n):
cumSum = cumSum + arr[i]
# as the sum can be negative,
# taking modulo twice
mod[((cumSum % k) + k) % k] = mod[((cumSum % k) + k) % k] + 1
result = 0 # Initialize result
for i in range(k):
if (mod[i] > 1):
result = result + (mod[i] * (mod[i] - 1)) // 2
result = result + mod[0]
return result
t=int(input())
while t:
t=t-1
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
if a[i]==100000000:
a[i]=1
elif a[i]==900000000:
a[i]=9
s=10
print(subCount(a,n,s))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COCA2020/problems/COCA2001"
} | |
d627 | train | t=int(input())
def reversebinary(bits,n):
bStr=''
for i in range(bits):
if n>0:
bStr=bStr+str(n%2)
else:
bStr=bStr+'0'
n=n>>1
return int(bStr,2)
for i in range(t):
k,msg=input().split()
k=int(k)
newmsg=[]
for j in msg:
newmsg.append(j)
for j in range(len(msg)):
newmsg[reversebinary(k,j)]=msg[j]
print(''.join(newmsg))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COOK02/problems/ARRANGE"
} | |
d628 | train | import math
p=7+10**9
n,k=list(map(int,input().split()))
c=math.factorial(n+k-1)//((math.factorial(k))*(math.factorial(n-1)))
print(c%p)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PCR12020/problems/BHARAT"
} | |
d629 | train | t =int(input()) #no. of test cases
while t>0:
t=t-1
str=input()
size=len(str)
pos=str.find('W')
left=pos
right=size-pos-1
arr = [[0 for i in range(right+1)] for j in range(left+1)]
#arr[i,j] = 1 if with i black cells on left and j on right 1st player can win, 0 otherwise.
#Recursion: arr[i][j]= or(arr[x][y])
arr[0][0]=0
for i in range(left+1):
for j in range(right+1):
if i==j:
arr[i][j]=0
else:
arr[i][j]=1
if(arr[left][right]==1):
print("Aleksa")
else:
print("Chef")
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/BWCELL"
} | |
d630 | train | import sys
import math
import heapq
def half(n):
return n//2
def main(arr,m):
a,b,c=arr
while m!=0:
s=max(a,b,c)
if s==a:
a=half(a)
elif s==b:
b=half(b)
else:
c=half(c)
m-=1
return max(a,b,c)
for i in range(int(input())):
r,g,b,m=list(map(int,input().split()))
arr=[]
for j in range(3):
c=max(list(map(int,input().split())))
arr.append(c)
print(main(arr,m))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/PRPOTION"
} | |
d631 | train | dt, a = None, None
def dfs(z):
r = [{}, {}];ln = len(dt[z])
if ln == 0:r[0][0] = 0;r[1][1 << a[z]] = 1
elif ln == 1:
l = dfs(dt[z][0]);r[0] = l[1]
for m in l[0]: r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
for m in l[1]: r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1
elif ln == 2:
l0 = dfs(dt[z][0]);l1 = dfs(dt[z][1])
for i0 in range(2):
for i1 in range(2):
for m0 in l0[i0]:
for m1 in l1[i1]:r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1
for m0 in l0[1]:
for m1 in l1[1]: r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]
return r
for i in range(int(input())):
n, m, k = map(int, input().split());a = [0] + [int(x) - 1 for x in input().split()];dt = [[] for i in range(n + 1)];
for i in range(m):u, v = map(int, input().split());dt[u].append(v)
r = dfs(1);k = (1 << k) - 1
if (k in r[0]): v = min(r[0][k], r[1][k])
elif (k in r[1]): v = r[1][k]
else: v = -1
print(v) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CERSOL"
} | |
d632 | train | a, b = [int(x) for x in input().split()]
r = list(str(a-b))
if r[0] == "1":
r[0] = "2"
else:
r[0]="1"
print("".join(r))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COOK17/problems/CIELAB"
} | |
d633 | train | n=int(input())
while n>0:
i=1
a,b=(int(i) for i in input().split())
if (b+1)%(i<<a)==0:
print("ON")
else:
print("OFF")
n=n-1
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/BTCD2012/problems/T05"
} | |
d634 | train | n=int(input())
def do():
t=int(input())
x=[]
for i in range(t):
x.append(int(input()))
print(max(x))
return
for i in range(n):
do() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/UWCOI20A"
} | |
d635 | train | string=input()
max_no=0
for i in range(len(string)):
var_occur=0
check_no=str()
j=i
while(j<len(string) and var_occur<2 ):
if(string[j].isalpha()):
if(var_occur==0):
check_no+='9'
var_occur+=1
else:
var_occur+=1
else:
check_no+=string[j]
j+=1
#print(check_no)
max_no=max(max_no,int(check_no))
print(max_no) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ALETHIO"
} | |
d636 | train | # cook your dish here
from collections import Counter
def solve(arr, n, k):
ans = 0
dict1 = {}
mod = 1000000007
for i in range(n):
if arr[i] in dict1:
dict1[arr[i]] += 1
else:
dict1[arr[i]] = 1
l1 = [0]+list(dict1.keys())
v = min(k, len(l1))
dp = [[0 for _ in range(v+1)]for _ in range(len(l1))]
dp[0][0] = 1
for i in range(1, len(l1)):
dp[i][0] = 1
for j in range(1, v+1):
dp[i][j] = dp[i-1][j] + dp[i-1][j-1]*dict1[l1[i]]
for i in range(v+1):
ans += dp[len(l1)-1][i]
ans = ans%mod
return ans
n, k = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
print(solve(arr, n, k)) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/GDSUB"
} | |
d637 | train | # cook your dish here
from itertools import combinations
a = list(map(int, input().split()))
n = a[0]
t = a[1]
q = list(combinations(a[2:], 4))
total = 0
for i in q:
if sum(i) == t:
total += 1
print(total)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ZCOPRAC/problems/ZCO17001"
} | |
d638 | train | import sys
def input():
return sys.stdin.readline().strip()
for i in range(int(input())):
n, k = map(int, input().split())
arr = []
if k == 2 or k == 4 or n % 2 != 0 or n == k:
arr.append('-1')
elif k % 2 != 0:
for i in range(int(n / 2)):
arr.append('(')
for i in range(int(n / 2)):
arr.append(')')
elif int(n / (k - 2)) == 1:
if (n - 2) % 4 == 0:
for i in range(int((n - 2) / 4)):
arr.append('(')
for i in range(int((n - 2) / 4)):
arr.append(')')
arr.append('()')
for i in range(int((n - 2) / 4)):
arr.append('(')
for i in range(int((n - 2) / 4)):
arr.append(')')
else:
for i in range(int((n - 4) / 4)):
arr.append('(')
for i in range(int((n - 4) / 4)):
arr.append(')')
arr.append('(())')
for i in range(int((n - 4) / 4)):
arr.append('(')
for i in range(int((n - 4) / 4)):
arr.append(')')
else:
for i in range(int((n % (k - 2)) / 2)):
arr.append('(')
for i in range(int(n / (k - 2))):
for j in range(int((k - 2) / 2)):
arr.append('(')
for j in range(int((k - 2) / 2)):
arr.append(')')
for i in range(int((n % (k - 2)) / 2)):
arr.append(')')
print("".join(arr))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/UNBAL"
} | |
d639 | train | for _ in range(int(input())):
n,m=map(int,input().split())
print("Case "+str(_+1)+":")
for i in range(m):
s=input()
ls=len(s)
if ls>n:
print("0")
else:
k=(n-ls+1)
print((k*pow(26,n-ls,1000000007))%1000000007)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ZUBAPCNT"
} | |
d640 | train | # cook your dish here
t=int(input())
for _ in range(t):
st=input()
s=set(st)
a=[]
f1=f2=0
for i in s:
a.append(st.count(i))
a.sort()
if len(a)>=3:
for i in range(2,len(a)):
if a[i]!=a[i-1]+a[i-2]:
f1=1
break
x=a[0]
a[0]=a[1]
a[1]=x
for i in range(2,len(a)):
if a[i]!=a[i-1]+a[i-2]:
f2=1
break
if f1==1 and f2==1:
print("Not")
else:
print("Dynamic")
else:
print("Dynamic") | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CLFIBD"
} | |
d641 | train | def gcd(a,b):
if b==0:return a
else:return gcd(b,a%b)
def lcm(a,b):
m=a*b
g=gcd(a,b)
return int(m/g)
for _ in range(int(input())):
x,y=[int(x) for x in input().split()]
l=lcm(x,y)
s=int(l/x)
t=int(l/y)
print(s+t-2) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PBK22020/problems/ITGUY22"
} | |
d642 | train | n=int(input())
cost=[]
d={}
val_desc=[0]*n
visited=set()
visited.add(0)
dfstack=[]
desc = [[False for i in range(n)] for i in range(n)]
for i in range(n):
cost.append(int(input()))
d[i]=[]
for i in range(n-1):
j,k=list(map(int,input().split()))
d[j-1].append(k-1)
d[k-1].append(j-1)
def dfs(u):
val_desc[u]+=cost[u]
dfstack.append(u)
for i in dfstack:
desc[u][i]=True
for i in d[u]:
if i not in visited:
visited.add(i)
dfs(i)
val_desc[u]+=val_desc[i]
dfstack.pop(-1)
dfs(0)
mp=10**9
coco=sum(cost)
for i in range(n):
for j in range(i+1,n):
vali=val_desc[i]
valj=val_desc[j]
if desc[i][j]:
valj-=val_desc[i]
if desc[j][i]:
vali-=val_desc[j]
p=max(vali,valj,coco-vali-valj)
mp=min(mp,p)
#print(desc)
#print(val_desc)
#print
print(mp)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/IARCSJUD/problems/SPLIT3"
} | |
d643 | train | # cook your dish here
def isValid(mid):
time = 0.0
for i in range(n):
if time < c[i]:
time = c[i]
time += mid # cannon cooling
elif time >= c[i] and time <= c[i] + d:
time += mid # cannon cooling
else:
return False
return True
t = int(input())
while t != 0:
n, d = list(map(int, input().split()))
c = list(map(int, input().split()))[:n]
ans = -1
c.sort()
low, high = 0, 10 ** 10
while (high - low) > 0.000001:
mid = (low + high) / 2
if isValid(mid):
ans = mid
low = mid
else:
high = mid
print("{0:.6f}".format(ans))
t -= 1
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ALIENIN"
} | |
d644 | train |
d = 10**9 + 7
t = int(input())
while t:
t-=1
n =int(input())
p =list(map(int, input().strip().split()))
a =list(map(int, input().strip().split()))
b =list(map(int, input().strip().split()))
ans = 1
for i in range(n):
c = a[i] - b[i] + 1
tmp = (( pow(p[i],b[i],d) * ((pow(p[i],c,d) - 1 + d)%d) * pow(p[i]-1 , d-2, d)%d))
ans *= tmp
ans = ans%d
print(ans) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/AARA2018/problems/ARMBH4"
} | |
d645 | train | # cook your dish here
for _ in range(int(input())):
friends = int(input())
candies = list(map(int,input().split()))
if (sum(candies) % friends == 0):
print("Yes")
else:
print("No") | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ENJU2020/problems/ECJN202"
} | |
d646 | train | t = int(input())
for _ in range(t):
n = int(input())
k = int(input())
num = int(k/n)
x = max(n*(1+num) - k, 0)
diff = abs(x - (n-x))
if diff == 0:
number = 2*x - 1
else:
number = min(x, n-x)*2
print(number) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/MMAX"
} | |
d647 | train | from sys import stdin,stdout
import math,bisect
from datetime import date
from collections import Counter,deque,defaultdict
L=lambda:list(map(int, stdin.readline().strip().split()))
M=lambda:list(map(int, stdin.readline().strip().split()))
I=lambda:int(stdin.readline().strip())
S=lambda:stdin.readline().strip()
C=lambda:stdin.readline().strip().split()
def pr(a):return("".join(list(map(str,a))))
#_________________________________________________#
def solve():
s = list(S())
a=[s[0]]
for i in range(1,len(s)):
if a and a[-1]==s[i]:
a.pop()
else:
a.append(s[i])
print(len(a))
for _ in range(I()):
solve()
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CDGO2021/problems/MINLEN"
} | |
d648 | train | """
Author : thekushalghosh
Team : CodeDiggers
"""
import sys,math
input = sys.stdin.readline
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(s[:len(s) - 1])
def invr():
return(map(int,input().split()))
################################################################
############ ---- THE ACTUAL CODE STARTS BELOW ---- ############
t = 1
t = inp()
for tt in range(t):
n,s = invr()
if n == 2 and s > 1:
print(s - 1)
elif n > 2 and s > 1:
print(0)
elif n == 1:
print(s)
else:
print(-1) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/NCC2020/problems/NCC005"
} | |
d649 | train | n,q=list(map(int,input().split()))
final=[]
height=list(map(int,input().split()))
for k in range(0,q):
b=input().split()
if int(b[0])==1:
step=int(b[1])-1
for k in range(0,int(b[2])):
temp = 0
j=1
while j in range(1,101) and temp==0 and step+j<n:
if height[step+j]>height[step]:
step=step+j
temp=1
j+=1
final.append(step+1)
elif int(b[0])==2:
for k in range(int(b[1])-1,int(b[2])):
height[k]=height[k]+int(b[3])
for l in range(0,len(final)):
print(final[l]) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/AUG17/problems/HILLJUMP"
} | |
d650 | train | def main():
for _ in range(int(input())):
rows,column = map(int,input().split())
arr = []
for i in range(rows):
arr.append(list(input()))
string = input()
last = string[-1]
operation = Find(string,last)
for i in string[0]+operation:
if i == "L":
arr = Left(arr)
if i == "R":
arr = Right(arr)
if i == "U":
arr = Transpose(arr)
arr = Left(arr)
arr = Transpose(arr)
if i == "D":
arr = Transpose(arr)
arr = Right(arr)
arr = Transpose(arr)
for i in arr:
print(i)
def Left(arr):
for i in range(len(arr)):
ans = arr[i].count("1")
arr[i] = "1"*ans + (len(arr[i]) - ans)*"0"
return arr
def Right(arr):
for i in range(len(arr)):
ans = arr[i].count("1")
arr[i] = (len(arr[i]) - ans)*"0"+"1"*ans
return arr
def Transpose(arr):
ansss = []
ans = list(map(list, zip(*arr)))
for i in ans:
ass = i
hello = ""
for j in ass:
hello += j
ansss.append(hello)
return ansss
def Find(string,last):
for i in string[-2::-1]:
if last == "L":
if i in ["D","U"]:
last = i + last
break
if last == "R":
if i in ["D","U"]:
last = i + last
break
if last == "D":
if i in ["L","R"]:
last = i + last
break
if last == "U":
if i in ["L","R"]:
last = i + last
break
return last
def __starting_point():
main()
__starting_point() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/FRCPRT"
} | |
d651 | train | import sys
# import math
from math import gcd
# import re
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(10**5)#thsis is must
# mod = 10**9+7; md = 998244353
# m = 2**32
input = lambda: sys.stdin.readline().strip()
inp = lambda: list(map(int,sys.stdin.readline().strip().split()))
# def C(n,r,mod):
# if r>n:
# return 0
# num = den = 1
# for i in range(r):
# num = (num*(n-i))%mod
# den = (den*(i+1))%mod
# return (num*pow(den,mod-2,mod))%mod
# M = 1000000+1
# pfc = [i for i in range(M+1)]
# def pfcs(M):
# for i in range(2,M+1):
# if pfc[i]==i:
# for j in range(i+i,M+1,i):
# if pfc[j]==j:
# pfc[j] = i
# return
#______________________________________________________
for _ in range(int(input())):
n,k = map(int,input().split())
d = [[] for i in range(k+1)]
for i in range(n):
l,r,p = map(int,input().split())
d[p].append([l,r])
ans = 0
for i in d:
if len(i)==0:
continue
ans+=1
t = sorted(i,key = lambda x:(x[1],x[0]))
final = t[0][1]
for j in range(1,len(t)):
if t[j][0]>=final:
ans+=1
final = t[j][1]
print(ans) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ENNO2020/problems/ENCNOV4"
} | |
d652 | train | # cook your dish here
try:
for i in range(int(input())):
n=int(input())
l=[int(j) for j in input().split()][:n]
d={}
for j in l:
d[j]=d.get(j,0)+1
a=len(d)
c=0
for j in list(d.keys()):
while(d[j]>=3):
d[j]=(d[j]//3)+(d[j]%3)
if(d[j]==2):
c=c+1
if(c&1):
s=0
for j in list(d.values()):
s=s+j
print(s-c-1)
else:
s=0
for j in list(d.values()):
s=s+j
print(s-c)
except:
pass
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/BTCH2020/problems/UNQCARD"
} | |
d653 | train | t=eval(input())
while t:
t=t-1
s1=input().lower()
s2=input().lower()
res="equal"
for i in range(len(s1)):
if(s1[i]!=s2[i]):
res="first" if s1[i]<s2[i] else "second"
break
print(res)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CDFXOQ16/problems/CDFX01"
} | |
d654 | train | def game(n,l,p):
if(len(l)==0):
return 0
l.sort()
if(len(l)>=1 and p<l[0]):
return 0
l.sort()
c=0
ma=set()
ma.add(0)
while(len(l)):
if(p>=l[0]):
p-=l[0]
c+=1
ma.add(c)
l=l[1:]
else:
if(c>0):
c-=1
ma.add(c)
p+=l[-1]
l=l[:-1]
else:
return max(ma)
return max(ma)
n=int(input())
l=list(map(int,input().split()))
p=int(input())
print(game(n,l,p)) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COFJ2020/problems/JUN1"
} | |
d655 | train | # cook your dish here
x=int(input())
for i in range(x):
s=list(map(int,input().split()))
s.sort()
print(s[1])
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/FLOW017"
} | |
d656 | train | def __starting_point():
t=int(input())
for _ in range(t):
n,k,v=map(int,input().split())
li=list(map(int,input().split()))
sumn=0
for i in range(n):
sumn=sumn+li[i]
sumk=v*(n+k)-sumn
e=int(sumk/k)
r=sumk%k
if e<=0:
print(-1)
elif r!=0:
print(-1)
else:
print(e)
__starting_point() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/AVG"
} | |
d657 | train | # cook your dish here
n = int(input())
if(n%4==0):
print(n+1)
else:
print(n-1) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/DECINC"
} | |
d658 | train | def matrixScore(A):
"""
:type A: List[List[int]]
:rtype: int
"""
m,n = len(A),len(A[0])
# 行变换
for i in range(m):
if A[i][0] == 1: continue
for j in range(n):
A[i][j] = 1 - A[i][j]
# 列变换
res = 0
for rows in zip(*A):
# 始终使1的个数是更大的
cnt1 = max(rows.count(1), rows.count(0))
res += cnt1 * 2**(n-1)
n -= 1
return res
m, n = [int(s) for s in input().split(" ")]
arr = [[int(s) for s in input().split(" ")] for i in range(m)]
ans = matrixScore(arr)
print(ans)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COFDEC20/problems/COFDQ2"
} | |
d659 | train | # cook your dish here
t=int(input())
for _ in range(t):
n=int(input())
array=list(map(int, input().split()))
list_sub=[]
idx=0
counter=0
for i in range(n-1):
if counter%2==0 and array[i]<=array[i+1]:
counter+=1
elif counter%2==1 and array[i]>=array[i+1]:
counter+=1
else:
list_sub.append((idx,i))
if counter%2==1:
idx=i
counter=1
else:
idx=i+1
counter=0
list_sub.append((idx, n-1))
massimo=0
if len(list_sub)==1:
massimo=list_sub[0][1]-list_sub[0][0]+2
for i in range(len(list_sub)-1):
if list_sub[i][1]==list_sub[i+1][0]:
massimo=max(massimo, list_sub[i][1]-list_sub[i][0]+2+list_sub[i+1][1]-list_sub[i+1][0])
else:
massimo=max(massimo, list_sub[i][1]-list_sub[i][0]+3+list_sub[i+1][1]-list_sub[i+1][0])
print(massimo) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ZCOPRAC/problems/UPDOWSEQ"
} | |
d660 | train | #binarr
def binarr(a, k, s):
a.sort(reverse=True)
arr = [0]*k
for i in range(k):
arr[i] = a[i]
if sum(arr) <= s:
return binarr(a, k+1, s)
return len(arr)
try:
n, k, s = list(map(int, input().split()))
a = list(map(int, input().split()))
print(binarr(a, k+1, s))
except Exception:
pass
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COVO2020/problems/BINARR"
} | |
d661 | train | t = int(input())
for i in range(t):
n = int(input())
if n == 1 or n == 2 or n == 145 or n == 40585:
print(1)
else:
print(0)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PBK22020/problems/ITGUY26"
} | |
d662 | train | try:
from math import sqrt
t,x=list(map(int,input().split()))
for _ in range(t):
n=int(input())
if(n<0):
print("no")
else:
diff=(x/100)*n
ans=int(sqrt(n))
ans1=ans**2
if(n-ans1<=diff):
print("yes")
else:
print("no")
except:
pass
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COMT2020/problems/ROOTSQR"
} | |
d663 | train | # cook your dish here
for t in range(int(input().strip())):
d = int(input().strip())
L, R = map(int, input().strip().split(" "))
if L % 2 == 0:
L += 1
sum = (((((R - L + 2)//2)//d)+1)//2) - 1
sum = (sum * 2 * d * (sum + 1) * d) + (sum+1) *d * (L + d -1)
print(sum%1000000007) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/HECS2020/problems/CC002"
} | |
d664 | train | def least_rotation(S: str) -> int:
"""Booth's algorithm."""
f = [-1] * len(S) # Failure function
k = 0 # Least rotation of string found so far
for j in range(1, len(S)):
sj = S[j]
i = f[j - k - 1]
while i != -1 and sj != S[k + i + 1]:
if sj < S[k + i + 1]:
k = j - i - 1
i = f[i]
if sj != S[k + i + 1]: # if sj != S[k+i+1], then i == -1
if sj < S[k]: # k+i+1 = k
k = j
f[j - k] = -1
else:
f[j - k] = i + 1
return k
for _ in range(int(input())):
l, s = input().split()
if int(l) == 1:
l = len(s)
s += s
k = least_rotation(s)
print(s[k:k+l])
else:
print(''.join(sorted(s))) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/BIT32020/problems/BIT3B"
} | |
d665 | train | # cook your dish here
MOD = 998244353
fball = [ [0]*101 for _ in range(101) ]
cric = [ [0]*101 for _ in range(101) ]
def calSNum(n, r):
if n == r or r == 1:
fball[r][n] = 1
return
if n > 0 and r > 0 and n > r:
fball[r][n] = (fball[r-1][n-1]%MOD + (r*fball[r][n-1])%MOD )%MOD
return
fball[r][n] = 0
def calASNum(n, r):
if n == 0 and r == 0 :
cric[r][n] = 0
return
if n >= 2 and r == 1:
cric[r][n] = 1
return
if r > 0 and n > 0 and n >= 2*r:
cric[r][n] = ((r*cric[r][n-1])%MOD + ((n-1)*cric[r-1][n-2])%MOD )%MOD
return
cric[r][n] = 0
def preCompute():
for r in range(1,101):
for n in range(1, 101):
calSNum(n, r)
calASNum(n, r)
def main():
preCompute()
for _ in range(int(input())):
f, c, r = list(map(int, input().split()))
ans = 0
if f + (c//2) >= r:
minv = min(f, r)
for i in range(1, minv+1):
if r-i <= c//2:
ans = (ans + (fball[i][f] * cric[r-i][c])%MOD )%MOD
print(ans)
def __starting_point():
main()
__starting_point() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/FCIPL"
} | |
d666 | train | # cook your dish here
t=int(input())
for _ in range(t):
n,m=list(map(int,input().split()))
r=list(map(int,input().split()))
rating=[[r[i]]*(m) for i in range(n)]
ranking=[[0]*m for i in range(n)]
for i in range(n):
diff=list(map(int,input().split()))
for j in range(m):
rating[i][j]+=diff[j]
if j+1<m:
rating[i][j+1]=rating[i][j]
for i in range(m):
rate=[[j,rating[j][i]] for j in range(n)]
rate=sorted(rate,key=lambda x: x[1],reverse=True)
c=1
gap=0
for j in range(n):
if j>0 and rate[j-1][1]==rate[j][1]:
gap+=1
if j>0 and rate[j-1][1]!=rate[j][1]:
c+=1+gap
gap=0
ranking[rate[j][0]][i]=c
count=0
for i in range(n):
rate=rating[i].copy()
i1=rate.index(max(rate))
rank=ranking[i].copy()
i2=rank.index(min(rank))
if i1!=i2:
count+=1
print(count)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ELOMAX"
} | |
d667 | train | # cook your dish here
t = int(input())
for _ in range(t):
s = ''
n = int(input())
if n==1:
print(1)
continue
for i in range(1, n+1):
s = s + str(i)
print(s)
p = 1
for i in range(n-1):
s = ''
for j in range(n):
s = s + str(p + n)
p = p+1
print(s)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PTRN2021/problems/ITGUY49"
} | |
d668 | train | t = int(input())
for _ in range(t):
nd = list(map(int, input().split()))
n = nd[0]
d = nd[1]
cutOff = []
x = d
buses = list(map(int, input().split()))
for i in range(len(buses)-1,-1,-1):
x = x - x%buses[i]
print(x)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COX22020/problems/CCODEX2"
} | |
d669 | train | def max_sum(arr):
# Finds the maximum sum of sub-arrays of arr
max_till_now = -1000000 #minimum possible number
current_sum = 0
for i in range(len(arr)):
if current_sum < 0:
# If sum of previous elements is negative, then ignore them. Start fresh
# with `current_sum = 0`
current_sum = 0
current_sum += arr[i]
# Update max
if max_till_now < current_sum:
max_till_now = current_sum
return max_till_now
def solve(A, k):
if k == 1:
return max_sum(A)
# Find sum of elements of A
sum_A = 0
for i in range(len(A)):
sum_A += A[i]
Max_Suffix_Sum = -1000000
current = 0
for i in range(len(A)):
current += A[-i-1]
if current > Max_Suffix_Sum:
Max_Suffix_Sum = current
Max_Prefix_Sum = -1000000
current = 0
for i in range(len(A)):
current += A[i]
if current > Max_Prefix_Sum:
Max_Prefix_Sum = current
if sum_A <= 0:
# Check two cases:
# Case 1 : Check the max_sum of A
case_1_max_sum = max_sum(A)
# Case 2 : Check the max_sum of A + A
case_2_max_sum = Max_Suffix_Sum + Max_Prefix_Sum
# Return the maximum of the two cases
return max([case_1_max_sum, case_2_max_sum])
else: # if sum_A > 0
#Check two cases:
# Case 1 : Check the max_sum of A
case_1_max_sum = max_sum(A)
# Case 2
# Max sum = Max_Suffix_Sum + (k - 2)*sum_A + Max_Prefix_Sum
case_2_max_sum = Max_Suffix_Sum + (k - 2)*sum_A + Max_Prefix_Sum
# Return the maximum of the two cases
return max([case_1_max_sum, case_2_max_sum])
# Main
T = int(input()) # No of test cases
for i in range(T):
[N, k] = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
answer = solve(A,k)
print(answer) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/KCON"
} | |
d670 | train | T = int(input())
for _ in range(T):
N, M, K = [int(x) for x in input().split()]
UV = [[int(x) for x in input().split()] for _ in range(M)]
Q = int(input())
AB = [[int(x) for x in input().split()] for _ in range(Q)]
X = [[i] for i in range(N)]
for u, v in UV:
X[u - 1] += [v - 1]
X[v - 1] += [u - 1]
A = [[1 if i > 0 or j == 0 else 0 for j in range(N)] for i in range(K + 1)]
for a, b in AB:
A[b] = [1 if i == a - 1 else 0 for i in range(N)]
if A[0][0] == 1:
for k in range(K - 1, -1, -1):
for i in range(N):
if A[k][i] != 0:
A[k][i] = sum(A[k + 1][j] for j in X[i])
print(A[0][0]) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/WNDR"
} | |
d671 | train | def gcd(a,b):
if b==0:
return a
else:
return gcd(b,a%b)
def main():
t=int(input())
while t!=0:
t=t-1
n=int(input())
if n==1:
print(input())
else:
a=list(map(int,input().split(" ")))
p=a[0]
for i in range(1,n):
p=gcd(p,a[i])
if p==1:
break
print(n*p)
def __starting_point():
main()
__starting_point() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COOK64/problems/SEAARASU"
} | |
d672 | train | for i in range(int(input())):
n,s =map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
m=[]
n=[]
for i in range(len(l1)):
if l2[i]==0:
m.append(l1[i])
else:
n.append(l1[i])
if len(m)>0 and len(n)>0:
if 100-s>=(min(m)+min(n)):
print("yes")
else:
print("no")
else:
print("no") | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/FFL"
} | |
d673 | train | # cook your dish here
from math import sqrt
for i in range(int(input())):
x1,y1,x2,y2=list(map(float,input().split()))
m=(y2-y1)/(x2-x1)
c=y2-m*x2
print('Test case : ',i+1)
q=int(input())
for i in range(q):
x3,y3=list(map(float,input().split()))
if(y3-m*x3-c==0):
print("YES")
else:
d=(abs(y3-m*x3-c))/sqrt(1+m*m)
print("NO")
print("%.6f" % d)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ABCC2020/problems/POINT"
} | |
d674 | train | from math import gcd
from math import ceil
from itertools import combinations as c
t=int(input())
for _ in range(t):
n,m,a,d=list(map(int,input().split()))
l=[]
for i in range(5):
l.append(a+i*d)
ans=m-n+1
for i in range(1,6):
x=list(c(l,i))
for j in x:
e=j[0]
for v in j:
e=(e*v)//gcd(e,v)
#print(e)
if i%2:
ans-=m//e-(n-1)//e
else:
ans+=m//e-(n-1)//e
print(ans)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/NQST2020/problems/XMASGIFT"
} | |
d675 | train | import numpy as np
for _ in range(int(input())):
ans = np.float('inf')
n, m = (int(x) for x in input().split())
sig = np.zeros((n,m))
img = np.zeros((3*n,3*m))
for row in range(n):
sig[row,:] = np.array([int(x) for x in input()])
for row in range(n):
img[row+n,m:2*m] = np.array([int(x) for x in input()])
for i in range(2*n):
for j in range(2*m):
ans = min(ans, np.abs(np.sum(img[i:n+i, j:m+j] != sig)))
print(ans) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/SIGNTURE"
} | |
d676 | train | import math
def ispoweroftwo(y):
return math.ceil(math.log(y,2))==math.floor(math.log(y,2))
t=int(input())
for i in range(t):
n=int(input())
a=[]
if(ispoweroftwo(n) and n!=1):
print(-1,end=" ")
if(n==1):
print(1)
if(n>=3 and not(ispoweroftwo(n))):
a.append(2)
a.append(3)
a.append(1)
if(n>3 and not ispoweroftwo(n)):
i=4
while(i<=n):
if(ispoweroftwo(i)):
a.append(i+1)
a.append(i)
i+=2
else:
a.append(i)
i+=1
print(*a)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/POSAND"
} | |
d677 | train | from collections import Counter
for _ in range(int(input())):
n=int(input())
l=[i for i in input().split()]
ll=[]
c=Counter(l)
cc=[]
m=0
for l,count in c.most_common(len(l)-1):
if m==0:
ll.append(l)
cc.append(count)
if m==count:
ll.append(l)
cc.append(count)
if count<m:
break
m=count
k=set(cc)
leng=len(list(k))
if leng==1:
sor=sorted(ll)
print(sor[0])
else:
print(ll[0])
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/DCC2020/problems/DCC202"
} | |
d678 | train | import sys
import datetime
a,b,c = list(map(int,sys.stdin.readline().split()))
d = datetime.date(c,b,a)
print(d.strftime("%A")) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/TCTR2012/problems/NOPC10"
} | |
d679 | train | # cook your dish here
test_case = int(input())
while test_case :
n_people = int(input())
array = list(map(int, input().strip().split()))
sums =[0 for i in range(n_people)]
sums[0] = array[0]
for i in range(1, n_people) :
sums[i] = sums[i-1] + array[i]
# print(sums)
k = 1
count = 0
i = 0
while(k < n_people) :
k = k + sums[i]
# print(k)
i = i + sums[i]
count = count + 1
print(count)
test_case -= 1
# 2 1 1 5 5 5 5
# [2, 3, 4, 9, 14, 19, 24]
# 0 1 2 3 4 5 6
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/SPREAD2"
} | |
d680 | train | t=eval(input())
a=[]
b=[]
top=-1
for __ in range(0,t):
x=input().split()
if(x[0]!="-1" and x[0]!="0"):
add=int(x[0])
if top!=-1 and add>a[top][0] :
b[top]+=1
else:
a.append((add,x[1]))
b.append(0)
top+=1
elif (x[0]=="-1"):
#print("%s %s" %(b[top],a[top][1]))
print((b[top]), end=' ')
print(a[top][1])
foo=a.pop()
bar=b.pop()
top-=1 | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/DEC12/problems/BEX"
} | |
d681 | train | t = int(input())
l,r,x = 0,0,0
ans = []
for i in range(t):
(n,m) = tuple(map(int,input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
suma = sum(a)
sumb = sum(b)
q = int(input())
for j in range(q):
l1 = list(map(int,input().split()))
if l1[0] == 1:
l = l1[1]
r = l1[2]
x = l1[3]
suma = suma + (r-l+1)*x
elif l1[0] == 2:
l = l1[1]
r = l1[2]
x = l1[3]
sumb = sumb + (r-l+1)*x
else:
ans.append((suma*sumb)%998244353)
for i in range(len(ans)):
print(ans[i]) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ARRQRY"
} | |
d682 | train |
from math import ceil
from bisect import bisect_right as b_r
from bisect import bisect_left as b_l
ar = list(map(int , input().split()))
a = [int(ceil((ar[1]-int(x)+1)/ar[2])) for x in input().split()]
s = sum(a)
ar[1] = max(a)
m = ar[1] - (s-ar[1])%2
mi = s%2
print(int( (m-mi)//2 +1)%(10**9+7))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/RRGAME"
} | |
d683 | train | n=int(input())
a=list(map(int,input().split()))
l,r=-1,-1
for i in range(n):
if a[i]!=i+1:
l=i
break
for i in range(n-1,-1,-1):
if a[i]!=i+1:
r=i
break
j=r+1
for i in range(l,r+1):
if a[i]==j:
j-=1
continue
else:
print(0,0)
return
print(l+1,r+1) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/BRBG2020/problems/PRMA"
} | |
d684 | train | # cook your dish here
a = int(input())
print(a) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/START01"
} | |
d685 | train | # cook your dish here
import math
# Function to find the Largest
# Odd Divisor Game to check
# which player wins
def findWinner(n, k):
cnt = 0;
# Check if n == 1 then
# player 2 will win
if (n == 1):
print("Grinch");
# Check if n == 2 or n is odd
elif ((n & 1) or n == 2):
print("Me");
else:
tmp = n;
val = 1;
# While n is greater than k and
# divisible by 2 keep
# incrementing tha val
while (tmp > k and tmp % 2 == 0):
tmp //= 2;
val *= 2;
# Loop to find greatest
# odd divisor
for i in range(3, int(math.sqrt(tmp)) + 1):
while (tmp % i == 0):
cnt += 1;
tmp //= i;
if (tmp > 1):
cnt += 1;
# Check if n is a power of 2
if (val == n):
print("Grinch");
elif (n / tmp == 2 and cnt == 1):
print("Grinch");
# Check if cnt is not one
# then player 1 wins
else:
print("Me");
# Driver code
def __starting_point():
for i in range(int(input())):
n=int(input())
findWinner(n, 1);
__starting_point() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/NQST2020/problems/WINALL"
} | |
d686 | train | # https://www.codechef.com/problems/RECTLIT
def assess(sq,points):
EWct = 0
NSct = 0
for a,b in points:
EW = (a == 0 or a == sq)
NS = (b == 0 or b == sq)
if EW and NS:
return 'yes'
EWct += EW
NSct += NS
if NSct + EWct == 0 or len(points) == 1:
return 'no'
if EWct >= 2 or NSct >= 2:
return 'yes'
if len(points) == 2:
return 'no'
# now 3 points
if NSct == 1 and EWct == 1:
return 'yes'
# 3 points, one on edge
x = -1
for a,b in points:
if EWct > 0:
if a == 0 or a == sq:
e = b
elif x == -1:
x = b
else:
y = b
else:
if b == 0 or b == sq:
e = a
elif x == -1:
x = a
else:
y = a
if (e-x)*(e-y) < 0: # edge splits mids
return 'no'
else:
return 'yes'
for ti in range(int(input())):
k,n = map(int, input().split())
if k > 3:
for ki in range(k):
input()
print('yes')
else:
pos = [tuple(map(int, input().split())) for ki in range(k)]
print(assess(n-1,pos))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/RECTLIT"
} | |
d687 | train | n=int(input())
l=[]
for i in range(0,n):
a,b,c=map(int,input().split())
n1=(2**0.5)*(a/b)
n2=2*(a/c)
if n1>n2:
l.append("Elevator")
else:
l.append("Stairs")
for i in l:
print(i) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ELEVSTRS"
} | |
d688 | train | from sys import stdin
t = int(stdin.readline())
def count(n, arr):
loc = 0
glob = 0
for i in range(n-1):
if arr[i] > arr[i+1]:
loc += 1
for i in range(n-1):
for j in range(i+1, n):
if glob > loc:
return 0
if arr[i] > arr[j]:
glob += 1;
if glob == loc:
return 1
return 0
for _ in range(t):
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
result = count(n, arr)
if result:
print("YES")
else:
print("NO") | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COOK28/problems/LEPERMUT"
} | |
d689 | train | n = int(input())
# this code only for 8 bits string and it isn't possible to more than 8 bits of string
# for i in range(n):
# s = input()
# subString1, subString2 = s[:4], s[4:]
# rev = subString2[::-1]
# print( 'uniform' if(subString1 == rev) else 'non-uniform')
for i in range(n):
count = 0
s = input()
for i in range(1, len(s)):
if(s[i-1] != s[i]):
count += 1
if(s[0] != s[-1]):
count += 1
print("uniform" if(count <=2 ) else "non-uniform")
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/STRLBP"
} | |
d690 | train | # cook your dish here
t=int(input())
i=0
a=0
d=dict()
while i<t:
l=input().split()
d[int(l[0])]=int(l[0])+int(l[1])
i+=1
for k in d:
if d[k] in d:
if d[d[k]]==k:
a=1
break
if a==1:
print("YES")
else:
print("NO") | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CMR12121/problems/ZBJNG"
} | |
d691 | train | n,k,m = map(int,input().split())
ar = list(map(int,input().split()))
fsum = [ar[0]]
for i in range(1,n):
fsum.append(fsum[i-1]+ar[i])
i = k
#print(fsum)
c = 0
while i <= n:
if i == k:
s = fsum[i-1]
else:
s = fsum[i-1]-fsum[i-k-1]
if s == 0:
c = -1
break
if s < m:
c += 1
if i<n:
for j in range(i,i-k-1,-1):
if ar[j-1] >0:
j += k-1
i = j
break
if i<n:
for j in range(i,i-k-1,-1):
if ar[j-1] >0:
j += k-1
i = j
break
i += 1
i = k
while i <= n:
if i==k:
s = fsum[i-1]
else:
s = fsum[i-1] - fsum[i-k-1]
if s == 0 :
c = -1
break
i += 1
print(c) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/INTW2020/problems/MORALE99"
} | |
d692 | train | T = int(input())
for _ in range(T):
n = int(input())
arr = list(map(int, input().split()))
a = [0 for _ in range(max(arr)+1)]
star_val = []
for i in range(len(arr)):
j = 1
val = 0
while j*arr[i] <= len(a):
val += a[j*arr[i]-1]
j += 1
star_val.append(val)
a[arr[i]-1] += 1
print(max(star_val)) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/MSV"
} | |
d693 | train | VQ = "UAMmSs"
n = int(input())
a = list(map(int, input().split()))
for _ in range(int(input())):
q, x, y = input().split()
if q not in VQ:
print("!!!")
continue
if q == "U":
a[int(x) - 1] = int(y)
continue
l = int(x) - 1
r = int(y)
if q == "A":
print(sum(a[l:r]))
continue
if q == "M":
print(max(a[l:r]))
continue
if q == "m":
print(min(a[l:r]))
continue
s = sorted(set(a[l:r]))
if len(s) < 2:
print("NA")
else:
print(s[1] if q == "s" else s[-2])
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CRES2016/problems/CRES104"
} | |
d694 | train | # cook your dish here
x=int(input())
for i in range(x):
s=int(input())
fact=1
for i in range(1,s+1):
fact=fact*i
print(fact)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/FLOW018"
} | |
d695 | train | import math
def fun(num1,num2):
if num1>num2:
a=num1
b=num2
else:
a=num2
b=num1
rem=a%b
while(rem!=0):
a=b
b=rem
rem=a%b
gcd=b
return (int((num1*num2)/gcd))
for _ in range (int(input())):
hours=int(input())*24
x,y,z=list(map(int,input().split()))
lcm=x
lcm=fun(x,y)
lcm=fun(lcm,z)
print(int(hours//lcm))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/STRT2020/problems/CRWDCN"
} | |
d696 | train | # cook your dish here
tc=int(input())
for j in range(tc):
ip=list(map(int,input().rstrip().split()))
x=ip[0]
y=ip[1]
n=ip[2]
cnt=0
if(x==y):
print('0')
continue
ln=bin(x).replace("0b", "")
rn=bin(y).replace("0b", "")
ll=len(ln)
rl=len(rn)
#print(ln)
#print(rn)
if(ll==len(rn)):
for i in range(ll):
if(ln[i]!=rn[i]):
ln=ln[i:]
rn=rn[i:]
break
#print(ln)
if(ln[0]=='0'):
ln=ln[1:]
ll-=1
#print(rn)
if(rn[0]=='0'):
rn=rn[1:]
rl-=1
ll=len(ln)
rl=len(rn)
if(ll>rl):
lb=ll
else:
lb=rl
pl=2**lb
hpl=pl//2
amn=((n+1)//pl)*hpl
rm=(n+1)%pl
if((rm*2)<=pl):
amn+=rm
else:
amn+=hpl
#print("amn = ",amn)
aln=(n+1)-amn
#print("aln = ",aln)
if(x<y):
print(amn)
else:
print(aln) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/XORCOMP"
} | |
d697 | train | t=int(input())
for _ in range(t):
n,m=list(map(int,input().split()))
l=list(map(int,input().split()))
k=[]
for i in range(m):
a,b=list(map(int,input().split()))
k.append([a,b])
k.sort()
c=[]
flag=1
x=k[0][0]
y=k[0][1]
for i in k[1:]:
if i[0]<=y:
y=max(y,i[1])
else:
c.append([x-1,y-1])
x=i[0]
y=i[1]
c.append([x-1,y-1])
m=[]
j=0
for i in c:
while j<i[0]:
m.append(l[j])
j+=1
x=l[i[0]:i[1]+1]
m+=sorted(x)
j=i[1]+1
while j<n:
m.append(l[j])
j+=1
if m==sorted(l):
print('Possible')
else:
print('Impossible')
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/PERMSUFF"
} | |
d698 | train | # cook your dish here
def func(arr, k):
sumi = 0
for j in range(k):
sumi += arr[j]
maxi = sumi
for i in range(k,len(arr)):
sumi -= arr[i - k]
sumi += arr[i]
maxi = max(maxi,sumi)
return maxi
for _ in range(int(input())):
n, k = map(int,input().split())
arr = [int(x) for x in input().split()]
print(func(arr,k)) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ARYS2020/problems/CSHIP"
} | |
d699 | train | import math
t = int(input())
def phi(n):
res = n
i = 2
while i*i<=n:
if n%i==0:
res/=i
res*=(i-1)
while n%i==0:
n/=i
i+=1
if n>1:
res/=n
res*=(n-1)
return int(res)
while t:
a,m = list(map(int,input().split()))
g = math.gcd(a,m)
print(phi(m//g))
t-=1
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/NQST2020/problems/HEIST101"
} | |
d700 | train | for T in range(int (eval(input()))):
N,K,D=list(map(int,input().split()))
A=list(map(int,input().split()))
P=sum(A)//K
print(min(P,D))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/DIVTHREE"
} |