maze_python/codeforces_c3_final.py
2025-06-30 21:05:34 +08:00

98 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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()