题目链接:https://codeforces.com/contest/1362/problem/C

题意

计算从 $0$ 到 $n$ 相邻的数二进制下共有多少位不同,考虑二进制下的前导 $0$ 。($1≤n≤10^{18}$)

题解

逐位考虑。因为值是连续的,所以从右至左第 $i$ 位的 $01$ 周期为 $2^i$ 。计算出完整的 $01$ 周期个数 $t$,每个完整的周期内有一对 $01$ 相邻,相邻的完整周期间共有 $t - 1$ 对 $01$ 相邻,然后考虑多余的周期长度,如果之前有完整的 $01$ 周期且有多余的周期,答案 $+1$,如果多余周期长度大于完整周期的一半,答案 $+1$ 。

Tips

移位较多应使用 1LL 。

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std; void solve() {
ll n; cin >> n;
++n;
ll ans = 0;
for (int i = 1; i < 64; i++) {
ll len = (1LL << i);
ll t = n / len;
ans += t + max(0LL, t - 1) + (t and n % len > 0) + (n % len > len / 2);
if (len >= n) break;
}
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

最新文章

  1. Linux 下的dd命令使用详解(摘录)
  2. android wifi热点 socket通信
  3. Java数据结构——有序链表
  4. nopcommerce 初学2
  5. 清理Win8.1更新冗余的批处理代码
  6. ubuntu下的软件安装
  7. Bootstrap_Javascript_提示框
  8. python用parammiko模块实现linux的远程操作
  9. Silverlight Visifire控件 后台设置颜色
  10. 函数嵌套 lisp表达式求值
  11. tab面板,html+css
  12. SLAM+语音机器人DIY系列:(五)树莓派3开发环境搭建——1.安装系统ubuntu_mate_16.04
  13. Qt5和VS2017建立开发环境,安装后新建项目找不到Qt选项!!!
  14. 使用phpunit测试yaf项目操作步骤
  15. 使用doxc4j将word转pdf遇到的一个问题
  16. ORA-00444: background process DBRM failed while starting
  17. unity 背景无限循环滚动效果
  18. UI自动化(二)css选择器
  19. HDU-2612.Find way .(不同起点不同终点的BFS)
  20. 深入理解 Neutron -- OpenStack 网络实现(4):网络名字空间

热门文章

  1. oracle数据库psu升级(本实验是将10.2.0.3.12升级到10.2.0.3.15)
  2. MyBatis初级实战之一:Spring Boot集成
  3. 【MyBatis】MyBatis 延迟加载策略
  4. dd命令的详细介绍
  5. kubernets之存活探针
  6. 使用ogg实现oracle到postgresql表的实时同步
  7. Spring Validation 验证
  8. 2020年12月18号--21号 人工智能(深度学习DeepLearning)python、TensorFlow技术实战
  9. 网络流量预测入门(二)之LSTM介绍
  10. IDEA_2019.1版本中Protobuf的使用