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的更多相关文章

  1. codeforces Good Bye 2015 B. New Year and Old Property

    题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...

  2. 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 ...

  3. 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 ...

  4. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)

    题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. good bye 2015 B - New Year and Old Property

    题意:统计在n,m之间的数的二进制表示形式只有一个零的数目. 位运算模拟+dfs #include<iostream> #include<string> #include< ...

  9. 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 ...

随机推荐

  1. WebAPI初探

    由于即将要接手的新项目计划用ASP.NET MVC3来开发,所以最近一段时间一直在看相关的书或文章.因为之前在大学里也曾学习过MVC2开发,也做过几个简单的MVC2的小型测试项目,不过在后来工作以后主 ...

  2. selenium python (五)打印信息及设置等待时间

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #一般情况下我们要验证打开的页面是否正确,可通过网页的Title和Cur ...

  3. javascript对象事件绑定方法

    javascript对象事件绑定方法 今天在做对象事件绑定的过程中出现了一点异外情况,由于事件方法是由参数传过来的,需要将当前对象call过去,方便方法体里直接调用this 错误写法 obj.oncl ...

  4. linux安装lua相关编译报错

    1.报之类的错误 /usr/lib/libreadline.so: undefined reference to `PC' /usr/lib/libreadline.so: undefined ref ...

  5. 【转】类中如何引用server.MapPath()

    转至:http://blog.csdn.net/tangjianft/article/details/5357151 今天在写一个上传图片方法时遇到了两个问题:1.public string getI ...

  6. 转载-SQL中的where条件,在数据库中提取与应用浅析

    1        问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当然,要完整描述一条SQL在数据库中的生命周期,这是一个非常巨大的问题,涵盖了SQL的词法解析.语 ...

  7. 第一百九十七-第二百天 how can I 坚持

    又是四天,how 快. 第一天,晚上要坐车回济南,没下班就躁动了.晚上高铁竟然是知道济南西,中间没有停,到济南九点半,去刘松家又吃了一顿.喝了不少酒.挺爽. 第二天,早上五点多就醒了,睡的婚床,哈哈, ...

  8. Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱(转)

    原文:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开 ...

  9. 解决SQLServer2008 Express远程连接出错的问题[Error: 1326错误]

    sqlserver2008 Express版本默认是只能本机测试连接,不能被其他客户端访问,原因是因为Express版本的数据库被连接默认的TCP/IP监听是被关闭的,我们可以做一些设置实现我们的远程 ...

  10. C#应用Newtonsoft.Json操作json[2]-反序列化不定类型

    在读json时,有时不知道对方的数据类型是什么样的,本文用Newtonsoft,把json反序列化为List>,在某种情况下还是有用的. private static List<Dicti ...