• 题意:有一个正整数\(n\),要求写出所有\(1\)~\(n\)的二进制数,统计相邻的两个二进制同位置上不同数的个数.

  • 题解:打表找规律,不难发现:

    ​ \(00000\)

    ​ \(00001\)

    ​ \(00010\)

    ​ \(00011\)

    ​ \(00100\)

    ​ \(00101\)

    ​ 当最低位时,每次都变换,由低位向高位,每\(2*i\)次变换一次,直接位运算暴力即可.

  • 代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <map>
    #include <set>
    #include <unordered_set>
    #include <unordered_map>
    #define ll long long
    #define fi first
    #define se second
    #define pb push_back
    #define me memset
    const int N = 1e6 + 10;
    const int mod = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    using namespace std;
    typedef pair<int,int> PII;
    typedef pair<ll,ll> PLL; int t;
    ll n; int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>t;
    while(t--){
    cin>>n;
    ll now=n;
    ll ans=0,p=1;
    while(now){
    ans+=n/p;
    p*=2;
    now>>=1;
    }
    printf("%lld\n",ans);
    } return 0;
    }

最新文章

  1. 理清C++常量指针和指针常量这团乱麻
  2. Linux查看可执行程序所在路径
  3. Oracle数据库入门——物化视图日志结构
  4. 面试java简答题
  5. dedecms网站如何做在线订单功能
  6. zoj 3672 Gao The Sequence
  7. Android Studio编译好的apk放在哪里?
  8. for()循环
  9. 服务器 Disk full
  10. linux kernel内存碎片防治技术
  11. Python类元编程初探
  12. Mac下显示网页全屏快捷键
  13. Hexo-Next6.7.0主题优化
  14. Codeforces 822C Hacker, pack your bags! - 贪心
  15. HTML5中的data-*属性和jQuery中的.data()方法使用
  16. MIT Molecular Biology 笔记6 转录的调控
  17. Error:Execution failed for task &#39;:app:transformClassesWithJarMergingForDebug
  18. Android 7.1 SystemUI--任务管理--场景一:长按某个缩略图,拖动分屏的流程
  19. Selenium报错整理
  20. node 中 npm报错 Error: ENOENT, stat &#39;C:\Users\Administrator\AppData\Roaming\npm&#39;

热门文章

  1. 【译】Async/Await(二)——Futures
  2. alter column和modify column
  3. [USACO13DEC]牛奶调度Milk Scheduling
  4. 【.NET 与树莓派】矩阵按键
  5. 全栈性能测试修炼宝典-JMeter实战笔记(二)
  6. Docker逃逸
  7. 2 安装部署flume
  8. IDE 阅读代码时候如何防止误触
  9. Vue技术点整理-指令
  10. Language Guide (proto3) | proto3 语言指南(十三)JSON映射