http://acm.hdu.edu.cn/showproblem.php?pid=5898

题意:给出一个区间[l, r],问其中数位中连续的奇数长度为偶数并且连续的偶数长度为奇数的个数。(1<=L<=R<= 9*10^18)

思路:在比赛的时候只大概记得是怎么写的,但是就是不会写,虽然写过好几道可还是写不出来。回去后重新写又错了几次,主要前导零的情况没有考虑清楚。存的时候存长度和之前一位是偶数还是奇数和是否有前导零。然后根据条件判断才写了出来。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define N 100010
#define INF 0x3f3f3f3f long long dp[][][][];
int bit[];
//pos记录第几位,len记录目前的段的长度,pre记录之前一个位是奇偶,zero判断前导零
long long dfs(int pos, int pre, int len, int zero, bool f)
{
if(pos <= ) return (pre & ) != (len & );
if(f && dp[pos][len][pre][zero] != -) return dp[pos][len][pre][zero];
int d = f ? : bit[pos];
long long ans = ;
for(int i = ; i <= d; i++) {
if(zero == ) { //如果前面都是0,记得特殊考虑这种情况
if(i == ) ans += dfs(pos - , , , , f || i < d);
else ans += dfs(pos - , i & , , , f || i < d);
} else {
if(i & ) {
if(pre & ) ans += dfs(pos - , i & , len + , , f || i < d);
else {
if(len & ) ans += dfs(pos - , i & , , , f || i < d);
}
} else {
if(pre & ) {
if(!(len & )) ans += dfs(pos - , i & , , , f || i < d);
} else {
ans += dfs(pos - , i & , len + , , f || i < d);
}
}
}
}
if(f) dp[pos][len][pre][zero] = ans;
return ans;
} long long solve(long long x)
{
int len = ;
while(x) {
bit[++len] = x % ;
x /= ;
}
return dfs(len, , , , );
} int main()
{
int t;
scanf("%d", &t);
for(int cas = ; cas <= t; cas++) {
memset(dp, -, sizeof(dp));
long long l, r;
scanf("%I64d%I64d", &l, &r);
printf("Case #%d: %I64d\n", cas, solve(r) - solve(l - ));
}
return ;
}

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)

    Problem Description For a number,if the length of continuous odd digits is even and the length of co ...

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

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

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

  6. HDU 3709 Balanced Number (数位DP)

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

  7. HDU 5179 beautiful number 数位dp

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

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

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

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

  10. codeforces Hill Number 数位dp

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

随机推荐

  1. 电量显示Binding Converter MVVM

    用一个ProcessBar显示电量,低于20%时候,ForeGround为红色,否则为绿色, 页面使用了MVVM绑定到了ViewModel, ProcessBar XAML为 <Progress ...

  2. box2dweb之关节joint(连接器)

    1 概述 前篇基础文章看完后基本上就应该对box2dweb能上手了,下面来介绍一下box2dweb非常重要的一个概念,关节(joint)也有叫连接器的,总之是一个意思.下面是关节详细的类库说明: BO ...

  3. Visual Studio 快速返回上次浏览/编辑的位置

    参考资料: http://stackoverflow.com/questions/4927375/how-to-navigate-back-to-the-last-cursor-position-in ...

  4. UIButton属性

    1.UIButton状态: UIControlStateNormal          // 正常状态    UIControlStateHighlighted     // 高亮状态    UICo ...

  5. 并发调用get请求

    http://zeusami.iteye.com/blog/1172864 package com.alibaba.xteam.web.travel.module.rpc; import java.i ...

  6. linux c

    #include <stdio.h>#include <string.h>#include <strings.h> int main(){    char buf[ ...

  7. .NET 4.0 MemoryCache with SqlChangeMonitor

    Summary There isn't a lot of documentation on the internet about how to use the SqlChangeMonitor wit ...

  8. (转)【ASP.NET开发】获取客户端IP地址 via C#

    [ASP.NET开发]获取客户端IP地址 via C# 说明:本文中的内容是我综合博客园上的博文和MSDN讨论区的资料,再通过自己的实际测试而得来,属于自己原创的内容说实话很少,写这一篇是为了记录自己 ...

  9. YbRapidSolution.MVC项目首页缓存没有起作用

    Response.Cache.SetOmitVaryStar(true); 文件方面的内容,增加了这个语句 没有的话缓存没起作用,增加这个语句可提高系统性能. HomeController: usin ...

  10. fnd_profile.value('AFLOG_ENABLED')的取值 和配置文件相关SQL

    SELECT * FROM FND_PROFILE_OPTIONS_VL TT WHERE TT.PROFILE_OPTION_NAME LIKE '%AFLOG%' FND:启用调试日志 详细的参考 ...