题目:

Given an integer n, return the decimal value of the binary string formed by concatenating the binary representations of 1 to n in order, modulo 109 + 7.

Example 1:

Input: n = 1
Output: 1
Explanation: "1" in binary corresponds to the decimal value 1.

Example 2:

Input: n = 3
Output: 27
Explanation: In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.

Example 3:

Input: n = 12
Output: 505379714
Explanation: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 109 + 7, the result is 505379714.

Constraints:

  • 1 <= n <= 10^5

思路:

首先如果是真的按照思路走用字符串拼接的话肯定会超时,因为数据规模在10^5,只能或者更小。

根据观察得出规律:

n=1, f(1)=1

n=2, f(2)=[f(1) << 2] + 2 = 6

n=3, f(3)=[f(2) << 2] + 3 = 27

n=4, f(4)=[f(3) << 3] + 4 = 220

可以看出左移的位数和n的位数是相同的。

因此手动计算当前n的位数,然后进行左移和加n,中间不要忘记取模。另外因为数值过大,最初的记录答案的类型可以为long long。

代码:

class Solution {
public:
    int concatenatedBinary(int n) {
        int mode =1e9+7;
        long long ans=1;
        for(int i=2;i<=n;i++)
        {
            ans=ans<<count(i);
            ans%=mode;
            ans+=i;
            ans%=mode;
        }
        return ans;
    }
    
private:
    int count(int n)
    {
        int ans=0;
        while(n>0)
        {
            ans++;
            n=n>>1;
        }
        return ans;
    }
};

1680. Concatenation of Consecutive Binary Numbers相关推荐

  1. Sum of Consecutive Prime Numbers POJ - 2739(线性欧拉筛+尺取法)

    题意: 一些正整数可以由一个或多个连续质数的总和表示.给定一个的正整数n,问满足条件的有多少种情况? 题目: Some positive integers can be represented by ...

  2. HDU1390 ZOJ1383 Binary Numbers

    问题链接:HDU1390 ZOJ1383 Binary Numbers.入门练习题,用C语言编写程序. 对输入的整数的各个二进制位进行判断,如果为1则输出其所在的位(从右边开始分别是0,1,2,3,. ...

  3. Binary Numbers(HDU1390)

    Binary Numbers 点我 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. poj 2739 Sum of Consecutive Prime Numbers

    悲剧,如此水题,竟搞了许久.唉,只因题目没看清. 准确的说:应该是准备工作没做好就开始敲代码了!! 下次一定要想的很清楚了再code ing ! //144K 63MS #include <st ...

  5. spoj 26130 Binary numbers

    题意:给出二进制表示的数,位数不会超过200,根据操作码(大于,加 ,减,乘,除)计算结果 思路:先转成10进制数,计算结果后,再转化成二进制数 <?php$debug = false;$fil ...

  6. UVA 1210 Sum of Consecutive Prime Numbers

    https://vjudge.net/problem/UVA-1210 统计质数前缀和,枚举左右端点,这一段的区间和+1 #include<cstdio> #define N 10001 ...

  7. Sum of Consecutive Prime Numbers

    我们都知道数字是个好玩意,那么我们想知道一个数字能是否能用若干个(或许是一个)连续的素数之和表示,并且想知道有多少种方法.例如,53 有两种表示方法 5 + 7 + 11 + 13 + 17 和 53 ...

  8. 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)

    链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...

  9. UVALive 3958 Weird Numbers (负进制数)

    Weird Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/F Description Binary number ...

  10. Product of consecutive Fib numbers-连续Fib数的乘积

    我的个人博客 更多内容,请跳转我的个人博客 题目 Product of consecutive Fib numbers 连续Fib数的乘积 描述 The Fibonacci numbers are t ...

最新文章

  1. 开启机器学习的第一课:用Pandas进行数据分析
  2. Sass Nesting for:悬停不起作用[重复]
  3. SHD0新建屏幕变式
  4. linux(ubuntu)下分区和格式化sd卡
  5. storm的并行度的解释--- ( 看完就能理解 )
  6. 字体样式 font-family 0911
  7. 7种方式,教你提升 SpringBoot 项目的吞吐量
  8. [转]SQL Server 存储过程 一些常用用法(事物、异常捕捉、循环)
  9. 品鉴贝叶斯公式里的大道理
  10. zzbird的bbsmax出了?
  11. Cesium + Vue 点击获取经纬度 气泡窗(三)
  12. 《高效能人士的七个习惯》:运用才是关键
  13. Apache NiFi简介
  14. 中点圆c语言程序,[图形学] 画圆(基于中点算法)
  15. 计算机word资料,怎样快速找到电脑中的Word文档
  16. Linux系统常见目录分类
  17. 嵌入式linux实验截图,嵌入式linux实验二.pdf
  18. linux内核丢弃udp报文,内核udp报文截取、修改和发送
  19. 霹雳火s毕老师_$_Java笔记(一)
  20. 分区表的分区统计信息

热门文章

  1. 程序员也敢吃10元的盒饭
  2. 信息安全作业7-社会网络分析练习
  3. Kubernetes从Private Registry中拉取容器镜像的方法(两种方法总结的很好)
  4. Android超酷的加载效果
  5. 欧债收益率大幅攀升,欧央行政策放宽力度令人失望
  6. 2013年4月25日 19:49:52
  7. NSNotification通知的使用
  8. python 计算循环次数查询_跟小白学Python数据分析——For循环
  9. Serv-U到期后解决办法
  10. 干货 | 一文读懂4种整流电路、5种滤波电路