Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP
B. New Year and Old Property
题目连接:
http://www.codeforces.com/contest/611/problem/B
Description
The year 2015 is almost over.
Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system — 201510 = 111110111112. Note that he doesn't care about the number of zeros in the decimal representation.
Limak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?
Assume that all positive integers are always written without leading zeros.
Input
The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 1018) — the first year and the last year in Limak's interval respectively.
Output
Print one integer – the number of years Limak will count in his chosen interval.
Sample Input
5 10
Sample Output
2
Hint
题意:
给你l,r区间,问你[l,r]中,二进制中只含有1个0的数有多少个
题解
直接dfs跑就好了,dfs(int x,int flag)表示现在是x,这个数里面是否只含有1个0。当然,也可以数位Dp(误。
dfs代码
#include<bits/stdc++.h>
using namespace std;
long long ans = 0;
long long l,r;
void dfs(long long x,int flag)
{
if(x>r)return;
if(x>=l&&x<=r&&flag==1)
ans++;
if(flag==0)dfs(x*2,1);
dfs(x*2+1,flag);
}
int main()
{
cin>>l>>r;
dfs(1,0);
cout<<ans<<endl;
}
数位DP 代码
#include<bits/stdc++.h>
using namespace std;
long long pre[100];
long long dp[100][5][5];
int vis[100][5][5];
long long dfs(long long x,int flag1,int flag2)
{
if(x==-1)
{
if(flag2==2)
return 1;
else
return 0;
}
if(vis[x][flag1][flag2])
return dp[x][flag1][flag2];
vis[x][flag1][flag2]=1;
long long ans = 0;
int T = 0;
if(flag1==1)T = 1;
else T = pre[x];
for(int i=0;i<=T;i++)
{
int k = flag1 | (i<T);
if(flag2 == 1)
{
if(i==1)
ans = ans + dfs(x-1,k,1);
else
ans = ans + dfs(x-1,k,2);
}
else if(flag2==0)
{
if(i==1)
ans = ans + dfs(x-1,k,1);
else
ans = ans + dfs(x-1,k,0);
}
else
{
if(i!=0)
ans = ans + dfs(x-1,k,2);
}
}
dp[x][flag1][flag2]=ans;
return ans;
}
long long solve(long long x)
{
memset(vis,0,sizeof(vis));
for(long long i=62;i>=0;i--)
{
if((x>>i)&1LL)pre[i]=1;
else pre[i]=0;
}
return dfs(60,0,0);
}
int main()
{
long long a,b;
cin>>a>>b;
cout<<solve(b)-solve(a-1)<<endl;
}
Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP的更多相关文章
- codeforces Good Bye 2015 B. New Year and Old Property
题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...
- Good Bye 2015 B. New Year and Old Property —— dfs 数学
题目链接:http://codeforces.com/problemset/problem/611/B B. New Year and Old Property time limit per test ...
- Good Bye 2015 B. New Year and Old Property 计数问题
B. New Year and Old Property The year 2015 is almost over. Limak is a little polar bear. He has re ...
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)
题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...
- Codeforces Good Bye 2015 A. New Year and Days 水题
A. New Year and Days 题目连接: http://www.codeforces.com/contest/611/problem/A Description Today is Wedn ...
- Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp
D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...
- Codeforces Good Bye 2015 C. New Year and Domino 前缀和
C. New Year and Domino 题目连接: http://www.codeforces.com/contest/611/problem/C Description They say &q ...
- good bye 2015 B - New Year and Old Property
题意:统计在n,m之间的数的二进制表示形式只有一个零的数目. 位运算模拟+dfs #include<iostream> #include<string> #include< ...
- Codeforces Round #358 (Div. 2) A B C 水 水 dfs序+dp
A. Alyona and Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- maven(一)初步搭建,项目结构
一.环境准备 java环境, jdk 1.5 以上 MyEclipse集成工具,我装的是8.5 版本 二.快速安装及配置 1.下载maven: http://maven.apache.org/docs ...
- .NET下用C#实现邮箱激活功能
最近要用到安全邮箱激活的功能,故写篇博客记录下. 思路:在表中增加一个字段State来记录邮箱是否激活(0激活,1未激活.) 1.发送邮件. 1-1,给邮箱发送邮件.内容:激活地址+GUID. ...
- os和os.path模块
Os和os.path模块函数 1. Os模块 函数 描述 文件处理 Mkfifo()/mknod() 创建命名管道/创建文件系统节点 Remove()/unlink() 删除文件 Renam ...
- html页面加载和解析流程
HTML页面加载和解析流程 用户输入网址(假设是个html页面,并且是第一次访问),浏览器向服务器发出请求,服务器返回html文件: 浏览器开始载入html代码,发现<head>标签内有一 ...
- echart图表控件配置入门(二)常用图表数据动态绑定
上一节 <echart图表控件配置入门(一)>介绍了echarts图表控件的入门配置,使开发人员可以快速搭建出一个静态的图表.但是在实际开发过程这还是不够的,不可能所有的图表控件都是静态数 ...
- mysql执行update报错1175解决方法
mysql执行update报错 update library set status=true where 1=1 Error Code: 1175. You are using safe update ...
- 项目常用jquery/easyui函数小结
#项目常用jquery/easyui函数小结 ##背景 项目中经常需要使用到一些功能,封装.重构.整理后形成代码沉淀,在此进行分享 ##代码 ```javascript /** * @author g ...
- 桶排序-OC
NSArray * b = @[@,@,@,@,@]; NSMutableArray *a = @[].mutableCopy; ; i<; i++) { a[i] = @; } for (NS ...
- 手势冲突UIPanGestureRecognizer 和UIPinchGestureRecognizer
当同时使用pan和pin手势时假如冲突,需要加入下面方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shou ...
- Struct2提交表单数据到Acion
Struct2提交表单数据到Action,Action取表单的数据,传递变量.对象 HTML.jsp <form action="reg.do" method="p ...