Problem Statement

This is an interactive task.

Snuke has a favorite positive integer, N. You can ask him the following type of question at most 64 times: "Is n your favorite integer?" Identify N.

Snuke is twisted, and when asked "Is n your favorite integer?", he answers "Yes" if one of the two conditions below is satisfied, and answers "No" otherwise:

  • Both nN and str(n)≤str(N) hold.
  • Both n>N and str(n)>str(N) hold.

Here, str(x) is the decimal representation of x (without leading zeros) as a string. For example, str(123)= 123 and str(2000) = 2000. Strings are compared lexicographically. For example, 11111 < 123 and 123456789 < 9.

Constraints

  • 1≤N≤109

Input and Output

Write your question to Standard Output in the following format:

? n

Here, n must be an integer between 1 and 1018 (inclusive).

Then, the response to the question shall be given from Standard Input in the following format:

ans

Here, ans is either Y or NY represents "Yes"; N represents "No".

Finally, write your answer in the following format:

! n

Here, n=N must hold.

Judging

  • After each output, you must flush Standard Output. Otherwise you may get TLE.
  • After you print the answer, the program must be terminated immediately. Otherwise, the behavior of the judge is undefined.
  • When your output is invalid or incorrect, the behavior of the judge is undefined (it does not necessarily give WA).

Sample

Below is a sample communication for the case N=123:

Input Output
  ? 1
Y  
  ? 32
N  
  ? 1010
N  
  ? 999
Y  
  ! 123
  • Since 1≤123 and str(1)≤str(123), the first response is "Yes".
  • Since 32≤123 but str(32)>str(123), the second response is "No".
  • Since 1010>123 but str(1010)≤str(123), the third response is "No".
  • Since 999≥123 and str(999)>str(123), the fourth response is "Yes".
  • The program successfully identifies N=123 in four questions, and thus passes the case.

题意:有一个1~1e9的数字,让你去猜。

你可以做最多64个询问,每一个询问评测机会根据这个规定来返回信息。

Snuke is twisted, and when asked "Is n your favorite integer?", he answers "Yes" if one of the two conditions below is satisfied, and answers "No" otherwise:

  • Both nN and str(n)≤str(N) hold.
  • Both n>N and str(n)>str(N) hold.

思路:

先确定这个数多少位,然后每一位用二分去得到具体这一位的数字。

我们从1到10,再100 ,1000, 每一次*10的去询问,就可以得到这个数字的位数。

如果我们问10,返回Y,问100,返回N,那么这一位是2位数,可以对照规定自己琢磨为什么。

知道多少位只有,我们每一个位置

int mid;
int l=0;
int r=9;

这样二分。

这里我利用了多一位的数一定n>N来一直进入第二个条件来询问的,

比如 是二位数,我问的时候问三位数,已知位放再数字中,未知位放0,询问位通过二分进行变化,

那么一定进入第二个条件,根据字典序的关系,来确定这一位的数字是几。

对于1和100这种1和1后面只有0的数,我们通过询问全是9的数来特判。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <strstream>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int query(string ans)
{
char x;
cout<<"? "<<ans<<endl;
cin>>x;
if(x=='Y')
{
return ;
}else
{
return ;
}
}
void solve()
{
string temp="";
string ans="";
while(!query(temp))
{
temp+="";
ans+="";
}
cout<<"! "<<ans<<endl;
}
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
string ans="";
string res;
while()
{
cout<<"? "<<ans<<endl;
cin>>res;
if(res[]=='Y')
{
ans+="";
}else
{
break;
}
if(ans.length()>)
{
solve();
return ;
}
}
int len=ans.length();
string temp;
rep(i,,len-)
{
int mid;
int l=;
int r=;
while(l<=r)
{
mid=(l+r)>>;
ans[i]=''+mid;
if(query(ans))
{
r=mid-;
}else
{
l=mid+;
temp=ans;
}
}
ans=temp;
}
temp.pop_back();
int num=temp.length();
stringstream ss;
ss.clear();
ss<<temp;
ll fans;
ss>>fans;
fans++;
cout<<"! "<<fans<<endl; return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Awkward Response AtCoder - 2656 ( 二分+交互题)的更多相关文章

  1. 【ECNU3542】神奇的魔术(二分交互题)

    点此看题面 大致题意: 有一个\(1\sim 2^n\)的排列,\(n\le7\),每次交互告诉你有几个位置上的数是正确的,让你在\(1000\)轮以内猜出每个位置上的数. 二分 显然,我们可以通过二 ...

  2. CF1114E Arithmetic Progression(交互题,二分,随机算法)

    既然是在CF上AC的第一道交互题,而且正是这场比赛让我升紫了,所以十分值得纪念. 题目链接:CF原网 题目大意:交互题. 有一个长度为 $n$ 的序列 $a$,保证它从小到大排序后是个等差数列.你不知 ...

  3. Codeforces Round #371 (Div. 2) D. Searching Rectangles 交互题 二分

    D. Searching Rectangles 题目连接: http://codeforces.com/contest/714/problem/D Description Filya just lea ...

  4. Subway Pursuit (二分)(交互题)

    题目来源:codeforces1039B Subway Pursuit 题目大意: 在1到n里有一个运动的点,要求找到这个点,每次可以查询一个区间内有没有这个点,每次这个点往左或者往右移动1到k个位置 ...

  5. Codeforces Round #499 (Div. 2) D. Rocket_交互题_二分

    第一次作交互题,有点不习惯. 由于序列是循环的,我们可以将一半的机会用于判断当前是否是在说谎,另一半的机会用于二分的判断. 对于判断是否实在说谎,用1判断即可.因为不可能有比1还小的数. 本题虽然非常 ...

  6. 交互题[CF1103B Game with modulo、CF1019B The hat、CF896B Ithea Plays With Chtholly]

    交互题就是程序与电脑代码的交互. 比如没有主函数的程序,而spj则给你一段主函,就变成了一个整体函数. 还有一种就是程序和spj之间有互动,这个用到fflush(stdout);这个函数就可以实现交互 ...

  7. CF1153E Serval and Snake(交互题)

    题目 CF1153E Serval and Snake 很有意思的一道交互题 做法 我们观察到,每次查询一行,当这一行仅包含一端是返回的答案是奇数 根据这个性质查询每一行每一列,我们大体能知道两端的位 ...

  8. Codeforces Round #551 (Div. 2) E. Serval and Snake (交互题)

    人生第一次交互题ac! 其实比较水 容易发现如果查询的矩阵里面包含一个端点,得到的值是奇数:否则是偶数. 所以只要花2*n次查询每一行和每一列,找出其中查询答案为奇数的行和列,就表示这一行有一个端点. ...

  9. D. Game with modulo 交互题(取余(膜)性质)附带a mod b<a/2证明

    D. Game with modulo 交互题(取余(膜)性质) 题意 猜一个点\(a\)可以向机器提问 点对\((x,y)\) 如果\(x\mod(a)>=y\mod(a)\)回答\(x\) ...

随机推荐

  1. shell初级-----处理用户输入

    命令行参数 读取参数 位置参数变量是标准的数字:$0是程序名,$1是第一个参数,$2,是第二个参数,直到第九个参数$9. 特殊的变量:$#表示参数个数,$?表示最后运行的命令的结束代码(返回值) 每个 ...

  2. 代码实现:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n

    import java.util.Scanner; //编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n public ...

  3. Xpath 和Css Selector使用

    Xpath是xml的路径语言,就是通过元素的路径来查找标签元素. Xpath直接在火狐浏览器的firebug中练习,49版本一下的火狐才能用firebug插件. Xpath的使用方法 注://*    ...

  4. 阶段3 2.Spring_07.银行转账案例_3 分析事务的问题并编写ConnectionUtils

    不是没有事务造成的 这样相当于有四个connection 每一个都有自己独立的事物 每一个自己成功就提交事务. 已经提交的就执行结束.没有提交的就报异常 让这些操作使用同一个connection 事物 ...

  5. 锋利的jQuery(第二版) 初读笔记

    window.onload(): 必须等待网页中所有的内容加载完毕后(包括图片)才能执行. $(document).ready(): 网页中所有DOM结构绘制完毕后就执行,可能DOM元素关联的东西并没 ...

  6. 关系/对象映射 多对多关系(@ManyToMany 注释)【重新认识】

    old: @ManyToMany 注释:表示此类是多对多关系的一边, mappedBy 属性定义了此类为双向关系的维护端, 注意:mappedBy 属性的值为此关系的另一端的属性名. 例如,在Stud ...

  7. mesh重叠闪烁问题

    我用正交摄像机做了2d游戏,但是导出spine动画文件是个mesh ,在游戏里有时会出现2个Mesh来回切换显示顺序问题,导致闪烁 查了下并没有发现什么解决方案 后面突然发现只要将摄像机的Y轴偏移一点 ...

  8. LeetCode.989-数组形式的整数做加法(Add to Array-Form of Integer)

    这是悦乐书的第371次更新,第399篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第233题(顺位题号是989).对于非负整数X,X的数组形式是从左到右顺序的数字数组.例 ...

  9. 9.Jmeter 多个threadgroup 中的配置元件会一次性进行初始化

    例如3个threadGroup,每一个threadGroup中都会定义了 一些配置原件,例如 用户定义变量,  jdbc 链接配置等.  当执行testplan(测试计划)时, 这些配置元件会一起初始 ...

  10. python 列表的(总结)

    列表(自我总结) 1.在python中什么是列表 列:排列,表:一排数据 在python中的表达就是 l = [1,2,3,4,5,6,7] 2.列表是可变类型还是不可变类型 也就是说列表能不能被ha ...