Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)
题目链接: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();
}
Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)的更多相关文章
- Codeforces Round #647 (Div. 2) D. Johnny and Contribution(BFS)
题目链接:https://codeforces.com/contest/1362/problem/D 题意 有一个 $n$ 点 $m$ 边的图,每个结点有一个从 $1 \sim n$ 的指定数字,每个 ...
- Codeforces Round #647 (Div. 2) B. Johnny and His Hobbies(枚举)
题目链接:https://codeforces.com/contest/1362/problem/B 题意 有一个大小及元素值均不超过 $1024$ 的正整数集合,求最小正整数 $k$,使得集合中的每 ...
- Codeforces Round #647 (Div. 2) A. Johnny and Ancient Computer
题目链接:https://codeforces.com/contest/1362/problem/A 题意 有一个正整数 $a$,可选择的操作如下: $a \times 2$ $a \times 4$ ...
- Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! A、Johnny and Ancient Computer B、Johnny and His Hobbies C、Johnny and Another Rating Drop
题目链接:A.Johnny and Ancient Computer 题意: 给你两个数a,b.问你可不可以通过左移位运算或者右移位运算使得它们两个相等.可以的话输出操作次数,不可以输出-1 一次操作 ...
- Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! D. Johnny and Contribution (贪心,模拟)
题意:有\(n\)个点,\(m\)条边,现在要给这些点赋值,,每次只能赋给某一点的四周(所连边)的最小没出现过的值.如果不能按照所给的数赋值,输出\(-1\),否则输出赋值顺序. 题解:我们用\(pa ...
- Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! C. Johnny and Another Rating Drop (规律,二进制)
题意:有一个正整数\(n\),要求写出所有\(1\)~\(n\)的二进制数,统计相邻的两个二进制同位置上不同数的个数. 题解:打表找规律,不难发现: \(00000\) \(00001\) ...
- Codeforces Round #647 (Div. 2)
Problem A https://codeforces.com/contest/1362/problem/A 判断x/y是不是2的k次方, 如果是 k/3 + (k%3)/2 + (k%3%2)即为 ...
- Codeforces Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学
C. Vasya and Petya's Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)
主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...
随机推荐
- Unity优化图解
花了2天把之前学到的一些关于优化的知识全都写了下来,放到一张表里面 https://www.processon.com/mindmap/5cf64f53e4b0bc8329e8112e
- python基础学习总结
python管理cisco设备:http://www.linuxeye.com/program/1680.html 学习:https://www.liaoxuefeng.com/wiki/001431 ...
- mysql—information_schema数据库
一.介绍 MySQL中有一个默认数据库名为information_schema,在MySQL中我们把 information_schema 看作是一个数据库,确切说是信息数据库.其中保存着关于MySQ ...
- 在recover database时,如何决定该从哪一个SCN开始恢复
使用备份恢复的方法搭建DG库,还原数据文件后,打开数据库时报错 SQL> ALTER DATABASE OPEN READ ONLY; ALTER DATABASE OPEN READ ONLY ...
- merge join pg伪代码
Join { get initial outer and inner tuples INITIALIZE do forever { while (outer != inner) { SKIP_TEST ...
- druid discard long time none received connection问题解析
最新项目中用的druid连接数据库遇到一个困扰很久的问题 1 开始用的druid版本是1.1.22版本,由于业务需求,单个连接需要执行很久,理论上不需要用到自动回收,但为了安全,还是加了自动回收,时间 ...
- sap的内核升级,修补了源代码保护的方式
众所周知,在SAP的内核位701或者之前的版本中,我们可以通过向源代码的中加入"*@#@@[SAP]"这样的代码,来实现对源代码的保护.但是在内核升级到721和以后的版本中,你会发 ...
- Python入门之修改jupyter启动目录
[导读]在给大家分享知识的过程中,我们也会分享一些小技巧,能够帮助大家在学习过程中有更好的体验.之前我们给大家分享了anaconda安装教程以及jupyter notebook使用方法,今天我们为大家 ...
- 在nodejs中创建child process
目录 简介 child process 异步创建进程 同步创建进程 在nodejs中创建child process 简介 nodejs的main event loop是单线程的,nodejs本身也维护 ...
- 每月一更的《HelloGitHub》第 58 期,来啦!
HelloGitHub 分享 GitHub 上有趣.入门级的开源项目.欢迎大家: 贡献代码 宣传你觉得优秀的项目 Star 项目️ 本月刊是每月 28 号更新,再见月刊就是年后了.在这里提前祝大家:新 ...