This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bin_search_check(arr): | |
min_el = max_el = last = None | |
for el in arr: | |
if last != None: | |
if (min_el != None and el < min_el) or (max_el != None and el > max_el): return False | |
if el < last: max_el = last | |
if el >= last: min_el = last | |
last = el | |
return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def primes(n): | |
lst = {} | |
i = 2 | |
while i*i <= n: | |
while n % i == 0: | |
lst[i] = lst[i]+1 if i in lst else 1 | |
n //= i | |
i += 1 | |
if n > 1: lst[n] = 1 | |
return lst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Вычисление кол-ва "уравнений обратной суммы" 1/n=(1/x+1/y) для натуральных x и y через разложение на простые множители | |
# В данной функции перемножаются степени каждого множителя, умноженные на 2 и увеличенные на 1 | |
# Например, 12250 = 2^1 * 5^3 * 7^2. Сами простые множители нас не интересуют, нас интересуют только их степени (1, 3, 2) | |
# Таким образом ответом будет (1*2+1) * (3*2+1) * (2*2+1) = 105 | |
def RSEnum(n): | |
i = 2 | |
# вычисляем простые числа | |
result = 1 | |
while i*i <= n: | |
k = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import factorial | |
def anagrams(string): | |
count = {} | |
for ch in string: | |
count[ch] = count[ch]+1 if ch in count else 1 | |
n = factorial(len(string)) | |
for key in count.keys(): | |
if count[key] > 1: n //= factorial(count[key]) | |
return n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import sqrt | |
from math import ceil | |
# Найти минимальное число n, при котором ± 1 ± 2 ± ... ± n = k | |
def min_row_len(k): | |
if k == 0: return 3 # частный случай: 1+2-3 = -1-2+3 = 0 | |
if k < 1: k = -k # для отрицательных чисел результат будет тем же (знаки чисел ряда будут инвертированы) | |
n = ceil((sqrt(1+8*k)-1)/2) # мин. длина ряда 1..n, сумма которого >= k (решаем уравнение n^2 + n = 2*k) | |
dif = n*(n+1)//2 - k # разница между суммой ряда и k | |
# если разница чётная, её можно аннулировать инверсией знака одного из чисел ряда (т.к. при изменении |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
format PE64 Console 5.0 | |
include 'win64axp.inc' | |
;-- CODE SECTION ------------------------------------------------------------------------------------------------------- | |
.code | |
entry: | |
frame |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get maximum side size of three squares inside rectangle with specified width and height | |
def sqr3_side_in_rect(x, y): | |
# assume: | |
# length (of rectangle) = maximum of x and y | |
# width (of rectangle) = minimum of x and y | |
# | |
# if width divided by 2 is more than | |
# minimum of (length divided by 3) and width | |
# we'll cut by this scheme: | |
# +-----+-----+--+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
format PE64 Console 5.0 | |
include 'win64axp.inc' | |
;-- CODE SECTION ------------------------------------------------------------------------------------------------------- | |
.code | |
entry: | |
frame |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Для решения используем числа Фибоначчи | |
# с добавлением единицы на каждом шаге | |
# Т.е. если число фибоначчи N[k] = N[k-1] + N[k-2], | |
# то здесь будет N[k] = N[k-1] + N[k-2] + 1 | |
def in_rank(k): | |
n, m = 0, 0 | |
for i in range(k): | |
n, m = n+m+1, n | |
return n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import factorial | |
# Вычислить биномиальный коэффициент Cnk | |
# Возвращает None при k > n, k < 0 и результате, превышающем 2**64-1 | |
def C(n, k): | |
if not 0 <= k <= n: return None # неверные параметры | |
if k > n-k: k = n-k # C(n, k) = C(n, n-k) | |
# при k > 33 результат однозначно будет больше, чем 2**64-1, | |
# т.к. C(67, 33) < 2**64, но C(68, 34) уже больше |
OlderNewer