id stringlengths 2 5 | title stringclasses 1
value | text stringlengths 5 289k |
|---|---|---|
d940 | lucky = {4, 774, 7, 744, 777, 74, 747, 44, 77, 47, 474, 444, 477, 447}
from functools import lru_cache
import sys
sys.setrecursionlimit(10 ** 6)
mod = 10 ** 9 + 7
fact = [1]
for i in range(1, 1001):
fact.append(fact[-1] * i % mod)
inv = [pow(i, mod-2, mod) for i in fact]
C = lambda k, n: fact[n] * inv[n-k] * inv[k... | |
d941 | T=int(input())
l=[]
for t in range(T):
R=int(input())
a=2*(R**2)
l.append(a)
for s in l:
print(s) | |
d942 | try:
t=int(input())
while t>0:
[a,b]=[int(x) for x in input().split()]
if a==1 and b==1:
print(1)
continue
if a%2==0:
o1=a//2
e1=a//2
else:
o1=a//2+1
e1=a//2
if b%2==0:
o2=b//2
e2=b//2
else:
o2=b//2+1
e2=b//2
print(e1*e2+o1*o2)
t-=1
except:
pass | |
d943 | # cook your dish here
# from queue import PriorityQueue
# import bisect
def insort(l, v):
s = 0
e = len(l)
while True:
mid = (s+e)//2
if s == e or mid > len(l):
break
if l[mid][0] < v[0]:
s = mid+1
elif l[mid][0] > v[0]:
e = mid
els... | |
d944 | # cook your dish here
t=int(input())
for i in range(t):
v,w=list(map(int,input().strip().split(" ")))
if v==w:
print(v+1)
elif v<w:
print(v+1)
else:
print(w+1)
#s="a"
#s=s*v
#l=list(s)
#print(l)
ct=0
#for i in range(w):
| |
d945 | # cook your dish here
from sys import stdin, stdout
from collections import defaultdict
for _ in range(int(stdin.readline())):
n=int(stdin.readline())
lst=list(map(int, stdin.readline().split()))
prefix_odd=[0]*n
prefix_even=[0]*n
odd_val=0
even_val=0
for i in range(n):
if lst[i]%2==... | |
d946 | for i in range(int(input())):
n=int(input())
print(n//2+1) | |
d947 | import sys
import math
def main(arr):
k=len(arr[0])
n=len(arr)
dp=[[0]*k for i in range(n+1)]
summation_arr=[]
for i in range(len(arr)):
a=1 if i!=0 else 0
summation_arr.append(sum(arr[i])+a)
for i in range(1,len(dp)):
for j in range(len(dp[0])):
dp[i][j]=(dp[i-1][j]+arr[i-1][j])/summation_arr[i-1... | |
d948 | def codn1(s1,s2,p):
c=0
ind=0
for i in range(len(s1)):
if s1[i]!=s2[i]:
c+=1
ind=i
if c>1 or ind==len(s1)-1:
return 0
if s1[ind]>s2[ind] and s1[ind] in s2[ind+1:]:
p[0]=True
if s1[ind]<s2[ind] and s2[ind] in s1[ind+1:]:
p[1]=True
return 1
def codn2(s1,s2):
if len(s1)<len(s2):
... | |
d949 | from math import sqrt
a, b = map(int, input().split())
pairs = 0
for i in range(1, a+1):
for j in range(1, b+1):
root = sqrt(i**2 + j)
if not root - int(root):
pairs += 1
print(pairs) | |
d95 | T = int(input())
for test in range(T):
n,t = list(map(int,input().split()))
a = list(map(int,input().split()))
res = []
j=0
for i in a:
if(i*2<t):
res+=["0"]
elif(i*2>t):
res+=["1"]
else:
res.append(["0","1"][j])
j = 1-j
pri... | |
d950 | def check(l,n):
m = 0
for i in range(n-1):
x = l[i]
c = 0
j = i
while (j+2<n and l[j+2]==x) or (j+1<n and l[j+1] == x):
if l[j+1] == x:
j = j+1
else:
j+=2
c+=1
if m<c:
m = c
return m
t = ... | |
d951 | N = int(input())
i = N + 1
flag = 0
for i in range(N+1, 987654321):
a = str(i)
b = list(a)
c = set(a)
if '0' not in b:
if len(b) == len(c):
print(i)
flag += 1
break
if flag < 1:
print(0)
| |
d952 | import math
from itertools import permutations as p
def diff(li1, li2):
li_dif = [i for i in li1 if i not in li2]
return li_dif
def segments(b):
"""A sequence of (x,y) numeric coordinates pairs """
poly = [(i[0],i[1]) for i in b]
return zip(poly, poly[1:] + [poly[0]])
def peri... | |
d953 | # cook your dish here
str1=int(input())
for i in range(str1):
l1=[]
str2=input()
for i in str2:
l1.append(i)
count=0
d=dict()
d={'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':10,'k':11,'l':12,'m':13,'n':14}
d1={'o':15,'p':16,'q':17,'r':18,'s':19,'t':20,'u'... | |
d954 | # cook your dish here
import sys
def get_array(): return list(map(int , sys.stdin.readline().strip().split()))
def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
def input(): return sys.stdin.readline().strip()
import math
from collections import defaultdict
from itertools import combinations_w... | |
d955 | t=int(input())
while(t!=0):
t=t-1
n=int(input())
ans=0
for i in range(1,n+1,1):
sum=0;
for j in range(1,i+1,1):
sum=sum+j
s=sum-i
sum=sum+s
if(i!=n):
ans=ans+2*sum*i
else:
ans=ans+sum*i
print(ans) | |
d956 | isPrime=[1 for i in range(10001)]
cnt=[0 for i in range(10001)]
isPrime[0]=0
isPrime[1]=0
prime=[]
for i in range(2,10001):
if isPrime[i]:
prime.append(i)
for j in range(i*i,10001,i):
isPrime[j]=0
#print(prime)
for i in prime:
for j in prime:
if (i + 2*j)>10000:
break
else:
cnt[i + 2*j]+=1
#print(le)... | |
d957 | # cook your dish here
n=int(input())
x=[]
y=[]
for _ in range(n):
s=input().split(' ')
x.append(int(s[0]))
y.append(int(s[1]))
q=x[0]
w=y[0]
for t in range(n-1):
x[t]=(x[t]+x[t+1])/2
y[t]=(y[t]+y[t+1])/2
x[n-1]=(x[n-1]+q)/2
y[n-1]=(y[n-1]+w)/2
i=0
a=0
while(i<n-1):
a+=(x[i]*y[i+1]-y[i]*x[i+1])/2
i+=1
a+=(x[i... | |
d958 | n = int(input())
for i in range(n) :
t = int(input())
li = sorted(list(map(int , input().split())))
ans = 1
dp = [li[1]-li[0]] + [0] * (t-2) + [li[t-1] - li[t-2]]
for i in range(1 , t-1) :
dp[i] = min(li[i] - li[i-1] , li[i+1] - li[i])
print(max(dp))
| |
d959 | from sys import stdin, stdout
from collections import Counter
n = int(stdin.readline())
#l = list(map(int, stdin.readline().split()))
#l = [int(stdin.readline()) for _ in range(n)]
#a, b = map(int, stdin.readline().split())
for _ in range(n):
n1 = int(stdin.readline())
if n1==1:
print('*')
... | |
d96 | #JMD
#Nagendra Jha-4096
import sys
import math
#import fractions
#import numpy
###File Operations###
fileoperation=0
if(fileoperation):
orig_stdout = sys.stdout
orig_stdin = sys.stdin
inputfile = open('W:/Competitive Programming/input.txt', 'r')
outputfile = open('W:/Competitive Programming/output... | |
d960 | for i in range(int(input())):
n=int(input())
m=list(map(int,input().split()))[:n]
m.sort()
t=0
for j in range(n//2):
t+=abs(m[j]-m[n-j-1])
print(t)
| |
d961 | t = int(input())
for _ in range(t):
k = int(input())
count = 1
for _ in range(k):
output = []
for index in range(1,k+1):
output.append(bin(count).replace("0b", ""))
count += 1
print(*output) | |
d962 | # cook your dish here
def mergeSort(arr, n):
# A temp_arr is created to store
# sorted array in merge function
temp_arr = [0]*n
return _mergeSort(arr, temp_arr, 0, n-1)
# This Function will use MergeSort to count inversions
def _mergeSort(arr, temp_arr, left, right):
# A variable inv... | |
d963 | for _ in range(int(input())):
n = int(input())
s = ''
for i in range(1, n + 1): s += str(i)
for i in range(n, 0, -1):
if i % 2 == 0:
for j in range(i, 0, -1): print(j, end = '')
else:
for j in range(1, i + 1): print(j, end = '')
print()
| |
d964 | def solve(l):
m = l.index(max(l))
if m == 0 or m == len(l) - 1:
return 1
return 1 + min(solve(l[0:m]), solve(l[m+1:]))
tc = int(input())
for test in range(tc):
n = int(input())
l = list(map(int, input().split()))
print(solve(l)) | |
d965 | import fractions
for t in range(int(input())):
h,u,d = list(map(int,input().split()))
g = fractions.gcd(u,d)
if (h%g!=0):
print(-1)
else:
m = 0
n = 0
while (True):
n = (float(m)*u-h)/d
if (n>0 and int(n) == n):
break
m+=1
print(int(m+n)) | |
d966 | # cook your dish here
try:
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
if k == 0:
print(0, n)
else:
print(n//k, n%k)
except:
pass | |
d967 | for _ in range(int(input())):
N,U,D=list(map(int,input().split()))
H=list(map(int,input().split()))
jumps=0
paracount=0
for i in range(len(H)-1):
if H[i+1]-H[i]<=U and H[i+1]>=H[i]:
jumps+=1
elif H[i]>=H[i+1] and H[i]-H[i+1]<=D:
jumps+=1
elif H[i]-H[i+1]>D and paracount==0:
jumps+=1
paracount=1
... | |
d968 | try:
def countsubsetsum(S,arr,n):
k=[[0 for i in range(S+1)] for i in range(n+1)]
for i in range(n+1):
for j in range(S+1):
if(j==0):
k[i][j]=1
elif(i==0):
k[i][j]=0
elif(arr[i-1]<=j):
... | |
d969 | import sys
n = eval(input())
parents = [int(x)-1 for x in input().split(' ')]
values = list(map(int , input().split(' ')))
parents = [0]+parents
# print(parents)
# print(values)
def single_node_cost(i):
cost = 0
# print('started with ',i)
min_value = sys.maxsize
while i != 0:
min_value = min(min_value,values[i])
... | |
d97 | import sys
from operator import itemgetter
def count(a, b, num_a, num_b, cur_time):
current_result = 0
#print('count time = ', cur_time, "num_a =", num_a, 'num_b = ', num_b)
if num_a * a + num_b * b <= cur_time and cur_time >= 0:
cur_time -= num_a * a + num_b * b
current_result = num_a + num_b
if num_a < tota... | |
d970 | for i in range(int(input())):
n,k=input().split()
laddus=0
for j in range(int(n)):
t=input().split()
if t[0]=='CONTEST_WON':
if(int(t[1])<=20):
laddus+=300+20-int(t[1])
else:
laddus+=300
elif t[0]=='TOP_CONTRIBUTOR':
laddus+=300
elif t[0]=='BUG_FOUND':
laddus+=int(t[1])
elif t[0]=='CONT... | |
d971 | def posSearch(arr, num):
l = 0
r = len(arr)
if num < arr[l]:
return 0
elif num > arr[r-1]:
return r
while l < r:
m = (l+r)//2
if arr[m] == num:
return -1
if arr[m] < num < arr[m+1]:
return m+1
if arr[m] > num:
r = m
elif arr[m] < num:
l = m+1
for _ in range(int(input())):
n = int(inpu... | |
d972 | for _ in range(int(input())):
n=int(input())
a=[int(z) for z in input().split()]
m=0
a1=list(set(a))
for i in range(len(a1)):
if a.count(a1[i])>m:
m=a.count(a1[i])
print(n-m)
| |
d973 | try:
a=list(map(int,input().split()))
n=a[0]
k=a[1]
hi=[]
for i in range(n):
hi.append(int(input()))
hi.sort()
diff=[]
for i in range(n-k+1):
diff.append(hi[i+k-1]-hi[i])
print(min(diff))
except:
pass | |
d974 | # cook your dish here
testcases=int(input())
for _ in range(testcases):
(N,K)=list(map(int,input().split()))
array=list(map(int,input().split()))
max=array[0]
min=array[0]
for i in array:
if i>max:
max=i
if i<min:
min=i
max=max+K
... | |
d975 | for _ in range(int(input())):
a,b,c,d=list(map(int,input().split()))
if(a==b):
print('YES')
elif(c==d):
print('NO')
else:
if(abs(a-b)%abs(c-d)==0):
print('YES')
else:
print('NO')
| |
d976 | for _ in range(int(input())):
n,r,x,y=map(int,input().split())
if x>0:
a=list(map(int,input().split()))
if y>0:
b=list(map(int,input().split()))
if x>0 and y>0:
a=a+b
c=n-len(list(set(a)))
else:
l=x+y
c=n-l
x=min(c,r)
print(x) | |
d977 | # cook your dish here
n=int(input())
stringa=list(map(int,input().split()))
counter=0
counter1=0
counter3=0
somma1=0
somma2=0
massimo=0
massimo1=0
massimo3=0
stack=[]
for par in stringa:
if par==1 or par==3:
if counter1==0 and par==1:
counter1=1
somma1=1
massimo1=max(mas... | |
d978 | # cook your dish here
try:
for _ in range(int(input())):
n = int(input())
li = [i for i in input()]
a = 0
while a+1<len(li):
li[a],li[a+1] = li[a+1],li[a]
a+=2
li2 = li.copy()
for i in li2:
fh = 109
sh = 110
li.remove(i)
if ord(i)>fh:
li.append(chr(fh-(ord(i)-sh)))
else:
li.app... | |
d979 | for i in range(int(input())):
n=int(input())
if(n<=2 or n>1000000007):
print("0")
else:
prod=1
for i in range(2,n):
prod*=i
if(prod>1000000007):
prod=prod%1000000007
print(((prod)*(n*prod-2))%1000000007)
| |
d98 | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
def solve():
s,c = input().split()
# i,jでjが複数あるとき
n = len(s)
for i in range(n-1):
prev = s[i]
pos = i
for j in range(i+1, n):
if s[j]<prev:
prev = s[j]
pos = j... | |
d980 | def for1(M,k):
ret = 0.0
x = k*k+0.0
z=x
for m in range(1,M):
ret+=(M-m)/x
x*=z
return ret
def for2(M,k):
ret = 0.0
x = k+0.0
for m in range(1,M):
ret+=(M-m)/x
x*=k
return ret
def ans(M,N,K):
return int(round(M*N+M*for2(N,K)+N*for2(M,K)+K*for1(M,K)*for1(N,K),0))
M,N,K = list(map(int,input(... | |
d981 | for t in range(int(input())):
n,b,m = list(map(int,input().split()))
ans = 0
while n>0:
ans+=b
half = (n+1)/2 if n%2 else n/2
ans+=m*half
m*=2
n=n-half
print(ans-b)
| |
d982 | t=eval(input())
for q in range(t):
l=eval(input())
a=input().split()
a=[int(x) for x in a]
a=sorted(a)
for i in range(l-1):
a[i]=(a[i+1]-a[i])
print(min(a[0:l-1]))
| |
d983 | # cook your code here
n=input()
x=len(n)
no=list(map(int,n))
temp=[0]*x
if (x>2):
sum=99
for i in range(3,x):
sum=sum+90
sum=sum+10*(int(n[0])-1)
sum=sum+int(n[1])
f=int(n[0])%10
s=int(n[1])%10
cd=s-f
temp[0]=n[0]
temp[1]=n[1]
for i in range(2,x):
nxt=(s+cd)%10
temp[i]=(chr(nxt+48))
s=nxt
temp=... | |
d984 | import sys
n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()
alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
arr = [0]*26
pref = []
for i in range(len(s)):
for j in range(26):
if alph[j] == s[i]:
arr[j] += 1
break
pref += [arr[:]]
# for x in pref:
# print(x)
q = int(sys.std... | |
d985 | t = int(input())
for i in range(t):
n = int(input())
l = list(map(int, input().split()))
counter = 0
even = 0
for num in l:
if num % 2 == 0:
even += 1
if num % 2 == 1:
counter += even
print(counter)
| |
d986 | # coding: utf-8
# Your code here!
n=int(input())
a=[]
for i in range(n):
x=int(input())
a.append(x)
# print(a)
ans=0
m=[1]*n
for i in range(n):
for j in range(i):
if a[i]%a[j]==0:
m[i]=max(m[i],m[j]+1)
print(max(m)) | |
d987 | t = int(input())
while t> 0:
t =t -1
n,k = list(map(int,input().split()))
a = [0]*n
done = True
def swap(z):
for j in range(0,n):
if a[j] == 0:
a[j] = z
done = True
break
else:
if a[j] > z:
swap(j)
a[j] = z
else:
done = False
break
for i in range(0,n):
for j in ... | |
d988 | for i in range(int(input())):
finish,distanetobolt,tigerAcceleration,boltspeed=map(int,input().split())
t1=((2*(finish+distanetobolt)/(tigerAcceleration))**0.5)
t2=(finish/boltspeed)
if t1>t2:
print("Bolt")
elif t1<t2:
print("Tiger")
else:
print("Tiger") | |
d989 | # cook your dish here
# cook your dish here
test=int(input())
for _ in range(test):
n=int(input())
a=[(bin(int(x))[2:][::-1]+("0")*32)for x in input().split()]
res=""
mysum=0
for i in range(32):
mycount=0
for j in range(n):
if(a[j][i]=="0"):
mycount+=1
if(mycount==n):
break
if(mycount>(n//2)):
... | |
d99 | q = int(input())
for _ in range(q):
c, m, x = list(map(int, input().split()))
print(min([c, m, (c + m + x) // 3])) | |
d990 | n=int(input())
while(n>0):
x,y,z=map(int,input().split())
t=(x+y)//z
if t%2==0:
print('Chef')
else:
print('Paja')
n-=1 | |
d991 | try:
n,m,a=map(int,input().split())
if n%a!=0:
number1=(n//a)+1
else:
number1=(n//a)
if m%a!=0:
number2=(m//a)+1
else:
number2=(m//a)
print(number1*number2)
except:
pass | |
d992 | n,k, q = list(map(int, input().split()))
mod = 10 ** 9 + 7
A = [0] * n
a, b, c, d, e, f, r, s, t, m, x = list(map(int, input().split()))
A[0] = x
for x in range(1, n):
if pow(t, x+1, s) <= r:
A[x] = (a*pow(A[x-1], 2, m) + b*A[x-1] + c) % m
else:
A[x] = (d*pow(A[x-1], 2, m) + e*A[x-1] + f) % m
def SRMQ(arr, ... | |
d993 | from math import sqrt
import sys
sys.setrecursionlimit(10**8)
intMax = 10**18
def knapsack(rl,l,c,m):
if m==0 and rl>0:
return intMax
if rl<=0:
return 0
return min(c[m-1]+knapsack(rl-l[m-1],l,c,m),knapsack(rl,l,c,m-1))
for _ in range(int(input())):
n= int(input())
cost=[]
length=[]
sides=[]
for i in range(n... | |
d994 | import math
def check(s):
if s==2:
return 1
for i in range(2,math.ceil(math.sqrt(s))+1):
if s%i == 0:
return 0
return 1
def Solve(slots):
if slots<3:
return 0
#check
s = math.sqrt(slots)
if math.floor(s) == math.ceil(s):
... | |
d995 | # cook your dish here
import sys
input=sys.stdin.readline
for _ in range(int(input())):
n,x=list(map(int,input().split()))
l=[0]
pre=[0]*(n+1)
sum=0
i=1
for m in input().split():
l.append(int(m))
sum+=int(m)
pre[i]=sum
i+=1
dict={}
k=[]
i=1
while (i*i)<=x:
if x%i==0:
k.append(i)
if (i*i)!=x:
... | |
d996 | rooms=int(input())
money=list(map(int,input().split()))
keys=int(input())
rev=-1
a,b=0,-1
tot=[]
#print(money[-3:-1])
x=0
for i in range(keys):
#print(money[b:-1],money[0:a])
x=sum(money[b:])+sum(money[0:keys-abs(b)])
tot.append(x)
b-=1
print(max(tot))
| |
d997 | s1=s2=lead=0
mlead1=mlead2=0
for _ in range(int(input())):
x, y= list(map(int, input().split()))
s1, s2= s1+x, s2+y
if(s1>s2):
lead=(s1-s2)
mlead1= max(mlead1, lead)
else:
lead=(s2-s1)
mlead2= max(mlead2, lead)
if(mlead1<(mlead2)):
print('2', mlead2)
else:
print('... | |
d998 | try:
t=int(input())
for a in range(t):
l=input().split()
n=int(l[0])
m=int(l[1])
d={}
s=0
for b in range(m):
l1=input().split()
i=int(l1[0])
j=int(l1[1])
k=int(l1[2])
for c in range(i,j+1):
if c not in d:
d[c]=10
for c in range(i,j+1):
d[c]=d[c]*k
for i in d:
s=s+d[i]
... | |
d999 | n,q=map(int,input().split())
dr={}
dc={}
for i in range(1,n+1):
dr[i]=0
dc[i]=0
mer=0
mec=0
for i in range(q):
s,j,k=input().split()
j=int(j)
k=int(k)
if s=="RowAdd":
dr[j]+=k
if dr[j]>mer:
mer=dr[j]
else:
dc[j]+=k
if mec<dc[j]:
mec=dc[j]
# m=max(list(dr.values()))+max(list(dc.values()))
# for i i... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.