DIQQAT:EDITORIAL COPY PASTE QILIB QOYISH UCHUN BERILMAGAN!!!
https://youtu.be/dQw4w9WgXcQ?si=06OmpGL7RkOUdI_p
A masala:
N=int(input())
print((N**2-2*N+1)*N**2)
B masala:
import math
def binomial_expansion(n):
terms=[]
for k in range(n+1):
coeff=math.comb(n,k)
a_exp=n-k
b_exp=k
term=f"{coeff}*a^{a_exp}*b^{b_exp}"
terms.append(term)
return " + ".join(terms)
n=int(input())
print(binomial_expansion(n))
E masala:
def parse_board(lines):
b=[list(line.strip()) for line in lines[:8]]
p=lines[8].strip()
return b,p
def find_pos(b,p):
for i in range(8):
for j in range(8):
if b[i][j]==p:
return i,j
return None
def king_moves(b,x,y):
m=0
dirs=[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
for dx,dy in dirs:
nx,ny=x+dx,y+dy
if 0<=nx<8 and 0<=ny<8 and b[nx][ny]=='*':
m+=1
if x==7 and y==4:
if b[7][5]=='*' and b[7][6]=='*' and b[7][7]=='R':
m+=1
if b[7][3]=='*' and b[7][2]=='*' and b[7][1]=='*' and b[7][0]=='R':
m+=1
return m
def linear_moves(b,x,y,dirs):
m=0
for dx,dy in dirs:
nx,ny=x+dx,y+dy
while 0<=nx<8 and 0<=ny<8:
if b[nx][ny]=='*':
m+=1
else:
break
nx+=dx
ny+=dy
return m
def knight_moves(b,x,y):
m=0
dirs=[(-2,-1),(-2,1),(-1,-2),(-1,2),(1,-2),(1,2),(2,-1),(2,1)]
for dx,dy in dirs:
nx,ny=x+dx,y+dy
if 0<=nx<8 and 0<=ny<8 and b[nx][ny]=='*':
m+=1
return m
def pawn_moves(b,x,y):
m=0
if x>0 and b[x-1][y]=='*':
m+=1
if x==6 and b[x-2][y]=='*':
m+=1
return m
def count_moves(b,p,x,y):
if p=='K':
return king_moves(b,x,y)
elif p=='Q':
return linear_moves(b,x,y,[(-1,0),(1,0),(0,-1),(0,1)])+linear_moves(b,x,y,[(-1,-1),(-1,1),(1,-1),(1,1)])
elif p=='R':
m=linear_moves(b,x,y,[(-1,0),(1,0),(0,-1),(0,1)])
if x==7 and y==7 and b[7][5]=='*' and b[7][6]=='*' and b[7][4]=='K':
m+=1
if x==7 and y==0 and b[7][1]=='*' and b[7][2]=='*' and b[7][3]=='*' and b[7][4]=='K':
m+=1
return m
elif p=='B':
return linear_moves(b,x,y,[(-1,-1),(-1,1),(1,-1),(1,1)])
elif p=='H':
return knight_moves(b,x,y)
elif p=='P':
return pawn_moves(b,x,y)
return 0
def main(lines):
b,p=parse_board(lines)
x,y=find_pos(b,p)
return count_moves(b,p,x,y) if x is not None and y is not None else 0
lines=[input() for _ in range(9)]
print(main(lines))
C masala:
def knapsack(weights, values, capacity):
n=len(values)
dp=[[0]*(capacity+1) for _ in range(n+1)]
for i in range(1, n+1):
for w in range(1, capacity+1):
if weights[i-1]<=w:
dp[i][w]=max(dp[i-1][w], dp[i-1][w-weights[i-1]]+values[i-1])
else:
dp[i][w]=dp[i-1][w]
max_value=dp[n][capacity]
items=[]
wgt=capacity
for i in range(n, 0, -1):
if max_value<=0:
break
if max_value!=dp[i-1][wgt]:
items.append(i-1)
max_value-=values[i-1]
wgt-=weights[i-1]
return dp[n][capacity], items
c=int(input())
w=[]
v=[]
item_count=int(input())
w=[*map(int,input().split())]
v=[*map(int,input().split())]
max_value, selected_items=knapsack(w, v, c)
print(max_value)
D masala:
import re
token = re.compile(r'([+-]?)\s*(\d*)\s*([a-zA-Z\(\)])')
def substitute(formula, substitutes):
res = formula
for var, sub in substitutes:
res = res.replace(var, '({})'.format(sub))
return res if res == formula else substitute(res, substitutes)
def reduce(tokens):
res = dict()
for sign, num, var in tokens:
if var == ')':
return res
coef = int(sign + (num or '1'))
if var == '(':
for k, v in reduce(tokens).items():
res[k] = res.get(k, 0) + coef * v
else:
res[var] = res.get(var, 0) + coef
return res
def simplify(examples, formula):
substitutes = [(k.strip(), v) for v, k in map(lambda x: x.split('='), examples)]
subbed = substitute(formula, substitutes)
reduced = reduce(iter(token.findall(subbed)))
return ''.join(map('{0[1]}{0[0]}'.format, reduced.items()))
examples=input().split()
formula=input()
print(simplify(examples, formula))
F masala:
d=dict()
b="0123456789"
c="+-*/%="
while 1:
a=list(input().replace(" ","").replace("/","//"))
if a==["0"]:
break
if len(a)!=1 and a[1]=="=":
for i in range(2,len(a)):
if a[i] not in b and a[i] not in c:
a[i]=d[a[i]]
d[a[0]]=str(eval("".join(a[2:])))
print(eval("".join(a[2:])))
else:
for x in range(len(a)):
if a[x] not in b and a[x] not in c:
a[x]=d[a[x]]
print(eval("".join(a)))
G masala:
def motmi(e,d,a,b,c,j,k,l,p,f,g,h,i):
hod=[e]
shax=[d]
for x in range(g,9):
if a[0]+str(x)!=e:hod.append(a[0]+str(x))
else:
hod.append(a[0]+str(x))
break
for x in range(g,0,-1):
if a[0]+str(x)!=e:hod.append(a[0]+str(x))
else:
hod.append(a[0]+str(x))
break
for x in range(f,9):
if c[x]+a[1]!=e:hod.append(c[x]+a[1])
else:
hod.append(c[x]+a[1])
break
for x in range(f,0,-1):
if c[x]+a[1]!=e:hod.append(c[x]+a[1])
else:
hod.append(c[x]+a[1])
break
for x in range(i,9):
if b[0]+str(x)!=e:hod.append(b[0]+str(x))
else:
hod.append(b[0]+str(x))
break
for x in range(i,0,-1):
if b[0]+str(x)!=e:hod.append(b[0]+str(x))
else:
hod.append(b[0]+str(x))
break
for x in range(h,9):
if c[x]+b[1]!=e:hod.append(c[x]+b[1])
else:
hod.append(c[x]+b[1])
break
for x in range(h,0,-1):
if c[x]+b[1]!=e:hod.append(c[x]+b[1])
else:
hod.append(c[x]+b[1])
break
if j+1<=8:
hod.append(c[j+1]+str(k))
if j-1>=1:
hod.append(c[j-1]+str(k))
if k+1<=8:
hod.append(c[j]+str(k+1))
if k-1>=1:
hod.append(c[j]+str(k-1))
if j+1<=8 and k+1<=8:
hod.append(c[j+1]+str(k+1))
if j+1<=8 and k-1>=1:
hod.append(c[j+1]+str(k-1))
if j-1>=1 and k+1<=8:
hod.append(c[j-1]+str(k+1))
if j-1>=1 and k-1>=1:
hod.append(c[j-1]+str(k-1))
if l+1<=8:
shax.append(c[l+1]+str(p))
if l-1>=1:
shax.append(c[l-1]+str(p))
if p+1<=8:
shax.append(c[l]+str(p+1))
if p-1>=1:
shax.append(c[l]+str(p-1))
if l+1<=8 and p+1<=8:
shax.append(c[l+1]+str(p+1))
if l+1<=8 and p-1>=1:
shax.append(c[l+1]+str(p-1))
if l-1>=1 and p+1<=8:
shax.append(c[l-1]+str(p+1))
if l-1>=1 and p-1>=1:
shax.append(c[l-1]+str(p-1))
i=0
for x in shax:
if x==a or x==b:
if hod.count(x)>4:
i+=1
elif x in hod:
i+=1
if i==len(shax):
return 1
else:
return 0
def turahodyesno(n,m,x1,y1,f,g,h,i):
x,y=n,m
while x!=1:
x-=1
if f==x and y==g or h==x and y==i:
break
if x==x1 and y==y1:
return 1
if x==1:
break
x,y=n,m
while x!=8:
x+=1
if f==x and y==g or h==x and y==i:
break
if x==x1 and y==y1:
return 1
if x==8:
break
x,y=n,m
while y!=1:
y-=1
if f==x and y==g or h==x and y==i:
break
if x==x1 and y==y1:
return 1
if y==1:
break
x,y=n,m
while y!=8:
y+=1
if f==x and y==g or h==x and y==i:
break
if x==x1 and y==y1:
return 1
if y==8:
break
return 0
a,b,e,d=input().split()
c=["","a","b","c","d","e","f","g","h"]
f,g,h,i,j,k,l,p=c.index(a[0]),int(a[1]),c.index(b[0]),int(b[1]),c.index(e[0]),int(e[1]),c.index(d[0]),int(d[1])
def biryurishmot(a,b,e,d,c,f,g,h,i,j,k,l,p):
for x in range(1,9):
for y in range(1,9):
if turahodyesno(j,k,x,y,l,p,f,g):
if motmi(a,b,c[x]+str(y),d,c,f,g,h,i,x,y,l,p):
return 1
if turahodyesno(l,p,x,y,j,k,f,g):
if motmi(a,b,e,c[x]+str(y),c,f,g,h,i,j,k,x,y):
return 1
return 0
if a=="e1" and (e=="a1" or d=="a1"):
if e=="a1":
if motmi("c1",b,"d1",d,c,3,1,h,i,4,1,l,p):
print("Yes")
else:
print("No")
else:
if motmi("c1",b,e,"d1",c,3,1,h,i,j,k,4,1):
print("Yes")
else:
print("No")
elif a=="e1" and (e=="h1" or d=="h1"):
if e=="h1":
if motmi("g1",b,"f1",d,c,7,1,h,i,6,1,l,p):
print("Yes")
else:
print("No")
else:
if motmi("g1",b,e,"f1",c,7,1,h,i,j,k,6,1):
print("Yes")
else:
print("No")
elif biryurishmot(a,b,e,d,c,f,g,h,i,j,k,l,p):
print("Yes")
else:
print("No")