66 lines
1.6 KiB
Python
66 lines
1.6 KiB
Python
import math
|
|
from decimal import *
|
|
|
|
getcontext().prec = 102
|
|
sqrt_2 = Decimal(2).sqrt()
|
|
|
|
def solution(s):
|
|
total = 0
|
|
for i in range(int(s)+1):
|
|
total += int(math.floor(i*sqrt_2))
|
|
#print i, total
|
|
return str(total)
|
|
|
|
def check_known_pattern(start, end, memo):
|
|
if '0'*(end-start+1) in memo: # TODO: checkk value in the memo
|
|
return '0'*(end-start+1)
|
|
return False
|
|
|
|
def generate_dfs(start, end, memo):
|
|
pattern = check_known_pattern(start, end, memo)
|
|
if pattern:
|
|
yield pattern, memo[pattern]
|
|
else:
|
|
new_pattern = ''
|
|
new_value = 0 # TODO: calculate new_value
|
|
pivot = int(math.floor((start+end)/2)) # TODO: power of 2 is optimal
|
|
for pattern, value in generate_dfs(start, pivot, memo):
|
|
new_pattern += pattern
|
|
new_value += value
|
|
yield pattern, value
|
|
for pattern, value in generate_dfs(pivot + 1, end, memo):
|
|
new_pattern += pattern
|
|
new_value += value
|
|
yield pattern, value
|
|
memo[new_pattern] = new_value
|
|
|
|
memo = {'0': 1, '1': 1} # TODO: upgrade memo
|
|
total = 0
|
|
for pattern, value in generate_dfs(1, 5, memo):
|
|
total += value
|
|
print pattern, value
|
|
print total, memo
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
tests = [
|
|
['77', '4208'],
|
|
['1', '1'],
|
|
['2', '3'],
|
|
['3', '7'],
|
|
['4', '12'],
|
|
['5', '19'],
|
|
['6', '27'],
|
|
['7', '36'],
|
|
['8', '47'],
|
|
['9', '59'],
|
|
['10', '73'],
|
|
]
|
|
|
|
|
|
for test in tests:
|
|
result = solution(test[0])
|
|
print(test[0], result == test[1], result, test[1])
|