您的位置:首页 > 其它

hdu 5898 odd-even number(基础数位DP)

2016-10-26 13:12 369 查看
odd-even number
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u
Submit Status

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


#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

using namespace std;

typedef long long LL;

const int N = 20;

int num
;

LL dp

;

LL dfs(int pos,int pre,int cnt,int zero,int limit)

{

    if(pos==0)

    {

        return (pre&&(cnt%2)!=0)||(!pre&&(cnt%2)==0);

    }

    if(!limit&&dp[pos][pre][cnt]!=-1)

        return dp[pos][pre][cnt];

    int m = limit?num[pos]:9;

    LL ans=0;

    for(int i=0;i<=m;i++)

    {

        if(zero)

            ans+=dfs(pos-1,i%2==0,cnt+(i!=0),zero&&i==0,limit&&i==m);

        else

        {

            if(i%2==0)

            {

                if(pre)

                    ans+=dfs(pos-1,1,cnt+1,0,limit&&i==m);

                else if(!pre&&(cnt%2)==0)

                    ans+=dfs(pos-1,1,1,0,limit&&i==m);

            }

            else

            {

                if(pre&&(cnt%2)!=0)

                    ans+=dfs(pos-1,0,1,0,limit&&i==m);

                else if(!pre)

                    ans+=dfs(pos-1,0,cnt+1,0,limit&&i==m);

            }

        }

    }

    if(!limit)

        dp[pos][pre][cnt]=ans;

    return ans;

}

LL solve(LL x)

{

    if(x==0)

        return 0;

    int cnt=0;

    while(x)

    {

        num[++cnt]=x%10;

        x/=10;

    }

    return dfs(cnt,0,0,1,1);

}

int main()

{

    int t, ncase=1;

    scanf("%d", &t);

    while(t--)

    {

        LL l ,r;

        memset(dp,-1,sizeof(dp));

        scanf("%I64d %I64d", &l, &r);

        printf("Case #%d: %I64d\n", ncase++, solve(r)-solve(l-1));

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: