Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)
2024-09-08 04:33:11
题目链接: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();
}
最新文章
- Linux 下的dd命令使用详解(摘录)
- android wifi热点 socket通信
- Java数据结构——有序链表
- nopcommerce 初学2
- 清理Win8.1更新冗余的批处理代码
- ubuntu下的软件安装
- Bootstrap_Javascript_提示框
- python用parammiko模块实现linux的远程操作
- Silverlight Visifire控件 后台设置颜色
- 函数嵌套 lisp表达式求值
- tab面板,html+css
- SLAM+语音机器人DIY系列:(五)树莓派3开发环境搭建——1.安装系统ubuntu_mate_16.04
- Qt5和VS2017建立开发环境,安装后新建项目找不到Qt选项!!!
- 使用phpunit测试yaf项目操作步骤
- 使用doxc4j将word转pdf遇到的一个问题
- ORA-00444: background process DBRM failed while starting
- unity 背景无限循环滚动效果
- UI自动化(二)css选择器
- HDU-2612.Find way .(不同起点不同终点的BFS)
- 深入理解 Neutron -- OpenStack 网络实现(4):网络名字空间
热门文章
- oracle数据库psu升级(本实验是将10.2.0.3.12升级到10.2.0.3.15)
- MyBatis初级实战之一:Spring Boot集成
- 【MyBatis】MyBatis 延迟加载策略
- dd命令的详细介绍
- kubernets之存活探针
- 使用ogg实现oracle到postgresql表的实时同步
- Spring Validation 验证
- 2020年12月18号--21号 人工智能(深度学习DeepLearning)python、TensorFlow技术实战
- 网络流量预测入门(二)之LSTM介绍
- IDEA_2019.1版本中Protobuf的使用