In [1]:
!python -V

Python 3.5.5 :: Anaconda custom (64-bit)


# 驴啃计算器¶

## 解法二：二进制逼近¶

$\cosh 2x\ = \cosh^2 x + \sinh^2 x = 2\cosh^2 x - 1 = 2\sinh^2 x + 1$

$t + 1 = \cosh 2x = \cosh(2(\rm{asinh}(\rm{sqrt}(t/2))))$，换回去：

$x + 1 = \cosh(2(\rm{asinh}(\rm{sqrt}(x/2))))$

In [5]:
import json
import requests
host = "http://202.38.93.241:10024"

class Calc:
def __init__(self):
self.ops = []

def op(self, name, *kws):
if name in dir(self):
getattr(self, name)(*kws)
else:
self.ops.append(name)

def mul2(self):
# 乘以 2
self.op('exp')
self.op('x^2')
self.op('log')

def div2(self):
# 除以 2
self.op('exp')
self.op('sqrt')
self.op('log')

# 利用双曲函数公式加 1
self.op('div2')
self.op('sqrt')
self.op('asinh')
self.op('mul2')
self.op('cosh')

# 利用多次加 1 加 n
for _ in range(n):

def solve(x):
# 二进制小数点后的位数
n = 20
calc = Calc()

# 利用除以 2 （相当于二进制右移）构造小数部分
for i in range(n):
if int(x * 2 ** (n - i)) % 2:
calc.op('div2')

seq = ','.join(calc.ops)
return seq

with requests.session() as sess:
r = sess.get(host + '/challenges')
print(X)
data = {
"a1": solve(X[0]),
"a2": solve(X[1]),
"a3": solve(X[2])
}
r = sess.post(host + "/submit", data=data)
print(resp["msg"])

[35.82831063699986, 80.6361952659885, 88.59694036265915]
flag{you_are_good_at_using_calculators_by_amiya}


## 解法三：连分数¶

In [7]:
import json
import requests
host = "http://202.38.93.241:10024"

import sympy
EPS = 1e-7

class Calc:
def __init__(self):
self.ops = []

def op(self, name, *kws):
if name in dir(self):
getattr(self, name)(*kws)
else:
self.ops.append(name)

def mul2(self):
# 乘以 2
self.op('exp')
self.op('x^2')
self.op('log')

def div2(self):
# 除以 2
self.op('exp')
self.op('sqrt')
self.op('log')

# 利用双曲函数公式加 1
self.op('div2')
self.op('sqrt')
self.op('asinh')
self.op('mul2')
self.op('cosh')

# 利用多次加 1 加 n
for _ in range(n):

def solve(x):
# 将小数化为分数
p = int(x / EPS)
q = int(p / x)

# 计算连分数数组
cfs = sympy.ntheory.continued_fraction.continued_fraction_periodic(p, q, 0)

# 利用 addn 和 1/x 计算连分数
calc = Calc()
for k in cfs[::-1]:
calc.op('1/x')
calc.op('1/x')

seq = ','.join(calc.ops)
return seq

with requests.session() as sess:
r = sess.get(host + '/challenges')
print(X)
data = {
"a1": solve(X[0]),
"a2": solve(X[1]),
"a3": solve(X[2])
}
r = sess.post(host + "/submit", data=data)
print(resp["msg"])

[8.537001664460819, 13.772290777666662, 90.89072584304898]
flag{you_are_good_at_using_calculators_by_amiya}


## 附录¶

• 为什么会 system error？

• 为什么在构造 add1 时，选择了 $[1]\ \cosh 2x\ =[2]\ \cosh^2 x + \sinh^2 x =[3]\ 2\cosh^2 x - 1 = [4]\ 2\sinh^2 x + 1$ 中的 $[1] = [4]$ 而不是 $[1] = [3]$ ？