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 ...
随机推荐
- db file sequential read (数据文件顺序读取)
转载:http://www.dbtan.com/2010/04/db-file-sequential-read.html db file sequential read (数据文件顺序读取): db ...
- EFSQLserver
1.增加一条烽据 FLYNEWQKEntities dataContext = new FLYNEWQKEntities(); Log log = new Log(); log.Data1 = &qu ...
- Nodejs_day04
Nodejs模块系统 1.如何创建一个模块 创建一个js(hello.js) exports.world = function(){//为什么可以这么写,因为exports是nodejs公开的借口 c ...
- Java中的路径问题
Java中的路径问题 代码说明,如下: package com.merlin.test; import java.io.InputStream; public class Test { public ...
- C++ 串
♣ string 的基类basic_string中没有虚函数,它无意成为基类.更像是为了处理字符相关的问题而专门提供的一个工具及操作方法.如:想要在一个字符串str1中查找str2,没必要每次都去写K ...
- jqueryMobile
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- mysql执行update报错1175解决方法
mysql执行update报错 update library set status=true where 1=1 Error Code: 1175. You are using safe update ...
- 《LINUX程序设计 第四版》 阅读笔记:(一)
1. 头文件 使用-I标志来包含头文件. gcc -I/usr/openwin/include fred.c 2. 库文件 通过给出 完整的库文件路径名 或 用-l标志 来告诉编译器要搜索的库文件. ...
- Flex布局如何让子类在超出边界时隐藏掉
在flex4中,因为必须添加<s:Scroller>标签才能出现滚动条,如果一个容器例如Panel没有添加滚动条,那么添加到Panel中的child的位置如果超出了Panel的边界,那么这 ...
- Linux 修改hostname 文件
linux 的机器修改hostname: 修改 /etc/hosts 修改 /etc/sysconfig/network 重启机器reboot