数位DP!!!(hdu3555 hdu2089 hdu5898 2016弱校10.5 I)

博客描述:

各种数位DP,均是用模板做的。


先把模板一放!

//    pos    = 当前处理的位置(一般从高位到低位)
//    pre    = 上一个位的数字(更高的那一位)
//    status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回,
//            给计数器+1。
//    limit  = 是否受限,也即当前处理这位能否随便取值。如567,当前处理6这位,
//            如果前面取的是4,则当前这位可以取0-9。如果前面取的5,那么当前
//            这位就不能随便取,不然会超出这个数的范围,所以如果前面取5的
//            话此时的limit=1,也就是说当前只可以取0-6。
//
//    用DP数组保存这三个状态是因为往后转移的时候会遇到很多重复的情况。
int    dfs(int pos,int pre,int status,int limit)
{
    //已结搜到尽头,返回"是否找到了答案"这个状态。
    if(pos < 1)
        return    status;

    //DP里保存的是完整的,也即不受限的答案,所以如果满足的话,可以直接返回。
    if(!limit && DP[pos][pre][status] != -1)
        return    DP[pos][pre][status];

    int    end = limit ? DIG[pos] : 9;
    int    ret = 0;

    //往下搜的状态表示的很巧妙,status用||是因为如果前面找到了答案那么后面
    //还有没有答案都无所谓了。而limti用&&是因为只有前面受限、当前受限才能
    //推出下一步也受限,比如567,如果是46X的情况,虽然6已经到尽头,但是后面的
    //个位仍然可以随便取,因为百位没受限,所以如果个位要受限,那么前面必须是56。
    //
    //这里用"不要49"一题来做例子。
    for(int i = 0;i <= end;i ++)
        ret += dfs(pos - 1,i,status || (pre == 4 && i == 9),limit && (i == end));

    //DP里保存完整的、取到尽头的数据
    if(!limit)
        DP[pos][pre][status] = ret;

    return    ret;
}

题目描述:

HDU 3555 Bomb

Problem Description
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence “49”, the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?

Input
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker.

Output
For each test case, output an integer indicating the final points of the power.

Sample Input

3
1
50
500

Sample Output

0
1
15

Hint
From 1 to 500, the numbers that include the sub-sequence “49” are “49”,”149”,”249”,”349”,”449”,”490”,”491”,”492”,”493”,”494”,”495”,”496”,”497”,”498”,”499”,


HDU 2089 不要62

Problem Description
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。

Input
输入的都是整数对n、m(0 < n≤m<1000000),如果遇到都是0的整数对,则输入结束。

Output
对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。

Sample Input

1 100
0 0

Sample Output

80

HDU 5898 odd-even number

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


2016弱校10.5 I Increasing or Decreasing

We all like monotonic things, and solved many problems about that like Longest Increasing Subsequence(LIS). Here is another one which is easier than LIS (in my opinion).
We say an integer is a momo number if its decimal representation is monotonic. For example, 123, 321,777 and 5566 are momo numbers; But 514, 50216 and 120908 are not.
Please answer m queries. The i-th query is a interval [li, ri], and please calculate the number of momo numbers in it.
Input
The first line contains an integer m.
Each of the following m lines contains two integers li, ri.
• 1 ≤ m ≤ 105
• 1 ≤ li ≤ ri ≤ 1018
Output
For each query, please output the number of momo numbers in that range.

Example
standard input

2
1 100
100 200

standard output

100
48

题目分析:

HDU 3555 取49;
HDU 2089 不要62和4;
HDU 5898 取各数位上有奇数个连续的偶数和偶数个连续的奇数;
2016弱校10.5 I 取各数位从高到低递增(不严格)或递减(不严格);

都采用模板写。

代码:

hdu3555

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>

using namespace std;
long long dp[25][5];//dp[i][0]表示没有49且最高位不是4 dp[i][1]表示没有49最高位是4 dp[i][2]表示有49
int dig[25];
long long dfs(int pos,int status,bool limit)//表示dp[pos][status]为pos位status状态,limit代表pos位是否受限,即表示可以取0-9任意数字还是规定了最大值
{
    if(pos < 1) return (!limit && status == 2) ? 1 : 0;//只有在status为满足题意得状态时才返回1
    if(!limit && dp[pos][status] != -1) return dp[pos][status];
    int end = limit ? dig[pos] : 9;//若受限就用分解的最大值来规定最大值
    long long ans = 0;
    for(int i=0; i<=end; i++)
    {
        if(i==4)
        {
            if(status==1) ans+=dfs(pos-1,1,limit & (end==i));
            if(status==2) ans+=dfs(pos-1,2,limit & (end==i));
            if(status==0) ans+=dfs(pos-1,1,limit & (end==i));
        }
        else if(i==9)
        {

            if(status==1) ans+=dfs(pos-1,2,limit & (end==i));
            if(status==2) ans+=dfs(pos-1,2,limit & (end==i));
            if(status==0) ans+=dfs(pos-1,0,limit & (end==i));
        }
        else
        {
            if(status==1) ans+=dfs(pos-1,0,limit & (end==i));
            if(status==2) ans+=dfs(pos-1,2,limit & (end==i));
            if(status==0) ans+=dfs(pos-1,0,limit & (end==i));
        }
    }
    if(!limit) dp[pos][status]=ans;//若不受限就是统计了后面位数的数字和,可以记录
    return ans;
}
long long solve(long long x)
{
    int pos=1;
    while(x)
    {
        dig[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,true);
}
int main()
{
    memset(dp,-1,sizeof(dp));
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long right;
        scanf("%lld",&right);
        printf("%lld\n",solve(right+1));
    }
    return 0;
}

hdu 2089

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>

using namespace std;
typedef long long LL;
long long dp[25][5];
int dig[25];

long long dfs(int pos,int status,bool limit)
{
    if(pos < 1) return (!limit && status == 2) ? 1 : 0;
    if(!limit && dp[pos][status] != -1) return dp[pos][status];
    int end = limit ? dig[pos] : 9;
    long long ans = 0;
    for(int i=0; i<=end; i++)
    {
        if (i==4)
        {
            ans+=dfs(pos-1,2,limit & (end==i));
        }
        else if(i==6)
        {
            if(status==1) ans+=dfs(pos-1,1,limit & (end==i));
            if(status==2) ans+=dfs(pos-1,2,limit & (end==i));
            if(status==0) ans+=dfs(pos-1,1,limit & (end==i));
        }
        else if(i==2)
        {

            if(status==1) ans+=dfs(pos-1,2,limit & (end==i));
            if(status==2) ans+=dfs(pos-1,2,limit & (end==i));
            if(status==0) ans+=dfs(pos-1,0,limit & (end==i));
        }
        else
        {
            if(status==1) ans+=dfs(pos-1,0,limit & (end==i));
            if(status==2) ans+=dfs(pos-1,2,limit & (end==i));
            if(status==0) ans+=dfs(pos-1,0,limit & (end==i));
        }
    }
    if(!limit) dp[pos][status]=ans;//若不受限就是统计了后面位数的数字和,可以记录
    return ans;
}
long long solve(long long x)
{
    int pos=1;
    while(x)
    {
        dig[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,true);
}
int main()
{
    memset(dp,-1,sizeof(dp));
    LL left,right;
    while(scanf("%lld %lld",&left,&right)!=-1)
    {
        if (left==0 && right==0) break;
        printf("%lld\n",right-left+1-(solve(right+1)-solve(left)));
    }
    return 0;
}

hdu 5898

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstring>

using namespace std;
typedef long long LL;

LL l,r;
int T;
int dit[25];
LL dp[25][2][25];

LL dfs(int pos,int pre,int status,bool limit)//pre表示前一位是奇数还是偶数
{
    if(pos ==-1)
    {
        if(status%2 != pre) return 1;
        else return 0;
    }

    if(!limit && dp[pos][pre][status] != -1)
        return  dp[pos][pre][status];

    int end = limit ? dit[pos] : 9;
    printf("%d\n",end);
    LL  ret = 0;

    for(int i = 0; i <= end; i ++)
        if(status==0 && i==0)
        {
            ret += dfs(pos-1,1,0,limit && i==end);
        }
        else if(i%2==0)//oven
        {
            if(pre==1 && status%2==1) continue;
            ret += dfs(pos-1,0,pre==0 ? status+1 : 1,limit && i==end);
        }
        else//odd
        {
            if(pre==0 && status%2==0) continue;
            ret += dfs(pos-1,1,pre==1 ? status+1 : 1,limit && i==end);
        }

    if(!limit)
        dp[pos][pre][status] = ret;
    return  ret;
}

LL solve(LL x)
{
    int pos=0;
    while(x)
    {
        dit[pos]=x%10;
        x/=10;
        pos++;
    }
    memset(dp,-1,sizeof(dp));
    return dfs(pos-1,1,0,true);
}
int main()
{
    scanf("%d",&T);
    for(int t=1; t<=T; t++)
    {
        scanf("%lld%lld",&l,&r);
        printf("Case #%d: ",t);
        printf("%lld\n",solve(r)-solve(l-1));
    }
}

2016弱校10.5 I

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>

using namespace std;
typedef long long LL;

LL l,r;
int T;
int dit[22];
LL DP[22][12][5][3];
LL dp[22][12][5];

void getdp()
{
    memset(dp,0,sizeof(dp));
    for(int i=0; i<=9; i++) dp[0][i][0]=dp[0][i][1]=dp[0][i][2]=1;
    for(int i=0; i<19; i++) dp[i][9][1]=1;
    for(int i=1; i<19; i++)
        for(int j=8; j>=0; j--)
            for(int k=j; k<=9; k++)
                dp[i][j][1]+=dp[i-1][k][1];
    for(int i=0; i<19; i++)
        for(int j=0; j<=9; j++) dp[i][9-j][2]=dp[i][j][1];
    for(int i=0; i<19; i++)
        for(int j=0; j<=9; j++) dp[i][j][0]=dp[i][j][1]+dp[i][j][2]-1;
}


LL dfs(int pos,int pre,int status,bool limit,int judge)
{
    if(pos<0) return 1;
    if(!limit && DP[pos][pre][status][judge]!=-1)   // 对于有限制的询问我们是不能够记忆化的
        return DP[pos][pre][status][judge];
    LL ans=0;
    int end=limit ? dit[pos] : 9;
    if (judge==0)
    {
        ans+=dfs(pos-1,0,status,0,0);
        for(int i=1; i<=end; i++)
        {
            ans+=dp[pos][i][1]+dp[pos][i][2]-1;
        }
    }
    else
    {
        for(int i=0; i<=end; i++)   // 每一位有这么多的选择
        {
            if (status==1 && i<pre) continue;
            if (status==2 && i>pre) continue;
            if (status==0)
            {
                int s=status;
                if (i>pre) s=1;
                if (i<pre) s=2;
                if (limit) ans+=dfs(pos-1,i,s,limit&&(end==i),1);
                else ans+=dp[pos][i][s];
            }
            else
            {
                if (limit) ans+=dfs(pos-1,i,status,limit&&(end==i),1);
                else ans+=dp[pos][i][status];
            }
        }
    }
    if(!limit)
        DP[pos][pre][status][judge]=ans;
    return ans;
}


LL solve(LL x)
{
    LL ret=0;
    int pos=0;
    memset(DP,-1,sizeof(DP));
    while(x)
    {
        dit[pos]=x%10;
        x/=10;
        pos++;
    }
    ret+=dfs(pos-2,0,0,0,0);
    for(int i=1; i<=dit[pos-1]; i++)
    {
        ret+=dfs(pos-2,i,0,dit[pos-1]==i,1);
    }
    return ret;
}

int main()
{
    scanf("%d",&T);
    getdp();
    while(T--)
    {
        scanf("%lld %lld",&l,&r);
        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、付费专栏及课程。

余额充值