HDU 5898 odd-even number (数位DP)

152 篇文章 0 订阅

题意:

求l~r 连续的奇数个数有偶数个,连续偶数的个数有奇数个 的 数字个数


#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;
ll dp[20][5];
int digit[20];
/// 0 前导0
/// 1 奇数的个数有奇数个
/// 2 奇数的个数有偶数个
/// 3 偶数的个数有奇数个
/// 4 偶数的个数有偶数个
ll dfs(int len,int limit,int status)
{
    if(len<0)
    {
        if(status==2||status==3)
            return 1;
        return 0;
    }
    if(!limit&&dp[len][status]!=-1)
        return dp[len][status];
    int last;
    if(limit) last=digit[len];
    else last=9;
    ll ans=0;
    for(int i=0;i<=last;i++)
    {
        if(status==0)
        {
            if(i==0) ans+=dfs(len-1,0,0);
            else
            {
                if(i&1)
                    ans+=dfs(len-1,limit&&i==last,1);
                else
                    ans+=dfs(len-1,limit&&i==last,3);
            }
        }
        else if(status==1)
        {
            if(i%2)
                ans+=dfs(len-1,limit&&i==last,2);
        }
        else if(status==2)
        {
            if(i%2==0)
                ans+=dfs(len-1,limit&&i==last,3);
            else ans+=dfs(len-1,limit&&i==last,1);
        }
        else if(status==3)
        {
            if(i%2==0)
                ans+=dfs(len-1,limit&&i==last,4);
            else ans+=dfs(len-1,limit&&i==last,1);
        }
        else if(status==4)
        {
            if(i%2==0) ans+=dfs(len-1,limit&&i==last,3);
        }
    }
    if(!limit)
        dp[len][status] = ans;
    return ans;

}
ll solve(ll x)
{
    memset(dp,-1,sizeof(dp));
    int cnt=0;
    while(x!=0)
    {
        digit[cnt++]=x%10;
        x/=10;
    }
    return dfs( cnt-1, 1, 0 ) ;
}

int main()
{
    int t;
    scanf("%d",&t);
    for(int cs=1;cs<=t;cs++)
    {
        ll l,r;

        scanf("%lld%lld",&l,&r);
        printf("Case #%d: ",cs);
        printf("%lld\n",solve(r)-solve(l-1));
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值