_id
stringlengths
2
5
partition
stringclasses
2 values
text
stringlengths
5
289k
language
stringclasses
1 value
meta_information
dict
title
stringclasses
1 value
d1001
train
# cook your dish here from math import ceil for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) ans=ceil(n/min(a)) print(int(ans))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ECPG2020/problems/ECPC10B" }
d1002
train
for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) g=1 for j in range(1,n): if j-5<0: mi=min(a[0:j]) #print(a[0:j]) if mi>a[j]: g=g+1 else: mi=min(a[j-5:j]) #print(a[j-5:j]) if mi>a[j]: g=g+1 print(g)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/S10E" }
d1003
train
# cook your dish here def solution(b,n1,d): first=b[0] b.sort() for j in range(n1-1): if(a[j+1]-a[j]>d): return "NO" for j in range(n1): if(b[j]==first): pos=j if(pos==0 or pos==n1-1): return "YES" rec=1 for j in range(pos-1,n1-2): if(a[j+2]-a[j]>d): rec=0 break if(rec): return "YES" rec=1 for j in range(pos+1,1,-1): if(a[j]-a[j-2]>d): rec=0 break if(rec): return "YES" else: return "NO" testcase=int(input()) for i in range(testcase): n,d=list(map(int,input().split())) a=list(map(int,input().split())) print(solution(a,n,d))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/TRVLCHEF" }
d1004
train
t=eval(input()) for i in range(0,t): x=input() nm=x.split(' ') nm[0]=int(nm[0]) nm[1]=int(nm[1]) csoint=[] lsoint=[] csofloat=[] lsofloat=[] for j in range(0,nm[0]): a=input() b=a.split(' ') b[0]=int(b[0]) b[1]=int(b[1]) csoint.append(b[0]) lsoint.append(b[1]) for k in range(0,nm[1]): a=input() b=a.split(' ') b[0]=int(b[0]) b[1]=int(b[1]) csofloat.append(b[0]) lsofloat.append(b[1]) chakra=0 ## for j in range(0,nm[0]): ## if csofloat==[0]*nm[1]: ## print chakra ## break ## for k in range(0,nm[1]): ## if csofloat==[0]*nm[1]: ## print chakra ## break ## if lsoint[j]==lsofloat[k]: ## if csoint[j]>csofloat[k]: ## csoint[j]-=csofloat[k] ## csofloat[k]=0 ## lsofloat[k]=0 ## elif csofloat[k]>csoint[j]: ## if lsoint.count(lsoint[j])==1==lsofloat.count(lsofloat[k]): ## chakra+=csofloat[k]-csoint[j] ## csoint[j]=csofloat[k] ## csoint[j]=0 ## csofloat[k]=0 ## lsoint[j]=0 ## lsofloat[k]=0 ## elif lsoint.count(lsoint[j])==1 and lsofloat.count(lsofloat[k])>lsoint.count(lsoint[j]): ## csoint[j]=csofloat[k]+1 ## chakra+=csofloat[k]-csoint[j]+1 ## csoint[j]=1 ## csofloat[k]=0 ## lsofloat[k]=0 ## else: ## csofloat[k]-=csoint[j] ## csoint[j]=0 ## lsoint[j]=0 ## break ## else: ## if lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])>=lsofloat.count(lsofloat[k]): ## csoint[j]=0 ## csofloat[k]=0 ## lsoint[j]=0 ## lsofloat[k]=0 ## break ## elif lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])<lsofloat.count(lsofloat[k]): ## csoint[j]=1 ## chakra+=1 ## csofloat[k]=0 ## lsofloat[k]=0 l=min(lsoint)-1 ci=[] for a in range(l+1,max(lsoint)+1): c=0 l+=1 if l not in lsoint: continue for j in range(0,nm[0]): if lsoint[j]==l: c+=csoint[j] ci.append(c) l=min(lsofloat)-1 cf=[] for a in range(l+1,max(lsofloat)+1): c=0 l+=1 if l not in lsofloat: continue for j in range(0,nm[1]): if lsofloat[j]==l: c+=csofloat[j] cf.append(c) for i in range(0,len(ci)): if ci[i]<cf[i]: chakra+=cf[i]-ci[i] print(chakra)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/OCT12/problems/DRGNBOOL" }
d1005
train
a=int(input()) if(a%2==0): print("4") print(a/2,a/2,a/2,a/2) else: print("6") print((a-1)/2,(a-1)/2,(a-1)/2,(a-1)/2,(a-1)/2,(a+1)/2)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/SOPC2015/problems/SOPC1505" }
d1006
train
MAXX = 10**9+1 N = eval(input()) nodes = list(map(int, input().split(" "))) edges = [set() for _ in range(N)] for _ in range(N-1): a, b = list(map(int, input().split(" "))) edges[a-1].add(b-1) edges[b-1].add(a-1) path = [[] for _ in range(N)] visited, tovisit = set(), [(0, 0)] while tovisit: p, v = tovisit.pop() if v not in visited: path[v] = path[p] + [v] visited.add(v) news = edges[v] - visited tovisit.extend([(v, x) for x in news]) # print path Q = eval(input()) for _ in range(Q): q, a, b = input().split(" ") a, b = int(a)-1, int(b)-1 i = 1 while i < min(len(path[a]), len(path[b])): if path[a][i] != path[b][i]: break i += 1 s = path[a][i-1:] + path[b][i:] # print s if q == "C": s = sorted([nodes[i] for i in s]) d = s[-1] - s[0] for i in range(len(s)-1): d = min(d, s[i+1]-s[i]) print(d) else: M = 0 m = MAXX for i in range(len(s)): M = max(M, nodes[s[i]]) m = min(m, nodes[s[i]]) print(M - m) # print M[(s, l)] - m[(s, l)]
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/LTIME38/problems/CLOSEFAR" }
d1007
train
for _ in range(int(input())): n,d=map(str,input().split()) k=list(n) dd,c,n=d,0,len(n) for x in range(n): if int(k[n-x-1])>int(d): k.pop(n-x-1) c+=1 else: d=k[n-x-1] print(''.join(k)+c*dd)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CHDIGER" }
d1008
train
import math for _ in range(int(input())): n=int(input()) ar=[int(x) for x in input().split()] # dp=[1]*n f=0 g=ar[0] for i in range(1,n): g=math.gcd(g,ar[i]) if g==1: f=1 print(n) break if f==0: print(-1)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/SUBGCD" }
d1009
train
t=int(input()) for k in range(t): n=int(input()) l=[int(i) for i in input().split()] m={} count=1 for i in range(1,n): if l[i]==l[i-1]: count+=1 else: if l[i-1] not in m: m[l[i-1]]=(count*(count+1))/2 else: m[l[i-1]]+=(count*(count+1))/2 count=1 if(l[n-1]) not in m: m[l[n-1]]=(count*(count+1))/2 else: m[l[n-1]]+=(count*(count+1))/2 s=1 for x in m: s=(s*m[x])%(1000000007) print(s)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/MXSUBARR" }
d1010
train
# cook your dish here from sys import stdin import functools def gcd(a, b): if (a == 0): return b return gcd(b % a, a) MAX=10001 def func(ind, g, dp, n, a): if (ind == n): if (g == 1): return 1 else: return 0 if (dp[ind][g] != -1): return dp[ind][g] ans = (func(ind + 1, g, dp, n, a) + func(ind + 1, gcd(a[ind], g), dp, n, a)) dp[ind][g] = ans return dp[ind][g] def countSubsequences(a, n): dp = [[-1 for i in range(MAX)] for i in range(n)] count = 0 for i in range(n): count += func(i + 1, a[i], dp, n, a) return count test=int(stdin.readline()) for _ in range(test): n=int(stdin.readline()) list1=list(map(int,stdin.readline().split())) print(countSubsequences(list1,n))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/AMSGAME2" }
d1011
train
#code snippet reference:http://www.geeksforgeeks.org/searching-for-patterns-set    -2-kmp-algorithm/ def KMPMatch(pattern, string): M = len(pattern) N = len(string) nonlocal ans lps = [0]*M j = 0 LPSCompute(pattern, M, lps) i = 0 while i < N: if pattern[j] == string[i]: i+=1 j+=1 if j==M: ans+=1 j = lps[j-1] elif i < N and pattern[j] != string[i]: if j != 0: j = lps[j-1] else: i+=1 def LPSCompute(pattern, M, lps): len = 0 lps[0] i = 1 while i < M: if pattern[i]==pattern[len]: len+=1 lps[i] = len i+=1 else: if len!=0: len = lps[len-1] else: lps[i] = 0 i+=1 for _ in range(int(input())): n,k=list(map(int,input().split())) s=list(map(int,input().split())) pattern=list(map(int,input().split())) ans=0 string=[] for i in range(n-1): string.append(s[i+1]-s[i]) KMPMatch(pattern, string) print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/LOCAPR16/problems/RUNNING" }
d1012
train
t=int(input()) def do(): n,k=map(int,input().split()) s=input() upper=0 lower=0 for i in s: if i.isupper(): upper+=1 else: lower+=1 if lower>k and upper<=k: print('chef') elif(upper>k and lower<=k): print('brother') elif(upper<=k and lower<=k): print('both') else: print('none') return for i in range(t): do()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/GOODBAD" }
d1013
train
import sys from collections import defaultdict from copy import copy MOD = 10**9 + 7 R = lambda t = int: t(input()) RL = lambda t = int: [t(x) for x in input().split()] RLL = lambda n, t = int: [RL(t) for _ in range(n)] # primes up to n def primes(n): P = [] n = int(n) U = [1] * (n+1) p = 2 while p <= n: if U[p]: P += [p] x = p while x <= n: U[x] = 0 x += p p += 1 return P def solve(): S = R(str).strip() X = [ord(c)-ord('a') for c in S] P = primes(10000) L = defaultdict(lambda : 0) s = 0 for i in range(len(S)): p = i in P NL = defaultdict(lambda : 0) for a in range(26): for l in L: NL[l + a * p] += L[l] for a in range(X[i]): NL[s + a * p] += 1 s += X[i] * p L = NL L[s] += 1 r = 0 for p in P: r += L[p] print(r % MOD) T = R() for t in range(1, T + 1): solve()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ENJU2020/problems/ECJN207" }
d1014
train
# cook your dish here from collections import defaultdict d=defaultdict(list) def dfs(i): p=0 nonlocal v e=[i] while(e!=[]): p+=1 x=e.pop(0) v[x]=1 for i in d[x]: if v[i]==-1: v[i]=1 e.append(i) return p n,m=list(map(int,input().split())) for i in range(n+1): d[i]=[] for _ in range(m): a,b=list(map(int,input().split())) d[a].append(b) d[b].append(a) v=[] for i in range(n+1): v.append(-1) c=0 p=[] for i in range(1,n+1): if v[i]==-1: c+=1 p.append(dfs(i)) an=0 s=0 for i in range(c): s+=p[i] an+=p[i]*(n-s) print(an)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/INDC2021/problems/NAV28" }
d1015
train
from math import log2, ceil MOD = int(1e9 + 7) srt = lambda s: ''.join(sorted(s)) for _ in range(int(input())): s = srt(input()) res = -1 for p in range(ceil(log2(int(s))), int(log2(int(s[::-1]))) + 1): if int(srt(str(pow(2, p)))) == int(s): if res == -1: res = 0 res = (res + pow(2, p, MOD)) % MOD print(res)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ALQU2018/problems/POWTWO" }
d1016
train
# cook your dish here try: for _ in range(int(input())): k = int(input()) num = 1 for i in range(1,k+1,1): for j in range(1,k+1,1): print(num*2,end="") num = num +1 print("") except: pass
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/PTRN2021/problems/ITGUY50" }
d1017
train
# cook your dish here for t in range(int(input())): c=0 for i in range(int(input())): s,j=list(map(int,input().split())) if (j-s)>5: c+=1 print(c)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/JDELAY" }
d1018
train
# cook your dish here for t in range(int(input())): a1,a2,a3,a4,a5,p=[int(x)for x in input().rstrip().split()] if (a1+a2+a3+a4+a5)*p >120: print("Yes") else: print("No")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/LOSTWKND" }
d1019
train
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) hrs = arr[0] - arr[1] for i in range(1, n-1): if hrs > arr[i] - arr[i+1]: hrs = arr[i] - arr[i+1] print(hrs)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/INFTINCR" }
d1020
train
# cook your dish here for i in range(int(input())): N=int(input()) L=list(map(int,input().split())) l,h=0,N-1 flag=1 if L[l]!=1 and L[h]!=1: flag=0 else: while(l<h): if (L[l]!=L[h]) or (L[l+1]-L[l]!=1 and L[h-1]-L[h]!=1): flag=0 break l+=1 h-=1 if flag: print("yes") else: print("no")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/TEMPLELA" }
d1021
train
# cook your dish here for _ in range(int(input())): n,k=list(map(int,input().split())) a=list(map(int,input().split())) m=0 for i in range(n): if i%2==0: if m<0: m-=a[i] else: m+=a[i] else: if m<0: m+=a[i] else: m-=a[i] if abs(m)>=k: print(1) else: print(2)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/MIXGA" }
d1022
train
class Node: def __init__(self,x): self.x=x self.next=None self.prev=None self.flag=True for t in range(1): n=int(input()) arr=list(map(int,input().split())) for i in range(n): arr[i]=Node(arr[i]) for i in arr: d=[i.x%3==0,i.x,i.x//3,i.x*2] if d[0]: for j in arr: if j.x==d[2]: i.next=j j.prev=i break else: for j in arr: if j.x == d[3]: i.next = j j.prev = i break else: for j in arr: if j.x==d[3]: i.next=j j.prev=i break f,l=None,None for i in arr: if i.prev==None: f=i elif i.next==None: l=i while f!=l and l!=None: print(f.x,end=" ") f=f.next print(f.x)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/BRBG2020/problems/THTOG" }
d1023
train
# cook your dish here # cook your dish here import math test=int(input()) for _ in range(test): n=int(input()) l=list(map(int,input().split())) f=0 for i in range(math.ceil(n//2)): if n%2==1: f=1 break else: if l[i]!=l[i+n//2]: if min(l[i],l[i+n//2])==-1: l[i]=max(l[i],l[i+n//2]) l[i+n//2]=max(l[i],l[i+n//2]) else: f=1 break else: if l[i]==-1: l[i]=1 l[i+n//2]=1 if f==1: print("NO") else: print("YES") print(*l)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/OPPOSITE" }
d1024
train
# cook your dish here for _ in range(int(input())): n = int(input()) count = 1 l = 3*(n-1) i = 0 if n==1: print(1) continue while count<=l-n: for j in range(i+1): if j==i: print(count) count += 1 elif j==0: print(count,end="") count += 1 else: print(" ",end="") i+=1 while count<=l: print(count,end="") count += 1 print()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/PSTR2020/problems/ITGUY05" }
d1025
train
# cook your dish here extra, less = 0,0 for _ in range(int(input())): sli,mem,sma,luc = list(map(int, input().split())) total = sma t = sma while mem > 1: t *= luc total += t mem -= 1 if total <= sli: extra += sli-total print('POSSIBLE',sli-total) else: less += total-sli print('IMPOSSIBLE',total-sli) if extra >= less: print('POSSIBLE') else: print('IMPOSSIBLE')
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/PCO12020/problems/MORECAKE" }
d1026
train
from collections import defaultdict as dd,deque as dq def opbfs(u,vis,ll,parr): q=dq([(u,0)]) uu=u su=0 while q: u,lol=q.pop() par=parr[u] if(lol%2==0): vis[u]=1 su+=ll[u-1] ll[u-1]=0 for j in d[u]: if(j!=par): q.appendleft((j,lol+1)) ll[uu-1]=su def bfs(height,d,parr): q=dq([1]) while q: u=q.pop() height[u]=height[parr[u]]+1 for i in d[u]: if(i!=parr[u]): q.appendleft(i) parr[i]=u t=int(input()) while t: n,q=map(int,input().split()) ll=list(map(int,input().split())) d=dd(list) for i in range(n-1): u,v=map(int,input().split()) d[u].append(v) d[v].append(u) vis=[0]*(n+1) l=[] height=[0]*(n+1) parr=[0]*(n+1) bfs(height,d,parr) for i in range(q): u=int(input()) l.append((height[u],u,i)) l.sort() vis=[0]*(n+1) #print(l) for i in l: he,u,ind=i if(vis[u]==0): #print(u) opbfs(u,vis,ll,parr) print(*ll) t-=1
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CENS20F" }
d1027
train
d=1000000007 for _ in range(int(input())): l=sorted(list(map(int,input().split()))) ans=(l[0]%d)*((l[1]-1)%d)*((l[2]-2)%d) print(ans%d)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/THREEDIF" }
d1028
train
T=int(input()) for i in range(T): n,m=list(map(int,input().split())) if(m<=2): print("impossible") else: l=[0]*m if(m%2==0): a=m//2 else: a=(m//2)+1 for j in range(a): if(j%2==0): l[j]="a" l[m-j-1]="a" else: l[j]="b" l[m-j-1]="b" r="" s=n//m for e in l: r=r+e print(r*s)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PERPALIN" }
d1029
train
def power(x, y): if y == 0: return 1 if y % 2 == 0: return power(x, y // 2) * power(x, y // 2) return x * power(x, y // 2) * power(x, y // 2) # Function to calculate order of the number def order(x): # Variable to store of the number n = 0 while (x != 0): n = n + 1 x = x // 10 return n # Function to check whether the given # number is Armstrong number or not def isArmstrong(x): n = order(x) temp = x sum1 = 0 while (temp != 0): r = temp % 10 sum1 = sum1 + power(r, n) temp = temp // 10 # If condition satisfies return (sum1 == x) # Driver code for _ in range(int(input())): num = int(input()) if isArmstrong(num): print("FEELS GOOD") else: print("FEELS BAD")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/SPTC2020/problems/CPCEJC5" }
d1030
train
T = int(input()) for _ in range(T): n,m = map(int,input().split()) completed = list(map(int,input().split())) jobs = [] for i in range(1,n+1): if i not in completed: jobs.append(i) jobs.sort() chef = [] ass = [] for i in range(len(jobs)): if i%2==0: chef.append(str(jobs[i])) else: ass.append(str(jobs[i])) print(' '.join(chef)) print(' '.join(ass))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CLEANUP" }
d1031
train
t = int(input()) while(t>0): t-=1; n,l,r = list(map(int,input().split())); a = bin(l)[2:]; b = bin(r)[2:]; # find matching z = 0; l = min(len(a),len(b)); for i in range(l): if a[i]==b[i]: z+=1; else: break; #find base string a = a[z:] b = b[z:] if(len(a)==0 and len(b)==0): print(n); else : m = max(len(a),len(b)) #print m; zz = bin(n)[2:] x= len(zz) y = zz[:x-m] f1 = y+a; f2 = y+b; ans = int(y,2) if(int(f1,2)>n or int(f2,2)>n): ans-=1; print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COOK69/problems/BINTREEQ" }
d1032
train
import math t = eval(input()) while(t > 0): h,s = input().split() h = int(h) s = int(s) if(((h*h*h*h) - (16*s*s)) < 0): print("-1") else: B = (math.sqrt((h*h) + math.sqrt((h*h*h*h) - (16*s*s))))/math.sqrt(2) P = (2*s)/B if(B > P): print('{0:.6f}'.format(P),'{0:.6f}'.format(B),'{0:.6f}'.format(h)) else: print('{0:.6f}'.format(B),'{0:.6f}'.format(P),'{0:.6f}'.format(h)) t = t-1
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COOK71/problems/RIGHTTRI" }
d1033
train
a = [1] M = 10**6 + 3 for ii in range(1, 1000005): a.append((a[-1]*ii)%M) for __ in range(eval(input())): n, x = list(map(int, input().split())) if n>=M: print(0) else: print((a[n]*x)%M)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/REC2016/problems/RECIICHA" }
d1034
train
print(0)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/KM252020/problems/KM25P5C" }
d1035
train
from math import log2; import bisect; from bisect import bisect_left,bisect_right import sys; from math import gcd,sqrt sys.setrecursionlimit(10**7) from collections import defaultdict inf=float("inf") # n=int(input()) # n,m=map(int,input().split()) # l=list(map(int,input().split())) def get_factors(x): if x==1: return []; sqrta=int(sqrt(x))+1 for i in range(2,sqrta): if x%i==0: return [i]+get_factors(x//i) return [x] def min_generator(fac,k,index,new_list): if index==len(fac): return sum(new_list) mina=inf; for i in range(0,min(index+1,len(new_list))): new_list[i]*=fac[index] theta=min_generator(fac,k,index+1,new_list) if theta<mina: mina=theta; new_list[i]//=fac[index] return mina; def fun(k,x): dict=defaultdict(lambda :1) factors=get_factors(x) for i in factors: dict[i]*=i; if len(dict)==k: print(sum(dict.values())) return; if len(dict)<k: suma=sum(dict.values()) left=k-len(dict) suma+=left; print(suma) return; if k==1: print(x) return; fac=list(dict.values()) new_list=[1]*k theta=min_generator(fac,k,0,new_list) print(theta) for i in range(int(input())): k,x=map(int,input().split()) fun(k,x)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/HIRINGWO" }
d1036
train
# cook your dish here from collections import namedtuple CurrentPosition = namedtuple('current_position', 'points, cell, pairs') T = int(input()) for _ in range(T): R, C, N = map(int, input().split()) Sx, Sy = map(int, input().split()) tx = map(int, input().split()) ty = map(int, input().split()) tel_pairs = list(zip(tx, ty)) board = [] for _ in range(R): board += [[int(c) for c in input().split()]] def explore(p): next_pos = [] for i, (dx, dy) in enumerate(p.pairs): sx, sy = p.cell new_pairs = p.pairs[:i]+p.pairs[i+1:] # case (+, +) px, py = sx + dx, sy + dy if px < R and py < C: next_pos += [CurrentPosition(p.points+board[px][py], (px, py), new_pairs)] # case (+, -) px, py = sx + dx, sy - dy if px < R and 0 <= py: next_pos += [CurrentPosition(p.points+board[px][py], (px, py), new_pairs)] # case (-, +) px, py = sx - dx, sy + dy if 0 <= px and py < C: next_pos += [CurrentPosition(p.points+board[px][py], (px, py), new_pairs)] # case (-, -) px, py = sx - dx, sy - dy if 0 <= px and 0 <= py: next_pos += [CurrentPosition(p.points+board[px][py], (px, py), new_pairs)] return next_pos pos = [CurrentPosition(board[Sx][Sy], (Sx, Sy), tel_pairs)] result = board[Sx][Sy] while pos: p = pos.pop(0) if p.pairs: pos += explore(p) else: result = max(result, p.points) print(result)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/MATTEG" }
d1037
train
# cook your dish here t=int(input()) for _ in range(t): x1,y1,x2,y2=map(int,input().split()) x3,y3,x4,y4=map(int,input().split()) if (x1==x3 and y1==y3)or(x2==x4 and y2==y4): print("yes") elif (x1==x4 and y1==y4)or(x2==x3 and y2==y3): print("yes") else: if(y1==y2)and(y1==y3)and(y1==y4): a1=max(x1,x2);a2=min(x1,x2) b1=max(x3,x4);b2=min(x3,x4) if a1>=b2 and a2<=b1: print("yes") else: print("no") elif (x1==x2)and(x1==x3)and(x1==x4): a1=max(y1,y2);a2=min(y1,y2) b1=max(y3,y4);b2=min(y3,y4) if a1>=b2 and a2<=b1: print("yes") else: print("no") else: print("no")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/SAMESNAK" }
d1038
train
for _ in range(int(input())): s = input().strip() a = [] last = 0 for i in range(len(s)): if s[i] == 'P': a.append(i - last) last = i + 1 x = 0 a = a[::-1] for v in a[::2]: x ^= v % 3 print('Yes' if x else 'No')
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/ADAPWNS" }
d1039
train
MOD = int(1e9+7) def mult(a, b): rsp = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] for i in range(3): for j in range(3): for k in range(3): rsp[i][j] += a[i][k] * b[k][j] rsp[i][j] %= MOD return rsp ident = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] m = [[1, 1, 0], [1, 0, 1], [1, 0, 0]] powers = [m] for _ in range(53): p = powers[-1] powers.append(mult(p ,p)) def pow2(e): y = ident i = 0 for p in powers: if e & (1 << i): y = mult(p, y) i += 1 return y t = eval(input()) for _ in range(t): n = eval(input()) if n < 3: print(0) continue r = pow(2, n, MOD) b = pow2(n - 2) # print(b) r -= (4 * b[0][0]) % MOD r -= (2 * b[1][0]) % MOD r -= b[2][0] r = (MOD + r) % MOD print(r)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CROWD" }
d1040
train
t=int(input()) for i in range(t): ans=0 x,y=list(map(int,input().split())) if y>x: if (y-x)%4==0:ans=3 elif (y-x)%2==0: ans=2 else: ans=1 if y<x: if (y-x)%2==0:ans=1 else: ans=2 print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/EOOPR" }
d1041
train
t=int(input()) for _ in range(t): n,q=map(int,input().split()) s=input() l=[0]*(n-1) for i in range(n-2): a,b,c=s[i],s[i+1],s[i+2] if len(set([a,b,c]))<3: l[i]=l[i-1]+1 else: l[i]=l[i-1] for i in range(q): left,right=map(int,input().split()) left-=1 right-=1 if right-left+1 <3: print('NO') continue if (l[right-2]-l[left-1])>0: print('YES') else: print('NO')
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/RICHSTR" }
d1042
train
# cook your dish here for u in range(int(input())): n=int(input()) l=list(map(int,input().split())) d=[] dd=[] s=1 for i in range(n-1): s=l[i] d.append(s) dd.append([i,i]) for j in range(i+1,n): s=s*l[j] d.append(s) dd.append([i,j]) d.append(l[n-1]) dd.append([n-1,n-1]) k=len(d) m=max(d) x,y=0,0 for i in range(k): if(d[i]==m): x=dd[i] print(m,*x)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COH12020/problems/RIPUGEMS" }
d1043
train
# cook your dish here def G(x, y): while(y): x, y = y, x % y return x # t=int(input()) # l=list(map(int,input().split())) for _ in range(int(input())): n,p=map(int,input().split()) c=0 for i in range(1,n+1): if G(i,p)==1: c+=1 ans=c*(c-1)//2 print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/NITJ2021/problems/CIN004" }
d1044
train
test_case = int(input()) for w in range(test_case): n, k = map(int,input().split()) l = list(map(str,input().split())) ans = [] for q in range(k): l2 = list(map(str,input().split())) ans.extend(l2[1:]) for i in l: if i in ans: print('YES',end=' ') else: print('NO',end=' ') print()# cook your dish here
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/FRGTNLNG" }
d1045
train
# cook your dish here number = int(input()) for i in range(number): a = list(input()) for k in range(len(a)): a[k] = eval(a[k]) print(sum(a))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/FLOW006" }
d1046
train
vow = ['a', 'e', 'i','o', 'u'] for _ in range(int(input())): name = str(input()) tmp = '' for i in range(len(name)): if name[i] not in vow and name[i].isalpha(): tmp+='1' elif name[i] in vow and name[i].isalpha(): tmp+='0' print( int(tmp, 2)% (10**9 + 7))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/LDT42020/problems/NAMEVAL" }
d1047
train
for t in range(int(input())): limakMax, bobMax = list(map(int, input().split())) limakEat = 0; bobEat = 0 eating = 1 while limakEat <= limakMax or bobEat <= bobMax: if eating % 2 != 0 and limakEat <= limakMax: limakEat += eating eating += 1 if limakEat > limakMax: print("Bob") break elif eating % 2 == 0 and bobEat <= bobMax: bobEat += eating eating += 1 if bobEat > bobMax: print("Limak") break
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CANDY123" }
d1048
train
import sys def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def rinput(): return list(map(int, sys.stdin.readline().strip().split())) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) t=iinput() for _ in range(t): n=iinput() p=[] mi=[] for i in range(n): x,y=rinput() p.append(x+y) mi.append(x-y) p.sort() mi.sort() m=float('inf') for i in range(1,n): if(p[i]-p[i-1]<m): m=p[i]-p[i-1] if(mi[i]-mi[i-1]<m): m=mi[i]-mi[i-1] if m%2==0: print(m//2) else: print(m/2)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/WTBTR" }
d1049
train
t=int(input()) for i in range(t): a,k=list(map(int,input().split())) x1,x2,x3=list(map(int,input().split())) big=max(x1,x2,x3) small=min(x1,x2,x3) q=big-small-2*k if q>=a: print(0) elif -1*q>=0: print(a*a) else: print(a*(a-q))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/TALESQUA" }
d1050
train
# for _ in range(int(input())): n,k = list(map(int,input().split())) arr = list(map(int,input().split())) s=set(arr) t1=len(s) max=-1 for i in range(n-k+1): temp=set(arr[i:i+k]) #print(temp,i,k+i+1) t=len(temp) if t1 == t: if max<sum(arr[i:k+i]): max=sum(arr[i:k+i]) print(max)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/APO12020/problems/APOC2_02" }
d1051
train
# cook your dish here t=int(input()) for j in range(t): s=input() st=[] ans=0 for i in range(len(s)): if(s[i]=='>'): if(len(st)!=0 and st[-1]=='<'): st.pop() if(len(st)==0): ans=i+1 else: break else: st.append('<') print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/COMPILER" }
d1052
train
# cook your dish here t=int(input()) for i in range(0,t): my_ip = int(input().strip()) for xyz in range(my_ip+1): for abc in range(0,xyz+1): if abc == xyz: print(xyz,end="") else: print('*',end="") print()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/PTRN2020/problems/ITGUY38" }
d1053
train
from collections import deque T=int(input()) def break_down(num): count=0 while(len(num)!=1): temp=0 for i in range(0,len(num)): temp=temp+int(num[i]) num=str(temp) count=count+1 return (int(num),count) def digit_sum(num): temp=0 for i in range(0,len(num)): temp=temp+int(num[i]) num=temp return (num) while(T): queue=deque() count_n=0 count_d=0 T=T-1 N,d=[i for i in input().split()] n,count_n=break_down(N) D,count_D=break_down(d) dic={} if(D==1 or D==2 or D==4 or D==5 or D==7 or D==8): mini=1 elif(D==3 or D==6): mini=min(digit_sum(str(n+3)),digit_sum(str(n+6)),digit_sum(str(n+9))) else: mini=n queue.append((int(N),0)) ele=int(N) count=0 while(len(queue)!=0): ele,count=queue.popleft() if(ele==mini): break else: if(len(str(ele))==1): temp1=ele+int(d) queue.append((temp1,count+1)) else: temp2=digit_sum(str(ele)) temp1=ele+int(d) queue.append((temp2,count+1)) queue.append((temp1,count+1)) print(ele,count)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/MINDSUM" }
d1054
train
# cook your dish here for _ in range(int(input())): n=int(input()) A=list(map(int,input().split())) A.sort() for i in range(len(A)): if A[i]==1: print(i) break
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/PSTR2020/problems/ANITGUY5" }
d1055
train
test=int(input()) for i in range(test): s=input() b=len(s) list1=[] for j in range(len(s)): if s[j]=='.': list1.append(j) for i in list1: if b-i-1 in list1 : if i!=b-i-1 and ((s[i] and s[b-i-1]) != 'a' ): s=s[:i]+'a'+s[i+1:b-i-1]+'a'+s[b-i:] else: s=s[:i]+'a'+s[i+1:] else: s=s[:i]+s[b-i-1]+s[i+1:] if s==s[::-1]: print(s) else: print(-1)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/LEXOPAL" }
d1056
train
# cook your dish here m,n=[int(i) for i in input().split()] arr=list(map(int,input().split())) arr=sorted(arr,reverse=True) ans=0 w=0 q=m for m in range(q): if(arr[m]>n): w=1 break ans+=1+(arr[m]*(arr[m]+1))//2 n-=arr[m] if(n==0): print(ans) else: if(w==1): print(ans+q-m+(n*(n+1))//2) else: print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PIZPAR" }
d1057
train
n=int(input()) for i in range(n): a,b,c=map(int,input().split()) if a>0 and b>0 and c>0 and a+b+c==180: print("YES") else: print("NO")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/FLOW013" }
d1058
train
import math def magic(a,digits): m=a%10 if(m==4): return a+3 elif(m==7): p=list(str(a)) #print p for i in range(digits-1,-1,-1): #print p[i] if (p[i]=='4'): #print 'four' p[i]='7' p = ''.join(str(n) for n in p) return int(p) if ((p[i]=='7')&(i==0)): #print 'seven' p[i]='4' p.insert(0,4) p = ''.join(str(n) for n in p) return int(p) if(p[i]=='7'): #print 'seven only' p[i]='4' #print p[i] t=eval(input()) n=[] op=[] for i in range(0,t): n.append(eval(input())) for i in range(0,t): digits = int(math.log10(n[i]))+1 #print digits op.append(magic(n[i],digits)) #for i in range(0,t): #print n[i] for i in range(0,t): print(op[i])
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ALKH2012/problems/NMAGIC" }
d1059
train
# cook your dish here t=int(input()) for i in range(0,t): p=input() l=list(p) for j in range(0,len(l)): l[j]=int(l[j]) l[j]=l[j]-2 for j in range(0,len(l)): l[j]=str(l[j]) q=''.join(l) print(q)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ABCC2020/problems/PASSHACK" }
d1060
train
n = int(input()) a = [] for i in range(n): a.append(int(input())) m1 = 0 m2 = 0 for e in a: if (e > m1): m2 = m1 m1 = e elif (e > m2 and e != m1): m2 = e ans = 0 for e in a: temp = m1%e if (temp>ans): ans = temp print(max(m2%m1,ans))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/LOCMAR16/problems/MOD" }
d1061
train
def countSubstr(str, n, x, y): tot_count = 0 count_x = 0 for i in range(n): if str[i] == x: count_x += 1 if str[i] == y: tot_count += count_x return tot_count t=int(input()) for _ in range(t): n=int(input()) str=input() x='0' y='1' x1='1' y1='0' c1=countSubstr(str,n,x,y) c2=countSubstr(str,n,x1,y1) print(c1+c2)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/CEFT2020/problems/QFUN" }
d1062
train
# cook your dish here def value(a, b, c): if(c == '&'): return a&b elif(c == '^'): return a^b elif(c == '|'): return a|b def break_rules(n, operator): if(len(n) == 1): return n elif(len(n) == 2): return [value(n[0], n[1], operator[0])] else: cont_ans = [] for i in range(1,len(n)): l1 = n[:i] l2 = n[i:] o1 = operator[:i] o2 = operator[i:] l1_ans = break_rules(l1, o1) l2_ans = break_rules(l2, o2) for k in l1_ans: for j in l2_ans: cont_ans.append(value(k, j, operator[i - 1])) return cont_ans t = int(input()) while t > 0 : operator = [] num = [] exp = input() temp = '' for i in range(len(exp)): if(ord(exp[i]) > 47 and ord(exp[i]) < 58): temp = temp + exp[i] else: num.append(int(temp)) temp = '' operator.append(exp[i]) if(i == len(exp) - 1): num.append(int(temp)) t -= 1 # print(num,operator) print(max(break_rules(num, operator)))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/BIT2C" }
d1063
train
# cook your dish here def read_i_l(l=False): m = list(map(int, input().strip().split(" "))) if l: return list(m) else: return m def i(): return int(input().strip()) T = i() L = [] """for current in range(T): line = "" for i in range(current): line+=str((T-i)%10) for i in range(2*(T-current)-1): line+=str((T-current)%10) for i in range(current-1,-1,-1): line+=str((T-i)%10) L.append(line) L += L[-2::-1]""" if T >= 1: L = ["1"] for i in range(2,T+1): nL = [str(i)+(2*i-2)*(" "+str(i))] for l in L: nL.append(str(i)+" "+l+" "+str(i)) nL.append(str(i)+(2*i-2)*(" "+str(i))) L = nL for l in L: print(l)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/NQST2020/problems/ICEWRAP" }
d1064
train
number = int(input()) for i in range(number): x = list(map(int, input().split(' '))) print(x[0]%x[1])
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/FLOW002" }
d1065
train
from math import gcd import sys input=lambda : sys.stdin.readline().strip() c=lambda x: 10**9 if(x=="?") else int(x) def main(): for _ in range(int(input())): s=list(input())[::-1] l=['F','E','H','C'] i=0 while(i<len(s)): if(i+3<len(s)): f=True for j in range(i,i+4): if(l[j-i]==s[j] or s[j]=='?'): pass else: f=False break if(f): for j in range(i,i+4): s[j]=l[j-i] if(s[i]=="?"): s[i]='A' else: if(s[i]=="?"): s[i]="A" i+=1 print(*s[::-1],sep='') main()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/MANYCHEF" }
d1066
train
# cook your dish here for a in range(int(input())): N,M=map(int,input().split()) b=[] for o in range(N): b.append(input()) c=[] for d in b: f=[] for e in range(len(d)): if d[e]=='1': f.append(e) c.append(f) i=[] for g in range(len(c)): for h in range(len(c[g])): for j in range(len(c)): for k in range(len(c[j])): if (j>g) or(j==g and k>h): if c[g][h]-c[j][k]>=0: i.append(c[g][h]-c[j][k]+j-g) else: i.append(-1*(c[g][h]-c[j][k])+j-g) l=[m for m in range(1,N+M-1)] for n in l: print(i.count(n),end=' ')
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/AVGMAT" }
d1067
train
for _ in range(int(input())): n=input().rstrip() n=[ele for ele in n] l=len(n) m=10**18+8 ini=1 for i in range(l-1,-1,-1): if int(n[i])<=m: if ini==1: m=int(n[i]) else: m=max(m,n[i]) else: m=int(n[i])-1 n[i]=str(m) for j in range(l-1,i,-1): n[j]='9' i=0 while n[i]=='0': i+=1 print("".join(n[i:]))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/APO12020/problems/APOC2_04" }
d1068
train
# Fibonacci Series using # Optimized Method # function that returns nth # Fibonacci number MOD = 1000000007 def fib(n): F = [[2, 2], [1, 0]] power(F, n - 1) ans = [6, 2] return (F[0][0] * 6 + F[0][1] * 2) % MOD # return F[0][0] def multiply(F, M): x = (F[0][0] * M[0][0] + F[0][1] * M[1][0]) % MOD y = (F[0][0] * M[0][1] + F[0][1] * M[1][1]) % MOD z = (F[1][0] * M[0][0] + F[1][1] * M[1][0]) % MOD w = (F[1][0] * M[0][1] + F[1][1] * M[1][1]) % MOD F[0][0] = x F[0][1] = y F[1][0] = z F[1][1] = w def power(F, n): if n == 0 or n == 1: return M = [[2, 2], [1, 0]] power(F, n // 2) multiply(F, F) if n % 2 != 0: multiply(F, M) for _ in range(int(input())): n = int(input()) ans = 1 if n == 0: ans = 1 elif n == 1: ans = 2 elif n == 2: ans = 6 else: ans = fib(n-1) print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COG2020/problems/COG2004" }
d1069
train
# cook your dish here t=int(input()) for _ in range(t): N, M=map(int,input().split()) if(N%2==0 or M%2==0): print("YES") else: print("NO")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/ADASCOOL" }
d1070
train
#Note that it's python3 Code. Here, we are using input() instead of raw_input(). #You can check on your local machine the version of python by typing "python --version" in the terminal. #Read the number of test cases. T = int(input()) for tc in range(T): # Read integers a and b. (a, b) = list(map(int, input().split(' '))) ans = a + b print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/FLOW001" }
d1071
train
# cook your dish here # cook your dish here class Animal: def __init__(self): start, end, starting_time = map(int, input().split()) self.ending_time = starting_time + abs(start - end) self.velocity = 1 if end >= start else -1 self.eaten_by = -1, 10 ** 10 self.start = start self.end = end self.starting_time = starting_time def will_collide(self, z): if self.starting_time > z.ending_time or self.ending_time < z.starting_time: return False if self.velocity == z.velocity: if self.starting_time > z.starting_time: self, z = z, self if z.start == self.start + self.velocity * (z.starting_time - self.starting_time): return z.starting_time else: return False if self.velocity == -1: self, z = z, self t = ( z.start - self.start + z.starting_time + self.starting_time ) / 2 return t if self.starting_time <= t <= self.ending_time and z.starting_time <= t <= z.ending_time else False def main(): for _ in range(int(input())): no_cats, no_rats = map(int, input().split()) Cats = [Animal() for i in range(no_cats)] for i in range(no_rats): rat = Animal() for j in range(no_cats): time = rat.will_collide(Cats[j]) if time: # print(time) if time < rat.eaten_by[1]: rat.eaten_by = j + 1, time print(rat.eaten_by[0]) main()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CATSRATS" }
d1072
train
# cook your dish here t=int(input()) while t>0: n,q=list(map(int,input().split())) blst=[0] for i in range(1,65): blst.append(0) i=1 while n>0: if n%2: blst[i]=1 n//=2 i+=1 while q>0: n=int(input()) if n==1: p=int(input()) if blst[p]: print('ON') else: print('OFF') elif n==2: p=int(input()) if blst[p]==0: blst[p]=1 elif n==3: p=int(input()) if blst[p]==1: blst[p]=0 else: p,r=list(map(int,input().split())) if blst[p]!=blst[r]: blst[p]+=1 blst[p]%=2 blst[r]+=1 blst[r]%=2 q-=1 t-=1
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ENDE2020/problems/ENCDEC03" }
d1073
train
t=int(input()) for i in range(t): n=int(input()) r=int(n**(.5)) d=n-r*r m=d%r print('X'*m+'D'*(m>0)+'X'*(r-m)+'D'*(r+d//r))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/XDS" }
d1074
train
import sys def fin(): return sys.stdin.readline().strip() def fout(s, end="\n"): sys.stdout.write(str(s)+end) MOD = pow(10, 9)+7 t = int(input()) while t>0: t -= 1 n, m = list(map(int, fin().split())) if n == 1: print(m%MOD) continue dp1 = m*(m-1) dp2 = m for i in range(3, n+1): temp = dp2 dp2 = dp1 dp1 = (temp*(m-1))%MOD+(dp1*(m-1))%MOD print((dp1+dp2)%MOD)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CARR" }
d1075
train
# cook your dish here t=int(input()) j=0 while j<t: n=int(input()) lst=list(map(int,input().split())) s=set() d=list() for i in lst: if i in s: s.remove(i) d.append(i) else: s.add(i) x=len(d) if x%2==0: print(x//2) else: print((x-1)//2) j+=1
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/NITJ2021/problems/CIN005" }
d1076
train
gb = [0, 1, 2, 2, 3, 3] ga = [0 for x in range(70)] gag = [0 for x in range(70)] ga[0] = 1 gag[0] = 0 for i in range(1, 70): if i % 4 == 0: ga[i] = 1.5 * ga[i-1] gag[i] = 0 else: ga[i] = 2 * ga[i-1] gag[i] = gag[i-1] + 1 def g(n): if n < 6: return gb[n] else: x = n / 6 a = 0 for i, k in enumerate(ga): if k <= x: a = i else: break return gag[a] t = int(input()) for q in range(t): n = int(input()) a = list(map(int, input().split())) res = g(a[0]) for i in range(1, n): res ^= g(a[i]) if res == 0: print("Derek") else: print("Henry")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/SNCKQL16/problems/FDIVGAME" }
d1077
train
# cook your dish here class TestCase: def __init__(self): [self.node_count, self.query_count] = read_line() def fill_nodes(self): self.nodes = {n+1: [] for n in range(self.node_count)} for i in range(self.node_count -1): new_node_1, new_node_2 = read_line() self.nodes[new_node_1].append(new_node_2) self.nodes[new_node_2].append(new_node_1) def resolve_query(self, query): a, d_a, b, d_b = query suiting_a = self.find_nodes_with_distance(a, d_a) suiting_b = self.find_nodes_with_distance(b, d_b) fitting = [node for node in suiting_a if node in suiting_b] if len(fitting) == 0: return -1 else: return fitting[0] def find_nodes_with_distance(self, start_node, distance): from_nodes = {start_node} passed_nodes = from_nodes for i in range(distance): to_nodes = set() # add all adjacent nodes for node in from_nodes: to_nodes.update(self.nodes[node]) # no backtracking for node in passed_nodes: if node in to_nodes: to_nodes.remove(node) # update which nodes are passed passed_nodes.update(to_nodes) # go another round with the new nodes found from_nodes = to_nodes return list(from_nodes) def read_line(): line = input() return [int(s) for s in line.split(' ')] num_testcases = int(input()) for i in range(num_testcases): testcase = TestCase() testcase.fill_nodes() for q in range(testcase.query_count): query = read_line() print(testcase.resolve_query(query))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/DDQUERY" }
d1078
train
t = int(input()) for i in range(t): n = int(input()) dir = [] for j in range(n): dir.append(input().strip().split()) for j in range(n-1): if dir[j+1][0] == 'Right': dir[j][0] = 'Left' else: dir[j][0] = 'Right' dir[n-1][0] = 'Begin' for j in reversed(dir): print(' '.join(j))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COOK29/problems/DIRECTI" }
d1079
train
t=int(input()) for i in range(t): n,w1,w2,w3=map(int,input().split()) if n>=w1+w2+w3: print(1) elif n>=w1+w2 or n>=w2+w3: print(2) else: print(3)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/BRKBKS" }
d1080
train
# cook your dish here x=int(input()) for i in range(x): h=input() print(h.count('4'))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/LUCKFOUR" }
d1081
train
def res(s): if len(s) == 2: if s[0] == s[1]: print("NO") else: print("YES") elif s[0] != s[1]: counte = 0 for i in range(2, len(s)): if i % 2 == 0: if s[i] != s[0]: counte = 1 break else: if s[i] != s[1]: counte = 1 break if counte == 0: print("YES") else: print("NO") else: print("NO") def __starting_point(): t = int(input()) for _ in range(t): stri = str(input()) res(stri) __starting_point()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/TICKETS5" }
d1082
train
# cook your dish here import string from collections import OrderedDict from itertools import zip_longest dic = OrderedDict(zip(string.ascii_uppercase, range(0, 26))) keys = [98, 57, 31, 45, 46] t = int(input()) # number of test cases s1 = [] for i in range(t): s = input() for i in s: if i in dic.keys(): s1.append(int(i.replace(i, str(dic[i])))) s2 = [sum(t) for t in zip_longest(s1, keys, fillvalue=0)] inv_dic = {v:k for k,v in dic.items()} s_1 = list() for i in range(len(s1)): s_1.append(s2[i]%26) res= [inv_dic[i] for i in s_1] print(''.join(res)) inv_dic.clear() res.clear() s1.clear() s2.clear() s_1.clear()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/STRG2020/problems/ENCYP" }
d1083
train
counter = -1 def flattree(node): nonlocal counter if visited[node]==1: return else: visited[node]=1 counter += 1 i_c[node] = counter flat_tree[counter] = swt[node] for i in graph[node]: if visited[i]==0: flattree(i) counter += 1 o_c[node] = counter flat_tree[counter] = -swt[node] return def getsum(BITTree, i): s = 0 # initialize result i = i + 1 while i > 0: s += BITTree[i] i -= i & (-i) return s def upd(BITTree, n, i, v): i += 1 while i <= n: BITTree[i] += v i += i & (-i) def construct(arr, n): BITTree = [0] * (n + 1) for i in range(n): upd(BITTree, n, i, arr[i]) return BITTree from collections import defaultdict n = int(input()) swt = list(map(int, input().split())) graph = defaultdict(list) for i in range(n-1): n1, n2 = list(map(int, input().split())) graph[n1-1].append(n2-1) graph[n2-1].append(n1-1) flat_tree = [0]*(2*n+1) i_c = [0]*n o_c = [0]*n visited = [0]*n flattree(0) tre = construct(flat_tree, 2*n) q = int(input()) for i in range(q): query = list(map(int, input().split())) if query[0] == 1: node = query[1] - 1 answer = getsum(tre, i_c[node]) print(answer) else: node = query[1]-1 upd(flat_tree, (2*n), i_c[node], query[2]) upd(flat_tree, (2*n), o_c[node], -query[2])
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COFY2020/problems/GKSMNHM" }
d1084
train
for i in range(eval(input())): n,m,z,l,r,b = list(map(int, input().split())) rows=n columns=m hand_rest=n*(m+1) if(m%2==0): hand_rest -=max(0,n-l-r) if(l+r+(2*b)<=hand_rest): # print "kanu" print(min(n*m,l+r+z+b)) else: temp=l+r+(hand_rest-l-r)/2 # print "parth" print(min(n*m,temp+z))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/DEC15/problems/CHCINEMA" }
d1085
train
# cook your dish here s=input() s1=s[::-1] arr=[] cnt=0 for i in range(len(s1)): arr.append(s1[i]) for i in range(len(arr)): if(arr[i]=="1"): for j in range(i,len(arr)): if(arr[j]=="1"): arr[j]="0" else: arr[j]="1" cnt+=1 print(cnt)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PREFINVS" }
d1086
train
d = {} for i in range(26): char = chr(i+ord('a')) d[char] = [] for i in range(26): char = chr(i+ord('a')) temp = list(map(int,input().strip().split())) for j in range(26): if (temp[j] == 1): follow= chr(j+ord('a')) d[follow].append(char) def f(char,i,n,count): if (i==n): return count+1 else: ans = 0 for c in d[char]: ans+=f(c,i+1,n,0) ans%=(10**9+7) return ans for q in range(int(input().strip())): c, n = input().strip().split() n = int(n) print(f(c,1,n,0))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COMN2016/problems/THANPK" }
d1087
train
# cook your dish here mod = 10**9 + 7 from math import gcd def fac50(): f = [0]*51 f[0] ,f[1] = 1,1 for i in range(1,51):f[i] = (f[i-1]*i)%mod return f def gcd110(): gc = [[0]*111 for i in range(111)] for i in range(111): for j in range(111):gc[i][j] = gcd(i,j) return gc factorials,gcds = fac50(),gcd110() def rule_asc(n,l): a,k = [0 for i in range(n + 1)],1 a[1] = n while k != 0: x,y = a[k - 1] + 1,a[k] - 1 k -= 1 while x <= y and k < l - 1: a[k],y = x,y-x k += 1 a[k] = x + y yield a[:k + 1] def niceness(s): t = 0 for i in range(len(s)): for j in range(i+1,len(s)):t = (t + gcds[s[i]][s[j]])%mod return t def permcount(s,c): f,p = [s.count(x) for x in set(s)],factorials[c] for e in f:p = (p*pow(factorials[e],mod-2,mod))%mod return p def main(): for i in range(int(input())): n,s = [int(item) for item in input().split()] a = [int(item) for item in input().split()] b = [i for i in a if i != -1] s , ones = s - sum(b),a.count(-1) if s < 0:print(0) elif (s == 0 and ones == 0):print(niceness(a)%mod) elif (s > 0 and ones == 0):print(0) else: t = 0 for seq in rule_asc(s,ones): if len(seq) == ones: t = (t + (((permcount(seq,ones))%mod)*(niceness(b+seq)%mod))%mod)%mod print(t) def __starting_point():main() __starting_point()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/NICARRAY" }
d1088
train
#!/usr/bin/env python def iscycle(E, v, EXPLORED_NODES, EXPLORED_EDGES): EXPLORED_NODES.add(v) r = False for e in [x for x in E if v in x]: if e in EXPLORED_EDGES: continue if e[0] == v: w = e[1] else: w = e[0] if w in EXPLORED_NODES: return True else: EXPLORED_EDGES.add(e) r = r or iscycle(E, w, EXPLORED_NODES, EXPLORED_EDGES) if r: break return r def process(E): return iscycle(E, 1, set(), set()) and 'NO' or 'YES' def main(): N, M = list(map(int, input().split())) E = [] for m in range(M): U, V = list(map(int, input().split())) if U > V: U, V = V, U E.append((U, V)) print(process(E)) main()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/PRFT2012/problems/PD31" }
d1089
train
t=int(input()) while(t): s=input().split() m=int(s[0]) p=float(s[1]) if(m%2==0): r=(1-p**m)/(p+1) else: r=(1+p**m)/(p+1) print(1000000000*r,1000000000*(1-r)) t-=1
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/BANROB" }
d1090
train
def dfs(node): nonlocal adj,leaf val=0 flag=0 for i in adj[node]: x= dfs(i) val+=x if x==0: flag=1 leaf+=val-val%3 if val%3==0 and flag==0: return 1 else: return 0 for _ in range(int(input())): n=int(input()) adj=[[] for i in range(n+2)] arr=[int(i) for i in input().split()] leaf=0 #print(adj) for i in range(2,n+1): #print(i,arr[i-2]) adj[arr[i-2]].append(i) dfs(1) print(n-leaf)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ECPG2020/problems/ECPC10D" }
d1091
train
# cook your dish here import collections def shortestSubarray(A, K): N = len(A) P = [0] for x in A: P.append(P[-1] + x) #Want smallest y-x with Py - Px >= K ans = N+1 # N+1 is impossible monoq = collections.deque() #opt(y) candidates, represented as indices             of P for y, Py in enumerate(P): #Want opt(y) = largest x with Px <= Py - K if not monoq: if Py>=K: return 1 while monoq and Py <= P[monoq[-1]]: monoq.pop() while monoq and Py - P[monoq[0]] >= K: ans = min(ans, y - monoq.popleft()) monoq.append(y) return ans if ans < N+1 else -1 for t in range(int(input())): N, D = [int(x) for x in input().split()] A = [int(x) for x in input().split()] print(shortestSubarray(A, D))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/MINSUBAR" }
d1092
train
# cook your dish here import math def isPos(num): if num%2==0: for i in range(num,2*num,1): if ((num**2)-((i/2)**2))**(1/2)==int(((num**2)-((i/2)**2))**(1/2)): return 'YES' return 'NO' else: for i in range(num+1,2*num,1): if ((num**2)-((i/2)**2))**(1/2)==int(((num**2)-((i/2)**2))**(1/2)): return 'YES' return 'NO' test = int(input()) for __ in range(test): num=int(input()) print(isPos(num))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/ADTRI" }
d1093
train
# cook your dish here t=int(input()) for i in range(t): (n,k,e,m)=tuple(map(int,input().split())) scores=[] for j in range(n-1): scores.append(sum(list(map(int,input().split())))) scores.sort(reverse=True); bsc=scores[k-1]; msc=sum(list(map(int,input().split()))) mini=bsc-msc+1 if(mini<0): print(0) elif(mini>m): print("Impossible") else: print(mini)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/ENTEXAM" }
d1094
train
# # # # n = int(input()) # # # # arr = list(map(int , input().split())) # # # # for _ in range(int(input())): # # # # l,r,mod = map(int , input().split()) # # # # pro = 1 # # # # for i in range(l - 1,r): # # # # pro *= arr[i] # # # # print(pro % mod) #sample testcases passed #TLE # # # import numpy #or use math # # # n = int(input()) # # # arr = list(map(int , input().split())) # # # for _ in range(int(input())): # # # l,r,mod = map(int , input().split()) # # # print(numpy.prod(arr[l - 1:r]) % mod) #sample cases passed, WA # # import math # # primes,dic,t = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],{},0 # # for i in primes: # # dic[i] = t # # t += 1 # # def primeFactors(n,arr): # # for i in range(2,int(math.sqrt(n)) + 1,2): # # while(n % i == 0): # # arr[dic[i]] += 1 # # n /= i # # if(n > 2): # # arr[dic[n]] += 1 # # return arr # # n = int(input()) # # A = list(map(int , input().split())) # # dp = [0]*len(primes) # # for i in range(1,n + 1): # # r = [dp[i - 1]].copy() # # dp.append(primeFactors(A[i - 1],r)) # # for _ in range(int(input())): # # li,ri,m=list(map(int,input().split())) # # ans = 1 # # for i in range(len(primes)): # # ans *= (pow(primes[i],dp[ri][i] - dp[li - 1][i],m)) % m # # print(ans % m) #NZEC # import math # primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] # dic={} # t=0 # for i in primes: # dic[i]=t # t+=1 # def primeFactors(n,arr): # while(n % 2 == 0): # arr[dic[2]] += 1 # n /= 2 # for i in range(3,int(math.sqrt(n))+1,2): # while(n % i == 0): # arr[dic[i]] += 1 # n /= i # if(n > 2): # arr[dic[n]] += 1 # return arr # N = int(input()) # A = list(map(int , input().split())) # dp = [[0]*len(primes)] # for i in range(1,N + 1): # r = dp[i - 1].copy() # dp.append(primeFactors(A[i - 1],r)) # for _ in range(int(input())): # l,r,m = list(map(int , input().split())) # ans = 1 # for i in range(len(primes)): # ans *= (pow(primes[i],dp[r][i] - dp[l - 1][i],m)) % m # print(ans % m) import sys import math input = sys.stdin.readline primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] dic={} t=0 for i in primes: dic[i]=t t+=1 def primeFactors(n,arr): while n % 2 == 0: arr[dic[2]]+=1 n = n / 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: arr[dic[i]]+=1 n = n / i if n > 2: arr[dic[n]]+=1 return arr def main(): N=int(input()) A=list(map(int,input().split())) tp=[0]*len(primes) dp=[] dp.append(tp) for i in range(1,N+1): # print(i) r=dp[i-1].copy() t=primeFactors(A[i-1],r) dp.append(t) t=int(input()) for _ in range(t): l,r,m=list(map(int,input().split())) if(m==1): print(0) else: ans=1 for i in range(len(primes)): ans=ans*(pow(primes[i],dp[r][i]-dp[l-1][i],m))%m print(ans%m) def __starting_point(): main() __starting_point()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CHMOD" }
d1095
train
t = int(input()) for i in range(t): n = int(input()) suffixes = {} xx = input().split() for x in range(n): try: a = suffixes[xx[x][-3:]] except Exception as e: a = [] a.append(xx[x]) suffixes.update({xx[x][-3:]: a}) print("Case : %d" % (i + 1)) for a in sorted(suffixes): print("".join(b + " " for b in sorted(suffixes[a])).strip())
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COWA2016/problems/CWAM1502" }
d1096
train
n=int(input()) arr=[int(x) for x in input().split()] l=[1]*n if sorted(arr)==arr: print('0') else: for i in range(0,len(arr)): for j in range(i): if arr[i]>=arr[j] and l[i]<l[j]+1: l[i]=l[j]+1 print(n-max(l))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/IARCSJUD/problems/BOOKSORT" }
d1097
train
import sys n, x, y = input().split(' ') n = int(n) x = int(x) y = int(y) contests = {} for i in range(n): s, e = input().split(' ') s = int(s) e = int(e) contests[(s, e)] = abs(s-e) v_time = input().split(' ') w_time = input().split(' ') v_time, w_time = list(map(int, v_time)), list(map(int, w_time)) v_time.sort() w_time.sort() score = sys.maxsize contests = dict(sorted(contests.items(), key=lambda item: item[1])) for k, v in contests.items(): start=-1 end = sys.maxsize for i in range(x): if v_time[i] > k[0]: break start = v_time[i] for j in range(y): if w_time[j] >= k[1]: end = w_time[j] break if start == -1: continue score = min(score, (end-start+1)) if score-1 <= v: break print(score)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ZCOPRAC/problems/ZCO12002" }
d1098
train
testcases=int(input()) results=[] for i in range(0,testcases): friends=int(input()) l,b=list(map(int,input().split())) over=False if b>l: temp=b b=l l=temp for counter in range(0,friends): if l==b: over=True break elif l>b: l=l-b if b>l: temp=b b=l l=temp if over: results.append("No") else: results.append("Yes "+str(l*b)) for i in range(0,testcases): print(results[i])
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/CDGO2016/problems/CDGO1601" }
d1099
train
# cook your dish here for i in range(int(input())): n = int(input()) l = list(map(int,input().split())) s = 0 l.sort(reverse = True) for i in range(0,n,2): s = s+l[i] print(s)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CHEFA" }
d1100
train
import sys def f(p): votes = {} for x in range(p): str = sys.stdin.readline() t = str.split() votes[t[0]] = t[1] ans = 0 for per in votes: if votes[per] == "+": ans= ans+1 else: ans = ans-1 return ans x = sys.stdin.readline() for t in range(int(x)): p = sys.stdin.readline() print(f(int(p)))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COOK10/problems/TIDRICE" }