finish level5/dodge-the-lasers

This commit is contained in:
Seongbeom Park 2022-03-07 22:00:25 +09:00
parent 3c7dd9524d
commit ae631d3c0a
4 changed files with 13 additions and 14 deletions

View File

@ -28,11 +28,12 @@
* Completed in: 6 hrs, 49 mins, 26 secs.
* DP & Optimization
## Encrypted message
CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY=
## Others
### dodge-the-laser
* Completed in: 2 days, 20 hrs, 22 mins, 39 secs.
* Reference
* [Beatty sequence](https://en.wikipedia.org/wiki/Beatty_sequence)
* How to find A001951 A Beatty sequence: a(n)=floor(n\*sqrt(2)), stackexchange, https://math.stackexchange.com/questions/2052179/how-to-find-sum-i-1n-left-lfloor-i-sqrt2-right-rfloor-a001951-a-beatty-s
## Encrypted message
CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY=

View File

@ -2,25 +2,23 @@ import math
from decimal import *
def solution(s):
getcontext().prec = 10000
sqrt_2 = Decimal(2).sqrt()
getcontext().prec = len(s)+1
return str(beatty_sum_sqrt2(int(s)))
return str(beatty_sum(sqrt_2, int(s)))
def beatty_sum(r, n):
def beatty_sum_sqrt2(n):
if n == 0:
return 0
if n == 1:
return 1
floor_r_n = int(math.floor(r*n))
floor_r_n = int(Decimal(2*(n**2)).sqrt())
return natural_number_sum(floor_r_n) - 2 * natural_number_sum(floor_r_n - n) - beatty_sum(r, floor_r_n - n)
return natural_number_sum(floor_r_n) - 2 * natural_number_sum(floor_r_n - n) - beatty_sum_sqrt2(floor_r_n - n)
def natural_number_sum(n):
return n*(n+1)/2
def ten(n):
return 10**n
return str(10**n)
tests = [
['77', '4208'],
@ -34,7 +32,7 @@ tests = [
['8', '47'],
['9', '59'],
['10', '73'],
[ten(100), '0']
[ten(100), '70710678118654752440084436210484903928483593768847403658833986899536623923105351942519376716382078638821760123411090095254685423841027253480565451739737157454059823250037671948325191776995310741236436']
]