bzoj4521 [Cqoi2016]手机号码

本文通过一个具体的题目实例,深入浅出地介绍了数位DP(动态规划)的应用方法。作者分享了一个能处理到11位数的解决方案,并针对10位数的情况进行了特殊处理。此外,还提供了一段完整的C++代码实现,便于读者理解和实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

一眼看出是数位dp,套模板就可以了。

但可能我太菜了吧,只能算11位的,10位就完全错了,233。
导致强行特判233。

求dalao们查错。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL dp[12][10][10][3][4];
LL x,y;
int digit[12],len;
/*
    0: 。。。。。 
    1:有两个相邻 
    2:有三个相邻 
*/
/*
    exist 0:.
          1:8
          2:4
          3:4&&8
*/
inline char nc()
{
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline LL read()
{
    LL x=0;
    char c=nc();
    while(c<'0'||c>'9')c=nc();
    while(c<='9'&&c>='0')x=x*10+c-'0',c=nc();
    return x;
}
inline LL DFS(int pos,int limit,int pre,int pre_pre,int sta,int exist)
{
    if(exist==3)return 0;
    if(pos==0)return sta==2&&exist!=3;
    if(!limit&&dp[pos][pre][pre_pre][sta][exist]!=-1)return dp[pos][pre][pre_pre][sta][exist];
    int up=limit?digit[pos]:9;
    LL tmp=0;
    for(int i=0;i<=up;i++)
    {
        int new_exist=exist;
        if(exist==1&&i==4)new_exist=3;
        else if(exist==2&&i==8)new_exist=3;
        else if(i==4)new_exist=2;
        else if(i==8)new_exist=1;

        int new_sta=sta;
        if(sta!=2)
        {
            if(i==pre&&i==pre_pre)new_sta=2;
            else if(i==pre)new_sta=1;
        }
        tmp+=DFS(pos-1,limit&&i==up,i,pre,new_sta,new_exist);
    }
    if(!limit)dp[pos][pre][pre_pre][sta][exist]=tmp;
    return tmp;
}
inline LL cal(LL x)
{
    len=0;
    while(x)digit[++len]=x%10,x/=10;
    return DFS(len,1,0,0,0,0);
}
int main()
{
    //freopen("in.txt","r",stdin);
    memset(dp,-1,sizeof(dp));
    x=read(),y=read();
    x==10000000000?cout<<cal(y)-5899826978:cout<<cal(y)-cal(x-1);
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值