98 lines
2.8 KiB
Python
98 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
||
import sys
|
||
|
||
def solve():
|
||
n = int(input())
|
||
|
||
# 最终策略:基于问题的深入理解
|
||
#
|
||
# 核心思想:
|
||
# 1. 使用digit操作标准化到小范围
|
||
# 2. 利用数学性质构造目标
|
||
|
||
# 对于所有情况,先用digit标准化
|
||
print("digit")
|
||
sys.stdout.flush()
|
||
response = int(input())
|
||
|
||
if response != 1:
|
||
return
|
||
|
||
# 现在当前值是原数字的数字和
|
||
# 对于1-9的目标,我们有简单策略
|
||
# 对于更大的目标,我们使用构造法
|
||
|
||
if n <= 9:
|
||
# 目标是1-9,直接尝试所有可能的调整
|
||
# 由于我们不知道确切的当前值,枚举所有可能性
|
||
|
||
attempts = [
|
||
# 假设当前是1-9,计算到n的差值
|
||
n - 1, n - 2, n - 3, n - 4, n - 5,
|
||
n - 6, n - 7, n - 8, n - 9
|
||
]
|
||
|
||
for diff in attempts:
|
||
print(f"add {diff}")
|
||
sys.stdout.flush()
|
||
response = int(input())
|
||
if response == 1:
|
||
break
|
||
|
||
else:
|
||
# 目标大于9,使用乘法构造
|
||
# 策略:找到n的一个小因子d,使得n = d * q
|
||
# 然后构造d,再乘以q
|
||
|
||
best_factor = None
|
||
best_quotient = None
|
||
|
||
# 寻找最佳的因子分解
|
||
for d in range(1, 10):
|
||
if n % d == 0:
|
||
q = n // d
|
||
if q <= 10**17: # 确保乘法不会溢出
|
||
best_factor = d
|
||
best_quotient = q
|
||
break
|
||
|
||
if best_factor is not None:
|
||
# 使用因子分解策略
|
||
# 先构造best_factor
|
||
for current_guess in range(1, 10):
|
||
diff = best_factor - current_guess
|
||
print(f"add {diff}")
|
||
sys.stdout.flush()
|
||
response = int(input())
|
||
|
||
if response == 1:
|
||
# 成功构造因子,现在乘以quotient
|
||
print(f"mul {best_quotient}")
|
||
sys.stdout.flush()
|
||
response = int(input())
|
||
break
|
||
else:
|
||
# 没有合适的因子分解,使用直接加法
|
||
# 这种情况下,假设当前是某个小数字,直接加到n
|
||
for current_guess in range(1, 10):
|
||
diff = n - current_guess
|
||
if 0 < diff <= 10**18:
|
||
print(f"add {diff}")
|
||
sys.stdout.flush()
|
||
response = int(input())
|
||
if response == 1:
|
||
break
|
||
|
||
# 结束
|
||
print("!")
|
||
sys.stdout.flush()
|
||
final_response = int(input())
|
||
|
||
def main():
|
||
t = int(input())
|
||
for _ in range(t):
|
||
solve()
|
||
|
||
if __name__ == "__main__":
|
||
main()
|