hdu 5898  odd-even number  数位dp基础

8 篇文章 0 订阅

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.
Sample Input
2
1 100
110 220
Sample Output
Case #1: 29
Case #2: 36

题意:统计 连续的奇数必须是偶数个,连续的偶数必须是奇数个的个数

这里不出现也算奇数个

状态很好想 上一个数 和 连续的长度 奇偶变化 长度就要重新算。
还有一个难点就是前导0的处理,加一个参数 zero 就可以
一开始以为同奇数同偶数才能继续走下去。后来发现不对 22333这样的就gg了。 所以应该是如果偶数的长度不是奇数就不允许转换,只有为奇数才允许转换。

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
int wei[30];
ll dp[30][30][30];
ll dfs(int pos,int zero,int len,int pre,int flag)
{
    if(pos<0){
        return (pre&1)!=(len&1);
    }
    if(!flag&&dp[pos][pre&1][len]!=-1){
        return dp[pos][pre&1][len];
    }
    int up=flag?wei[pos]:9;
    ll res=0;
    for(int i=0;i<=up;i++)
    {
        if(zero)
        {
            if(i==0)
                res+=dfs(pos-1,1,0,0,flag&&i==up);
            else 
               res+=dfs(pos-1,0,1,i,flag&&i==up);
       }
       else
       {
        if(pre&1)
        {
            if(i&1)
            res+=dfs(pos-1,0,len+1,i,flag&&i==up);
            else if(!(len&1))
            res+=dfs(pos-1,0,1,i,flag&&i==up);
        }
        else
        {
            if(i&1)
            {
            if(len&1)
            res+=dfs(pos-1,0,1,i,flag&&i==up);
            }
            else 
            res+=dfs(pos-1,0,len+1,i,flag&&i==up);
        }
       }
    }
    if(!flag)
        dp[pos][pre&1][len]=res;
    return res;
}

ll change(ll x)
{
    ll res=0;
    int len=0;
    while(x)
    {
        wei[len++]=x%10;
        x/=10;
    }
    res=dfs(len-1,1,0,0,1);
    return res;
}
int main()
{
    int t;
    cin>>t;
    memset(dp,-1,sizeof(dp));
    for(int f=1;f<=t;f++)
    {
        ll a,b;
        cin>>a>>b;
        printf("Case #%d: %lld\n",f,change(b)-change(a-1));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值