题解:CF1780D Bit Guessing Game

· · 个人记录

题目大意

# 思路 定义变量 $ans$ , $op$ , $x$ , $nowcnt$ , $lastcnt$ 。 | 变量名 | 含义 | |:-:|:-:| | $ans$ | 答案 | | $op$ | 最后一位 $1$ 的位置 | | $x$ | $2^{op}-1+1$ | | $nowcnt$ | 现在 $1$ 的个数 | | $lastcnt$ | 减去 $x$ 前 $1$ 的个数 | 每进行一轮, $ans+=x$ , $op=nowcnt+1-lastcnt$ ( 因为二进制从 $0$ 开算,所以 $op$ 应该等于变化量) , $x=2^{op}-1+1$ , $nowcnt$ 用 $-x$ 查,再减去 $op$ (减后增长的 $1$ 的数量), $lastcnt$ 继承上一轮 $nowcnt$ 。 当 $nowcnt=0$ 时, $n$ 原本的 $1$ 已经全部删过一遍,于是 $n$ 变成了此时的 $op$ 个 $1$ ,则 $ans+=(x-1)$ 。 # 代码 ```cpp #include<bits/stdc++.h> using namespace std; int t,nowcnt,op,ans,x,lastcnt; int main() { scanf("%d",&t); while(t--) { scanf("%d",&nowcnt); op=0; ans=0; x=1; while(nowcnt) { printf("- %d\n",x); fflush(stdout); ans+=x; lastcnt=nowcnt; scanf("%d",&nowcnt); op=nowcnt+1-lastcnt; nowcnt-=op; int s=1; for(int i=1;i<=op;i++) { s*=2; } x=s; if(nowcnt==0) { ans+=(x-1); } } printf("! %d\n",ans); fflush(stdout); } return 0; } ``` # 注意 这是一道交互题,别忘了 $fflush(stdout)$ 。 这题是多测,记得输入 T 。