Codeforces 1103B Game with modulo

传送门.

翻译:
交互
有一固定a,每次你可以询问,给出参数x,y,返回[x%a>=y%a]

求a,询问次数<=60次。

题解:

B题这么难可以退役了。

60次那肯定是什么二分乱搞。

假设现在确定a∈[x…y]
设mid=(x+y)/2

我们模拟一下会发现做不了。

其难点在于2*x不一定大于mid

于是不如倍增(那些10分钟切的人怎么想得这么快?),先查询 [ 2 i , 2 i + 1 ] [2^i,2^{i+1}] [2i,2i+1],可以得到在不在这个区间,然后再二分。
这样就一定会保持2*x>=mid,然后就行了。

注意a=1要特判。

Code:

#include
#include
#define pp printf
#define fl fflush(stdout)
#define fo(i ,x, y) for(int i = x; i <= y; i ++)
using namespace std;

char s[1005]; int ls;

int pd(int x, int y) {
	pp("? %d %d\n", x, y);
	fl;
	scanf("%s", s);
	return s[0] == 'x';
}

int main() {
	while(scanf("%s", s) != EOF) {
		if(s[0] == 'e')  return 0;
		int x, y;
		for(x = 1, y = 2; !pd(x, y); x *= 2, y *= 2);
		while(x + 1 < y) {
			int m = x + y >> 1;
			if(pd(x - 1, m)) y = m; else x = m + 1;
		}
		if(x == 1 && y == 2) {
			if(pd(1, 2) && pd(2, 3)) {
				pp("! 1\n");
				fl;
				continue;
			}
		}
		if(x + 1 == y) {
			pp("! %d\n", pd(x, y) ? y : x);
			fl;
		}
		if(x == y) {
			pp("! %d\n", x);
			fl;
		}
	}
}

你可能感兴趣的:(构造题,CF)