Game with modulo

本文介绍了一种猜数字游戏的算法实现,通过不断提问并分析返回结果,利用数学原理找到目标数字。游戏设定中,玩家需在不超过60次提问内猜出1到10^9之间的数字,每次提问比较两数对目标数取模后的大小。文章详细解释了算法思路,包括如何快速缩小搜索范围,以及特殊情况下对数字1和2的处理。

D. Game with modulo

http://codeforces.com/contest/1104/problem/D

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This is an interactive problem.

Vasya and Petya are going to play the following game: Petya has some positive integer number aa. After that Vasya should guess this number using the following questions. He can say a pair of non-negative integer numbers (x,y)(x,y). Petya will answer him:

  • "x", if (xmoda)≥(ymoda)(xmoda)≥(ymoda).
  • "y", if (xmoda)<(ymoda)(xmoda)<(ymoda).

We define (xmoda)(xmoda) as a remainder of division xx by aa.

Vasya should guess the number aa using no more, than 60 questions.

It's guaranteed that Petya has a number, that satisfies the inequality 1≤a≤1091≤a≤109.

Help Vasya playing this game and write a program, that will guess the number aa.

Interaction

Your program should play several games.

Before the start of any game your program should read the string:

  • "start" (without quotes) — the start of the new game.
  • "mistake" (without quotes) — in the previous game, you found the wrong answer. Your program should terminate after reading this string and it will get verdict "Wrong answer".
  • "end" (without quotes) — all games finished. Your program should terminate after reading this string.

After reading the string "start" (without quotes) the new game starts.

At the beginning, your program should ask several questions about pairs of non-negative integer numbers (x,y)(x,y). You can only ask the numbers, that satisfy the inequalities 0≤x,y≤2⋅1090≤x,y≤2⋅109. To ask a question print "? x y" (without quotes). As the answer, you should read one symbol:

  • "x" (without quotes), if (xmoda)≥(ymoda)(xmoda)≥(ymoda).
  • "y" (without quotes), if (xmoda)<(ymoda)(xmoda)<(ymoda).
  • "e" (without quotes) — you asked more than 6060 questions. Your program should terminate after reading this string and it will get verdict "Wrong answer".

After your program asked several questions your program should print the answer in form "! a" (without quotes). You should print the number aa satisfying the inequalities 1≤a≤1091≤a≤109. It's guaranteed that Petya's number aa satisfied this condition. After that, the current game will finish.

We recall that your program can't ask more than 6060 questions during one game.

If your program doesn't terminate after reading "mistake" (without quotes), "end" (without quotes) or "e" (without quotes), it can get any verdict, because it will continue reading from closed input. Also, if your program prints answer or question in the incorrect format it can get any verdict, too. Be careful.

Don't forget to flush the output after printing questions and answers.

To flush the output, you can use:

  • fflush(stdout) in C++.
  • System.out.flush() in Java.
  • stdout.flush() in Python.
  • flush(output) in Pascal.
  • See the documentation for other languages.

It's guaranteed that you should play at least 11 and no more than 100100 games.

Hacks:

In hacks, you can use only one game. To hack a solution with Petya's number aa (1≤a≤1091≤a≤109) in the first line you should write a single number 11 and in the second line you should write a single number aa.

Example

input

start
x
x
start
x
x
y
start
x
x
y
y
end

output

? 0 0
? 10 1
! 1
? 0 0
? 3 4
? 2 5
! 2
? 2 4
? 2 5
? 3 10
? 9 1
! 3

Note

In the first test, you should play 33 games with Petya's numbers 11, 22 and 33.

In the first game, Petya will answer "x" (without quotes) to any question, because (xmod1)=0(xmod1)=0 for any integer xx.

In the second game, if you will ask pair (0,0)(0,0), the answer will be "x" (without quotes), because (0mod2)≥(0mod2)(0mod2)≥(0mod2). But if you will ask pair (2,5)(2,5), the answer will be "y" (without quotes), because (2mod2)<(5mod2)(2mod2)<(5mod2), because (2mod2)=0(2mod2)=0 and (5mod2)=1(5mod2)=1.

题意:猜数字a,每次你输入? x y,若x%a>=y%a则返回x,否则返回y,输入! x表示你猜a是x,返回start表示你猜对了并且下一轮游戏开始,否则直接退出程序就好

题解:可以发现f(x)%a的图像如下: 

 是一个周期为a的函数,如果从1 2开始猜,每次翻倍,则必然会有一次x,y包含a,此时会输出y,因是在一个周期内,之后在x,y之间二分答案即可,注意特判a为1或2

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    long long l,r,mid;
    while(true)
    {
        cin >> s;
        if(s == "start")
        {
            l = 1,r = 2;
            printf("? %I64d %I64d\n",l,r);
            while(true)
            {
                fflush(stdout);
                cin >> s;
                if(l == 1 && r == 2 && s=="x")
                {
                    printf("? 0 1\n");
                    fflush(stdout);
                    cin >> s;
                    if(s == "x")
                        printf("! 1\n");
                    else
                        printf("! 2\n");
                    break;
                }
                else if(s == "x")
                {
                    while(l < r-1)
                    {
                        mid = (l+r) >> 1;
                        printf("? %I64d %I64d\n",mid,r);
                        fflush(stdout);
                        cin >> s;
                        if(s == "x")
                            l = mid;
                        else 
                            r = mid;
                    }
                    printf("! %I64d\n",r);
                    break;
                }
                else
                {
                    l *= 2;
                    r *= 2;
                    printf("? %I64d %I64d\n",l,r);
                }
            }
            fflush(stdout);
        }
        else
            break;
    }
    return 0;
}
 

### 下载资源失败的解决办法 #### 网络方面 - 确保网络连接稳定,可尝试重新连接Wi-Fi或者切换到其他网络环境,比如从Wi-Fi切换到移动数据网络,反之亦然。 - 检查防火墙和代理设置,确保防火墙没有阻止bd2_mod_packer的网络访问,或者检查代理设置是否正确。如果使用了代理,可以尝试暂时禁用代理,然后再次进行下载。 #### 服务器方面 - 确认提供资源的服务器是否正常运行。可以通过访问服务器的官方状态页面、相关社区论坛或者联系服务器管理员来获取服务器的状态信息。 #### 重试机制 在bd2_mod_packer中添加重试逻辑,当出现下载错误时,自动进行多次重试。以下是一个简单的Python示例,展示如何实现重试机制: ```python import requests import time max_retries = 5 retry_delay = 5 # 重试间隔时间(秒) for attempt in range(max_retries): try: response = requests.get('https://example.com/common-skeleton-data-group12_assets_all.bundle') response.raise_for_status() # 检查响应状态码 # 处理下载的数据 with open('common-skeleton-data-group12_assets_all.bundle', 'wb') as f: f.write(response.content) print("下载成功") break except requests.exceptions.RequestException as e: if attempt < max_retries - 1: print(f"下载失败,第 {attempt + 1} 次重试,错误信息: {e}") time.sleep(retry_delay) else: print(f"达到最大重试次数,下载失败,错误信息: {e}") ``` #### 磁盘空间 确保磁盘有足够的空间来存储下载的文件。可以清理磁盘上的不必要文件,或者更换到有足够空间的磁盘进行下载。 ### ImportError: attempted relative import with no known parent package错误的解决办法 #### 脚本执行方式 如果是以脚本方式执行Python文件,相对导入可能会失败。可以将脚本作为模块来运行,例如使用`python -m package.module`的方式运行。 #### 模块结构 确保项目有正确的模块结构,包含`__init__.py`文件(Python 3.3及以上版本中,包内的`__init__.py`文件不是必需的,但对于相对导入,正确的包结构很重要)。 #### PYTHONPATH环境变量 可以将项目根目录添加到`PYTHONPATH`环境变量中,这样Python解释器就能找到相关的包和模块。例如,在Linux或macOS中可以使用以下命令: ```bash export PYTHONPATH=$PYTHONPATH:/path/to/your/project ``` 在Windows中可以使用以下命令: ```batch set PYTHONPATH=%PYTHONPATH%;C:\path\to\your\project ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值