Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)

D. Game with modulo

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

题意:

这题是一个交互题,首先一开始会有一个数a,你最终的目的是要将它猜出来。

每次询问会输出"? x y",然后有:

  • "x" (without quotes), if (x % a)≥(y % a).
  • "y" (without quotes), if (x % a)<(y % a).

最多给你60次询问的机会,问最后这个a是多少。

题解:

这里会用到取余的性质,假设现在有一个数b,现在有b%a=c。假设a>b那么我们什么都不用管;假设a<=b,我们可以推出c是满足c<a/2的。

也就是说,当x,y都小于a时,必定会输出"y";否则,至少有y<a/2。

那么我们想到一开始令x=1,y=2进行倍增,因为当y=2*x时,若y>a,则有y%a<x%a恒成立,此时也x也是必定小于a的。

那么我们可以通过这个确立一个区间,然后在区间里面进行二分来询问a的值就好了。

注意一下a=1以及a=2时的特殊情况。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mx = 1000000000;
string s;
int query(ll x,ll y){
    printf("? %I64d %I64d\n",x,y);
    fflush(stdout);
    char c;
    getchar();
    scanf("%c",&c);
    if(c==‘x‘){
        return 0;
    }else return 1;
}
int main(){
    while(cin>>s){
        if(s=="end") break ;
        ll a=1,b=2;
        if(query(a,b)==0){
            if(query(2,1)==0) printf("! 1\n");
            else printf("! 2\n");
            continue ;
        }
        while(query(a,b)){
            a*=2;
            b*=2;
            if(b>=mx){
                b=mx;
                break ;
            }
        }
        ll l=a+1,r=b+1,mid;
        while(l<r){
            mid=l+r>>1;
            if(query(a,mid)) l=mid+1;
            else r=mid;
        }
        printf("! %I64d\n",r);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/heyuhhh/p/10352524.html

时间: 2024-07-31 02:20:41

Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)的相关文章

Codeforces Round #534 (Div. 2)题解

Codeforces Round #534 (Div. 2)题解 A. Splitting into digits 题目大意 将一个数字分成几部分,几部分求和既是原数,问如何分可以使得分出来的各个数之间的差值尽可能小 解题思路 将n分成n个1相加即可 AC代码 #include<cstring> #include<string> #include<iostream> #include<cstdio> using namespace std; int main

CF1103C Johnny Solving (Codeforces Round #534 (Div. 1)) 思维+构造

题目传送门 https://codeforces.com/contest/1103/problem/C 题解 这个题还算一个有难度的不错的题目吧. 题目给出了两种回答方式: 找出一条长度 \(\geq \frac nk\) 的路径: 找出 \(k\) 个简单环,满足长度不是 \(3\) 的倍数,并且每个环至少存在一个点不在别的环中. 很显然题目并不是要你随便挑一种回答方式开始单独研究.最有可能的情况是两种回答方式可以替补. 如果我们随便作出原图的一棵生成树,如果最长的路径长度 \(\geq \f

Codeforces Round #534 (Div. 2) Solution

A. Splitting into digits Solved. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n; 5 6 void solve() 7 { 8 printf("%d\n", n); 9 for (int i = 1; i <= n; ++i) printf("%d%c", 1, " \n"[i == n]); 10 } 11 12 int

Codeforces Round #534 (Div. 2)

A. Splitting into digits 题意:把一个数分成若干[1,9]之间的数字,使得这些数尽量相同. 思路:输出n个1. #include<bits/stdc++.h> #define CLR(a,b) memset(a,,sizeof(a)) using namespace std; typedef long long ll; const int maxn=100010; int main(){ int n; cin>>n; int flag=0,j=2; prin

[ACM]Codeforces Round #534 (Div. 2)

一.前言 二.题面 A. Splitting into digits Vasya has his favourite number n. He wants to split it to some non-zero digits. It means, that he wants to choose some digits d1,d2,-,dk, such that 1≤di≤9 for all i and d1+d2+-+dk=n. Vasya likes beauty in everything

Codeforces Round #534 (Div. 1)

A 构造题 有一个44的方格 每次放入一个横向12或竖向2*1的方格 满了一行或一列就会消掉 求方案 不放最后一行 这样竖行就不会消 然后竖着的放前两行 横着的放第三行 循环放就可以啦 #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> const int N = 1e3 + 5; using namespace

570D Codeforces Round #316 (Div. 2) D(dfs序,时间戳,二分

题目:一棵树上每个节点有个字符值,询问每个节点的深度为h的子节点的字符是否能组成一个回文串. 思路:首先是奇妙的dfs序和时间戳,通过记录每个节点的dfs进出时间,可以发现某个节点的子节点的进出时间均在该节点的进出时间范围内(这是很直观的dfs的性质),这样可以把树形结构转变为线性结构,方便进行各种处理.dfs一遍处理时间戳,每个节点的深度,并记录每个深度的节点都有哪些,此时每个深度的节点就是排好序的.然后对于一个询问,可以利用二分查找确定子节点在该层的哪一段.对于每一层,预先处理每个字符的前缀

Codeforces Round #220 (Div. 2)D. Inna and Sequence 树状数组+二分

题目链接:D. Inna and Sequence 题意:三种操作,1往序列后面加个1,0往序列后面加个0,-1,把给定位置上的数删除. 题解:用树状数组保存的数没被删的个数,每次二分找到位置,然后用数组标记这里被删除. #include<bits/stdc++.h> #include<set> #include<iostream> #include<string> #include<cstring> #include<algorithm&

[比赛] Codeforces Round #538 (Div. 2) solution (贪心,数学其他,二分,线段树)

已经写了100篇题解啦! link solution pdf #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; inline int read(){ int f=1,ans=0;char c; while(c<'0'||c>'9'){if(c=='-')f=-1;c