_id
stringlengths
2
5
partition
stringclasses
2 values
text
stringlengths
5
289k
language
stringclasses
1 value
meta_information
dict
title
stringclasses
1 value
d1401
train
# cook your dish here for _ in range(int(input())): n,l,h=list(map(int,input().split())) print(n-l+1+2**(l)-2,1+2**(h)-2+2**(h-1)*(n-h))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ENNO2020/problems/ENCNOV" }
d1402
train
n,k = map(int,input().split()) prices = list(map(int,input().split())) prices.sort() sum,cnt = 0, 0 for price in prices: sum += price if sum <= k: cnt += 1 else: break print(cnt)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COM12020/problems/CODE_01" }
d1403
train
def add(A, B): cnt = 0 while B > 0: U = A ^ B B = (A & B) * 2 A = U cnt += 1 return cnt for _ in range(int(input())): print(add(int(input(),2), int(input(), 2)))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/BINADD" }
d1404
train
# cook your dish here # cook your dish here def numDec(s): if not s: return 0 dp = [0 for _ in range(len(s) + 1)] dp[0] = 1 dp[1] = 0 if s[0] == '0' else 1 for i in range(2, len(dp)): if s[i-1] != '0': dp[i] += dp[i-1] two_digit = int(s[i-2 : i]) if two_digit >= 10 and two_digit <= 26: dp[i] += dp[i-2] return dp[len(s)] t = int(input()) while(t): t-=1 s = input() print(numDec(s)%1000000007)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/CEFT2020/problems/INTER" }
d1405
train
import sys test_cases = int(input()) for i in range(0,test_cases): count = input().split() #print count count_r = int(count[0]) count_g = int(count[1]) count_b = int(count[2]) k = int(input()) if k is 1: total = 1 else: total = 1 if count_r < k: total = total + count_r else: total = total + (k-1) if count_g < k: total = total + count_g else: total = total + (k-1) if count_b < k: total = total + count_b else: total = total + (k-1) print(total)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/APRIL16/problems/CHBLLNS" }
d1406
train
from bisect import bisect n = 32000 def primeSeive(n): prime = [True for i in range(n + 1)] primes = [] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * 2, n + 1, p): prime[i] = False p += 1 prime[0] = False prime[1] = False for p in range(n + 1): if prime[p]: primes.append(p) return primes arr = primeSeive(n) fin = [] for i in arr: fin.append(pow(i,4)) for _ in range(int(input())): n = int(input()) print(bisect(fin,n))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/RC152020/problems/REC15B" }
d1407
train
# cook your dish here from sys import stdin, stdout for _ in range(int(stdin.readline())): n, q = list(map(int, stdin.readline().split())) arr = list(map(int, stdin.readline().split()))[:n] od = ev = 0 for i in arr: if bin(i).count('1')%2==0: ev += 1 else: od += 1 for _ in range(q): p = int(stdin.readline()) if bin(p).count('1')%2==0: stdout.write(str(ev) + " " + str(od) + "\n") else: stdout.write(str(od) + " " + str(ev) + "\n")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/ENGXOR" }
d1408
train
# cook your dish here t = int(input()) for _ in range(t): m,n = [int(d) for d in input().split()] if m == 1: arr = [] if n%4 == 0: print(2) arr = [[1,1,2,2]*(n//4)] elif n%4 == 1: if n == 1: print(1) arr = [[1]] else: print(2) arr = [[1,1,2,2]*(n//4) + [1]] elif n%4 == 2: if n == 2: print(1) arr = [[1,1]] else: print(2) arr = [[1,1,2,2]*(n//4) + [1,1]] elif n%4 == 3: print(2) arr = [[1,1,2,2]*(n//4) + [1,1,2]] elif m == 2: if n%3 == 0: print(3) a1 = [1,2,3]*(n//3) arr = [a1,a1] elif n%3 == 1: if n == 1: print(1) arr = [[1],[1]] else: print(3) a1 = [1,2,3]*(n//3) + [1] arr = [a1,a1] elif n%3 == 2: if n == 2: print(2) arr = [[1,2],[1,2]] else: print(3) a1 = [1,2,3]*(n//3) + [1,2] arr = [a1,a1] elif m == 3: if n == 1: print(2) arr = [[1],[1],[2]] elif n == 2: print(3) arr = [[1,1],[2,2],[3,3]] elif n == 3: print(4) arr = [[1,3,4],[4,2,1],[4,2,1]] elif n == 4: print(4) arr = [[1,3,4,2],[4,2,1,3],[4,2,1,3]] else: if n%4 == 0: print(4) a1 = [1,3,4,2]*(n//4) a2 = [4,2,1,3]*(n//4) arr = [a1,a2,a2] elif n%4 == 1: print(4) a1 = [1,3,4,2]*(n//4) + [1] a2 = [4,2,1,3]*(n//4) + [4] arr = [a1,a2,a2] elif n%4 == 2: print(4) a1 = [1,3,4,2]*(n//4) + [1,3] a2 = [4,2,1,3]*(n//4) + [4,2] arr = [a1,a2,a2] elif n%4 == 3: print(4) a1 = [1,3,4,2]*(n//4) + [1,3,4] a2 = [4,2,1,3]*(n//4) + [4,2,1] arr = [a1,a2,a2] else: if n == 1: print(2) a1 = [1,3,4,2]*(n//4) + [1] a2 = [4,2,1,3]*(n//4) + [2] arr = [] i = 0 j = 0 c = 0 c1 = 0 for i in range(m): if j == 0 and c < 3: arr.append(a1) c = c + 1 if c == 2: j = 1 c = 0 else: arr.append(a2) c1 = c1 + 1 if c1 == 2: j = 0 c1 = 0 elif n == 2: print(3) arr = [] a1 = [1,1] a2 = [2,2] a3 = [3,3] if m%3 == 1: arr = [a1,a2,a3]*(m//3) + [a1] elif m%3 == 2: arr = [a1,a2,a3]*(m//3) + [a1,a2] elif m%3 == 0: arr = [a1,a2,a3]*(m//3) else: print(4) if n%4 == 0: a1 = [1,3,4,2]*(n//4) a2 = [4,2,1,3]*(n//4) arr = [] i = 0 j = 0 c = 0 c1 = 0 for i in range(m): if j == 0 and c < 3: arr.append(a1) c = c + 1 if c == 2: j = 1 c = 0 else: arr.append(a2) c1 = c1 + 1 if c1 == 2: j = 0 c1 = 0 elif n%4 == 1: a1 = [1,3,4,2]*(n//4) + [1] a2 = [4,2,1,3]*(n//4) + [4] arr = [] i = 0 j = 0 c = 0 c1 = 0 for i in range(m): if j == 0 and c < 3: arr.append(a1) c = c + 1 if c == 2: j = 1 c = 0 else: arr.append(a2) c1 = c1 + 1 if c1 == 2: j = 0 c1 = 0 elif n%4 == 2: a1 = [1,3,4,2]*(n//4) + [1,3] a2 = [4,2,1,3]*(n//4) + [4,2] arr = [] i = 0 j = 0 c = 0 c1 = 0 for i in range(m): if j == 0 and c < 3: arr.append(a1) c = c + 1 if c == 2: j = 1 c = 0 else: arr.append(a2) c1 = c1 + 1 if c1 == 2: j = 0 c1 = 0 elif n%4 == 3: a1 = [1,3,4,2]*(n//4) + [1,3,4] a2 = [4,2,1,3]*(n//4) + [4,2,1] arr = [] i = 0 j = 0 c = 0 c1 = 0 for i in range(m): if j == 0 and c < 3: arr.append(a1) c = c + 1 if c == 2: j = 1 c = 0 else: arr.append(a2) c1 = c1 + 1 if c1 == 2: j = 0 c1 = 0 for i in range(m): for j in range(n): print(arr[i][j],end = " ") print()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/DIFNEIGH" }
d1409
train
mod = 10 ** 9 + 7 from collections import Counter choice = {'1' : ['11', '21', '22'], '2' : ['11', '12', '21']} def solve(a,b): n = len(a) if n == 1: return 2 dp = Counter([('11', '11')]) for i in range(n-1): new = Counter() for x,y in (a[i], b[i]), (b[i], a[i]): for p in choice[x]: for q in choice[y]: m = p[-1] + x n = q[-1] + y new[m,n] += dp[p,q] new[m,n] %= mod dp = new ans = 0 for i in '11', '21', : for j in '11', '21': ans += dp[i,j] return (ans * 2) % mod t = int(input()) for _ in range(t): a = input() b = input() print(solve(a,b))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CHEFTWOS" }
d1410
train
for t in range(int(input())): n=int(input()) print(bin(n).count("1"))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/PBK22020/problems/ITGUY25" }
d1411
train
nCr = [[0 for x in range(1001)] for x in range(1001)] for i in range (0,1001): nCr[i][0]=1 nCr[i][i]=1 for i in range (1,1001): for j in range (1,1001): if i!=j: nCr[i][j] = nCr[i-1][j] + nCr[i-1][j-1] t=eval(input()) for rajarshisarkar in range(0,t): s,n,m,k=list(map(int,input().split(' '))) foo=0.000000 tot = float(nCr[s-1][n-1]) if s==n: print("1.000000\n") continue if k>n: print("0.000000\n") continue if m>n: wola=n else: wola=m for i in range(k,wola): foo+= ((nCr[m-1][i])*(nCr[s-m][n-i-1])) print("%f\n"% (float(foo/tot)))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/FTRIP" }
d1412
train
# cook your dish here import math def swap(a,b): return b,a t = int(input()) while(t!=0): z = list(map(int,input().strip().split(" "))) x=z[0] r=z[1] a=z[2] b=z[3] #p = math.pi peri = 2*r tp = x*peri if(a<b): a,b=swap(a,b) t1 = tp/a d2 = t1* b dd = abs(tp-d2) if(dd%peri==0): print(int(dd//peri)-1) else: n = int(dd//peri) print(n) t-=1
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/SMTC2020/problems/RROUNDS" }
d1413
train
from decimal import Decimal T = int(input()) for _ in range(T): N = int(input()) data = dict() for __ in range(N): ci, pi = input().split() data[ci] = pi S = list(input()) for i in range(len(S)): if S[i] in data.keys(): S[i] = data[S[i]] ### S = "".join(S) if '.' in S: S = S.strip('0').rstrip('.') else: S = S.lstrip('0') print(S or '0')
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/FORGETPW" }
d1414
train
def issafe(r,c,r1,c1,graph): if (graph[r][c] and graph[r1][c1]): return True return False t=int(input()) for mmmmmm in range(t): n,m=map(int,input().split()) x,y=map(int,input().split()) graph=[[False]*(m+2) for _ in range(n+2)] cost_graph=[[[-1,-1,-1] for __ in range(m)] for _ in range(n)] for i in range(n): str1=input() for j,val in enumerate(str1): graph[i][j]=(val=='1') x=x-1 y=y-1 set1=set() set1.add((x,y,0)) cost_graph[x][y][0]=0 while(set1): set2=set() while(set1): r,c,p=set1.pop() new_cost=cost_graph[r][c][p]+1 if(p==0): if issafe(r,c+1,r,c+2,graph): if cost_graph[r][c+1][1]==-1 or cost_graph[r][c+1][1]>new_cost: cost_graph[r][c+1][1]=new_cost set2.add((r,c+1,1)) if issafe(r+1,c,r+2,c,graph): if cost_graph[r+1][c][2]==-1 or cost_graph[r+1][c][2]>new_cost: cost_graph[r+1][c][2]=new_cost set2.add((r+1,c,2)) if issafe(r,c-2,r,c-1,graph): if cost_graph[r][c-2][1]==-1 or cost_graph[r][c-2][1]>new_cost: cost_graph[r][c-2][1]=new_cost set2.add((r,c-2,1)) if issafe(r-2,c,r-1,c,graph): if cost_graph[r-2][c][2]==-1 or cost_graph[r-2][c][2]>new_cost: cost_graph[r-2][c][2]=new_cost set2.add((r-2,c,2)) elif(p==1): if issafe(r,c+2,r,c+2,graph): if cost_graph[r][c+2][0]==-1 or cost_graph[r][c+2][0]>new_cost: cost_graph[r][c+2][0]=new_cost set2.add((r,c+2,0)) if issafe(r+1,c,r+1,c+1,graph): if cost_graph[r+1][c][1]==-1 or cost_graph[r+1][c][1]>new_cost: cost_graph[r+1][c][1]=new_cost set2.add((r+1,c,1)) if issafe(r,c-1,r,c-1,graph): if cost_graph[r][c-1][0]==-1 or cost_graph[r][c-1][0]>new_cost: cost_graph[r][c-1][0]=new_cost set2.add((r,c-1,0)) if issafe(r-1,c,r-1,c+1,graph): if cost_graph[r-1][c][1]==-1 or cost_graph[r-1][c][1]>new_cost: cost_graph[r-1][c][1]=new_cost set2.add((r-1,c,1)) elif(p==2): if issafe(r,c+1,r+1,c+1,graph): if cost_graph[r][c+1][2]==-1 or cost_graph[r][c+1][2]>new_cost: cost_graph[r][c+1][2]=new_cost set2.add((r,c+1,2)) if issafe(r+2,c,r+2,c,graph): if cost_graph[r+2][c][0]==-1 or cost_graph[r+2][c][0]>new_cost: cost_graph[r+2][c][0]=new_cost set2.add((r+2,c,0)) if issafe(r,c-1,r+1,c-1,graph): if cost_graph[r][c-1][2]==-1 or cost_graph[r][c-1][2]>new_cost: cost_graph[r][c-1][2]=new_cost set2.add((r,c-1,2)) if issafe(r-1,c,r-1,c,graph): if cost_graph[r-1][c][0]==-1 or cost_graph[r-1][c][0]>new_cost: cost_graph[r-1][c][0]=new_cost set2.add((r-1,c,0)) set1=set2 for _ in range(n): for __ in range(m): print(cost_graph[_][__][0],end=" ") print()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/ROLLBAR" }
d1415
train
def matrix(L,row,col,c): d={} dp=[] for i in range(row+1): temp=[] for i in range(col+1): temp.append([]) dp.append(temp) for i in range(row+1): dp[i][0]=0 for i in range(col+1): dp[0][i]=0 for i in range(1,row+1): for j in range(1,col+1): if L[i-1][j-1]==c: dp[i][j]=min(dp[i][j-1],dp[i-1][j],dp[i-1][j-1])+1 else: dp[i][j]=0 d[dp[i][j]]=d.get(dp[i][j],0)+1 ## for i in xrange(row+1): ## for j in xrange(col+1): ## print dp[i][j], ## print return d from sys import stdin n,m,q=list(map(int,stdin.readline().split())) L=[] for i in range(n): L.append(stdin.readline().strip()) male=matrix(L,n,m,'M') female=matrix(L,n,m,'F') for i in range(q): query=stdin.readline().split() if query[1]=='F': if female.get(int(query[0]),0)==0: print('no') else: print('yes') else: if male.get(int(query[0]),0)==0: print('no') else: print('yes')
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/CDVA16/problems/CDVA1605" }
d1416
train
for _ in range(int(input())): s=str(input()) n=len(s) k=s[::-1] a,b="","" for i in range(n): if s[i]!=k[i]: a+=s[i+1:] b+=k[i+1:] break else: a+=s[i] b+=k[i] #print(a,b) if a==a[::-1] or b==b[::-1]: print("YES") else: print("NO")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PRPALN" }
d1417
train
s,n,s1,lis,new1=[],[],[],[],[] import itertools q = int(input()) s.append(input().split(' ')) s1 = list([list(map(int,x)) for x in s]) sum1 = sum(s1[0]) if len(s1[0])%2!=0 : z = (len(s1[0])+1)//2 n = list(itertools.combinations(s1[0],z)) for j in range(len(n)) : x = sum(n[j]) if x==sum1//2 : lis = n[j] break new1 = list(lis) sum2 = sum(new1) for j in range(len(lis)) : y = lis[j] s1[0].remove(y) sum3=sum(s1[0]) if sum3>sum2 : print(' '.join(map(str,s1[0]))) print(' '.join(map(str,new1))) else : print(' '.join(map(str,new1))) print(' '.join(map(str,s1[0]))) else : z = len(s1[0])//2 n = list(itertools.combinations(s1[0],z)) for j in range(len(n)) : x = sum(n[j]) if x==sum1//2 : lis = n[j] break #print lis,len(lis) new1 = list(lis) sum2 = sum(new1) for j in range(len(lis)) : y = lis[j] s1[0].remove(y) sum3 = sum(s1[0]) if sum3>sum2 : print(' '.join(map(str,s1[0]))) print(' '.join(map(str,new1))) else : print(' '.join(map(str,new1))) print(' '.join(map(str,s1[0])))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/SOPC2015/problems/SOPC1504" }
d1418
train
for _ in range(int(input())): n = int(input()) ar = list(map(int,input().split())) d = {} for ele in ar: if ele in d: d[ele] += 1 else: d[ele] = 1 m = 99999 count = 0 for ele in d: count+=1 if m>d[ele]: m = d[ele] if count!=8: print(0) else: print(m)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/CSPR2020/problems/OCTAVE" }
d1419
train
# cook your dish here # cook your dish here for _ in range(int(input())): n=int(input()) l=list(map(int, input().split())) l.insert(0, 0) l1=[0]*(n+1) l1[1]=l[1] for i in range(2, n+1): l1[i]=max(l1[i-1]+l[i]*i, l1[i-2]+l[i-1]*i+l[i]*(i-1)) print(l1[-1])
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/SFRV" }
d1420
train
t = int(input()) def gcd (a, b): if (b == 0): return a return gcd (b, a % b) dp = {} def solve (p, k, g, s, m, x, y, n): if ((p, k, g) in dp): return dp[(p, k, g)]; ans = 0 if (p == n): if k >= x and k <= y: ans = g else: ans = 0 else: for i in range (p, n): if (i - p + 1 > m): break temp = solve (i + 1, k + 1, gcd(g, int(s[p:i + 1])), s, m, x, y, n) if (temp > ans): ans = temp dp[(p, k, g)] = ans return ans while t != 0: dp = {} t -= 1 n = int(input()) s = input() m, x, y = list(map (int, input().split())) x += 1 y += 1 print(solve (0, 0, 0, s, m, x, y, n))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/DIGITSEP" }
d1421
train
# cook your dish here # cook your dish here n=0;m=0; A=[];B=[]; anscount=0;k=0; def generate(n,m,l): nonlocal anscount if(len(l)==n+m): X=l i,j = 0,0 C=[0 for t in range(n+m)] while((i+j)<(n+m)): if(X[i+j] == 0): C[i+j] = A[i] i = i+1 else: C[i+j] = B[j] j = j+1 ans = len(C) for i in range(1,len(C)): if(C[i]==C[i-1]): ans-=1 if(ans==k): anscount+=1 else: if(l.count(1)<m): generate(n,m,l+[1]) if(l.count(0)<n): generate(n,m,l+[0]) else: if(l.count(0)<n): generate(n,m,l+[0]) for _ in range(int(input())): anscount=0 n,m,k=list(map(int,input().split())) A=list(map(int,input().split())) B=list(map(int,input().split())) generate(n,m,[]) print(anscount)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ZCOPRAC/problems/ZCO20002" }
d1422
train
def getsum(N): if N==1: return 9 if N==2: return 99 s = "" for i in range(0,N): s = s+'5' s = int(s) if N%2==0: s = s*pow(9,N//2-1) else: s = s*pow(9,N//2) return s%(pow(10,9)+7) def main(): t = int(input()) for _ in range(0,t): N = int(input()) result = getsum(N) print(result) def __starting_point(): main() __starting_point()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/CCOD2020/problems/PALL9" }
d1423
train
import sys T = int(sys.stdin.readline().strip()) for t in range(T): sys.stdin.readline().strip() st = '0'+sys.stdin.readline().strip()+'0' res = 0 for i in range(1,len(st)-1): if st[i] == st[i-1] == st[i+1] == '0': res+=1 print(res)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/AUG12/problems/LEBOMBS" }
d1424
train
# cook your dish here t=int(input()) for i in range(t): n=int(input()) nums=list(map(int,input().split())) k=int(input()) an=nums[k-1] cn=0 for i in range(n): if(nums[i]<an): cn+=1 print(cn+1)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/JOHNY" }
d1425
train
# cook your dish here n,a=map(int,input().split()) for i in range(a): if(n%10==0): n=n//10 else: n=n-1 print(n)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COTH2020/problems/SUBALGO" }
d1426
train
t=int(input()) count=[] for i in range(t) : s = input() a,b,c,n = s.split() n=int(n) d = int(a+b*n+c,2) count.append(0) while(d>0) : d=(d&(d+1))-1 count[i]+=1 for i in range(t) : print(count[i])
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/FENWITER" }
d1427
train
t=int(input()) for i in range(t): n,m=list(map(int,input().split())) l=[0]+list(map(int,input().split())) s=0 c=1 m1=[] for i in range(n): d,f,b=list(map(int,input().split())) if(l[d]>0): m1.append(d) s+=f l[d]-=1 else: m1.append(0) s+=b for i in range(n): if(m1[i]==0): for j in range(c,m+1): if(l[j]>0): m1[i]=j l[j]-=1 c=j break print(s) print(*m1)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/SLUSH" }
d1428
train
# cook your dish here from sys import stdin,stdout a,b=list(map(int,stdin.readline().split())) left=[] top=[] for i in range(a): c,d=list(map(int,stdin.readline().split())) left.append(c) top.append(d) left.sort() top.sort() from bisect import bisect_right as br from bisect import bisect_left as bl row=0 col=0 total=0 cons_x=0 cons_y=0 for i in range(len(left)): cons_x+=(abs(left[i])) cons_y+=(abs(top[i])) total=cons_x+cons_y cc=stdin.readline().rstrip() for i in cc: if i=="R": kk=br(left,col) cons_x=(cons_x+kk-(a-kk)) col+=1 if i=="L": kk=bl(left,col) cons_x=(cons_x+(a-kk)-kk) col-=1 if i=="U": kk=br(top,row) cons_y=(cons_y+kk-(a-kk)) row+=1 if i=="D": kk=bl(top,row) cons_y=(cons_y+(a-kk)-kk) row-=1 stdout.write(str(cons_x+cons_y)) stdout.write("\n")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PPCTS" }
d1429
train
# cook your dish here import heapq as hq from math import floor for _ in range(int(input())): n,a,b,x,y,z=map(int,input().split()) arr=[-int(i) for i in input().split()] days=((z-b-1)//y) ans=0 hq.heapify(arr) curr=a+days*x while curr<z : u=hq.heappop(arr) u=-u if u==0 : break else: curr+=u ans+=1 hq.heappush(arr,-(u//2)) if curr>=z: print(ans) else: print("RIP")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PEWDSVTS" }
d1430
train
# cook your dish here t=int(input()) while(t>0): n=int(input()) b=[int(x) for x in input().split()] p=[float(x) for x in input().split()] s=[0]*(10) yet=2 mx=0 for i in range(n): st=bin(b[i]) rng=len(st)-2 if(rng+2>yet): for ml in range(rng+2-yet): s.append(0) if(rng>mx): mx=rng for k in range(2,rng+2): if(st[k]=='1'): s[rng-k+1]=(s[rng-k+1]*(1-p[i]))+((1-s[rng-k+1])*(p[i])) # else: # s[k-2]=(s[k-2]*1) # print(s) # print(mx) mult=1 ans=0 for i in range(0,mx): ans+=mult*s[i] mult=mult*2 print("%.16f" % ans) t-=1
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/EXPXOR" }
d1431
train
# cook your dish here for _ in range(int(input())): n,k = [int(v) for v in input().split()] ans = (n//2)*(k+2) if n%2 == 0: ans = ans else: ans += 1 + 2*k print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/AGPR2020/problems/ALPR2000" }
d1432
train
m=1000000007 def gcd(a, b): if (a == 0): return b return gcd(b % a, a) def modexp(x, n): if (n == 0) : return 1 elif (n % 2 == 0) : return modexp((x * x) % m, n // 2) else : return (x * modexp((x * x) % m, (n - 1) / 2) % m) def getFractionModulo(a, b): c = gcd(a, b) a = a // c b = b // c d = modexp(b, m - 2) ans = ((a % m) * (d % m)) % m return ans t=int(input()) for i in range(t): n=int(input()) n=n-1 print(getFractionModulo(n-1,n+1))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COW42020/problems/COW3F" }
d1433
train
t = int(input()) for i in range(t): n = int(input()) A = [] for i in range(0, n): A.append([int(i) for i in input().split()]) ones = sum([sum(i) for i in A]) compare = n ans = 0 for i in range(0, n): if ones <= compare: ans = i break compare += 2*(n-1-i) print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/BANDMATR" }
d1434
train
t = int(input()) for i in range(0,t): a = input() b = input() agts=bgts=afour=bfour=aseven=bseven=altf=bltf=afts=bfts=0; for j in a: if j >= '7': if j > '7': agts += 1 else: aseven += 1 elif j >= '4': if j > '4': afts += 1 else: afour += 1 else: altf += 1 for j in b: if j >= '7': if j > '7': bgts += 1 else: bseven += 1 elif j >= '4': if j > '4': bfts += 1 else: bfour += 1 else: bltf += 1 nseven = 0 nfour = 0 if aseven > bfts: aseven -= bfts; nseven += bfts; bfts = 0; else: bfts -= aseven; nseven += aseven; aseven = 0; if bseven > afts: bseven -= afts; nseven += afts; afts = 0; else: afts -= bseven; nseven += bseven; bseven = 0; if aseven > bltf: aseven -= bltf; nseven += bltf; bltf = 0; else: bltf -= aseven; nseven += aseven; aseven = 0; if bseven > altf: bseven -= altf; nseven += altf; altf = 0; else: altf -= bseven; nseven += bseven; bseven = 0; if aseven > bfour: aseven -= bfour; nseven += bfour; bfour = 0; else: bfour -= aseven; nseven += aseven; aseven = 0; if bseven > afour: bseven -= afour; nseven += afour; afour = 0; else: afour -= bseven; nseven += bseven; bseven = 0; nseven += min(aseven,bseven) if afour > bltf: afour -= bltf; nfour += bltf; bltf = 0 else: bltf -= afour; nfour += afour; afour = 0; if bfour > altf: bfour -= altf; nfour += altf; altf = 0 else: altf -= bfour; nfour += bfour; bfour = 0; nfour += min(afour,bfour) print('7'*nseven + '4'*nfour)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/OCT12/problems/LUCKY10" }
d1435
train
# cook your dish here # cook your dish here import numpy as np n, s, q = [int(j) for j in input().split()] edges = [int(j)-1 for j in input().split()] costs = [int(j) for j in input().split()] special = [int(j)-1 for j in input().split()] queries = [[0] * 3 for _ in range(q)] for i in range(q): queries[i] = [int(j)-1 for j in input().split()] edge_set = [[] for _ in range(n)] for i in range(n-1): edge_set[i+1].append(edges[i]) edge_set[edges[i]].append(i+1) stored = np.zeros((s,n,1001),dtype=bool) visited = [[] for _ in range(s)] for i in range(s): s_vertex = special[i] s_cost = costs[s_vertex] s_visited = visited[i] s_visited.append(s_vertex) s_stored = stored[i] s_stored[s_vertex][0] = True s_stored[s_vertex][s_cost] = True for edge in edge_set[s_vertex]: s_visited.append(edge) s_stored[edge] = np.array(s_stored[s_vertex]) for j in range(1,n): vertex = s_visited[j] cost = costs[vertex] s_stored[vertex][cost:1001] = np.logical_or(s_stored[vertex][0:1001-cost],s_stored[vertex][cost:1001]) for edge in edge_set[vertex]: if edge not in s_visited: s_visited.append(edge) s_stored[edge] = np.array(s_stored[vertex]) for i in range(q): first, second, max_cost = queries[i] bool_array = np.zeros(max_cost+2,dtype=bool) for j in range(s): bool_array = np.logical_or(bool_array,np.logical_and(stored[j][first][:max_cost+2],stored[j][second][:max_cost+2])) for j in range(max_cost+1,-1,-1): if bool_array[j]: print(2 * j) break
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/DEMTREE" }
d1436
train
import copy n=int(input()) a=[int(x) for x in input().split()] b=[int(x) for x in input().split()] c=[] d=[] lcs=[] def lcsfn(a,c,corda,cordb): for i in range(n+1): d.append([0]*(n+1)) lcs.append([0]*(n+1)) for i in range(1,n+1): for j in range(1,n+1): if a[i-1]==c[j-1]: lcs[i][j]=lcs[i-1][j-1]+1 d[i][j]='d' elif lcs[i-1][j]>lcs[i][j-1]: lcs[i][j]=lcs[i-1][j] d[i][j]='u' else: lcs[i][j]=lcs[i][j-1] d[i][j]='l' i=n j=n cost=0 while i>=1 and j>=1: if d[i][j]=='d': corda.append(a[i-1]) cordb.append(b[j-1]) i-=1 j-=1 cost+=1 elif d[i][j]=='l': j-=1 elif d[i][j]=='u': i-=1 return cost ma=-10**9 p1=[] p2=[] for i in range(-1000,1001): c=[] corda=[] cordb=[] for j in range(n): c.append(b[j]+i) p=lcsfn(a,c,corda,cordb) if ma<p: ma=p p1=copy.deepcopy(corda) p1=p1[::-1] p2=copy.deepcopy(cordb) p2=p2[::-1] print(ma) print(*p1) print(*p2)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/IARCSJUD/problems/EXPT" }
d1437
train
# cook your dish here for _ in range(int(input())): g=input() h=g[::-1] if h==g : print(1) else: print(2)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/HHAL" }
d1438
train
import math def findnumber(l,n): l.sort() x = l[0] * l[-1] vec = [] i = 2 while (i*i)<=x: if x%i==0: vec.append(i) if x//i !=i: vec.append(x//i) i = i + 1 vec.sort() if len(vec)!=n: return -1 else: j = 0 for it in range(n): if(l[j] != vec[it]): return -1 else: j += 1 return x def __starting_point(): t = int(input()) while t: n = int(input()) arr = list(map(int,input().split())) n = len(arr) print(findnumber(arr,n)) print() t=t-1 __starting_point()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/BRBG2020/problems/FTNUM" }
d1439
train
# cook your dish here def prime_factors(n): i = 2 factors =set() while i * i <= n: if n % i: i += 1 else: n //= i factors.add(i) if n > 1: factors.add(n) return factors for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) ans=0 s=[] for i in range(n): s.append(sum(prime_factors(a[i]))) for i in range(n): for j in range(n): if i!=j and a[j]%a[i]==0 and s[j]%s[i]==0: ans=ans+1 print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PRMDIV" }
d1440
train
import math dp = [] dp.append(0) for i in range(1,1000005): dp.append(math.log(i) + dp[i-1]) t = int(input()) for i in range(t): n,m,p,k = input().split() n = int(n) m = int(m) p = int(p) k = int(k) if p==0 or (n%2==0 and m%2==0): ans = 1.0 print(ans) elif n%2==1 and m%2==1: ans=0.0 print(ans*100) else: P = 0 kln2 = k*math.log(2) for i in range(p, k+1): lnPi = dp[k] - dp[i] - dp[k-i] - kln2 Pi = pow(math.e, lnPi) P += Pi print(P)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CHEFYODA" }
d1441
train
# cook your dish here t=int(input()) for i in range(t): n=int(input()) a=list(map(int,input().split())) print(min(a))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/EXUNA" }
d1442
train
for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a = [-1] + a[::-1] mx = a.index(max(a)) dp = [0] * (n + 1) for i in range(1, n + 1): for x in b: if i - x < 0: continue if i - x < mx <= i: dp[i] = 1 else: dp[i] |= not dp[i - x] print('Chef' if dp[-1] else 'Garry')
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CLGAME" }
d1443
train
# cook your dish here for _ in range(int(input())): a=int(input()) print(a/2+2)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ECPG2020/problems/ECPC10C" }
d1444
train
for i in range(int(input())): n,k=map(int,input().split()) m=[] for j in range(n): l=list(input()) m.append(l) a=0 for k in range(k): b=0 for p in range(n): if m[p][k]=='1': b+=1 if b>1: a+=((b*(b-1))//2) print(a)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/LCOLLIS" }
d1445
train
for _ in range(int(input())): num=int(input()) arr=list(map(int,input().split())) dp=[0]*num dp[0]=1 ans=1 j=0 for i in range(1,num): j=i+1 count=1 dp[i]=dp[i-1]%1000000007 if i-2>=0 and arr[i-2]==2: dp[i]+=dp[i-2]%1000000007 if i-3>=0 and arr[i-3]==2: dp[i]+=dp[i-3] ans+=dp[i]%1000000007 if arr[i-1]==2 and i<num-1: if i>=j or j==0: j=i+1 while j<num and arr[j]==2: j+=1 count=j-i while j<len(arr) and arr[j]==2: j+=1 count+=1 if j==num: ans+=dp[i-1]*(count-1)%1000000007 elif count%2!=0: if j<num-1 and arr[j+1]==2: ans+=dp[i-1]*(count+1)%1000000007 else: ans+=dp[i-1]*(count)%1000000007 elif count%2==0: ans+=dp[i-1]*(count-1)%1000000007 print(ans%1000000007)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CHEFSOC" }
d1446
train
t= int(input()) for _ in range(t): n = int(input()) ar = [] y = [] for i in range(n): ar.append( list(map(int,input().split())) ) y.append(ar[-1][1]) ar[-1].append(i) y.sort() mny = y[0] mxy = y[-1] ar.sort() ssx,ssy,ssi = ar[0] bbx,bby,bbi = ar[-1] sbx,sby,sbi = ar[0] bsx,bsy,bsi = ar[-1] for i in range(len(ar)): if ar[i][0]>ssx: sbx,sby,sbi = ar[i-1] break for i in range(len(ar)-1,-1,-1): if ar[i][0]<bsx: bsx,bsy,bsi = ar[i+1] break if (ssy <=mny): print(1) print(ssi+1,'NE') continue if (sby>=mxy): print(1) print(sbi+1,'SE') continue if (bsy <=mny): print(1) print(bsi+1,'NW') continue if (bby>=mxy): print(1) print(bbi+1,'SW') continue print(2) if(ssy<bby): print(ssi+1,'NE') print(bbi+1,'SW') else: print(ssi+1,'SE') print(bbi+1,'NW')
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/LIGHTHSE" }
d1447
train
# from math import log2 # N = 10000 # for i in range(1,N): # # print(i) # for m in range(i): # if( (m^(m+1))==i ): # print(i) # print(m,m+1,bin(m)[2:]) # print() # break # # else: # # print(-1) # # print() T = int(input()) ans = [] for _ in range(T): N = int(input()) # x = log2(N+1) if(N==1): ans.append(2) elif('0' not in bin(N)[2:]): ans.append(N//2) else: ans.append(-1) for i in ans: print(i)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/XORNUBER" }
d1448
train
# cook your dish here for __ in range(int(input())): n=int(input()) arr=list(map(int,input().split())) d={} s=set() flag=0 for i in range(n): if arr[i] in list(d.keys()): d[arr[i]]+=1 else: d[arr[i]]=1 curr_ele=arr[i] if (curr_ele in s) and arr[i-1]!=arr[i]: flag=1 break else: s.add(arr[i]) c=list(d.values()) if len(c)!=len(set(c)): flag=1 if flag==1: print("NO") else: print("YES")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CHEFRECP" }
d1449
train
# cook your dish here t = int(input()) for _ in range(t): a,d,k,n,inc = map(int, input().strip().split()) res = a for i in range(1, n): if i%k == 0: d += inc res += d print(res)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/CFUS2020/problems/CFS2001" }
d1450
train
x=eval(input()) for x in range(0,x): ans=0 d=input() a=0 cont=0 for i in range(0,len(d)): a+=len(d)-i if d[i]=='7': ans+=1+cont cont+=1 else: cont=0 ans=a-ans print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COOK22/problems/LUCKYBAL" }
d1451
train
# cook your dish here for _ in range(int(input())): n = int(input()) a = list(map(int,input().split())) a.sort() i=1 while(i<n): a[i-1],a[i] = a[i],a[i-1] i+=2 print(*a)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/PBK32020/problems/ITGUY30" }
d1452
train
# cook your dish here for _ in range(int(input())): N, M = [int(x) for x in input().split()] edges = [0]*M dir = {} nodes = [[] for j in range(N+1)] ind = [0]*(N+1) graph = {} final_edges = [] for i in range(M): u, v = [int(x) for x in input().split()] nodes[u].append(v) nodes[v].append(u) dir[(u,v)]=1 dir[(v,u)] = 0 ind[v] += 1 graph[(u,v)] = graph[(v,u)] = i final_edges.append([u,v]) if M%2!=0: print(-1) continue for i in range(M): u, v = final_edges[i] if ind[u]%2!=0 and ind[v]%2!=0: d = dir[(u,v)] if d: ind[u] += 1 ind[v] -= 1 dir[(u,v)] = 0 dir[(v,u)] = 1 edges[i] = abs(edges[i]-1) else: ind[u] -= 1 ind[v] += 1 dir[(u, v)] = 1 dir[(v, u)] = 0 edges[i] = abs(edges[i]-1) s = [] for i in range(1, N+1): if ind[i]%2: s.append(i) while s: set1 = set() for u in s: if ind[u]%2: v = nodes[u][0] d = dir[(u,v)] index = graph[(u, v)] set1.add(v) if d: ind[u] += 1 ind[v] -= 1 dir[(u, v)] = 1 dir[(v, u)] = 1 edges[index] = abs(edges[index]-1) else: ind[u] -= 1 ind[v] += 1 dir[(u, v)] = 1 dir[(v, u)] = 0 edges[index] = abs(edges[index]-1) s = set1 print(*edges)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/EDGEDIR" }
d1453
train
#read input cases = int(input()) caselist = [] for i in range(0, cases): caselist.append(input()) #iterate each case for j in range(0, cases): #current case's parameters: current_input = caselist[j].split(' ') bots = int(current_input[0]) switch = int(current_input[1]) #generate botlist and cakelist botlist = list(range(switch, bots)) + list(range(0, switch)) cakelist = [False] * bots counter = 0 index = 0 for i in range(0,bots): if cakelist[index] == False: cakelist[index] = True counter += 1 index = botlist[index] else: break if counter == bots: print("Yes") else: print("No", counter)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/NOV15/problems/EGRCAKE" }
d1454
train
n, q, k = map(int, input().split()) arr = list(map(int, input().split())) query = list(input()) q_ = len(query) c1 = query.count('?') c = arr.count(0) if c == n: for i in range(c1): print(0) else: for i in range(q_): if (i!=0) and (query[i] == '?' and query[i-1] == '?'): print(max_c) elif query[i] == '?': max_c = cnt = 0 for j in range(n): if (j != n - 1) and (arr[j] == 1 and arr[j + 1] == 1): cnt += 1 else: max_c = max(max_c, cnt + 1) cnt = 0 if k < max_c: max_c = k break print(max_c) elif query[i] == '!': temp = arr[n - 1] del arr[n - 1] arr.insert(0, temp)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/HMAPPY1" }
d1455
train
import numpy as np N=10**6+1 t=eval(input()) inp = () t1=ord('z') #bag=[[0 for _ in xrange(t1)] for _ in xrange(N+1)] bag=np.zeros((N+1,t1),dtype=np.int) #print bag while t: t-=1 inp=input().split() t2=ord(inp[3]) - ord('a') t3=int(inp[1]) t4=int(inp[2]) + 1 if inp[0]=="1": #print "enter" bag[t3][t2]+=int(inp[2]) if inp[0]=="2": sum=0 for i in range(t3,t4): sum+=bag[i][t2] print(sum) # # for j in range(ord('z')-ord('a')): # for i in range(N+1): # if bag[i][j]!=0: # print bag[i][j] ,i,j
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/SIC2016/problems/SANTA" }
d1456
train
n=eval(input()) grades=list(map(int,input().split())) m=eval(input()) for df in range(m): x,y=list(map(int,input().split())) arr=[] arr=grades[x-1:y] arr.sort() sum=0 #arr.append(1000000) for nh in range(0,len(arr)-1,1): sum=sum+(arr[nh+1]-arr[nh])**2 #print sum,len(arr),nh+1,nh print(sum)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/LTIME29/problems/STDPRGS" }
d1457
train
import math def GLR(x): summation_N = (x*(x+1))//2 initial = x power = 0 sum_A = 0 while x>=1: count = (x+1)//2 sum_A += count * 2**power x = x - count power += 1 sum_B = summation_N - sum_A ans = sum_B - (int(math.log(initial,2))+1) return ans for _ in range(int(input())): l,r = list(map(int,input().split())) if l==1: print(GLR(r)) else: print((GLR(r) - GLR(l-1)))# cook your dish here
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/XORIT" }
d1458
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. (n, k) = list(map(int, input().split(' '))) ans = 0 for i in range(n): x = int(input()) if x % k == 0: ans += 1 print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/INTEST" }
d1459
train
# cook your dish here t=int(input()) for i in range(t): a=0 n=int(input()) while(n>0): a += n*n n=n-2 print(a)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PCJ18B" }
d1460
train
import math n,m = map(int, input().split()) hyp = math.sqrt(1+m*m) cosx = 1/hyp sinx = m/hyp pts = [[], []] for i in range(n): p = input().split() px = int(p[0]) py = int(p[1]) pts[0].append(cosx*px+sinx*py) pts[1].append(cosx*py-sinx*px) w = max(pts[0])-min(pts[0]) l = max(pts[1])-min(pts[1]) print(2*l+2*w)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PCJ18F" }
d1461
train
# cook your dish here l=[int(k) for k in input().split()] s=[int(k) for k in input().split()] x=l[1]*l[0] for i in range(l[0]): if(s[i]==1): x+=l[2] elif(s[i]==2): x+=(l[2]*98/100) elif(s[i]==3): x+=(l[2]*96/100) elif(s[i]==4): x+=(l[2]*94/100) elif(s[i]==5): x+=(l[2]*92/100) elif(s[i]==6): x+=(l[2]*90/100) if(x>=300): print("YES") else: print("NO")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/TAWY2020/problems/TAWY002" }
d1462
train
from sys import stdin from fractions import Fraction input = stdin.readline for _ in range(int(input())): a, b, n = list(map(int, input().split())) ab = Fraction(a, b) p = set() for i in range(1, n+1): for j in range(n, 0, -1): x = Fraction(i, j) if x > ab: break p.add(x) x = sorted(p)[-2] print(x.numerator, x.denominator)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/QTST2020/problems/NEGBFRAC" }
d1463
train
# cook your dish here try: t=int(input()) for i in range(t): n=input() n=n.lower() a="berhampore" b="serampore" if a in n: if b in n: print("Both") else: print("GCETTB") elif b in n: if a in n: print("Both") else: print("GCETTS") else: print("Others") except Exception as e: pass
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COMT2020/problems/BHPORSRP" }
d1464
train
def ugcd(n): ans = [[1]] if(n==1): return ans elif(n%2==1): ans = [[1, 2, n]] else: ans = [[1, 2]] for k in range(1, int(n//2)): ans.append([k*2+1, k*2+2]) return ans t = int(input()) for i in range(t): n = int(input()) s = (ugcd(n)) print(len(s)) for j in range(len(s)): print(len(s[j]), end=" ") print(*s[j])
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/UNITGCD" }
d1465
train
t=int(input()) li1=[31,29,31,30,31,30,31,31,30,31,30,31] li2=[31,28,31,30,31,30,31,31,30,31,30,31] for z in range(t): y,m,d=list(map(int,input().split(':'))) if y%4 == 0: if y%100 == 0: if y%400 == 0: li=li1 else: li=li2 else: li=li1 else: li=li2 c=0 if d%2 == 0: while d%2 == 0: c+=1 d+=2 if d>li[m-1]: d=d%li[m-1] m+=1 else: while d%2 != 0: c+=1 d+=2 if d>li[m-1]: d=d%li[m-1] m+=1 print(c)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/MEDIC" }
d1466
train
import sys def powc(x,n,m): res = 1 xx=x while n: if n&1: res = (res*xx)%m xx=xx*xx%m n >>= 1 return res def circles(u): r = 0 S = [(u,-1,0)] Visited[u] = 0 for s in S: for e in V[s[0]]: if e[0] != s[1]: if Visited[e[0]]==-1: Visited[e[0]] = s[2]^e[1] S.append((e[0], s[0], s[2]^e[1])) elif Visited[e[0]] != s[2]^e[1]: return -1 else: r += s[0]<e[0] return r T = int(sys.stdin.readline()) for _ in range(T): is_bad = False empty = 0 n,Q = list(map(int, sys.stdin.readline().split())) for _ in range(n-1): sys.stdin.readline() paths = [] V=list(map(list,[[]]*n)) for q in range(Q): u,v,x = list(map(int, sys.stdin.readline().split())) u-=1 v-=1 if (v,x^1) in V[u]: is_bad = True elif (v,x) in V[u]: empty += 1 elif u!=v: V[u].append((v,x)) V[v].append((u,x)) elif x==1: is_bad = True else: empty += 1 paths.append((u,v,x)) if is_bad: print(0) elif n<=1: print(1) else: Visited = [-1]*n components = 0 for i in range(n): if Visited[i]==-1: components += 1 c = circles(i) if c==-1: is_bad = True break empty += c if is_bad: print(0) else: print(powc(2,n-1-(Q-empty),10**9+7))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/MARCH16/problems/PARITREE" }
d1467
train
# cook your dish here n,q=map(int,input().split()) ls=[int(i) for i in input().split()] cur=0 s=[0] for i in ls: cur=cur^i s.append(cur) for i in range(q): k=int(input()) print(s[k%(n+1)])
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/NBONACCI" }
d1468
train
# cook your dish here def guessingGame (l): a = [] m = 1000000001 for i in range (len(l)): k=int(l[i][1]) if (l[i][0]=='<' and l[i][2]=='Yes'): a.append((1,1)) a.append((k,-1)) if (l[i][0]=='<' and l[i][2]=='No'): a.append((k,1)) a.append((m,-1)) if (l[i][0]=='=' and l[i][2]=='Yes'): a.append((k,1)) a.append((k+1,-1)) if (l[i][0]=='=' and l[i][2]=='No'): a.append((1,1)) a.append((k,-1)) a.append((k+1,1)) a.append((m,-1)) if (l[i][0]=='>' and l[i][2]=='Yes'): a.append((k+1,1)) a.append((m,-1)) if (l[i][0]=='>' and l[i][2]=='No'): a.append((1,1)) a.append((k+1,-1)) a.sort() w=0 r=0 for i in range (len(a)): w+=a[i][1] r=max(w,r) return len(l)-r def __starting_point(): T = int(input()) answer = [] for _ in range (T): e = int(input()) temp = [] for q_t in range (e): q = list(map(str,input().rstrip().split())) temp.append(q) result = guessingGame(temp) print(result) __starting_point()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/A3" }
d1469
train
# cook your dish here try: t=int(input()) for i in range(t): s=input() i=int(s,16) print(i) except EOFError as e: print(e)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/JLUG2020/problems/HXTDC" }
d1470
train
try: tc=int(input()) for _ in range(tc): n=int(input()) st="" b=1 for i in range(1,n+1): b+=1 a=b for j in range(1,n+1): print(a,end='') a+=1 print() except: pass
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/PTRN2021/problems/ITGUY53" }
d1471
train
# cook your dish here for _ in range(int(input())): s=input() count=0 i=0 while i<len(s)-1: ch=s[i] j=i+1 while j<len(s) and s[j]==ch: j+=1 l=j-i if i!=0 and j!=len(s) and s[i-1]==s[j] : count+=1 count+=l*(l-1)//2 #print(s[i:j],count) i=j print(count)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CK87GSUB" }
d1472
train
import sys def spaces(a,n,m,k,visit1,visit2,dist,position): queue = [position] lastedit = [] dist[position[0]][position[1]] = 0 while queue!=[]: point = queue[0] i = point[0] j = point[1] #print 'point',i,j if visit1[i][j]==False: visit1[i][j] = True startx = max(i-k,0) endx = min(i+k,n-1) for x in range(startx,endx+1): starty = max(0,j+abs(x-i)-k) endy = min(m-1,j-abs(x-i)+k) for y in range(starty,endy+1): if (a[x][y]==0 and visit1[x][y]==False): if visit2[x][y]==True: lastedit.append([x,y]) #print x,y, if dist[x][y]>dist[i][j]+1: dist[x][y]=dist[i][j]+1 queue.append([x,y]) #print queue,dist queue = queue[1:] #print return lastedit for t in range(int(input())): n,m,k1,k2 = list(map(int,input().split())) a = [] for i in range(n): a.append(list(map(int,input().split()))) #print a value = sys.maxsize listing = [] visit1 = [[False for i in range(m)]for j in range(n)] visit2 = [[False for i in range(m)]for j in range(n)] dist1 = [[sys.maxsize for i in range(m)]for j in range(n)] dist2 = [[sys.maxsize for i in range(m)]for j in range(n)] if k1>=k2: spaces(a,n,m,k1,visit1,visit2,dist1,[0,0]) else: spaces(a,n,m,k2,visit1,visit2,dist1,[0,m-1]) listing = spaces(a,n,m,k1,visit2,visit1,dist2,[0,0]) if k1>k2: listing = spaces(a,n,m,k2,visit2,visit1,dist2,[0,m-1]) #print visit1 #sprint visit2 if k1==k2: if dist1[0][m-1]==sys.maxsize: print('-1') else: print(int((dist1[0][m-1]+1)/2)) else: d = len(listing) for i in range(d-1,-1,-1): x = listing[i][0] y = listing[i][1] if visit1[x][y]==True and dist2[x][y]<value: value = dist2[x][y] if value!=sys.maxsize: print(value) else: print('-1')
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/JULY16/problems/CHEFARC" }
d1473
train
n=int(input()) a=[] b=[] for i in range(1,1000001): s = str(i) p=1 flag=0 for e in s: if e=='1': flag=1 p=p*int(e) if p==n: if flag!=1: a.append(i) else: b.append(i) print(len(a),len(b))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/CZEN2020/problems/NHSPN" }
d1474
train
#!/usr/bin/python import sys def __starting_point(): t = int(input()) for iteration in range(t): r,c,m_inp,k_inp,j_inp = input().strip().split(" ") r=int(r) c=int(c) m_inp=int(m_inp) k_inp=int(k_inp) j_inp=int(j_inp) ans = "" if (r*c) != (m_inp+k_inp+j_inp): print("No") continue else: flag = False for i in range(6): if flag: break if i==0: m = m_inp k = k_inp j = j_inp elif i==1: m = j_inp k = m_inp j = k_inp elif i==2: m = k_inp k = j_inp j = m_inp elif i==3: m = m_inp k = j_inp j = k_inp elif i==4: m = k_inp k = m_inp j = j_inp elif i==5: m = j_inp k = k_inp j = m_inp if m%r == 0: r_remain_1 = r c_remain_1 = c-(m/r) if k%r_remain_1 == 0: r_remain_2 = r_remain_1 c_remain_2 = c_remain_1 - (k/r_remain_1) if r_remain_2*c_remain_2 == j: print("Yes") flag = True continue if k%c_remain_1 == 0: c_remain_2 = c_remain_1 r_remain_2 = r_remain_1 - (k/c_remain_1) if r_remain_2*c_remain_2 == j: print("Yes") flag = True continue if j%r_remain_1 == 0: r_remain_2 = r_remain_1 c_remain_2 = c_remain_1 - (j/r_remain_1) if r_remain_2*c_remain_2 == k: print("Yes") flag = True continue if j%c_remain_1 == 0: c_remain_2 = c_remain_1 r_remain_2 = r_remain_1 - (j/c_remain_1) if r_remain_2*c_remain_2 == k: print("Yes") flag = True continue if m%c == 0: c_remain_1 = c r_remain_1 = r-(m/c) if k%r_remain_1 == 0: r_remain_2 = r_remain_1 c_remain_2 = c_remain_1 - (k/r_remain_1) if r_remain_2*c_remain_2 == j: print("Yes") flag = True continue if k%c_remain_1 == 0: c_remain_2 = c_remain_1 r_remain_2 = r_remain_1 - (k/c_remain_1) if r_remain_2*c_remain_2 == j: print("Yes") flag = True continue if j%r_remain_1 == 0: r_remain_2 = r_remain_1 c_remain_2 = c_remain_1 - (j/r_remain_1) if r_remain_2*c_remain_2 == k: print("Yes") flag = True continue if j%c_remain_1 == 0: c_remain_2 = c_remain_1 r_remain_2 = r_remain_1 - (j/c_remain_1) if r_remain_2*c_remain_2 == k: print("Yes") flag = True continue if not flag: print("No") __starting_point()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/SNCKEL16/problems/VCAKE" }
d1475
train
T = int(input()) def call_me(N,A,X): max = 0 ans = '' for i in A: if i.count(X) > max: max = i.count(X) ans = i return ans for i in range(T): N = int(input()) A = list(map(str,input().split())) X = input() print(call_me(N,A,X))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/CFSN2015/problems/LINNUM" }
d1476
train
x = input().split(" ") y = input() ans = '' l = 1 for i in x: if i!=y and sorted(i) == sorted(y): ans = ans + (str)(l) l=l+1 ans+='.' print("The antidote is found in",ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ITRA2016/problems/ITRA10" }
d1477
train
# cook your dish here from collections import Counter from math import factorial for _ in range(int(input())): s=input() c=Counter(s) k=factorial(len(s)) for value in c.values(): if value>1: k=k//factorial(value) print(k%(10**9+7))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/WCOUNT" }
d1478
train
for _1 in range(int(input())): n=int(input()) s=input().strip() answer=s for i in range(len(s)): c=s[i] string=s[:i]+s[i+1:] for j in range(len(string)+1): answer=min(answer, string[:j]+c+string[j:]) print(answer)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/DIVGOLD" }
d1479
train
# cook your dish here from math import gcd for _ in range(int(input())): n,a,k,min_k,e = int(input()),[int(i) for i in input().split()],0,0,-1 for j in range(n): if(a[j] != -1):break for i in range(j,n): if min_k==0:min_k,e = a[i],a[i]+1 else: if min_k < a[i]:min_k = a[i] if(a[i] == -1):pass else: if(a[i] == e):pass else: if( k == 0):k = e-a[i] else: new_k = e-a[i] if(new_k < 0):k = -1 else:k = gcd(k,new_k) if(k<min_k or k<0): k = -1; break if k != 0 and a[i]!=-1: e = a[i]%k+1 else:e += 1 if(k == -1):print("impossible") elif k == 0 :print("inf") else:print(k)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PERIODIC" }
d1480
train
# cook your dish here p=int(input()) for z in range(p): n=int(input()) a=[] for i in range(8): a.append(0) for i in range(n): x,y=list(map(int,input().split())) if x<=8 and y>a[x-1]: a[x-1]=y print(sum(a))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/WATSCORE" }
d1481
train
import math def dist(w,x,y,z): return math.hypot(y - w, z - x) t = int(input()) while (t>0): t = t -1 n, m = list(map(int,input().split())) a = [] for i in range(0,n): x,y = list(map(int,input().split())) a.append([x,y]) for j in range(0,m): p,q,r,s = list(map(int,input().split())) nearest = -1 distance = 10000000000 for i in range(0,n): way = dist(a[i][0],a[i][1],p,q) if way < distance: distance = way nearest = i print(nearest + 1) a[nearest][0] = r a[nearest][1] = s
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/SIC2016/problems/NEAR" }
d1482
train
t=int(input()) for i in range(t): s=input() zeroes=s.count('0') ones=s.count('1') if (len(s)%2==1 or zeroes==0 or ones==0): ans= -1 else: ans=abs(zeroes-ones)//2 print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/SDSTRING" }
d1483
train
for i in range(int(input())): n = int(input()) q = "1"+"0"*(n//2) print(1,q)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PINS" }
d1484
train
from math import gcd n, k = list(map(int, input().split())) a = [] for i in range(k): try: a += list(map(int, input().split())) except: pass ans = n for i in range(1, 2**k): b = bin(i)[2:].rjust(k, "0") c = [] for j in range(k): if(b[j] == '1'): c.append(a[j]) lcm = c[0] for j in c[1:]: lcm *= j // gcd(lcm, j) temp = ((n - 1) // lcm) + 1 if(b.count('1')&1): ans -= temp else: ans += temp print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/IARCSJUD/problems/LEAFEAT" }
d1485
train
for _ in range(int(input())): s = list(input().strip()) i = 0 while i < len(s) - 1: if s[i].isalpha() or s[i] == ')': if s[i + 1].isdigit(): if i + 2 >= len(s) or s[i + 2] == ')': s = s[:i+1] + ['*', s[i+1]] + s[i+2:] else: s = s[:i+1] + ['*', s[i+1], '+'] + s[i+2:] i += 1 elif s[i + 1].isalpha() or s[i + 1] == '(': s = s[:i+1] + ['+'] + s[i+1:] i += 1 s = ''.join(s) s = s.strip('+') x = 2 y = 4 z = 10 print(eval(s))
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/CORS2020/problems/PENEVAL" }
d1486
train
# cook your dish here for _ in range(int(input())): n=int(input()) l1=[] l2=[] for i in range(n): s=input() a=s[ :n//2].count('1') b=s[n//2: ].count('1') if a>b: l1.append(a-b) elif a<b: l2.append(b-a) p=sum(l1) q=sum(l2) if p==q: print(0) elif p>q: diff=p-q flag=0 for i in range(diff//2, 0, -1): a=diff-i if (i in l1) or (a in l1): print(abs(a-i)) flag=1 break if flag==0: print(diff) else: diff=q-p flag=0 for i in range(diff//2, 0, -1): a=diff-i if (i in l2) or (a in l2): print(abs(a-i)) flag=1 break if flag==0: print(diff)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/PEPPERON" }
d1487
train
for j in range(int(input())): input() a = list(map(int,input().split())) marks = 0 backlok = 0 top_marks = max(a) topper = [] for i in range(len(a)): if(a[i] >= 31): marks+=a[i] if(a[i]<31): backlok+=1 if(a[i] == top_marks): topper.append(i) print(backlok, "{:0.2f}".format(marks/len(a),2)) topper.sort(reverse=True) for i in topper: print(i," ") for i in a: print(top_marks-i)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/UCOD2020/problems/DSEXAM" }
d1488
train
import sys # stdin = open("testdata.txt", "r") ip = sys.stdin def solve(C, n, x): if n==1: return (1, 0) b1, b2 = 1, 1 a , b = C[0], C[-1] while b1 + b2 < n: if a < b*x: a += C[b1] b1 += 1 elif a > b*x: b2 += 1 b += C[n-b2] else: if b1 >= b2: a += C[b1] b1 += 1 else: b2 += 1 b += C[b2] return (b1, b2) t = int(ip.readline()) for _ in range(t): n = int(ip.readline()) C = list(map(int, ip.readline().split())) x = int(ip.readline()) ans = solve(C, n, x) print(*ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/BIT2B" }
d1489
train
from itertools import permutations for _ in range(int(input())): N,K=list(map(int,input().split())) arr=list(map(int,input().split())) arr1=[] arr2=[] for i in range(1,len(arr)+1): arr1.append(i) indexzero=[] for i in range(0,len(arr)): if(arr[i]==0): indexzero.append(i) else: arr2.append(arr[i]) # arr3 = [x for x in arr1 if x not in arr2] arr3= list(set(arr1)-set(arr2)) result=permutations(arr3) perm=[] for i in result: perm.append(i) step=0 count=0 for p in range(0,len(perm)): temp=[] for q in range(0,len(arr)): if(arr[q]==0): temp.append(perm[p][step]) step+=1 else: temp.append(arr[q]) k=0 step=0 for m in range(0,len(temp)-1): if(temp[m]<temp[m+1]): k+=1 if(k==K): count+=1 print(count)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/GOODPERM" }
d1490
train
a,b=[int(_) for _ in input().split()] if b==0: print(a) else: l=[] a=str(a) for i in range(len(a)): l.append(a[i]) for i in range(len(l)): if b==0: break if l[i]=='9': continue else: l[i]='9' b-=1 s='' for i in l: s+=i print(s)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/SPRT2020/problems/STCKSCAM" }
d1491
train
from math import ceil t=int(input()) for i in range(t): p=int(input()) l=list(map(int,input().split())) maxx=1 for i in range(len(l)): maxx=max(maxx,l.count(l[i])) if(maxx*2>p): print(maxx) else: q=p-maxx*2 maxx+=ceil(q/2) print(maxx)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COFY2020/problems/GKSMNRSK" }
d1492
train
def permutate(arr): if len(arr) == 1: yield arr for x in range(len(arr)): for perm in permutate(arr[:x] + arr[x+1:]): yield [arr[x]] + perm vals = [int(x) for x in input().split()] founded = False for val in permutate(vals): if (val[0] / float(val[1]) == val[2] / float(val[3])): print("Possible") founded = True break if not founded: print("Impossible")
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/COOK72/problems/CHEFCBA" }
d1493
train
t = int(input()) for j in range(0, t): n = int(input()) m = 100 for i in range(0, n): str = input() p = min(str.count("a",0,len(str)),str.count("b",0,len(str))) if (m > p): m = p print(m) t = t-1
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/DEC15/problems/ORACLCS" }
d1494
train
def outOfIndex(boys,girls,COST): if COST == 0: return len(boys) else: total_cost = [ abs(x-y) for x,y in zip(boys,girls)] total_cost = sum(total_cost) return total_cost for _ in range(int(input())): COST = int(input()) queue = input() B = queue.count('B') G = queue.count('G') boys=[] girls = [] if (abs(B-G)>1): print(-1) else: if B > G: for c in range(len(queue)): if c%2!=0 and queue[c]=='B': boys.append(c) if c%2==0 and queue[c] =='G': girls.append(c) print(outOfIndex(boys,girls,COST)) boys.clear() girls.clear() elif B < G: for c in range(len(queue)): if c%2!=0 and queue[c]=='G': girls.append(c) if c%2==0 and queue[c] =='B': boys.append(c) print(outOfIndex(boys,girls,COST)) boys.clear() girls.clear() else: for c in range(len(queue)): if c%2!=0 and queue[c]=='B': boys.append(c) if c%2==0 and queue[c] =='G': girls.append(c) attempt1 = outOfIndex(boys,girls,COST) boys.clear() girls.clear() for c in range(len(queue)): if c%2!=0 and queue[c]=='G': girls.append(c) if c%2==0 and queue[c] =='B': boys.append(c) attempt2 = outOfIndex(boys,girls,COST) print(min(attempt1,attempt2)) boys.clear() girls.clear()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/DEVCLASS" }
d1495
train
n=int(input()) l=[0]*n for i in range(n): l[i]=int(input()) l.sort() s=0 i=n-1 while i>=0: x=2*l[i] if l[-1]>=x: j=i while j<len(l): if l[j]>=x: l.pop(j) l.pop(i) s+=1 break j+=1 i-=1 s+=len(l) print(s)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/POPU2021/problems/BXRED" }
d1496
train
t=int(input()) for i in range(0,t): n=int(input()) lis=list(map(int,input().split())) lis2=[] for j in range(0,10): lis2.append(0) for j in range(0,len(lis)): lis2[lis[j]]+=1; s=sum(lis) while s%3!=0: if s%3==2: if lis2[2]>=1: lis2[2]-=1 s=s-2 elif lis2[5]>=1: lis2[5]-=1 s=s-5 elif lis2[8]>=1: lis2[8]-=1 s=s-8 elif lis2[1]>=2: lis2[1]-=2 s=s-2 elif lis2[1]>=1 and lis2[4]>=1: lis2[1]-=1 lis2[4]-=1 s=s-5 elif lis2[4]>=2: lis2[4]-=2 s=s-8 elif lis2[1]>=1 and lis2[7]>=1: lis2[1]-=1 lis2[7]-=1 s=s-8 elif lis2[4]>=1 and lis2[7]>=1: lis2[4]-=1 lis2[7]-=1 s=s-11 elif lis2[7]>=2: lis2[7]-=2 s=s-14 elif s%3==1: if lis2[1]>=1: lis2[1]-=1 s=s-1 elif lis2[4]>=1: lis2[4]-=1 s=s-4 elif lis2[7]>=1: lis2[7]-=1 s=s-7 elif lis2[2]>=2: lis2[2]-=2 s=s-4 elif lis2[5]>=1 and lis2[2]>=1: lis2[2]-=1 lis2[5]-=1 s=s-7 elif lis2[5]>=2: lis2[5]-=2 s=s-10 elif lis2[2]>=1 and lis2[8]>=1: lis2[2]-=1 lis2[8]-=1 s=s-10 elif lis2[8]>=1 and lis2[5]>=1: lis2[8]-=1 lis2[5]-=1 s=s-13 elif lis2[8]>=2: lis2[8]-=2 s=s-16 lis3=[] for j in range(1,10): if lis2[j]>=1: for k in range(0,lis2[j]): lis3.append(j) lis3.reverse() for k in range(0,lis2[0]): lis3.append(0) sol='' for k in range(0,len(lis3)): sol+=str(lis3[k]) print(sol)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/LOCJUL16/problems/NUMHOLMS" }
d1497
train
# cook your dish here MOD=10**9+7 for _ in range(int(input())): s=input() ind=1 level=1 for i in range(len(s)): if s[i]=='l': if level%2==1: ind=ind*2 else: ind=ind*2-1 if s[i]=='r': if level%2==1: ind=ind*2+2 else: ind=ind*2+1 level+=1 ind%=MOD print(ind)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/problems/CHEFLR" }
d1498
train
for t in range(int(input())): n = int(input()) mx = -1 for i in range(n): h, m, s = list(map(int,input().split(":"))) h %= 12 m %= 60 s %= 60 ha = h*30 + m*0.5 + s*0.5/60 ma = m*6 + s*0.1 sa = s*6 hm1 = abs(ha - ma) hm2 = 360 - hm1 hm3 = abs(hm1 - hm2) hm = min(hm1, hm2, hm3) ms1 = abs(ma - sa) ms2 = 360 - ms1 ms3 = abs(ms1 - ms2) ms = min(ms1, ms2, ms3) sh1 = abs(sa - ha) sh2 = 360 - sh1 sh3 = abs(sh1 - sh2) sh = min(sh1, sh2, sh3) avg = (hm + ms + sh) / 3 if (mx < avg): ans = i+1 mx = avg print(ans)
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/ITRA2016/problems/ITRA06" }
d1499
train
def Testcase(): h,x,y = [int(x) for x in input().strip().split()] h = h-1 yt = h//y +1 # print(yt) flag=0 ans = 100000000009 for i in range(0,yt): temp = x+i*y if h%temp==0: flag = 1 cl =i+int(h/temp) # print(temp,cl) ans = min(ans,cl) # print(temp,ans,i) print(ans if flag==1 else '-1') t = int(input()) while t>0: Testcase() t-=1
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/TCFL2020/problems/TCFL20D" }
d1500
train
from sys import stdin,stdout import math,bisect 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(): n, m = M() a = [] for i in range(n): a += [L()] s = S() p, q = M() ans = [[0,0] for i in range(n+m)] for i in range(n): for j in range(m): if a[i][j]==0: ans[i+j][0]+=1 else: ans[i+j][1]+=1 c = 0 for i in range(n+m-1): A,B,C,D = 0,0,0,0 if s[i]=='0': A = ans[i][1]*p B = q + ans[i][0]*p c+=min(A,B) else: C = ans[i][0]*p D = q + ans[i][1]*p c+=min(C,D) print(c) for _ in range(I()): solve()
PYTHON
{ "starter_code": "", "url": "https://www.codechef.com/LOGI2020/problems/MATMIN1" }