1680. Concatenation of Consecutive Binary Numbers
题目:
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相关推荐
- Sum of Consecutive Prime Numbers POJ - 2739(线性欧拉筛+尺取法)
题意: 一些正整数可以由一个或多个连续质数的总和表示.给定一个的正整数n,问满足条件的有多少种情况? 题目: Some positive integers can be represented by ...
- HDU1390 ZOJ1383 Binary Numbers
问题链接:HDU1390 ZOJ1383 Binary Numbers.入门练习题,用C语言编写程序. 对输入的整数的各个二进制位进行判断,如果为1则输出其所在的位(从右边开始分别是0,1,2,3,. ...
- Binary Numbers(HDU1390)
Binary Numbers 点我 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- poj 2739 Sum of Consecutive Prime Numbers
悲剧,如此水题,竟搞了许久.唉,只因题目没看清. 准确的说:应该是准备工作没做好就开始敲代码了!! 下次一定要想的很清楚了再code ing ! //144K 63MS #include <st ...
- spoj 26130 Binary numbers
题意:给出二进制表示的数,位数不会超过200,根据操作码(大于,加 ,减,乘,除)计算结果 思路:先转成10进制数,计算结果后,再转化成二进制数 <?php$debug = false;$fil ...
- UVA 1210 Sum of Consecutive Prime Numbers
https://vjudge.net/problem/UVA-1210 统计质数前缀和,枚举左右端点,这一段的区间和+1 #include<cstdio> #define N 10001 ...
- Sum of Consecutive Prime Numbers
我们都知道数字是个好玩意,那么我们想知道一个数字能是否能用若干个(或许是一个)连续的素数之和表示,并且想知道有多少种方法.例如,53 有两种表示方法 5 + 7 + 11 + 13 + 17 和 53 ...
- 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)
链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...
- UVALive 3958 Weird Numbers (负进制数)
Weird Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/F Description Binary number ...
- Product of consecutive Fib numbers-连续Fib数的乘积
我的个人博客 更多内容,请跳转我的个人博客 题目 Product of consecutive Fib numbers 连续Fib数的乘积 描述 The Fibonacci numbers are t ...
最新文章
- 开启机器学习的第一课:用Pandas进行数据分析
- Sass Nesting for:悬停不起作用[重复]
- SHD0新建屏幕变式
- linux(ubuntu)下分区和格式化sd卡
- storm的并行度的解释--- ( 看完就能理解 )
- 字体样式 font-family 0911
- 7种方式,教你提升 SpringBoot 项目的吞吐量
- [转]SQL Server 存储过程 一些常用用法(事物、异常捕捉、循环)
- 品鉴贝叶斯公式里的大道理
- zzbird的bbsmax出了?
- Cesium + Vue 点击获取经纬度 气泡窗(三)
- 《高效能人士的七个习惯》:运用才是关键
- Apache NiFi简介
- 中点圆c语言程序,[图形学] 画圆(基于中点算法)
- 计算机word资料,怎样快速找到电脑中的Word文档
- Linux系统常见目录分类
- 嵌入式linux实验截图,嵌入式linux实验二.pdf
- linux内核丢弃udp报文,内核udp报文截取、修改和发送
- 霹雳火s毕老师_$_Java笔记(一)
- 分区表的分区统计信息