HDU 5898 odd-even number (数位DP) 2016 ACM/ICPC Asia Regional Shenyang Online

odd-even number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 220 Accepted Submission(s): 115

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.

Sample Input
2
1 100
110 220

Sample Output
Case #1: 29
Case #2: 36

Source
2016 ACM/ICPC Asia Regional Shenyang Online

Recommend
wange2014 | We have carefully selected several similar problems for you: 5901 5900 5899 5897 5896

啊数位DP,状态看注释。。

#include "cstring"
#include "cstdio"
#include "string.h"
#include "cmath"
using namespace std;
long long dp[25][5];
int num[25];
/*status:
0 有前导0
1 连续奇数个奇数
2 连续偶数个奇数
3 连续奇数个偶数
4 连续偶数个奇数
 */
long long dfs(int pos,int limit,int status)
{
    if(pos<0)
    {
        if(status==2||status==3)
            return 1;
        else
            return 0;
    }
    if(!limit&&dp[pos][status]!=-1)
    //if(!limit&&~dp[pos][status])
        return dp[pos][status];
    int end= limit? num[pos]:9;
    long long ans=0;
    for(int i=0;i<=end;i++)
    {
        if(!status)
        {
            if(i==0)
                ans+=dfs(pos-1,0,0);
            else
            {
                if(i&1)
                    ans+=dfs(pos-1,limit&&i==end,1);
                else
                    ans+=dfs(pos-1,limit&&i==end,3);
            }
        }
        else if(status==1)
        {
            if(i&1)
                ans+=dfs(pos-1,limit&&i==end,2);
        }
        else if(status==2)
        {
            if(i&1)
                ans+=dfs(pos-1,limit&&i==end,1);
            else
                ans+=dfs(pos-1,limit&&i==end,3);
        }
        else if(status==3)
        {
            if(i&1)
                ans+=dfs(pos-1,limit&&i==end,1);
            else
                ans+=dfs(pos-1,limit&&i==end,4);
        }
        else
        {
            if(i&1)
            {}
            else
                ans+=dfs(pos-1,limit&&i==end,3);
        }
    }
    if(!limit)
        dp[pos][status]=ans;
    return ans;
}
long long solve(long long n)
{
    memset(dp,-1,sizeof(dp));
    int len=0;
    while(n)
    {
        num[len++]=n%10;
        n/=10;
    }
    return dfs(len-1,1,0);
}
int main()
{
    int cas;
    scanf("%d",&cas);
    for(int i=1;i<=cas;i++)
    {
        long long l,r;
        scanf("%lld%lld",&l,&r);
        printf("Case #%d: ",i);
        printf("%lld\n",solve(r)-solve(l-1));

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值