Problem Description

For a number,if the length of continuous odd digits is even and the length of continuous even digits is odd,we call it odd-even number.Now we want to know the amount of odd-even number between L,R(1<=L<=R<= 9*10^18). 
 
Input
First line a t,then t cases.every line contains two integers L and R. 
 
Output
Print the output for each case on one line in the format as shown below.

题意:一个数中所有连续的奇数长度都为偶数,所有连续偶数的长度都为奇数。我们要找一个区间内一共有多少个这样的数。

区间范围(1~9*10^18)所以显然是数位dp搞。设dp[len][count][temp],temp=1表示偶数,temp=2表示奇数,temp=0表示取首位0时,

count表示到len位置有几个奇数或偶数个。显然简单的搜索,标准数位dp

#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
ll dp[22][22][3];
int dig[20];
ll dfs(int len , int count , int temp , int flag , int first) {
if(len == 0) {
if(temp == 1) {
if(count % 2 != 0) {
return 1;
}
else {
return 0;
}
}
if(temp == 2) {
if(count % 2 == 0) {
return 1;
}
else {
return 0;
}
}
return 0;
}
if(!flag && !first && dp[len][count][temp] != -1) {
return dp[len][count][temp];
}
int n = flag ? dig[len] : 9;
ll sum = 0;
for(int i = 0 ; i <= n ; i++) {
if(first) {
if(i == 0) {
sum += dfs(len - 1 , 0 , 0 , flag && i == n , first && i == 0);
}
else {
if(i % 2 == 0) {
sum += dfs(len - 1 , count + 1 , 1 , flag && i == n , first && i == 0);
}
else {
sum += dfs(len - 1 , count + 1 , 2 , flag && i == n , first && i == 0);
}
}
}
else {
if(i % 2 == 0) {
if(temp == 1) {
sum += dfs(len - 1 , count + 1 , 1 , flag && i == n , first && i == 0);
}
if(temp == 2) {
if(count % 2 == 0) {
sum += dfs(len - 1 , 1 , 1 , flag && i == n , first && i == 0);
}
}
}
else {
if(temp == 1) {
if(count % 2 != 0) {
sum += dfs(len - 1 , 1 , 2 , flag && i == n , first && i == 0);
}
}
if(temp == 2) {
sum += dfs(len - 1 , count + 1 , 2 , flag && i == n , first && i == 0);
}
}
}
}
if(!flag && !first) {
dp[len][count][temp] = sum;
}
return sum;
}
ll Get(ll x) {
int len = 0;
memset(dp , -1 , sizeof(dp));
memset(dig , 0 , sizeof(dig));
if(x == 0)
len = 1;
while(x) {
dig[++len] = x % 10;
x /= 10;
}
return dfs(len , 0 , 0 , 1 , 1);
}
int main()
{
int t;
cin >> t;
int ans = 0;
while(t--) {
ans++;
ll l , r , gg;
cin >> l >> r;
cout << "Case #" << ans << ": " << Get(r) - Get(l - 1) << endl;
}
return 0;
}

hdu 5898 odd-even number(数位dp)的更多相关文章

  1. 多校5 HDU5787 K-wolf Number 数位DP

    // 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...

  2. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  3. HDU 5898 odd-even number (数位DP) -2016 ICPC沈阳赛区网络赛

    题目链接 题意:一个数字,它每个数位上的奇数都形成偶数长度的段,偶数位都形成奇数长度的段他就是好的.问[L , R]的好数个数. 题解:裸的数位dp, 从高到低考虑每个数位, 状态里存下到当前位为止的 ...

  4. HDU 5787 K-wolf Number 数位DP

    K-wolf Number Problem Description   Alice thinks an integer x is a K-wolf number, if every K adjacen ...

  5. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  6. HDU 5179 beautiful number 数位dp

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

  7. 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】

    Valley Numer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. hdu 4352 XHXJ's LIS 数位dp+状态压缩

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others ...

  9. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

随机推荐

  1. Flink状态专题:keyed state和Operator state

            众所周知,flink是有状态的计算.所以学习flink不可不知状态.         正好最近公司有个需求,要用到flink的状态计算,需求是这样的,收集数据库新增的数据.       ...

  2. 刷脸即可解锁让iDevice取证不再难如登天

    最近有则取证相关的消息,链接如下,光看标题便知道与Apple的Face ID有关. https://www.cnet.com/news/fbi-unlocked-an-iphone-x-by-forc ...

  3. 深入理解JVM-类加载器深入解析(3)

    深入理解JVM-类加载器深入解析(3) 获得ClassLoader的途径 获得当前类的ClassLoader clazz.getClassLoader() 获得当前线程上下文的ClassLoader ...

  4. Spring条件注解@Conditional

    @Conditional是Spring4新提供的注解,它的作用是根据某个条件创建特定的Bean,通过实现Condition接口,并重写matches接口来构造判断条件.总的来说,就是根据特定条件来控制 ...

  5. JavaScript数据结构——栈的实现与应用

    在计算机编程中,栈是一种很常见的数据结构,它遵从后进先出(LIFO——Last In First Out)原则,新添加或待删除的元素保存在栈的同一端,称作栈顶,另一端称作栈底.在栈中,新元素总是靠近栈 ...

  6. SpringMVC学习笔记之---深入使用

    SpringMVC深入使用 (一)基于XML配置的使用 (1)配置 1.SpringMVC基础配置 2.XML配置Controller,HandlerMapping组件映射 3.XML配置ViewRe ...

  7. 初试kafka消息队列中间件一 (只适合初学者哈)

    初试kafka消息队列中间件一 今天闲来有点无聊,然后就看了一下关于消息中间件的资料, 简单一点的理解哈,网上都说的太高大上档次了,字面意思都想半天: 也就是用作消息通知,比如你想告诉某某你喜欢他,或 ...

  8. 消息中间件-activemq实战整合Spring之Topic模式(五)

    这一节我们看一下Topic模式下的消息发布是如何处理的. applicationContext-ActiveMQ.xml配置: <?xml version="1.0" enc ...

  9. javascript中的浅拷贝和深拷贝(拷贝引用和拷贝实例)

    作者:千锋教育链接:https://www.zhihu.com/question/23031215/answer/326129003来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  10. JavaWeb——使用会话维持状态

    1.会话的作用 使用会话是为了维持状态,维持的是请求域请求之间的状态.因为HTTP请求自身是完全无状态的.从服务器的角度来看,当用户发出第一个请求开始,服务器无法将新的请求与之前的请求关联起来,举例说 ...