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 ...
随机推荐
- 转载:50个C/C++源代码网站
来源:http://www.cnblogs.com/feisky/archive/2010/03/05/1679160.html C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些 ...
- C语言实现memcpy和memmove
0.两者比较: memmove用于从src拷贝count个字符到dest,如果目标区域和源区域有重叠的话,memmove能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中.但复制后src内容会被 ...
- MFC中TRACE
错误 1 error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shar ...
- C++中string 的使用
string类的构造函数:string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化此外,string类还支持 ...
- RHadoop教程翻译系列 _Mapreduce(1)_第一个Mapreduce任务
如果单从概念上来说,Mapreduce和R中的函数lapply, tapply并无差别,它们都是把元素转化成列,然后计算索引(Mapreduce中的键),最后合并成一个定义好的组合.首先,让我们看一个 ...
- (转载)OC学习篇之---代理模式
在前一篇文章我们介绍了OC中的协议的概念,这篇文章我们就来介绍一下OC中的代理模式,关于代理模式,如果还有同学不太清楚的话,就自己去补充知识了,这里就不做介绍了,这里只介绍OC中是如何实现代理模式的. ...
- arm tiny6410双网卡桥接问题
这几天做实验,想用arm tiny6410板子做个mesh网络节点.该板子本身自带一个lan网卡,我自己配了一个tp-link的usb无线网卡.其中wlan网卡工作在adhoc模式下,作为mesh骨干 ...
- 第三百四十六天 how can I 坚持
徐斌的电脑来了,thinkpad,感觉还好,电脑也就这样,联想..不好说,不做评论,末日王者吧. 为什么写博客tab键不管用了呢. 下午又去奥体跑了一圈,好累,刚跑完腿疼,现在还好. 还没洗澡呢,都这 ...
- 转】Spark DataFrames入门指南:创建和操作DataFrame
原博文出自于: http://blog.csdn.net/lw_ghy/article/details/51480358 感谢! 一.从csv文件创建DataFrame 本文将介绍如何从csv文件创建 ...
- 【转】Hive导入10G数据的测试
原博文出自于: http://blog.fens.me/hadoop-hive-10g/ 感谢! Hive导入10G数据的测试 让Hadoop跑在云端系列文章,介绍了如何整合虚拟化和Hadoop,让H ...