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

130 lines
4.2 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: 目标100, x未知 -> add -10(失败) -> add 1(成功) -> mul 10(成功) -> 结果100
# 示例2: 目标5, x未知 -> digit(成功) -> div 2(成功) -> 结果5
# 观察:我们需要一个能处理任意初始值的通用策略
# 通用策略:
# 1. 先用digit将值标准化到较小范围
# 2. 然后用算术操作构造目标
# 第一步:标准化
print("digit")
sys.stdout.flush()
response = int(input())
if response != 1:
return
# 现在值在某个较小的范围内但可能不是1-9因为digit可能需要多次
# 第二步:构造目标
if n <= 9:
# 对于单位数目标,尝试多种调整策略
success = False
# 策略1尝试各种可能的当前值调整到n
for possible_current in range(1, 100): # 扩大搜索范围
if success:
break
# 计算需要的调整
diff = n - possible_current
# 尝试加法调整
if abs(diff) <= 10**18:
print(f"add {diff}")
sys.stdout.flush()
response = int(input())
if response == 1:
success = True
break
# 策略2如果加法失败尝试除法如示例2
if not success and n < 10:
for divisor in range(2, 20):
print(f"div {divisor}")
sys.stdout.flush()
response = int(input())
if response == 1:
# 除法成功,可能需要继续调整
for adj in range(-10, 11):
if adj == 0:
continue
print(f"add {adj}")
sys.stdout.flush()
response = int(input())
if response == 1:
success = True
break
break
else:
# 对于大数目标,使用乘法+加法组合
success = False
# 策略1找到合适的因数分解 n = a * b
for a in range(1, min(100, n+1)):
if success:
break
if n % a == 0:
b = n // a
if b <= 10**18:
# 尝试构造 a然后乘以 b
# 首先尝试调整到 a
for current_guess in range(1, 100):
diff = a - current_guess
print(f"add {diff}")
sys.stdout.flush()
response = int(input())
if response == 1:
# 成功调整到 a现在乘以 b
print(f"mul {b}")
sys.stdout.flush()
response = int(input())
if response == 1:
success = True
break
else:
# 乘法失败,尝试下一个分解
break
# 如果这个调整失败尝试下一个current_guess
if success:
break
# 策略2如果因数分解失败尝试直接构造
if not success:
for current_guess in range(1, 100):
diff = n - current_guess
if abs(diff) <= 10**18:
print(f"add {diff}")
sys.stdout.flush()
response = int(input())
if response == 1:
success = True
break
# 输出结果
print("!")
sys.stdout.flush()
final_response = int(input())
def main():
t = int(input())
for _ in range(t):
solve()
if __name__ == "__main__":
main()