_id
stringlengths 2
5
| partition
stringclasses 2
values | text
stringlengths 5
289k
| language
stringclasses 1
value | meta_information
dict | title
stringclasses 1
value |
---|---|---|---|---|---|
d701 | train | for t in range(int(input())):
n = int(input())
l = []
m = []
x = list(map(int,input().split()))
l.append(x)
m.append(list(x))
for i in range(1,n):
x = list(map(int,input().split()))
l.append(x)
temp = []
for i in range(4):
temp.append (x[i]+min(m[-1][:i]+m[-1][i+1:]))
m.append(temp)
print(min(m[-1])) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ICOD2016/problems/ICODE16D"
} | |
d702 | train | # cook your dish here
def main():
for _ in range(int(input())):
N, k = [int(x) for x in input().split()]
Powers = [k ** int(x) for x in input().split()]
s1, s2 = 0, sum(Powers)
ans = (0, None)
i = 0
while i < N - 1:
s1 += Powers[i]
s2 -= Powers[i]
z = s1 * s2
if z > ans[0]:
ans = (z, i)
# print(z)
i += 1
print(ans[1] + 1)
main() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/MXM"
} | |
d703 | train | for i in range(int(input())):
m,tc,th=map(int,input().split())
x=(th-tc)
if x%3!=0:
print("Yes")
else:
if (x//3)<=m:
print("No")
else:
print("Yes") | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COCA2020/problems/HOTNCOLD"
} | |
d704 | train | pref = []
for i in range(10 ** 5 + 10):
b = bin(i)[2:]
if not any(b[j] == b[j+1] == '1' for j in range(len(b) - 1)):
pref.append(i)
else:
pref.append(pref[-1])
for i in range(int(input())):
print(pref[int(input())])
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PBK02020/problems/ITGUY10"
} | |
d705 | train | # cook your dish here
for _ in range(int(input())):
a,n,m = map(int,input().split(' '))
s = len(str(a))
#print(s)
c = 10**s - 1
w = c*m
b = pow(10,n*s,w)-1
d = b//c
ans = (d%m)*(a%m)
print(ans%m) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/KBIGNUMB"
} | |
d706 | train | ar = []
ar.append(1)
for i in range(1, 31):
ar.append(ar[i-1]*(4*i-2)/(i+1))
t = int(input())
while(t>0):
n = int(input())
if(n==0):
print(0)
else:
print(ar[n]*2)
t=t-1
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ALGT2013/problems/TNMALG02"
} | |
d707 | train | t=int(input())
for i in range(t):
x,y=0,0
n,m=list(map(int,input().split()))
l=list(map(int,input().split()))
if(max(l)>m):
print(-1)
else:
for i in range(len(l)):
y+=l[i]
if(y>m):
y=l[i]
x+=1
if(y>0):
x+=1
print(x)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CHEFNWRK"
} | |
d708 | train | for t in range(eval(input())):
n = eval(input())
a = [ [] for i in range(n+1) ]
for i in range(n-1):
x,y = list(map( int, input().split() ))
a[x].append(y)
a[y].append(x)
vis = [0] * (n+1)
vis[1] = 1
ans = [1]
t1 = [1]
t2 = []
while len(t1) > 0 :
for u in t1:
for x in a[u]:
if vis[x] == 0 :
vis[x] = 1
t2.append(x)
if len(t2) > 1 :
ans.append(t2[0])
ans.append(t2[-1])
if len(t2) == 1 :
ans.append(t2[0])
t1 = t2
t2 = []
for x in sorted(ans):
print(x, end=' ')
print('') | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CDVA16/problems/CDVA1602"
} | |
d709 | train | # cook your dish here
for _ in range(int(input())):
n,k = list(map(int,input().split()))
mod = 10**9+7
s=0
for i in range(1,n+1):
p = pow(k,(2*i)-1,mod)
# print(p)
s=(s+p)%mod
# print(k)
k = (p*k)%mod
print(s)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/MATBREAK"
} | |
d710 | train | # cook your dish here
try:
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
gcd = max(a[0], a[-1])
print(gcd)
except EOFError:pass | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CORE2020/problems/CORE2002"
} | |
d711 | train | def maxval(arr):
fn = [float('-inf')]*(len(arr)+1)
sn = [float('-inf')]*len(arr)
tn = [float('-inf')]*(len(arr)-1)
fon = [float('-inf')]*(len(arr)-2)
for i in reversed(list(range(len(arr)))):
fn[i] = max(fn[i + 1], arr[i])
for i in reversed(list(range(len(arr) - 1))):
sn[i] = max(sn[i + 1], fn[i + 1] - arr[i])
for i in reversed(list(range(len(arr) - 2))):
tn[i] = max(tn[i + 1], sn[i + 1] + arr[i])
for i in reversed(list(range(len(arr) - 3))):
fon[i] = max(fon[i + 1], tn[i + 1] - arr[i])
return fon[0]
n = int(input())
arr = list(map(int,input().split()))
print(maxval(arr))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/WNTR2020/problems/WC04"
} | |
d712 | train | def convertToParitys(s):
"""
This converts the string s to an int, which is a bitMap of the parity of each letter
odd ? = first bit set
odd a = second bit set
odd b = third bit set
etc
"""
keys = '?abcdefghijklmnopqrstuvwxyz'
paritys = {c:0 for c in keys}
for c in s:
paritys[c] += 1
for c, v in paritys.items():
paritys[c] = v%2
out = 0
bitValue = 1
for c in keys:
if paritys[c]:
out += bitValue
bitValue *= 2
return out
def getSolutionBitMaps(s):
"""
these are the 27 valid bitmaps that a substring can have
even ? and the parities the same
26 cases of odd ? and one bit different in the parity compared to s
"""
out = []
sP = convertToParitys(s)
if sP%2:
sP -= 1 # to remove the '?' parity
#even case -
out.append(sP)
#odd cases - need to xor sP with 1 + 2**n n = 1 to 26 inc to flip ? bit and each of the others
for n in range(1,27):
out.append(sP^(1+2**n))
return out
def getLeadingSubStringBitMapCounts(s):
"""
This calculates the bit map of each of the len(s) substrings starting with the first character and stores as a dictionary.
Getting TLE calculating each individually, so calculating with a single pass
"""
out = {}
bM = 0
keys = '?abcdefghijklmnopqrstuvwxyz'
paritys = {c:0 for c in keys}
values = {c:2**i for i,c in enumerate(keys)}
out[bM] = out.setdefault(bM, 0) + 1 #add the null substring
bMis = []
i = 0
bMis = [0]
for c in s:
i += 1
if paritys[c]:
paritys[c] = 0
bM -= values[c]
else:
paritys[c] = 1
bM += values[c]
out[bM] = out.setdefault(bM, 0) + 1
bMis.append(bM)
return out,bMis
def solve(s):
out = 0
bMjCounts,bMis = getLeadingSubStringBitMapCounts(s)
#print('bMjCounts')
#print(bMjCounts)
solutions = getSolutionBitMaps(s)
#print('solutions')
#print(solutions)
for bMi in bMis:
for bMs in solutions:
if bMs^bMi in bMjCounts:
out += bMjCounts[bMs^bMi]
#print(i,bMi,bMs,bMs^bMi,bMjCounts[bMs^bMi])
if 0 in solutions:
out -= len(s) #remove all null substrings
out //= 2 # since j may be less that i each substring is counted twice
return out
T = int(input())
for tc in range(T):
s = input()
print(solve(s))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/SEDPASS"
} | |
d713 | train | def gcd(a,b):
if b==0: return a
return gcd(b,a%b)
for _ in range(int(input())):
n = int(input())
arr = list(map(int,input().split()))
value = arr[0]
if n!=1:
for i in arr[1:]:
value = value*i//gcd(value, i)
if value%2==0:
print("NO")
else:
print("YES") | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COLE2020/problems/CLLCM"
} | |
d714 | train | t=int(input())
i=0
while i<t:
n=int(input())
A=[]
A=input().split()
m=int(input())
B=[]
B=input().split()
j=0
a=-1
while j<m:
c=1
if B[j] in A:
b=A.index(B[j])
A.remove(B[j])
if b>=a:
a=b
c=1
else:
c=0
break
else:
c=0
break
j+=1
if c==1:
print("Yes")
else:
print("No")
i+=1 | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CHEFSQ"
} | |
d715 | train | # cook your dish here
import math
for t in range(int(input())):
n=int(input())
a=[int(i) for i in input().split()]
div=sum(a)/n
div=math.ceil(div)
count=div*n-sum(a)
for i in a:
if i>div:
count+=i-div
print(count) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/WNTR2020/problems/WC07"
} | |
d716 | train | # cook your dish here
s = input().strip()
start_w = 27
w_dict = {}
words = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
for word in words:
w_dict[word] = start_w
start_w = start_w - 1
total_wt = 0
for c in s:
total_wt = total_wt + w_dict[c]
print(total_wt) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CFUN2020/problems/CODSTAN6"
} | |
d717 | train | import math
n=1001
a=[True]*n
l=[]
for i in range(2,33):
if(a[i]):
for j in range(i*i,n,i):
a[j]=False
for pr in range(2,1001):
if(a[pr]):
l.append(pr)
t=int(input())
for j in range(t):
n,m=list(map(int,input().strip().split()))
arr=[int(num) for num in input().strip().split()]
Matrix =[]
index=[0]*100000
factors=[0]*100000
ans=''
for r in range(len(arr)):
li=[]
for val in l:
while((arr[r]%val)==0):
arr[r]=arr[r]/val
li.append(val)
factors[r]+=1
if(arr[r]!=1):
li.append(arr[r])
arr[r]=1
factors[r]+=1
Matrix.append(li)
for k in range(m):
opr=[int(o) for o in input().strip().split()]
L=opr[1]
R=opr[2]
if(opr[0]==0):
for ran in range(L-1,R):
if(index[ran]<factors[ran]):
index[ran]+=1
else:
result=1
for ran in range(L-1,R):
if(index[ran]<factors[ran]):
result=max(result,Matrix[ran][index[ran]])
ans+=str(result)
ans+=' '
print(ans[:-1]) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/SEPT16/problems/DIVMAC"
} | |
d718 | train | for i in range(int(input())):
print(2*(sum(list(map(int, input().split())))-1)) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/HECS2020/problems/CC001"
} | |
d719 | train | import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
#_______________________________________________________________________________ _______________________
# from math import *
# from bisect import *
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(2*(10**5)+100) this is must for dfs
# mod = 10**9+7; md = 998244353
# ______________________________________________________________________________ ________________________
# segment tree for range minimum query
# sys.setrecursionlimit(10**5)
# n = int(input())
# a = list(map(int,input().split()))
# st = [float('inf') for i in range(4*len(a))]
# def build(a,ind,start,end):
# if start == end:
# st[ind] = a[start]
# else:
# mid = (start+end)//2
# build(a,2*ind+1,start,mid)
# build(a,2*ind+2,mid+1,end)
# st[ind] = min(st[2*ind+1],st[2*ind+2])
# build(a,0,0,n-1)
# def query(ind,l,r,start,end):
# if start>r or end<l:
# return float('inf')
# if l<=start<=end<=r:
# return st[ind]
# mid = (start+end)//2
# return min(query(2*ind+1,l,r,start,mid),query(2*ind+2,l,r,mid+1,end))
# ______________________________________________________________________________ ________________________
# Checking prime in O(root(N))
# def isprime(n):
# if (n % 2 == 0 and n > 2) or n == 1: return 0
# else:
# s = int(n**(0.5)) + 1
# for i in range(3, s, 2):
# if n % i == 0:
# return 0
# return 1
# def lcm(a,b):
# return (a*b)//gcd(a,b)
# ______________________________________________________________________________ ________________________
# nCr under mod
# def C(n,r,mod):
# if r>n:
# return 0
# num = den = 1
# for i in range(r):
# num = (num*(n-i))%mod
# den = (den*(i+1))%mod
# return (num*pow(den,mod-2,mod))%mod
# M = 10**5 +10
# ______________________________________________________________________________ ________________________
# For smallest prime factor of a number
# M = 1000010
# pfc = [i for i in range(M)]
# def pfcs(M):
# for i in range(2,M):
# if pfc[i]==i:
# for j in range(i+i,M,i):
# if pfc[j]==j:
# pfc[j] = i
# return
# pfcs(M)
# ______________________________________________________________________________ ________________________
tc = 1
tc, = inp()
a = [0,1]
for i in range(100000):
a.append(a[-1]+a[-2])
for _ in range(tc):
n, = inp()
start = 0
for i in range(n):
print(*a[start:start+i+1])
print()
start +=i+1 | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PSTR2020/problems/ITGUY57"
} | |
d720 | train | import re,sys
def isCirlePossible(juices,distances):
if juices == [] or distances == []:
return -1;
total_juice_consumed = 0
juice_consumed = 0
start=0
for i in range(0,len(juices)):
diff = juices[i] - distances[i]
if juice_consumed >= 0:
juice_consumed += diff
else:
juice_consumed = diff
start = i
total_juice_consumed += diff
return start
juices = []
distances = []
numLines = int(input())
for each in range(0,numLines):
line = input()
result = [int(x) for x in re.findall('\d+',line)]
if len(result) == 2:
juices.append(result[0])
distances.append(result[1])
print(isCirlePossible(juices,distances))
return | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COMN2016/problems/SUMTOUR"
} | |
d721 | train | t = int(input())
for _ in range(t):
s = input()
pref = [0]*len(s)
if s[0]=="1":
pref[0]+=1
for i in range(1,len(s)):
if s[i]=="1":
pref[i]+=1
pref[i]=pref[i]+pref[i-1]
k=1
cnt=0
while (k+k*k)<=len(s):
r = k+k*k
i=r-1
while i<len(s):
if (i-r)>=0:
if pref[i]-pref[i-r]==k:
cnt+=1
i+=1
else:
i+=abs(k-(pref[i]-pref[i-r]))
else:
if pref[i]==k:
cnt+=1
i+=1
else:
i+=abs(k-(pref[i]))
k+=1
print(cnt) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/BDGFT"
} | |
d722 | train | # cook your dish here
def permutation(n,p):
r=26
if n==1:
return 26
elif n==2:
return 52
elif n==3:
return 728
else:
if n%2==0:
return ((2*(bin_expo(r,((n//2)+1),p)-r)*bin_expo(25,1000000005,p)))%p
else:
n=n+1
return ((2*((bin_expo(r,(n//2+1),p)-r)*bin_expo(r-1,1000000005,p)))- bin_expo(26,n//2,p))%p
def bin_expo(x,n,p):
if n==0:
return 1
elif n==1:
return x%p
else:
temp=bin_expo(x,n//2,p)
temp=(temp*temp)%p
if n%2==0:
return temp
else:
return ((x%p)*temp)%p
test=int(input())
for _ in range(test):
n=int(input())
p=1000000007
print(int(permutation(n,p))) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/TAPALIN"
} | |
d723 | train | '''input
2
7 3 5
5 2 5 2
2 4 2 6
6 2 6 4
5 6 5 7
7 1 7 4
7 3 7
1 1 6 1
1 2 3 2
5 2 5 2
2 6 2 6
6 2 6 4
5 6 5 7
7 1 7 4
'''
for _ in range(int(input())):
n, k, m = list(map(int, input().split()))
row_s = []
col_s = []
for _ in range(m):
h_x, h_y, t_x, t_y = list(map(int, input().split()))
if h_x == t_x:
if (h_x < (((n - k) // 2) + 1)) or (h_x > (((n - k) // 2) + k)):
col_s.append([min(h_y, t_y), max(h_y, t_y)])
else:
row_s.append([h_x, h_x])
if h_y == t_y:
if (h_y < (((n - k) // 2) + 1)) or (h_y > (((n - k) // 2) + k)):
row_s.append([min(h_x, t_x), max(h_x, t_x)])
else:
col_s.append([h_y, h_y])
row_s.sort()
col_s.sort()
poss = True
if len(col_s) == 0 or len(row_s) == 0:
print(-1)
continue
# print(row_s, col_s)
next_row = ((n - k) // 2) + 1
i = 0
count_row = 0
while i < len(row_s):
max_next = next_row
if next_row < row_s[i][0]:
poss = False
break
while i < len(row_s) and row_s[i][0] <= next_row:
# print(max_next, row_s[i], next_row)
max_next = max(max_next, row_s[i][1] + 1)
# print(max_next, row_s[i], next_row)
i += 1
next_row = max_next
count_row += 1
if next_row > (((n - k) // 2) + k):
break
if next_row < (((n - k) // 2) + k) and i >= len(row_s) :
poss = False
break
# print(count_row)
next_col = ((n - k) // 2) + 1
i = 0
count_col = 0
while i < len(col_s):
max_next = next_col
if next_col < col_s[i][0]:
poss = False
break
while i < len(col_s) and col_s[i][0] <= next_col:
# print(max_next, col_s[i], next_col)
max_next = max(max_next, col_s[i][1] + 1)
# print(max_next, col_s[i], next_col)
i += 1
next_col = max_next
count_col += 1
if next_col > (((n - k) // 2) + k):
break
if next_col < (((n - k) // 2) + k) and i >= len(col_s) :
poss = False
break
# print(count_col)
print(count_col + count_row if poss else -1)
# print(row_s, col_s)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/PROTEPOI"
} | |
d724 | train | def ans(l):
s = ""
i = 0
while (i < len(l)):
temp = l[i]
k = temp[1]
if (k != 0):
s += str(temp[0]) + "x^" + str(k)
else:
s += str(temp[0])
i += 1
if (i < len(l)):
s += " + "
if (len(s) > 0):
return s
else:
return "0"
test = int(input())
while (test != 0):
test -= 1
N = int(input())
l = []
while (N != 0):
n,m = list(map(int,input().split()))
if (m > 0):
l += [[n*m,m-1]]
N -= 1
print(ans(l))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/LOCFEB16/problems/POLYDIFR"
} | |
d725 | train | # cook your dish here
for _ in range(int(input())):
n,k = [int(c) for c in input().split()]
a = [int(c) for c in input().split()]
ls = a
if n==1:
print("YES")
print(1)
continue
if k==1:
print("NO")
continue
if k==2 and n>2:
if ls[0]!=ls[1]-1:
print("NO")
continue
ans = [0 for i in range(n+1)]
count = n
for i in range(1,a[1]):
if i != a[0]:
ans[i] =count
count-=1
for i in a[::-1]:
ans[i] = count
count-=1
for i in range(1,n+1):
if ans[i] == 0:
ans[i] = count
count-=1
print("YES")
print(*ans[1:]) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/INVLIS"
} | |
d726 | train | def magic():
def check(art,k,m):
n=len(art)
for i in range(n-k+1):
maxi=0
maxi=max(art[i:i+k])
total=0
total=art[i:i+k].count(maxi)
if total>=m:
return False
return True
for _ in range(eval(input())):
n,k,m=list(map(int,input().split()))
arr=list(map(int,input().split()))
dp=[]
ans=100
for mask in range(0,(1<<n)):
size=bin(mask).count('1')
if ans>size:
art=list(arr)
for i in range(n):
if mask & (1<<i):
art[i]+=1
if check(art,k,m):
ans=size
print(ans if ans!=100 else -1)
magic()
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/LEALCO"
} | |
d727 | train | # cook your dish here
t=int(input())
while t>0:
n=int(input())
li=[]
c,o,d,e,h,f=0,0,0,0,0,0
for i in range(0,n):
s=input()
for i in range(len(s)):
if s[i]=='c':
c=c+1
elif s[i]=='o':
o=o+1
elif s[i]=='d':
d=d+1
elif s[i]=='e':
e=e+1
elif s[i]=='h':
h=h+1
elif s[i]=='f':
f=f+1
e=e//2
c=c//2
print(min(c,o,d,e,h,f))
t-=1 | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CFMM"
} | |
d728 | train | D=[0]*31
D[1]=2
D[2]=5
for i in range(3,31):
best=10**10
for p in range(1,i+1):
best=min(best,D[p-1]+D[i-p]+i+1)
D[i]=best
t=int(input())
for i in range(t):
n,m=list(map(int,input().split()))
maxi=(n+2)*(n+1)/2-1
mini=D[n]
if mini<=m<=maxi: print(0)
elif m<mini: print(-1)
else: print(m-maxi)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COOK23/problems/NOKIA"
} | |
d729 | train | def diagonal_difference(matrix):
l = sum(matrix[i][i] for i in range(N))
r = sum(matrix[i][N-i-1] for i in range(N))
return abs(l - r)
matrix = []
N = eval(input())
for _ in range(N):
matrix.append(list(map(int, input().split())))
print(diagonal_difference(matrix)) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COFI2016/problems/CF212"
} | |
d730 | train | t=int(input())
for _ in range(t):
n,m=map(int,input().split())
d={}
e={}
l=[]
for i in range(n):
d[i]=0
for i in range(m):
e[i]=0
for i in range(n):
l.append(input())
for i in range(n):
for j in range(m):
if l[i][j]=='1':
d[i]=1
e[j]=1
#ans=[]
if sum(d.values())+sum(e.values())==0:
k=[-1]*m
for i in range(n):
print(*k)
else:
ans=[]
for i in range(n):
ans.append([0]*m)
for i in range(n):
for j in range(m):
if l[i][j]=='1':
ans[i][j]=0
else:
if (d[i] or e[j]):
ans[i][j]=1
else:
ans[i][j]=2
for i in range(n):
for j in range(m):
print(ans[i][j],end=" ")
print()
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ORMATRIX"
} | |
d731 | train | # cook your code here
for _ in range(eval(input())):
n=eval(input())
ind=0
m=-1
for i in range(n):
l=[int(x) for x in input().split()]
sc=l[0]
for j in range(1,len(l)):
sc+=int(l[j]>=4)+int(l[j]>=5)+2*int(l[j]>=6)
if sc==m:
ind=-2
if sc>m :
m=sc
ind=i+1
if (ind==-2):
print("tie")
elif (ind==1) :
print("chef")
else:
print(ind) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/SEPT16/problems/RESCALC"
} | |
d732 | train | c,f=list(map(int,input().split()))
l=[[1000001 for i in range(c)] for j in range(c)]
while f:
x,y,cost=list(map(int,input().split()))
l[x-1][y-1]=cost
l[y-1][x-1]=cost
f-=1
for i in range(c):
l[i][i]=0
for k in range(c):
for x in range(c):
for y in range(c):
if x==k or y==k or x==y:
continue
elif x!=y:
l[x][y]=min(l[x][y],l[x][k]+l[k][y])
m=-1
for i in range(c):
for j in range(c):
if m<l[i][j]:
m=l[i][j]
print(m)
# cook your dish here
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/INOIPRAC/problems/INOI1402"
} | |
d733 | train | # cook your dish here
for tc in range(int(input())):
n = int(input())
li1 = list(map(int,input().split(' ')))
li2 = list(map(int,input().split(' ')))
walk = 0
sum1 = 0
sum2 = 0
for i in range(n):
if li1[i] == li2[i] and sum1 == sum2:
walk += li1[i]
sum1 += li1[i]
sum2 += li2[i]
print(walk) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/WWALK"
} | |
d734 | train | # cook your dish here
T = int(input())
for t in range(T):
N = int(input())
s = sorted(list(str(input())))
print(s[0]) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ENCD2020/problems/ECAPR201"
} | |
d735 | train | # cook your dish here
from collections import Counter,defaultdict
for i in range(int(input())):
n=int(input())
arr=list(map(int,input().split()))
coun=Counter(arr)
check=True
for j in coun:
if coun[j]>n//2:
print("No")
check=False
break
if check==True:
print("Yes")
narr=sorted(arr)
dic=defaultdict()
j=0
for j in range(len(narr)):
if narr[j] not in dic:
dic[narr[j]]=j
ans=[0]*n
for j in range(len(arr)):
ans[j]=narr[(dic[arr[j]]+n//2)%n]
if coun[arr[j]]!=1:
dic[arr[j]]+=1
print(*ans)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/MARCAPS"
} | |
d736 | train | for _ in range(eval(input())):
n=eval(input())
if n%2:
print('NO')
else:
print('YES') | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/LOCMAY16/problems/MDOSA"
} | |
d737 | train | t = int(input())
for i in range(t):
s = input().rstrip()
sumv = 0
for j in range(len(s)):
sumv += ord(s[j])
minv = 10 ** 8;
for i in range(ord('a'), ord('z') + 1):
val = abs(sumv - i * len(s))
if minv > val:
minv = val
print(minv) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/RECJ1601/problems/SIDSTR"
} | |
d738 | train | from math import sqrt
def isPrime(n):
for i in range(2, int(sqrt(n))+1):
if(n%i==0): return True
return False
ans = []
for _ in range(int(input())):
x, y = map(int, input().split())
ans.append('NO' if(isPrime(x**2-y**2)) else 'YES')
print('\n'.join(ans)) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COTS2020/problems/CDCUR02"
} | |
d739 | train | # cook your dish here
# cook your dish here
#powerful numbers
n = int(input())
plist = [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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313]
power = 1
for i in range(2,n+1,1):
pdiv = []
count = 0
for p in plist:
if i>=p and i%p==0:
pdiv.append(p)
for pd in pdiv:
if i%(pd**2)==0:
count+=1
if count==len(pdiv) and count!=0:
power+=1
print(power) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/FULU2020/problems/ARMY1N"
} | |
d740 | train | #!/usr/bin/env python
from math import sqrt
def process(S):
P = [0,0,'S']
for i in S:
if i == 'L':
if P[-1] == 'N': P[-1] = 'W'
elif P[-1] == 'S': P[-1] = 'E'
elif P[-1] == 'E': P[-1] = 'N'
elif P[-1] == 'W': P[-1] = 'S'
elif i == 'R':
if P[-1] == 'N': P[-1] = 'E'
elif P[-1] == 'S': P[-1] = 'W'
elif P[-1] == 'E': P[-1] = 'S'
elif P[-1] == 'W': P[-1] = 'N'
else:
i = int(i)
if P[-1] == 'N': P[1] -= i
elif P[-1] == 'S': P[1] += i
elif P[-1] == 'E': P[0] += i
elif P[-1] == 'W': P[0] -= i
#print i, P
DIST = sqrt(P[0]**2+P[1]**2)
if P[0] == 0 and P[1] == 0: DIR = ''
elif P[0] == 0 and P[1] < 0: DIR = 'S'
elif P[0] == 0 and P[1] > 0: DIR = 'N'
elif P[0] < 0 and P[1] == 0: DIR = 'E'
elif P[0] < 0 and P[1] < 0: DIR = 'SE'
elif P[0] < 0 and P[1] > 0: DIR = 'NE'
elif P[0] > 0 and P[1] == 0: DIR = 'W'
elif P[0] > 0 and P[1] < 0: DIR = 'SW'
elif P[0] > 0 and P[1] > 0: DIR = 'NW'
DIST = int(DIST*10.)/10. # TOLD NO APPROXIMATION
return '%.1f%s' % (DIST, DIR)
def main():
T = int(input())
for t in range(T):
S = input().split()
print(process(S))
main()
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COZL2012/problems/RBX12R01"
} | |
d741 | train | t=int(input())
while(t):
t-=1
d={}
n,m,k=[int(x) for x in list(input().split())]
sum=0
while(k):
k-=1
x,y=[int(x) for x in list(input().split())]
a=[-1,1,0,0]
b=[0,0,-1,1]
for i in range(4):
if((x+a[i],y+b[i]) in d):
sum-=1
else:
sum+=1
d[(x,y)]=1
print(sum) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/FENCE"
} | |
d742 | train | import random
import os
yash=(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,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997)
def fix(m):
for ai in yash:
if m%ai==0:
return ai
return m
def rabin_miller(a,i,n):
if i==0:
return 1
x=rabin_miller(a,i/2,n)
if x==0:
return 0
y=(x*x)%n
if (y==1)and(x!=1)and(x!=n-1):
return 0
if i%2!=0:
y=(a*y)%n
return y
def gcd(x,y):
if y==0:
return x
return gcd(y,x%y)
def brent_rho(n):
if (n<=3)or(rabin_miller(random.randint(2,n-2),n-1,n)==1):
return n
y,r,q,m=1,1,1,203
while 1:
x=y
for i in range(1,r+1):
y=(y*y+1)%n
k=0
while 1:
ys=y
for i in range(1,min(m,r-k)+1):
y=(y*y+1)%n
q=(q*abs(x-y))%n
g=gcd(q,n)
k+=m
if (k>=r)or(g>1):
break
r*=2
if g>1:
break
if g==n:
while 1:
ys=(ys*ys+1)%n
g=gcd(abs(x-ys),n)
if g>1:
break
if g==n:
return n
return brent_rho(g)
def divsum2(n):
if n==1:
return 0
d=brent_rho(n)
d=fix(d)
assert (d<=3)or(rabin_miller(random.randint(2,d-2),d-1,d)==1)
f,m=0,n
while m%d==0:
m/=d
f = f + 1;
return (f*d)+(divsum2(m))
try:
while(1):
z=eval(input())
print(divsum2(z))
except:
os.sys.exit(0);
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ALGOTH10/problems/FACTSUM"
} | |
d743 | train | import random
def sign(i):
if i>0:
return 1
elif i<=0:
return 0
bleh = []
for _ in range(int(input())):
p = list(map(int,input().rstrip().split()))
max_rows = len(p)
if all([x==0 for x in p]):
print(1)
continue
if max_rows <= 1:
bleh.append(max_rows)
continue
max_pow = max_rows-1
if len(p)%2 !=0 and len(p)>0:
p.append(0)
max_col = len(p)//2
rows = [[0 for _ in range(max_col)] for _ in range(max_rows)]
rows[0] = p[::2]
rows[1] = p[1::2]
if sign(rows[0][0]) != sign(rows[1][0]):
print(0)
continue
for r in range(2,max_rows):
for n in range(max_col-1):
rows[r][n] = rows[r-1][0]*rows[r-2][n+1]-rows[r-2][0]*rows[r-1][n+1]
last = sign(rows[0][0])
flag = 1
for i in range(1,len(rows)):
curr = sign(rows[i][0])
if rows[r] == [0 for _ in range(max_col)]:
for n in range(max_col):
rows[r][n] = rows[r-1][n]*(max_pow+4-(r+1)-2*(n+1))
elif rows[i][0] == 0:
if any([x != 0 for x in rows[i]]):
flag = 0
break
else:
curr = last
if curr != last:
flag = 0
break
last = curr
print(flag)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CUBE2020/problems/STROCK"
} | |
d744 | train | # cook your dish here
t=int(input())
for i in range(t,0,-1):
x,y=map(int,input().split())
k=x//y
if k%y==0:
print("NO")
else:
print("YES") | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/DSTAPLS"
} | |
d745 | train | t=int(input())
for _ in range(t):
n=int(input())
l1=[]
if n==1:
print('*')
elif n==3:
print('*')
print('**')
print('*')
else:
s1=""
n1=n//2
n1+=1
for i in range(1,n1+1):
s1=""
if i==1:
s1+='*'
elif i==2:
s1+='**'
else:
s1+='*'
for j in range(2,i):
s1+=' '
s1+='*'
l1.append(s1)
for i in l1:
print(i)
l1.reverse()
for i in range(1,len(l1)):
print(l1[i])
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PEND2020/problems/ANITGUY6"
} | |
d746 | train | # cook your dish here
t = int(input())
while t:
t -= 1
n = int(input())
arr = list(map(int, input().split()))
sumi = sum(arr)
prev = 1
for i in range(n):
arr[i] = min(arr[i], prev)
prev = arr[i] + 1
prev = 1
for i in range(n - 1, -1, -1):
arr[i] = min(arr[i], prev)
prev = arr[i] + 1
temp = 0
for i in range(n):
temp = max(temp, arr[i])
print(sumi -( temp*temp))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/SNTEMPLE"
} | |
d747 | train | def treeProduct(num, h, root, ch):
if ch >= h:
return num[root]
left = (root * 2) + 1
right = (root * 2) + 2
ret1 = treeProduct(num, h, left, ch + 1)
ret2 = treeProduct(num, h, right, ch + 1)
return num[root] * max(ret1, ret2)
def main():
n = int(input())
while n!=0:
line = str(input())
s = line.split()
num = [int((e)) for e in s]
print(int(treeProduct(num,n,0,1)%1000000007))
n = int(input())
def __starting_point():
main()
__starting_point() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/MAY11/problems/TPRODUCT"
} | |
d748 | train | for _ in range(int(input())):
n = int(input())
arr= list(map(int,input().split()))
arr.sort()
d={}
for i in arr:
if i not in d:
d[i]=1
else:
d[i]+=1
flag = True
for i in d:
if d[i]>2:
flag=False
break
if arr.count(max(arr))!=1:
flag=False
if flag==True:
arr1=[]
arr2=[]
for i in d:
if d[i]<=2:
if d[i]==2:
arr2.append(i)
arr1.append(i)
arr2.reverse()
rearr= arr1+arr2
print("YES")
print(*rearr)
else:
print("NO")
# cook your dish here
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/INCRDEC"
} | |
d749 | train | from itertools import permutations
def solve(n,a):
ans=[]
for des in desire:
check=1
for i in range(n-1):
if (a[i]==a[i+1]):
return [-1]
if a[i+1]==des[a[i]-1]:
check=0
break
if check:
ans=des
break
if ans:
return ans
return [-1]
per=permutations([1,2,3,4,5,6])
desire=[]
for p in per:
check=1
for i in range(1,7):
if p[i-1]==i:
check=0
break
if check:
doublecheck=1
for i in range(6):
if p[p[i]-1]!=i+1:
doublecheck=0
break
if doublecheck:
desire.append(p)
#print(desire)
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split( )))
print(*solve(n,a)) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CHEFDICE"
} | |
d750 | train | n=int(input())
l=[]
for i in range(n):
l.append([int(x) for x in input().split()])
d=[10**9]*(n)
q=set([int(x) for x in range(1,n)])
d[1]=0
#print(q)
def extract():
mini=10**9
o=0
for i in range(1,len(d)):
if d[i]<mini and i in q:
mini=d[i]
o=i
q.remove(o)
return o
while len(q)!=0:
x=extract()
for i in range(1,n):
if i in q and l[x][i]<d[i]:
d[i]=l[x][i]
print(sum(d[1:])) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/IARCSJUD/problems/HISPDNET"
} | |
d751 | train | def invper(ar):
ar1=[0]*(len(ar))
for i in range(len(ar)):
ar1[ar[i]-1]=i+1
return ar1
t=int(input())
while(t!=0):
ar=list(map(int,input().split()))
ar1=invper(ar)
if(ar==ar1):
print("ambiguous")
else:
print("not ambiguous")
t = int(input())
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/PERMUT2"
} | |
d752 | train | for _ in range(int(input())):
n=int(input())
s=list(input())
coord=list(map(int,input().split()))
p=0
i=0
h=[]
for i in range(0,n):
if s[i]=='1':
h.append(i)
if h[0]!=0:
p=p+coord[h[0]]-coord[0]
if h[len(h)-1]!=n-1:
p=p+coord[n-1]-coord[h[len(h)-1]]
for j in range(0,len(h)-1):
if h[j]+1==h[j+1]:
continue
if h[j+1]-h[j]-1==1:
p=p+min(coord[h[j]+1]-coord[h[j]],coord[h[j+1]]-coord[h[j]+1])
else:
y=min(coord[h[j+1]]-coord[h[j]+1],coord[h[j+1]-1]-coord[h[j]])
for k in range(h[j]+1,h[j+1]-1):
y=min(y,coord[k]-coord[h[j]]+coord[h[j+1]]-coord[k+1])
p=p+y
print(p) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CHEFELEC"
} | |
d753 | train | # cook your dish here
n,m=list(map(int,input().split()))
a={}
for i in range(n):
x,y=input().split()
a[x]=y
for i in range(m):
c=input().strip()
if '.' not in c:
print("unknown")
else:
h=c.split('.')[-1]
if h in a:
print(a[h])
else:
print('unknown')
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/MIME2"
} | |
d754 | train | for t in range(int(input())):
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
l3=list(map(int,input().split()))
max=0
g=l1[0]+l2[0]+l3[0]
y=l1[1]+l2[1]+l3[1]
r=l1[2]+l2[2]+l3[2]
if g%2==0:
g-=1
if y%2==0:
y-=1
if r%2==0:
r-=1
if max<g:
max=g
if max<r:
max=r
if max<y:
max=y
m=l1[0]+l1[1]+l1[2]
o=l2[0]+l2[1]+l2[2]
p=l3[0]+l3[1]+l3[2]
if m%2==0:
m-=1
if o%2==0:
o-=1
if p%2==0:
p-=1
if max<m:
max=m
if max<o:
max=o
if max<p:
max=p
print(max)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/BOUQUET"
} | |
d755 | train | # cook your dish here
for i in range(int(input())):
n = int(input())
flag = 0
while(n>0):
if((n%10)%2 == 0):
flag = 1
break
n = n//10
if(flag == 0):
print(0)
else:
print(1) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PEND2020/problems/ITGUY03"
} | |
d756 | train | l = []
for _ in range(int(input())):
l.append(int(input()))
for i in range(2,max(l)):
r = [x%i for x in l]
if len(set([x%i for x in l])) == 1:
print(i, end = ' ')
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/INEK2019/problems/IF05"
} | |
d757 | train | # cook your dish here
import math
try:
def prime(n):
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
for t in range(int(input())):
x, y = list(map(int, input().split()))
s = x + y
i = s
while(1):
if prime(s + 1):
ans = s + 1
break
else:
s += 1
print(ans - i)
except: pass
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/POTATOES"
} | |
d758 | train | for t in range(int(input())):
n=int(input())
s=input().strip()
c=0
flag=0
for i in range(n):
if (s[i]=="A" or s[i]=="E" or s[i]=="I" or s[i]=="O" or s[i]=="U") and (s[i-1]=="A" or s[i-1]=="E" or s[i-1]=="I" or s[i-1]=="O" or s[i-1]=="U") :
flag=1
if flag and n!=1:
print("Yes")
else:
print("No")
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ALQU2018/problems/VOWPAIR"
} | |
d759 | train | import sys
import math
input=sys.stdin.readline
def binary(l,r,co,b,c):
x=(l+r)/2
#print(x)
val1=(2*x+b)*math.sin(x)
val2=(x**2+b*x+c)*math.cos(x)
x=(l+r)/2
val=val1-val2
if(abs(val)<.0000001 or co==150):
return (l+r)/2
if(val<0):
return binary((l+r)/2,r,co+1,b,c)
else:
return binary(l,(l+r)/2,co+1,b,c)
t=int(input())
for _ in range(t):
b,c=list(map(float,input().split()))
x=binary(.0000000001,math.pi/2-.0000000001,0,b,c)
#print("t=",_)
val=(x*x+b*x+c)/math.sin(x)
print(val)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ICM2020/problems/ICM2003"
} | |
d760 | train | # cook your dish here
store=[0]*(10**5+1)
def sieve():
for i in range(2,10**5+1):
if(store[i]==0):
store[i]=1
for j in range(i,10**5+1,i):
store[j]=i
sieve()
# print(store)
for _ in range(int(input())):
n=int(input())
li=[int(x) for x in input().split()]
dp=[0]*(10**5+1)
for i in li:
dp[store[i]]+=1
max_re=0
res=0
for i in li:
if(dp[store[i]]==max_re):
if(store[i]>res):
res=store[i]
elif(dp[store[i]]>max_re):
max_re=dp[store[i]]
res=store[i]
print(res)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/HECS2020/problems/CC005"
} | |
d761 | train | import math
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modin(a, m):
g, x, y = egcd(a, m)
return x % m
# def gcdexten(a,b,x,y):
# if a == 0:
# x = 0
# y = 1
# return b
# x1 = y1 = 0
# gcd = gcdexten(b%a,a,x1,y1)
# x = y1 - (b/a) * x1
# y = x1
# return gcd
# def modin(a):
# m = 10**9 + 7
# x = y = 0
# g = gcdexten(a,m,x,y)
# res = (x%m + m)%m
# return res
# void modInverse(int a, int m)
# {
# int x, y;
# int g = gcdExtended(a, m, &x, &y);
# if (g != 1)
# cout << "Inverse doesn't exist";
# else
# {
# // m is added to handle negative x
# int res = (x%m + m) % m;
# cout << "Modular multiplicative inverse is " << res;
# }
# }
# int gcdExtended(int a, int b, int *x, int *y)
# {
# // Base Case
# if (a == 0)
# {
# *x = 0, *y = 1;
# return b;
# }
# int x1, y1; // To store results of recursive call
# int gcd = gcdExtended(b%a, a, &x1, &y1);
# // Update x and y using results of recursive
# // call
# *x = y1 - (b/a) * x1;
# *y = x1;
# return gcd;
# }
def combi(a,b):
mod = 10**9 + 7
if a < b:
return 0
if a == 1:
return 1
if b < a/2:
b = a - b
temp = 1
for i in range(b + 1,a + 1):
temp = (temp * i%mod)%mod
denom = modin(math.factorial(a-b),mod)
# print denom
return (temp%mod * denom%mod)%mod
for _ in range(eval(input())):
mod = 10**9 + 7
string1 = input()
n = len(string1)
dict1 = {}
count = 0
alpha = set()
for ele in string1:
if ele in dict1:
dict1[ele] += 1
else:
dict1[ele] = 1
alpha.add(ele)
count += 1
count_list = []
total = 1
rem = n
for ele in alpha:
total = (total % mod) * (combi(rem,dict1[ele]) % mod)%mod
rem -= dict1[ele]
count_list.append(dict1[ele])
sum_list = [n - count_list[0]]
for i in range(1,count):
sum_list.append(sum_list[i - 1] - count_list[i])
sub_2 = 0
sub = 0
for i in count_list:
sub_2 += (n - i) * i
sub_2 /= 2
# print sub_2
sub_3 = 0
for i in range(count):
for j in range(i + 1,count):
sub_3 += count_list[i] * count_list[j] * sum_list[j]
sub_3 = 2 * sub_3
sub_4_4 = 0
for i in range(count):
for j in range(i + 1,count):
for k in range(j + 1,count):
sub_4_4 += count_list[i] * count_list[j] * count_list[k] * sum_list[k]
sub_4_4 *= 3
sub_4_2 = 0
for i in range(count):
for j in range(i + 1,count):
sub_4_2 += (count_list[i] * (count_list[i] - 1) * count_list[j] * (count_list[j] - 1))/4
sub_4_3 = 0
for i in range(count):
temp = 0
for j in range(count):
if j != i:
temp += count_list[j] * (n - count_list[i] - count_list[j])
temp /= 2
sub_4_3 += ((count_list[i] * (count_list[i] - 1)) * temp)/2
# print sub_4_3
sub_4_3 *= 2
# sub_4 = ((sub_4_2%mod + sub_4_3%mod) + sub_4_4%mod)%mod
# sub_tot = ((sub_2%mod + sub_3%mod)%mod + sub_4%mod)%mod
sub_4 = sub_4_3 + sub_4_4 + sub_4_2
sub_tot = sub_2 + sub_3 + sub_4
# print((total * (total - 1)) - (total * sub_tot%mod))%mod
# print ((total)* (total - 1 - (((sub_3 + sub_2)%mod + (sub_4_4 +sub_4_3)%mod)%mod + sub_4_2%mod)))% mod
print((total * (total - (sub_tot + 1)%mod)%mod)%mod) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/MARCH16/problems/SEATSTR2"
} | |
d762 | train | t=int(input())
def diffe(a,b):
return int(a-b)
while t :
t=t-1
lia=[]
lib=[]
lik=[]
lim=[]
liab=[]
likm=[]
n,k,m=list(map(int,input().split()))
lia=list(map(int,input().split()))
lib=list(map(int,input().split()))
lik=list(map(int,input().split()))
lim=list(map(int,input().split()))
liab=list(map(diffe,lia,lib))
likm=lik+lim
likm.sort()
liab.sort()
liab.reverse()
for i in range(0,len(liab)):
for j in range(len(likm)-1,0-1,-1):
a=likm.pop()
if (liab[i]-a)>=0:
liab[i]=liab[i]-a
break
print(sum(liab))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/JAN16/problems/CHEFTMA"
} | |
d763 | train | t=int(input())
while(t):
n=int(input())
cnt=1;
for i in range(n):
s=""
for j in range(n):
s=s+str(bin(cnt))[2:][: : -1]+" "
cnt=cnt+1
print(s)
t=t-1 | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PSTR2020/problems/ITGUY60"
} | |
d764 | train |
def solve(s, p):
diffs = 0
for x, y in zip(s, p):
if x == y:
continue
if x == '0':
if diffs < 1:
return "No"
diffs -= 1
else:
diffs += 1
return "Yes" if diffs == 0 else "No"
for _ in range(int(input())):
l = int(input())
s = input().strip()
p = input().strip()
print(solve(s, p))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/SWAP10HG"
} | |
d765 | train | # cook your dish here
t=int(input())
i=0
while i<t:
a=[]
a=input().split()
b=[]
b=input().split()
j=0
c=0
while j<4:
if a[j] in b:
c+=1
j+=1
if c>=2:
print("similar")
else:
print("dissimilar")
i+=1 | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/SIMDISH"
} | |
d766 | train | n=int(input())
a=list(map(int,input().split()))
q=int(input())
while q>0:
i=1
tot=a[0]
b=list(map(int,input().split()))
if b[0]==1:
#p,f=map(int,raw_input().split())
a[b[1]-1]=b[2]
else:
#r=int(raw_input())
tot=a[0]
while 1+i*b[1]<=n:
tot=tot*a[i*b[1]]
i=i+1
m=(str)(tot)
tot=tot%1000000007
print((int)(m[0]),tot)
q=q-1
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/JUNE16/problems/FRJUMP"
} | |
d767 | train | import sys
import math
import bisect
from sys import stdin,stdout
from math import gcd,floor,sqrt,log
from collections import defaultdict as dd
from bisect import bisect_left as bl,bisect_right as br
sys.setrecursionlimit(100000000)
ii =lambda: int(input())
si =lambda: input()
jn =lambda x,l: x.join(map(str,l))
sl =lambda: list(map(str,input().strip()))
mi =lambda: map(int,input().split())
mif =lambda: map(float,input().split())
lii =lambda: list(map(int,input().split()))
ceil =lambda x: int(x) if(x==int(x)) else int(x)+1
ceildiv=lambda x,d: x//d if(x%d==0) else x//d+1
flush =lambda: stdout.flush()
stdstr =lambda: stdin.readline()
stdint =lambda: int(stdin.readline())
stdpr =lambda x: stdout.write(str(x))
mod=1000000007
#main code
for _ in range(ii()):
n=ii()
arr=lii()
arr.sort()
ma=arr[-1]*arr[-2]
mi=arr[0]*arr[1]
print(ma,mi) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PEND2020/problems/ANITGUY7"
} | |
d768 | train | # cook your dish here
while True:
try:
n=int(input())
lis=[]
for i in range(n):
k=list(map(int,input().split()))
k.append(k[1]+k[2])
lis.append(k)
#print(lis)
p=sorted(lis,key=lambda x:x[3],reverse=True)
#print(p)
maxi=0
s=0
w=0
for i in range(n):
s+=p[i][0]
w=s+p[i][1]+p[i][2]
maxi=max(maxi,w)
print(maxi)
except:
break
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/INOIPRAC/problems/INOI1201"
} | |
d769 | train | # cook your dish here
# cook your dish here
from math import log2;
import sys;
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
inf = float("inf")
def find_height(node):
nodes[node]=1
for i in graph[node]:
nodes[node]+=find_height(i)
return nodes[node]
def find_sum(node):
suma=nodes[node]
maxa=0
for i in graph[node]:
maxa=max(find_sum(i),maxa)
return maxa+suma
for i in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
graph=defaultdict(set)
for i in range(len(l)):
graph[l[i]].add(i+2)
nodes=defaultdict(int)
find_height(1)
ans=find_sum(1)
print(ans)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/SUBMEXS"
} | |
d770 | train | import math
for i in range(int(input())):
a,b = map(int,input().split())
print(math.gcd(a,b)) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CACD2020/problems/PPPR"
} | |
d771 | train | import sys
import math
def main(arr,k):
x=[]
y=[]
for e in arr:
if e%2==0:
x.append(e)
y.append(0)
else:
x.append(0)
y.append(e)
a=[0]*n
b=[0]*n
a[0]=x[0]
b[0]=y[0]
for i in range(1,n):
if i<k:
a[i]=max(x[i],a[i-1])
b[i]=max(y[i],b[i-1])
else:
a[i]=max(x[i]+a[i-k-1],a[i-1])
b[i]=max(y[i]+b[i-k-1],b[i-1])
print(a[-1]+b[-1])
return
for i in range(int(input())):
n,k=list(map(int,input().split()))
arr=list(map(int,input().split()))
(main(arr,k))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/EOSUBSEQ"
} | |
d772 | train | from operator import itemgetter
t=int(input())
for i in range(t):
n=int(input())
m,f=list(map(int,input().split()))
x=list(map(int,input().split()))
my,fy=0,0
check=[0]*n
#print check
for j in range(n):
if x[j]>0 and x[j]%m==0 and check[j]==0:
check[j]=1
my+=1
#print check
for j in range(n):
if x[j]>0 and x[j]%f==0 and check[j]==0:
check[j]=1
fy+=1
if ((((my+fy)*1.0)/n)*100)>=70:
print("Yes")
if my>fy:
print("Multan")
elif fy>my:
print("Fultan")
else:
print("Both")
else:
print("No")
#print check
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CDGO2016/problems/CDGO1602"
} | |
d773 | train |
def powerset(s):
n = len(s)
masks = [1 << j for j in range(n)]
for i in range(2**n):
yield [j + 1 for j in range(n) if (masks[j] & i)]
def is_power2(num):
return num != 0 and ((num & (num - 1)) == 0)
def special(l):
n = len(l)
for i in range(n):
lis = [i + 1]
yield lis
for j in range(i + 1, n):
p = l[j] / l[i]
if p <= 1 or int(p) != p:
continue
lis = [i + 1, j + 1]
yield lis
sk = (j + 1) * int(p)
while sk <= n:
lis.append(sk)
sk *= int(p)
yield lis
def expIndices(l):
a = list(zip(l, l[1:]))
if len(a) == 0:
return True
else:
p = a[0][1] / a[0][0]
if p <= 1 or int(p) != p:
return False
for i in range(1, len(a)):
if a[i][1] / a[i][0] != p:
return False
return True
def main():
for _ in range(eval(input())):
S = input()
count = 0
for i in special(range(1, len(S) + 1)):
s = [S[j - 1] for j in i]
if s == s[::-1]:
count += 1
print(count)
def __starting_point():
main()
__starting_point() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/LTIME32/problems/EXPALIN"
} | |
d774 | train | tests = int(input())
for t in range(tests):
n = int(input())
permut='2'
permut_list=[]
if n%2==0:
for i in range(2, n+1):
if i%2==1:
permut=permut+' '+str(i+1)
else:
permut=permut+' '+str(i-1)
print(permut)
pass
elif n==1:
print(1)
pass
else:
for i in range(2, n):
if i%2==1:
permut_list.append(str(i+1))
else:
permut_list.append(str(i-1))
permut_list.pop(-1)
permut_list.append(str(n))
permut_list.append(str(n-2))
this_permut='2'
for el in permut_list:
this_permut=this_permut+' '+el
print(this_permut) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/MINPERM"
} | |
d775 | train | # cook your dish here
n, k, p = [int(i) for i in input().split()]
n_sep = list(map(int, input().split()))
count = 0
sep_sort = sorted(n_sep)
hashing = {sep_sort[0]: 0}
for j in range(1, n):
if (abs(sep_sort[j] - sep_sort[j - 1]) > k):
count += 1
hashing[sep_sort[j]] = count
#print(hashing)
for i in range(p):
pair = list(map(int, input().split()))
if hashing[n_sep[pair[1] - 1]] == hashing[n_sep[pair[0] - 1]]:
print("Yes")
else:
print("No")
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/FROGV"
} | |
d776 | train | try:
# https://www.codechef.com/LTIME63B/problems/GHMC
# Finally.... I properly understood what needs to be done.
def ctlt(arr, val):
# find number of values in sorted arr < val
if arr[0] >= val: return 0
lo = 0
hi = len(arr)
while hi-lo > 1:
md = (hi+lo)//2
if arr[md]<val:
lo = md
else:
hi = md
return hi
for _ in range(int(input())):
n,k,x,d = map(int, input().split())
z = input().strip().split()
if k > 0:
ps = list(map(int,z[:k]))
else:
ps = [x]
ps.sort()
if x<n or x<ps[-1] or n<k:
print(-1)
continue
valchecked = 0
fillval = 0
valsdone = False
isolbelow = True
lastp = ps[0]
while not valsdone and n>=k:
if n == k:
lo = x+d+1 # put out of range
else:
# find best maxfill (before val support)
lo = 1
hi = x+1
while hi-lo>1:
md = (hi+lo)//2
v = (x-md+1) + ctlt(ps,md)
if v<n:
hi = md
else:
lo = md
valsdone = True
checkto = ctlt(ps,lo)-1
if checkto >= valchecked:
# support all vals
for p in ps[valchecked+1:checkto+1]:
if lastp+d >= p:
isolbelow = False
elif isolbelow:
valsdone = False
fillval += lastp+d
n -= 1
isolbelow = (p > lastp + 2*d )
else:
isolbelow = True
lastp = p
valchecked = checkto
if valsdone and isolbelow:
# check gap to maxfill
if lastp + d >= lo:
isolbelow = False
else:
valsdone = False
fillval += lastp
ps[checkto] += d
lastp += d
isolbelow = False
n -= 1
if k > n:
print(-1)
elif k == n:
print(sum(ps) + fillval)
elif k == n-1 and lo > ps[-1]:
print(sum(ps) + fillval + min(x,ps[-1]+d))
else:
tot = (x+lo)*(x-lo+1)//2 + sum(ps[:ctlt(ps,lo)])
print(tot + fillval)
except:
pass | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/GHMC"
} | |
d777 | train | # cook your dish here
t=int(input())
for i in range(t):
D=int(input())
P=10**5-2
ans=[]
if(D==0):
ans.append(1)
while(D>0):
P=min(P,D)
ans.append(P+2);
ans.append(P+1);
ans.append(1);
D=D-P;
print(len(ans))
print(*ans,sep=" ",end="\n")
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/DIANE"
} | |
d778 | train | import math
t=eval(input())
while t:
t=t-1
r1,h1,r2,h2=list(map(float,input().split()))
vol1=(math.pi*r1*r1*h1)/3 + (2*math.pi*r1*r1*r1)/3
vol2=math.pi*r2*r2*h2
print("%.8f %.8f" % (vol1,vol2))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/LOCFEB16/problems/ICECREAM"
} | |
d779 | train | # cook your dish here
x=int(input())
for i in range(x):
s=input()
print(int(s[::-1]))
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/FLOW007"
} | |
d780 | train | t=int(input())
for i in range(t):
n=int(input())
N=list(map(int,input().split()))
N.sort()
k=n-1
ave=N[k]
for j in range(n-1):
ave=(ave+N[k-1])/2
k=k-1
print(ave) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/PROC18B"
} | |
d781 | train | import sys
user_input = sys.stdin.readline().split()
T = int(user_input[0])
for j in range(T) :
var = sys.stdin.readline().split()
N = int(var[0])
M = int(var[1])
if (N%M)%2 :
print("ODD")
else :
print("EVEN") | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/LOCMAY18/problems/REMAIN"
} | |
d782 | train | # cook your dish here
from sys import stdin,stdout
a0=0
a1=1
n,k=stdin.readline().strip().split(' ')
n,k=int(n),int(k)
arr=list(map(int,stdin.readline().strip().split(' ')))
def solve(n,k,arr):
sol=[]
l=0;u=k;
while l!=u:
sol.append(arr[l:min(len(arr),u)])
l=min(l+k,len(arr))
u=min(u+k,len(arr))
tiwari=[]
for i in range(k):
titi=0
gao=0
for j in range(len(sol)):
if len(sol[j])>i:
if sol[j][i]==0:
titi+=1
else:
gao+=1
tiwari.append((titi,gao))
minflip=(-1,-1)
ans=0
ctr=0
for i in tiwari:
if i[0]<i[1]:
ans+=i[0]
ctr+=(1*a1+a0*a1)*a1
if i[1]<minflip[0] or minflip[0]==-1:
minflip=(i[1],i[0])
else:
ans+=i[1]
if i[0]<minflip[0] or minflip[0]==-1:
minflip=(i[0],i[1])
#print(ans,ctr)
#print(tiwari)
#print(minflip)
if ctr%2==0:
ans+=minflip[0]
ans-=minflip[1]
stdout.write(str(ans)+"\n")
solve(n,k,arr) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/AGPR2020/problems/ALPR2005"
} | |
d783 | train | test = int(input())
for i in range(test):
flavor = int(input())
rate = input()
gaf = input()
gaf = gaf.split()
gaf = [int(x) for x in gaf]
rate = rate.split()
rate = [int(x) for x in rate]
rate.sort()
c = gaf[0] - gaf[1]
sum = rate[0]*c
t = True
if gaf[0] < gaf[1]:
t = False
j = 0
while(j<gaf[1] and t):
sum = sum + rate[j]
j = j + 1
if t :
print(sum)
else:
print("Not Possible") | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CCWR2016/problems/CCWR01"
} | |
d784 | train | # cook your dish here
epi=10**-2
def vision(t):
a1=x0+(dx*t)-x1
a2=y0+(dy*t)-y1
a3=z0+(dz*t)-z1
b=4*((a1*d1)+(a2*d2)+(a3*d3))*((a1*d1)+(a2*d2)+(a3*d3))
a=4*((a1*a1)+(a2*a2)+(a3*a3))
value=(b-(a*c))
return value
xrange=range
for _ in range(int(input())):
x1,y1,z1,x0,y0,z0,dx,dy,dz,cx,cy,cz,r=list(map(int,input().split()))
d1=x1-cx
d2=y1-cy
d3=z1-cz
c=(d1*d1)+(d2*d2)+(d3*d3)-(r*r)
low=0
high=10**9+1
while low<(high-10**-6):
mid=low+(high-low)*1.0/2;
value=vision(mid);
if abs(value)<=epi:
break;
elif value>0:
low=mid;
else:
high=mid;
print(mid)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/VSN"
} | |
d785 | train | import sys
import os
import random
import math
#nonlocal defs
n, m, p = list(map(int, input().split()))
arr = [dict() for _ in range(n)]
for _ in range(p):
i,j = list(map(int,input().split()))
i -= 1
j -= 1
if j not in arr[i]:
arr[i][j] = j+1
else:
arr[i][j] += 1
def chefbm(arr,i):
for (e,f) in arr[i].items():
if e == m-1:
continue
if e+1 in arr[i]:
c = arr[i][e+1]
else:
c = e+1
if arr[i][e] > c:
return -1
y = arr[i][m-1] if m-1 in arr[i] else m-1
x = arr[i][0] if 0 in arr[i] else 0
return y-x
for i in range(n):
print(chefbm(arr,i)) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/MAY14/problems/CHEFBM"
} | |
d786 | train | # cook your dish here
a = int(input())
for i in range(a):
b = int(input())
li = []
if b == 2:
print(2,1)
elif b == 3:
print(3,2)
elif b == 4:
print(4,2)
else:
for t in range(b+1):
if ((b*t)+1-(2**t))<0:
li.append(t-1)
break
for o in range(b+1):
if b<=2**(o):
li.append(o)
break
print(*li)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/TRICKYDL"
} | |
d787 | train | from sys import stdin, stdout
#from math import gcd as g
#a,b = map(int, stdin.readline().split())
#l1 = list(map(int, stdin.readline().split()))
l = [1,6,7]
c = 1
for x in range(3,100001):
if x%2==1:
a = l[c]*6
l.append(a)
else:
l.append(a+1)
c+=1
n = int(stdin.readline())
for _ in range(n):
s = int(stdin.readline())
print(l[s-1]) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/PBK22020/problems/ITGUY23"
} | |
d788 | train | for _ in range(int(input())):
l=list(map(int,input().strip()))
for j in range(len(l)-1,-1,-1):
if l[j]==1:
l.pop()
else:
break
if l.count(1):
time,prev,z,c=0,0,0,0
for j in range(len(l)-1,-1,-1):
if l[j]==0:
z+=1
continue
if prev!=z:
prev=z
c+=1
time+=c+z
print(time)
else:
print(0)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ROWSOLD"
} | |
d789 | train | n = int(input())
for i in range(n):
s = input()
l = len(s)
n1 = int(s[0])
n2 = int(s[l-1])
print(n1+n2)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/FLOW004"
} | |
d790 | train | tc=int(input())
for case in range(tc):
m,r=list(map(int,input().split()))
n=m**(r-1)
a=[i**n for i in range(1,2*n+1)]
tmp=2*n-1
for i in range(n):
for j in range(tmp-i):
a[j]=a[j+1]-a[j]
print((a[n-1]/m)%1000000007)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/CDST2016/problems/CDS2"
} | |
d791 | train | N,M,C = list(map(int, input().split()))
tree = [0] * (N+1)
def add(u,k):
while u < len(tree):
tree[u] += k
u += u&-u
def query(k):
ans = 0
while k:
ans += tree[k]
k -= k&-k
return ans
def solve():
for _ in range(M):
op = input().split()
if op[0] == 'Q':
print(query(int(op[1])) + C)
else:
u,v,k = int(op[1]), int(op[2]), int(op[3])
add(u, k)
add(v+1, -k)
def __starting_point():
solve()
__starting_point() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/SPREAD"
} | |
d792 | train | tc=int(input())
for case in range(tc):
n,d=list(map(int,input().split()))
a=list(map(int,input().split()))
sm=sum(a)
f=True
if sm%n==0:
avg=sm/n
for i in range(d):
tmp_sm=0
tmp_n=0
for j in range(i,n,d):
tmp_sm=tmp_sm+a[j]
tmp_n+=1
if tmp_sm%tmp_n==0:
if avg!=tmp_sm/tmp_n:
f=False
break
else:
f=False
break
else:
f=False
if f:
ans=0
cur=0
for i in range(d):
for j in range(i,n,d):
cur=cur+avg-a[j]
ans=ans+abs(cur)
print(ans)
else:
print(-1)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/AUG17/problems/CHEFMOVR"
} | |
d793 | train | # cook your dish here
t = int(input())
for _ in range(t):
n,s = input().split()
N = int(n)
r = N - len(s)
count = 0
if N>len(s):
count = pow(26, r-1,(10**9+7))
count*= (26+25*len(s))
count = count%(10**9 + 7)
print(count)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/ICL1904"
} | |
d794 | train | # cook your dish here
import math
try:
n,d=map( int,input().split() )
a=list(map(int,input().split()))
a.sort()
z=abs(a[0]-d)
for j in range(n):
x=abs(a[j]-d)
z=math.gcd(x,z)
print(z)
except:
pass | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/LDT42020/problems/DRONEDEL"
} | |
d795 | train | def multiple_input(): return map(int, input().split())
def list_input(): return list(map(int, input().split()))
mod = int(1e9) + 7
for _ in range(int(input())):
n, m = multiple_input()
a = list_input()
a.sort()
max_level = a[-1] + 1
levels = [0] * max_level
levels[0] = 1
for i in a:
levels[i] += 1
ans = 1
for i in range(1, max_level):
ans = (ans * (pow(levels[i - 1], levels[i], mod))) % mod
print(ans) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/CNTGRP"
} | |
d796 | train | t=int(input())
for i in range(t):
n,k,l=map(int,input().split())
if k*l<n:
print(-1)
elif (k==1 and n>1):
print(-1)
else:
for j in range(n):
print((j%k)+1,end=' ')
print() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/BOWLERS"
} | |
d797 | train | test=int(input())
for i in range(test):
n=int(input())
a=list(map(int,input().split()))
b=[0]*(n+2)
b[n-1]=1
for i in range(n-2,-1,-1):
if(a[i]*a[i+1]<0):
b[i]=b[i+1]+1
else:
b[i]=1
for i in range(n):
print(b[i], end=' ')
print() | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COOK68/problems/ALTARAY"
} | |
d798 | train | # cook your dish here
d=["saturday","sunday","monday","tuesday","wednesday","thursday","friday"]
t=int(input())
for i in range(t):
s,e,l,r=map(str,input().split())
l,r=int(l),int(r)
v=(d.index(e)-d.index(s)+8)%7
c=r+1
for i in range(l,r+1):
if i%7==v:
c=i
break
if c>r:
print('impossible')
elif c+7<=r:
print('many')
else:
print(c) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/problems/EVENT"
} | |
d799 | train | # cook your dish here
import sys
def dist(a,b):
return abs(a[0]-b[0])+abs(a[1]-b[1])
n, m = map(int, input().split())
matrix=[]
id_matrix=[[0 for i in range(n)] for i in range(n)]
for _ in range(n):
matrix.append(list(map(int, input().split())))
charms=[]
for _ in range(m):
x,y,lungh = map(int, input().split())
x-=1
y-=1
charms.append([x,y,lungh])
if m<=10:
for i in range(n):
for j in range(n):
flag=0
for charm in charms:
if dist([i,j],charm[:2])<=charm[2]:
flag=1
break
if flag==0:
matrix[i][j]=-float('Inf')
for i in range(1,n):
matrix[0][i]+=matrix[0][i-1]
matrix[i][0]+=matrix[i-1][0]
for i in range(1,n):
for j in range(1,n):
matrix[i][j]+=max(matrix[i-1][j], matrix[i][j-1])
else:
for charm in charms:
for i in range(-charm[2],charm[2]+1):
appo=charm[2]-abs(i)
for j in range(-appo, appo+1):
x=i+charm[0]
y=j+charm[1]
if x>=0 and x<n and y>=0 and y<n:
id_matrix[x][y]=1
if id_matrix[0][0]==0:
matrix[0][0]=-float('Inf')
for i in range(1,n):
if id_matrix[0][i]==0:
matrix[0][i]=-float('Inf')
else:
matrix[0][i]+=matrix[0][i-1]
if id_matrix[i][0]==0:
matrix[i][0]=-float('Inf')
else:
matrix[i][0]+=matrix[i-1][0]
for i in range(1,n):
for j in range(1,n):
if id_matrix[i][j]==0:
matrix[i][j]=-float('Inf')
else:
matrix[i][j]+=max(matrix[i-1][j], matrix[i][j-1])
if matrix[n-1][n-1]<-10**(10):
print('NO')
else:
print('YES')
print(matrix[n-1][n-1]) | PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/ZCOPRAC/problems/ZCO13002"
} | |
d800 | train | # cook your dish here
n = int(input())
count = 0
for _ in range(n):
L = list(map(int, input().split()))
if (L.count(1)>=2):
count+=1
print(count)
| PYTHON | {
"starter_code": "",
"url": "https://www.codechef.com/COX22020/problems/ACODEX2"
} |