Light OJ 1032 - Fast Bit Calculations(数学)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1032
题目大意:一个十进制数变化为二进制,那么对于这个数,如果连着两个二进制位都为1,那么称为一个相邻的点, 一个数可能有多个相邻的点。现在给你一个数n, 问从1到n中有多少相邻的点。
解题思路:由于n的范围,所以不能用循环做。对于输入的数n可以通过n,直接求得答案。
假设n的二进制数为111100101111000, 对于中间的两个数111100101111000, 对于中间那两个位置是11有多少种可能? 那不就是前面数字(1111001)2乘以后面全部的排列数!根据这个思路
代码如下:
#include<bits/stdc++.h>
using namespace std; void solve(int cases)
{
long long n;
scanf("%lld",&n);
long long ans = ;
long long res = , a = ;
while((n >> ) != )
{
if((n & ) && ((n >> ) & ))
ans += (n >> )*a + res;
else
{
if((n >> ) != )
ans += (n >> )*a;
}
if(n & )
res += a;
a = a*;
n >>= ;
}
printf("Case %d: %lld\n", cases, ans);
} int main()
{
int t;
scanf("%d", &t);
for(int i=; i<=t; ++ i)
solve(i);
return ;
}
Light OJ 1032 - Fast Bit Calculations(数学)的更多相关文章
- Light OJ 1032 - Fast Bit Calculations(数位DP)
题目大意: 一个数字把他看成二进制数字,数字里又会一些相邻的1,问从0到n至间所有相邻1的总和是多少? 分解成2进制数字,然后数位DP就行了. ======================== ...
- light oj 1032(数位DP)
求一段区间中,每个十进制数所对应的二进制数中连续的1的个数之和. 设dp[i][0]代表长度为i的二进制数,首位为0,所含有的连续的1的个数之和. dp[i][1]代表长度为i的二进制数,首位为1,所 ...
- LightOJ 1032 - Fast Bit Calculations 数位DP
http://www.lightoj.com/volume_showproblem.php?problem=1032 题意:问1~N二进制下连续两个1的个数 思路:数位DP,dp[i][j][k]代表 ...
- Light OJ 1032
数位dp,许多数位dp需要统计某种模式(子串)出现的数量,这种题通常需要在递归参数中加入高位已经出现过的模式的数量. #include <cstdio> #include <cstr ...
- light OJ 1282 - Leading and Trailing 数学 || double技巧
http://lightoj.com/volume_showproblem.php?problem=1282 #include <cstdio> #include <cstdlib& ...
- lightoj 1032 - Fast Bit Calculations(数位dp)
A bit is a binary digit, taking a logical value of either 1 or 0 (also referred to as "true&quo ...
- Light OJ Dynamic Programming
免费做一样新 1004 - Monkey Banana Problem 号码塔 1005 - Rooks 排列 1013 - Love Calculator LCS变形 dp[i][j][k]对于第一 ...
- Fast Bit Calculations LightOJ - 1032
Fast Bit Calculations LightOJ - 1032 题意:求0到n的所有数的二进制表示中,"11"的总数量.(如果有连续的n(n>2)个1,记(n-1) ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
随机推荐
- EF4.1DbContext使用现成的数据库
在配置文件中使用 <configuration> <connectionStrings> <add name="BlogDB" providerNam ...
- java温故系列之环境配置
-----下面的配置符号全部复制,否则会出错 JDK配置: 首先去度娘下载jdk,然后安装.这个就不贴地址了,可能会找不到 1.右键我的电脑->属性->高级系统设置->环境变量 2. ...
- echarts学习网站
echarts : http://echarts.baidu.com/echarts2/doc/example.html 相关脚本学习网站:http://www.jb51.net/html/list/ ...
- webstorm编辑RN代码提示功能
需要下载一个文件. 1. 进入你想存储这个文件的目录,按住shift+鼠标右键,选择“在此处打开命令窗口” 2. 在命令窗口中输入 git clone https://github.com/virt ...
- Hibernate配置文档详解
Hibernate配置文档有框架总部署文档hibernate.cfg.xml 和映射类的配置文档 ***.hbm.xml hibernate.cfg.xml(文件位置直接放在src源文件夹即可) (在 ...
- jms的俩种模式
package com.jiangchong.job; import java.util.Date; import javax.jms.Connection; import javax.jms.Con ...
- According to TLD or attribute directive in tag file, attribute test does not accept any expressions
HTTP Status 500 - /WEB-INF/views/emp/list.jsp (line: 30, column: 4) According to TLD or attribute di ...
- Linux 命令 创建文件
1.vi vi 1.txt 会直接创建并打开一个文件1.txt 2.touch touch的作用是更改一个文件或目录的时间.touch 2.txt 如果2.txt不存在,则创建空文件2.txt 3.e ...
- golang在linux下的开发环境部署[未完]
uname -a Linux symons_laptop 4.8.2-1-ARCH #1 SMP PREEMPT Mon Oct 17 08:11:46 CEST 2016 x86_64 GNU/Li ...
- 《SSM框架搭建》一.构建maven web项目
一.创建maven工程File-New-other-Maven Project 二.设置项目工作空间,取消默认勾选,手动设置 三.选择internal,输入web,选择maven.archetypes ...