交互题(二分)(D. Game with modulo)

题目链接:http://codeforces.com/contest/1104/problem/D

题目大意:给出一个式子 x%a y%a,会返回结果,如果返回x代表x%a>=y%a.如果返回y代表x%a<y%a.然后让你判断a的值。

具体思路:首先确定范围(0,1),(1,2),(2,4),(4,8)这样确定范围,就可以把大体的范围确定好了。确定好了之后还是运用二分的思路逐步逼近最佳结果,判断结束二分的条件是mid+1==r.举个例子, 15 16 ,如果返回的是x,那么这个时候a就是16了,如果返回的是y那么这个时候a就是15了。

AC代码:

#include<bits/stdc++.h>
using namespace std;
# define inf 0x3f3f3f3f
const int maxn = 1e5+100;
string str;
int main()
{
    while(cin>>str)
    {
        if(str[0]=='s')
        {
            int x,y;
            for(x=0,y=1;; x=y,y*=2)
            {
                cout<<'?'<<" "<<x<<" "<<y<<endl;
                cin>>str;
                if(str=="x")
                    break;
            }
            while(x<y)
            {
                 int mid = (x+y)>>1;
                cout<<'?'<<" "<<mid<<" "<<y<<endl;
                fflush(stdout);
                cin>>str;
                if(mid+1==y)
                {
                    cout<<"! ";
                    if(str=="x")cout<<y<<endl;
                    else cout<<mid<<endl;
                    break;
                }
                if(str=="x")//这个地方和正常的二分是相反的。
                {
                   x=mid;
                }
                if(str=="y")
                {
                    y=mid;
                }
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/letlifestop/p/10316520.html