• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
jacklee404
Never Stop!
博客园    首页    新随笔    联系   管理    订阅  订阅
CF#846 D-Bit Guessing Game 位运算

链接🔗

Bit Guessing Game

思路🤔🎈

对\((11000)_2\) 我们求最低位的\(1\), 可以经过以下步骤

  1. \((11000)_2 - 1 = (10111)_2\)
  2. \(count_1(11000_2) - count_1(10111) + 1\)得到最低位的1的位数, 其中\(coung_1\)为1的个数
  3. \((11000)_2 - (10111)_2 = (10000)_2\) , 设\(pos是得到的位数\), 则该操作减去\((1<<pos) - 1\)

​ 循环进行上述步骤,我们可以最终得到该数, 可以看到上述步骤每消去一个bit位都用了两次减法,显然对于\(30bit\)数要进行\(60\)次减法,不符合题目要求。

​ 我们可以把减法缩减为一步,每消去一个\(bit\)位我们让 \(cnt\)--, 减去一个bit位的操作可以缩减为\((1 << pos) - 1 + 1\), 即将下一步的减一操作在这一步减去。

Code 🗡

#include <bits/stdc++.h>

using i64 = long long;

int ask(int x) {
	int t;
	std::cout << "- " << x << std::endl;
	std::cin >> t;
	return t;
}

void solve() {
	int cnt;
	std::cin >> cnt;

	i64 last = 0, ans = 0, ot = 1;

	while(cnt > 0) {
		int curr_cnt = ask(ot);
		ans += 1ll<<(curr_cnt-cnt+1);			
		ot = 1ll<<(curr_cnt-cnt+1);
		cnt --;
	}

	std::cout << "! " << ans << std::endl;
}

int main() {
	int _;
	std::cin >> _;
	while(_ --) {
		solve();
	}
}
posted on 2023-01-26 12:36  Jack404  阅读(92)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3